Skip to content

Commit

Permalink
[chore] change extensions config tests to unmarshal only the config f…
Browse files Browse the repository at this point in the history
…or that component (open-telemetry#13226)

Signed-off-by: Bogdan Drutu <[email protected]>

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Aug 12, 2022
1 parent f79bfa1 commit 2341bf7
Show file tree
Hide file tree
Showing 49 changed files with 1,073 additions and 1,513 deletions.
78 changes: 37 additions & 41 deletions extension/asapauthextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/service/servicetest"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

// Test keys. Not for use anywhere but these tests.
Expand All @@ -36,59 +35,56 @@ f9IYHQL5srVgTF0CWHcJCtnRScMGXFiVYSRnDiQQ3wf/LXG3SXd+CmUCAwEAAQ==
)

func TestLoadConfig(t *testing.T) {
factories, err := componenttest.NopFactories()
assert.NoError(t, err)
factory := NewFactory()
factories.Extensions[typeStr] = factory

cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)
assert.NotNil(t, cfg)
assert.NoError(t, err)
assert.NoError(t, cfg.Validate())

expected := factory.CreateDefaultConfig().(*Config)
expected.TTL = 60 * time.Second
expected.Audience = []string{"test_service1", "test_service2"}
expected.Issuer = "test_issuer"
expected.KeyID = "test_issuer/test_kid"
expected.PrivateKey = privateKey
ext := cfg.Extensions[config.NewComponentID(typeStr)]
assert.Equal(t, expected, ext)
}

func TestLoadBadConfig(t *testing.T) {
t.Parallel()
factories, err := componenttest.NopFactories()
require.NoError(t, err)

tests := []struct {
configName string
id config.ComponentID
expected config.Extension
expectedErr error
}{
{
"missingkeyid",
errNoKeyIDProvided,
id: config.NewComponentID(typeStr),
expected: &Config{
ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)),
TTL: 60 * time.Second,
Audience: []string{"test_service1", "test_service2"},
Issuer: "test_issuer",
KeyID: "test_issuer/test_kid",
PrivateKey: privateKey,
},
},
{
id: config.NewComponentIDWithName(typeStr, "missingkeyid"),
expectedErr: errNoKeyIDProvided,
},
{
"missingissuer",
errNoIssuerProvided,
id: config.NewComponentIDWithName(typeStr, "missingissuer"),
expectedErr: errNoIssuerProvided,
},
{
"missingaudience",
errNoAudienceProvided,
id: config.NewComponentIDWithName(typeStr, "missingaudience"),
expectedErr: errNoAudienceProvided,
},
{
"missingpk",
errNoPrivateKeyProvided,
id: config.NewComponentIDWithName(typeStr, "missingpk"),
expectedErr: errNoPrivateKeyProvided,
},
}
for _, tt := range tests {
factory := NewFactory()
factories.Extensions[typeStr] = factory
cfg, err := servicetest.LoadConfig(filepath.Join("testdata", "config_bad.yaml"), factories)
assert.NoError(t, err)
extension := cfg.Extensions[config.NewComponentIDWithName(typeStr, tt.configName)]
verr := extension.Validate()
require.ErrorIs(t, verr, tt.expectedErr)
t.Run(tt.id.String(), func(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(tt.id.String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalExtension(sub, cfg))
if tt.expectedErr != nil {
assert.ErrorIs(t, cfg.Validate(), tt.expectedErr)
return
}
assert.NoError(t, cfg.Validate())
assert.Equal(t, tt.expected, cfg)
})
}
}
67 changes: 45 additions & 22 deletions extension/asapauthextension/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
extensions:
asapclient:
key_id: test_issuer/test_kid
issuer: test_issuer
audience:
- test_service1
- test_service2
private_key: "data:application/pkcs8;kid=test;base64,MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEA0ZPr5JeyVDoB8RyZqQsx6qUD+9gMFg1/0hgdAvmytWBMXQJYdwkK2dFJwwZcWJVhJGcOJBDfB/8tcbdJd34KZQIDAQABAkBZD20tJTHJDSWKGsdJyNIbjqhUu4jXTkFFPK4Hd6jz3gV3fFvGnaolsD5Bt50dTXAiSCpFNSb9M9GY6XUAAdlBAiEA6MccfdZRfVapxKtAZbjXuAgMvnPtTvkVmwvhWLT5Wy0CIQDmfE8Et/pou0Jl6eM0eniT8/8oRzBWgy9ejDGfj86PGQIgWePqIL4OofRBgu0O5TlINI0HPtTNo12U9lbUIslgMdECICXT2RQpLcvqj+cyD7wZLZj6vrHZnTFVrnyR/cL2UyxhAiBswe/MCcD7T7J4QkNrCG+ceQGypc7LsxlIxQuKh5GWYA=="
asapclient:
key_id: test_issuer/test_kid
issuer: test_issuer
audience:
- test_service1
- test_service2
private_key: "data:application/pkcs8;kid=test;base64,MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEA0ZPr5JeyVDoB8RyZqQsx6qUD+9gMFg1/0hgdAvmytWBMXQJYdwkK2dFJwwZcWJVhJGcOJBDfB/8tcbdJd34KZQIDAQABAkBZD20tJTHJDSWKGsdJyNIbjqhUu4jXTkFFPK4Hd6jz3gV3fFvGnaolsD5Bt50dTXAiSCpFNSb9M9GY6XUAAdlBAiEA6MccfdZRfVapxKtAZbjXuAgMvnPtTvkVmwvhWLT5Wy0CIQDmfE8Et/pou0Jl6eM0eniT8/8oRzBWgy9ejDGfj86PGQIgWePqIL4OofRBgu0O5TlINI0HPtTNo12U9lbUIslgMdECICXT2RQpLcvqj+cyD7wZLZj6vrHZnTFVrnyR/cL2UyxhAiBswe/MCcD7T7J4QkNrCG+ceQGypc7LsxlIxQuKh5GWYA=="
ttl: 60s

ttl: 60s
asapclient/missingkeyid:
issuer: test_issuer
audience:
- test_service1
- test_service2
private_key: "data:application/pkcs8;kid=test;base64,MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA5DswDqF/YTq+c7PmMlc3CZcpJppdcvlNviMy4lCjS9FILXO9bjME6kCNfQE/AxgE3yp3rEmZ/j4oEf1jXUz5AQIDAQABAkBl7b0fu6ac8NRf7idPsj3FTbo2IFi94XOECEpQYr0bPWt6pQyoArlgqF8TlZD/H3zjn+y95DLOeZijleZlh67xAiEA80FkGtTfJRbcLBymJxPjlV9+Agj1o11bLLv0IqS2wYUCIQDwMEmc3pZlfpazeJPwvEmX1h3T72V8NQoRleFr7vr0TQIhALeC8GMxjnoricQZhNtcLMfGd4hPfAhXaG4SCTaNbnYFAiAx1QLgzfmMEyh3EdQ3xQjLvLuxheCbVXHCVkNPnmRonQIgYcX1m7kzkJatn5XuMeU8VndbGT66cpRoGY2FNPzvhZI="
ttl: 60s

service:
extensions: [asapclient]
pipelines:
traces:
receivers: [nop]
processors: [nop]
exporters: [nop]
asapclient/missingissuer:
key_id: test_issuer/test_kid
audience:
- test_service1
- test_service2
private_key: "data:application/pkcs8;kid=test;base64,MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA5DswDqF/YTq+c7PmMlc3CZcpJppdcvlNviMy4lCjS9FILXO9bjME6kCNfQE/AxgE3yp3rEmZ/j4oEf1jXUz5AQIDAQABAkBl7b0fu6ac8NRf7idPsj3FTbo2IFi94XOECEpQYr0bPWt6pQyoArlgqF8TlZD/H3zjn+y95DLOeZijleZlh67xAiEA80FkGtTfJRbcLBymJxPjlV9+Agj1o11bLLv0IqS2wYUCIQDwMEmc3pZlfpazeJPwvEmX1h3T72V8NQoRleFr7vr0TQIhALeC8GMxjnoricQZhNtcLMfGd4hPfAhXaG4SCTaNbnYFAiAx1QLgzfmMEyh3EdQ3xQjLvLuxheCbVXHCVkNPnmRonQIgYcX1m7kzkJatn5XuMeU8VndbGT66cpRoGY2FNPzvhZI="
ttl: 60s

receivers:
nop:
processors:
nop:
exporters:
nop:
asapclient/missingaudience:
key_id: test_issuer/test_kid
issuer: test_issuer
audience:
private_key: "data:application/pkcs8;kid=test;base64,MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA5DswDqF/YTq+c7PmMlc3CZcpJppdcvlNviMy4lCjS9FILXO9bjME6kCNfQE/AxgE3yp3rEmZ/j4oEf1jXUz5AQIDAQABAkBl7b0fu6ac8NRf7idPsj3FTbo2IFi94XOECEpQYr0bPWt6pQyoArlgqF8TlZD/H3zjn+y95DLOeZijleZlh67xAiEA80FkGtTfJRbcLBymJxPjlV9+Agj1o11bLLv0IqS2wYUCIQDwMEmc3pZlfpazeJPwvEmX1h3T72V8NQoRleFr7vr0TQIhALeC8GMxjnoricQZhNtcLMfGd4hPfAhXaG4SCTaNbnYFAiAx1QLgzfmMEyh3EdQ3xQjLvLuxheCbVXHCVkNPnmRonQIgYcX1m7kzkJatn5XuMeU8VndbGT66cpRoGY2FNPzvhZI="
ttl: 60s

asapclient/missingpk:
key_id: test_issuer/test_kid
issuer: test_issuer
audience:
- test_service1
- test_service2
ttl: 60s

asapclient/invalidpk:
key_id: test_issuer/test_kid
issuer: test_issuer
audience:
- test_service1
- test_service2
ttl: 60s
private_key: "invalidpk"
57 changes: 0 additions & 57 deletions extension/asapauthextension/testdata/config_bad.yaml

This file was deleted.

72 changes: 40 additions & 32 deletions extension/awsproxy/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,57 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/service/servicetest"
"go.opentelemetry.io/collector/confmap/confmaptest"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/proxy"
)

func TestLoadConfig(t *testing.T) {
factories, err := componenttest.NopFactories()
assert.NoError(t, err)
t.Parallel()

factory := NewFactory()
factories.Extensions[typeStr] = factory
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)

require.Nil(t, err)
require.NotNil(t, cfg)

ext0 := cfg.Extensions[config.NewComponentID(typeStr)]
assert.Equal(t, factory.CreateDefaultConfig(), ext0)

ext1 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "1")]
assert.Equal(t,
&Config{
ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "1")),
ProxyConfig: proxy.Config{
TCPAddr: confignet.TCPAddr{
Endpoint: "0.0.0.0:1234",
},
ProxyAddress: "https://proxy.proxy.com",
TLSSetting: configtls.TLSClientSetting{
Insecure: true,
ServerName: "something",
tests := []struct {
id config.ComponentID
expected config.Extension
}{
{
id: config.NewComponentID(typeStr),
expected: NewFactory().CreateDefaultConfig(),
},
{
id: config.NewComponentIDWithName(typeStr, "1"),
expected: &Config{
ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)),
ProxyConfig: proxy.Config{
TCPAddr: confignet.TCPAddr{
Endpoint: "0.0.0.0:1234",
},
ProxyAddress: "https://proxy.proxy.com",
TLSSetting: configtls.TLSClientSetting{
Insecure: true,
ServerName: "something",
},
Region: "us-west-1",
RoleARN: "arn:aws:iam::123456789012:role/awesome_role",
AWSEndpoint: "https://another.aws.endpoint.com",
},
Region: "us-west-1",
RoleARN: "arn:aws:iam::123456789012:role/awesome_role",
AWSEndpoint: "https://another.aws.endpoint.com",
},
},
ext1)
}
for _, tt := range tests {
t.Run(tt.id.String(), func(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(tt.id.String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalExtension(sub, cfg))

assert.Equal(t, 1, len(cfg.Service.Extensions))
assert.Equal(t, config.NewComponentIDWithName(typeStr, "1"), cfg.Service.Extensions[0])
assert.NoError(t, cfg.Validate())
assert.Equal(t, tt.expected, cfg)
})
}
}
37 changes: 10 additions & 27 deletions extension/awsproxy/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
extensions:
awsproxy:
awsproxy/1:
endpoint: "0.0.0.0:1234"
proxy_address: "https://proxy.proxy.com"
tls:
insecure: true
server_name_override: "something"
region: "us-west-1"
role_arn: "arn:aws:iam::123456789012:role/awesome_role"
aws_endpoint: "https://another.aws.endpoint.com"

service:
extensions: [awsproxy/1]
pipelines:
traces:
receivers: [nop]
processors: [nop]
exporters: [nop]

# Data pipeline is required to load the config.
receivers:
nop:
processors:
nop:
exporters:
nop:
awsproxy:
awsproxy/1:
endpoint: "0.0.0.0:1234"
proxy_address: "https://proxy.proxy.com"
tls:
insecure: true
server_name_override: "something"
region: "us-west-1"
role_arn: "arn:aws:iam::123456789012:role/awesome_role"
aws_endpoint: "https://another.aws.endpoint.com"
Loading

0 comments on commit 2341bf7

Please sign in to comment.