From 2341bf78c258b3a77bf025647c62fc19431b3c8c Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Fri, 12 Aug 2022 07:54:52 -0700 Subject: [PATCH] [chore] change extensions config tests to unmarshal only the config for that component (#13226) Signed-off-by: Bogdan Drutu Signed-off-by: Bogdan Drutu --- extension/asapauthextension/config_test.go | 78 +++++----- .../asapauthextension/testdata/config.yaml | 67 ++++++--- .../testdata/config_bad.yaml | 57 -------- extension/awsproxy/config_test.go | 72 +++++----- extension/awsproxy/testdata/config.yaml | 37 ++--- extension/basicauthextension/config_test.go | 96 +++++++------ .../basicauthextension/testdata/config.yaml | 19 +++ .../testdata/invalid_config_both.yml | 23 --- .../testdata/invalid_config_none.yml | 18 --- .../testdata/valid_config.yml | 26 ---- .../bearertokenauthextension/config_test.go | 71 +++++---- .../testdata/config.yaml | 24 +--- .../testdata/config_missing_token.yaml | 18 --- extension/fluentbitextension/config_test.go | 56 ++++---- .../fluentbitextension/testdata/config.yaml | 23 +-- extension/headerssetter/config_test.go | 71 ++++----- extension/headerssetter/testdata/config.yaml | 30 +--- extension/healthcheckextension/config_test.go | 99 ++++++------- .../healthcheckextension/testdata/config.yaml | 65 ++++----- .../testdata/config_bad.yaml | 37 ----- extension/httpforwarder/config_test.go | 70 +++++++++ extension/httpforwarder/testdata/config.yaml | 35 ++--- extension/jaegerremotesampling/config_test.go | 86 +++++------ .../jaegerremotesampling/testdata/config.yaml | 33 ++--- .../oauth2clientauthextension/config_test.go | 135 ++++++++---------- .../testdata/config.yaml | 68 +++++---- .../testdata/config_bad.yaml | 33 ----- .../observer/dockerobserver/config_test.go | 72 ++++++---- .../observer/dockerobserver/extension_test.go | 63 ++------ .../dockerobserver/testdata/config.yaml | 51 +++---- extension/observer/ecsobserver/config.go | 15 +- extension/observer/ecsobserver/config_test.go | 101 +++++++------ .../observer/ecsobserver/testdata/config.yaml | 70 ++++----- .../ecsobserver/testdata/config_invalid.yaml | 24 ---- .../observer/ecstaskobserver/config_test.go | 93 ++++++------ .../ecstaskobserver/testdata/config.yaml | 41 ++---- .../observer/hostobserver/config_test.go | 55 ++++--- .../hostobserver/testdata/config.yaml | 23 +-- extension/observer/k8sobserver/config_test.go | 113 +++++++-------- .../observer/k8sobserver/testdata/config.yaml | 41 ++---- .../k8sobserver/testdata/invalid_auth.yaml | 20 --- .../testdata/invalid_no_observing.yaml | 21 --- extension/pprofextension/config_test.go | 60 ++++---- extension/pprofextension/testdata/config.yaml | 27 +--- extension/sigv4authextension/config_test.go | 71 ++++----- .../sigv4authextension/testdata/config.yaml | 31 ++-- .../testdata/config_bad.yaml | 20 --- extension/storage/filestorage/config_test.go | 82 ++++++----- .../storage/filestorage/testdata/config.yaml | 45 ++---- 49 files changed, 1073 insertions(+), 1513 deletions(-) delete mode 100644 extension/asapauthextension/testdata/config_bad.yaml create mode 100644 extension/basicauthextension/testdata/config.yaml delete mode 100644 extension/basicauthextension/testdata/invalid_config_both.yml delete mode 100644 extension/basicauthextension/testdata/invalid_config_none.yml delete mode 100644 extension/basicauthextension/testdata/valid_config.yml delete mode 100644 extension/bearertokenauthextension/testdata/config_missing_token.yaml delete mode 100644 extension/healthcheckextension/testdata/config_bad.yaml create mode 100644 extension/httpforwarder/config_test.go delete mode 100644 extension/oauth2clientauthextension/testdata/config_bad.yaml delete mode 100644 extension/observer/ecsobserver/testdata/config_invalid.yaml delete mode 100644 extension/observer/k8sobserver/testdata/invalid_auth.yaml delete mode 100644 extension/observer/k8sobserver/testdata/invalid_no_observing.yaml delete mode 100644 extension/sigv4authextension/testdata/config_bad.yaml diff --git a/extension/asapauthextension/config_test.go b/extension/asapauthextension/config_test.go index 517a5784e4f5..2344245dcc79 100644 --- a/extension/asapauthextension/config_test.go +++ b/extension/asapauthextension/config_test.go @@ -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. @@ -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) + }) } } diff --git a/extension/asapauthextension/testdata/config.yaml b/extension/asapauthextension/testdata/config.yaml index dfb09d79aea7..d2aa0b84e965 100644 --- a/extension/asapauthextension/testdata/config.yaml +++ b/extension/asapauthextension/testdata/config.yaml @@ -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: \ No newline at end of file +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" diff --git a/extension/asapauthextension/testdata/config_bad.yaml b/extension/asapauthextension/testdata/config_bad.yaml deleted file mode 100644 index 06ae5e62f728..000000000000 --- a/extension/asapauthextension/testdata/config_bad.yaml +++ /dev/null @@ -1,57 +0,0 @@ -extensions: - 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 - - 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 - - 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" - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: - -service: - extensions: - - asapclient/missingkeyid - - asapclient/missingissuer - - asapclient/missingaudience - - asapclient/missingpk - - asapclient/invalidpk - pipelines: \ No newline at end of file diff --git a/extension/awsproxy/config_test.go b/extension/awsproxy/config_test.go index 784a5c53c8f2..a5e9f9258af7 100644 --- a/extension/awsproxy/config_test.go +++ b/extension/awsproxy/config_test.go @@ -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) + }) + } } diff --git a/extension/awsproxy/testdata/config.yaml b/extension/awsproxy/testdata/config.yaml index 2ccbebc555c0..4c49c01f9f05 100644 --- a/extension/awsproxy/testdata/config.yaml +++ b/extension/awsproxy/testdata/config.yaml @@ -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: \ No newline at end of file +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" diff --git a/extension/basicauthextension/config_test.go b/extension/basicauthextension/config_test.go index f0bc78dae833..01a132836966 100644 --- a/extension/basicauthextension/config_test.go +++ b/extension/basicauthextension/config_test.go @@ -20,59 +20,61 @@ 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" ) func TestLoadConfig(t *testing.T) { - factories, err := componenttest.NopFactories() - require.NoError(t, err) + t.Parallel() - factory := NewFactory() - factories.Extensions[typeStr] = factory - cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "valid_config.yml"), factories) - - require.NoError(t, err) - require.NotNil(t, cfg) - - ext0 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "server")] - assert.Equal(t, &Config{ - ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "server")), - Htpasswd: &HtpasswdSettings{ - Inline: "username1:password1\nusername2:password2\n", + tests := []struct { + id config.ComponentID + expected config.Extension + expectedErr bool + }{ + { + id: config.NewComponentID(typeStr), + expectedErr: true, }, - }, ext0) - - ext1 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "client")] - assert.Equal(t, - &Config{ - ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "client")), - ClientAuth: &ClientAuthSettings{ - Username: "username", - Password: "password", + { + id: config.NewComponentIDWithName(typeStr, "server"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + Htpasswd: &HtpasswdSettings{ + Inline: "username1:password1\nusername2:password2\n", + }, }, }, - ext1) - - assert.Equal(t, 2, len(cfg.Service.Extensions)) - assert.Equal(t, config.NewComponentIDWithName(typeStr, "client"), cfg.Service.Extensions[0]) - assert.Equal(t, config.NewComponentIDWithName(typeStr, "server"), cfg.Service.Extensions[1]) -} - -func TestLoadConfigError(t *testing.T) { - factories, err := componenttest.NopFactories() - require.NoError(t, err) - - factory := NewFactory() - factories.Extensions[typeStr] = factory - t.Run("invalid config both present", func(t *testing.T) { - _, err = servicetest.LoadConfigAndValidate(filepath.Join("testdata", "invalid_config_both.yml"), factories) - assert.Error(t, err) - }) - t.Run("invalid config none present", func(t *testing.T) { - _, err = servicetest.LoadConfigAndValidate(filepath.Join("testdata", "invalid_config_none.yml"), factories) - assert.Error(t, err) - }) - + { + id: config.NewComponentIDWithName(typeStr, "client"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + ClientAuth: &ClientAuthSettings{ + Username: "username", + Password: "password", + }, + }, + }, + { + id: config.NewComponentIDWithName(typeStr, "both"), + expectedErr: true, + }, + } + 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)) + if tt.expectedErr { + assert.Error(t, cfg.Validate()) + return + } + assert.NoError(t, cfg.Validate()) + assert.Equal(t, tt.expected, cfg) + }) + } } diff --git a/extension/basicauthextension/testdata/config.yaml b/extension/basicauthextension/testdata/config.yaml new file mode 100644 index 000000000000..f20183bb52fa --- /dev/null +++ b/extension/basicauthextension/testdata/config.yaml @@ -0,0 +1,19 @@ +basicauth: + +basicauth/client: + client_auth: + username: username + password: password + +basicauth/server: + htpasswd: + inline: | + username1:password1 + username2:password2 + +basicauth/both: + client_auth: + username: user + password: pass + htpasswd: + file: /etc/nginx/htpasswd diff --git a/extension/basicauthextension/testdata/invalid_config_both.yml b/extension/basicauthextension/testdata/invalid_config_both.yml deleted file mode 100644 index 877f830aee32..000000000000 --- a/extension/basicauthextension/testdata/invalid_config_both.yml +++ /dev/null @@ -1,23 +0,0 @@ -extensions: - basicauth/both: - client_auth: - username: user - password: pass - htpasswd: - file: /etc/nginx/htpasswd - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: - -service: - extensions: [basicauth/both] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] diff --git a/extension/basicauthextension/testdata/invalid_config_none.yml b/extension/basicauthextension/testdata/invalid_config_none.yml deleted file mode 100644 index f60cf0d03c1c..000000000000 --- a/extension/basicauthextension/testdata/invalid_config_none.yml +++ /dev/null @@ -1,18 +0,0 @@ -extensions: - basicauth/none: - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: - -service: - extensions: [basicauth/none] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] diff --git a/extension/basicauthextension/testdata/valid_config.yml b/extension/basicauthextension/testdata/valid_config.yml deleted file mode 100644 index 46ac085cf41b..000000000000 --- a/extension/basicauthextension/testdata/valid_config.yml +++ /dev/null @@ -1,26 +0,0 @@ -extensions: - basicauth/client: - client_auth: - username: username - password: password - basicauth/server: - htpasswd: - inline: | - username1:password1 - username2:password2 - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: - -service: - extensions: [basicauth/client, basicauth/server] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] diff --git a/extension/bearertokenauthextension/config_test.go b/extension/bearertokenauthextension/config_test.go index 3cf0cf639f6c..fbeacdb4ab8c 100644 --- a/extension/bearertokenauthextension/config_test.go +++ b/extension/bearertokenauthextension/config_test.go @@ -20,46 +20,45 @@ 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" ) 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.NoError(t, err) - require.NotNil(t, cfg) - - expected := factory.CreateDefaultConfig().(*Config) - expected.BearerToken = "sometoken" - - ext0 := cfg.Extensions[config.NewComponentID(typeStr)] - assert.Equal(t, expected, ext0) - - ext1 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "1")] - assert.Equal(t, - &Config{ - ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "1")), - BearerToken: "sometesttoken", + tests := []struct { + id config.ComponentID + expected config.Extension + expectedErr bool + }{ + { + id: config.NewComponentID(typeStr), + expectedErr: true, }, - ext1) - - assert.Equal(t, 1, len(cfg.Service.Extensions)) - assert.Equal(t, config.NewComponentIDWithName(typeStr, "1"), cfg.Service.Extensions[0]) -} - -func TestLoadConfigError(t *testing.T) { - factories, err := componenttest.NopFactories() - assert.NoError(t, err) - - factory := NewFactory() - factories.Extensions[typeStr] = factory - _, err = servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config_missing_token.yaml"), factories) - require.Error(t, err) + { + id: config.NewComponentIDWithName(typeStr, "sometoken"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + BearerToken: "sometoken", + }, + }, + } + 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)) + if tt.expectedErr { + assert.Error(t, cfg.Validate()) + return + } + assert.NoError(t, cfg.Validate()) + assert.Equal(t, tt.expected, cfg) + }) + } } diff --git a/extension/bearertokenauthextension/testdata/config.yaml b/extension/bearertokenauthextension/testdata/config.yaml index 3672ac2e6995..eac0b345ea9c 100644 --- a/extension/bearertokenauthextension/testdata/config.yaml +++ b/extension/bearertokenauthextension/testdata/config.yaml @@ -1,21 +1,3 @@ -extensions: - bearertokenauth: - token: "sometoken" - bearertokenauth/1: - token: "sometesttoken" - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: - -service: - extensions: [bearertokenauth/1] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] +bearertokenauth: +bearertokenauth/sometoken: + token: "sometoken" diff --git a/extension/bearertokenauthextension/testdata/config_missing_token.yaml b/extension/bearertokenauthextension/testdata/config_missing_token.yaml deleted file mode 100644 index ccd474c560a0..000000000000 --- a/extension/bearertokenauthextension/testdata/config_missing_token.yaml +++ /dev/null @@ -1,18 +0,0 @@ -extensions: - bearertokenauth: - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: - -service: - extensions: [bearertokenauth] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] diff --git a/extension/fluentbitextension/config_test.go b/extension/fluentbitextension/config_test.go index affc081bdc6e..8fcbcff41e3d 100644 --- a/extension/fluentbitextension/config_test.go +++ b/extension/fluentbitextension/config_test.go @@ -20,33 +20,41 @@ 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" ) 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) - - 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")), - ExecutablePath: "/usr/local/bin/fluent-bit", + t.Parallel() + + tests := []struct { + id config.ComponentID + expected config.Extension + }{ + { + id: config.NewComponentID(typeStr), + expected: NewFactory().CreateDefaultConfig(), }, - ext1) - - assert.Equal(t, 1, len(cfg.Service.Extensions)) - assert.Equal(t, config.NewComponentIDWithName(typeStr, "1"), cfg.Service.Extensions[0]) + { + id: config.NewComponentIDWithName(typeStr, "1"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + ExecutablePath: "/usr/local/bin/fluent-bit", + }, + }, + } + 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.NoError(t, cfg.Validate()) + assert.Equal(t, tt.expected, cfg) + }) + } } diff --git a/extension/fluentbitextension/testdata/config.yaml b/extension/fluentbitextension/testdata/config.yaml index 7c955584ea3f..822ee5979d92 100644 --- a/extension/fluentbitextension/testdata/config.yaml +++ b/extension/fluentbitextension/testdata/config.yaml @@ -1,20 +1,3 @@ -extensions: - fluentbit: - fluentbit/1: - executable_path: /usr/local/bin/fluent-bit - -service: - extensions: [fluentbit/1] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: +fluentbit: +fluentbit/1: + executable_path: /usr/local/bin/fluent-bit diff --git a/extension/headerssetter/config_test.go b/extension/headerssetter/config_test.go index 7bbe2351facf..b33c52f95b48 100644 --- a/extension/headerssetter/config_test.go +++ b/extension/headerssetter/config_test.go @@ -20,47 +20,52 @@ 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" ) 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, - &Config{ - ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), - HeadersConfig: []HeaderConfig{ - { - Key: stringp("X-Scope-OrgID"), - FromContext: stringp("tenant_id"), - Value: nil, - }, - { - Key: stringp("User-ID"), - FromContext: stringp("user_id"), - Value: nil, + 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)), + HeadersConfig: []HeaderConfig{ + { + Key: stringp("X-Scope-OrgID"), + FromContext: stringp("tenant_id"), + Value: nil, + }, + { + Key: stringp("User-ID"), + FromContext: stringp("user_id"), + Value: nil, + }, }, }, }, - ext0) - - assert.Equal(t, 1, len(cfg.Service.Extensions)) - assert.Equal(t, config.NewComponentID(typeStr), cfg.Service.Extensions[0]) + } + 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, tt.expected, cfg) + }) + } } func TestValidateConfig(t *testing.T) { diff --git a/extension/headerssetter/testdata/config.yaml b/extension/headerssetter/testdata/config.yaml index c62e258e860e..40a51552c949 100644 --- a/extension/headerssetter/testdata/config.yaml +++ b/extension/headerssetter/testdata/config.yaml @@ -1,23 +1,7 @@ -extensions: - headers_setter: - headers: - - key: X-Scope-OrgID - from_context: "tenant_id" - - key: User-ID - from_context: "user_id" - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: - -service: - extensions: [headers_setter] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] +headers_setter: +headers_setter/1: + headers: + - key: X-Scope-OrgID + from_context: "tenant_id" + - key: User-ID + from_context: "user_id" diff --git a/extension/healthcheckextension/config_test.go b/extension/healthcheckextension/config_test.go index 17646d21f2c0..998898c6053d 100644 --- a/extension/healthcheckextension/config_test.go +++ b/extension/healthcheckextension/config_test.go @@ -20,77 +20,70 @@ 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/confighttp" "go.opentelemetry.io/collector/config/configtls" - "go.opentelemetry.io/collector/service/servicetest" + "go.opentelemetry.io/collector/confmap/confmaptest" ) 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")), - HTTPServerSettings: confighttp.HTTPServerSettings{ - Endpoint: "localhost:13", - TLSSetting: &configtls.TLSServerSetting{ - TLSSetting: configtls.TLSSetting{ - CAFile: "/path/to/ca", - CertFile: "/path/to/cert", - KeyFile: "/path/to/key", + tests := []struct { + id config.ComponentID + expected config.Extension + expectedErr error + }{ + { + id: config.NewComponentID(typeStr), + expected: NewFactory().CreateDefaultConfig(), + }, + { + id: config.NewComponentIDWithName(typeStr, "1"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + HTTPServerSettings: confighttp.HTTPServerSettings{ + Endpoint: "localhost:13", + TLSSetting: &configtls.TLSServerSetting{ + TLSSetting: configtls.TLSSetting{ + CAFile: "/path/to/ca", + CertFile: "/path/to/cert", + KeyFile: "/path/to/key", + }, }, }, + CheckCollectorPipeline: defaultCheckCollectorPipelineSettings(), + Path: "/", }, - CheckCollectorPipeline: defaultCheckCollectorPipelineSettings(), - Path: "/", }, - ext1) - - assert.Equal(t, 1, len(cfg.Service.Extensions)) - assert.Equal(t, config.NewComponentIDWithName(typeStr, "1"), cfg.Service.Extensions[0]) -} - -func TestLoadConfigError(t *testing.T) { - factories, err := componenttest.NopFactories() - assert.NoError(t, err) - - tests := []struct { - configName string - expectedErr error - }{ { - "missingendpoint", - errNoEndpointProvided, + id: config.NewComponentIDWithName(typeStr, "missingendpoint"), + expectedErr: errNoEndpointProvided, }, { - "invalidthreshold", - errInvalidExporterFailureThresholdProvided, + id: config.NewComponentIDWithName(typeStr, "invalidthreshold"), + expectedErr: errInvalidExporterFailureThresholdProvided, }, { - "invalidpath", - errInvalidPath, + id: config.NewComponentIDWithName(typeStr, "invalidpath"), + expectedErr: errInvalidPath, }, } for _, tt := range tests { - factory := NewFactory() - factories.Extensions[typeStr] = factory - cfg, _ := servicetest.LoadConfig(filepath.Join("testdata", "config_bad.yaml"), factories) - extension := cfg.Extensions[config.NewComponentIDWithName(typeStr, tt.configName)] - err := extension.Validate() - require.ErrorIs(t, err, 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) + }) } } diff --git a/extension/healthcheckextension/testdata/config.yaml b/extension/healthcheckextension/testdata/config.yaml index 4f2a71822b06..b9ff04c3e8df 100644 --- a/extension/healthcheckextension/testdata/config.yaml +++ b/extension/healthcheckextension/testdata/config.yaml @@ -1,35 +1,30 @@ -extensions: - health_check: - health_check/1: - endpoint: "localhost:13" - tls: - ca_file: "/path/to/ca" - key_file: "/path/to/key" - cert_file: "/path/to/cert" - check_collector_pipeline: - enabled: false - interval: "5m" - exporter_failure_threshold: 5 - health_check/2: - endpoint: "localhost:13" - path: "/health" - check_collector_pipeline: - enabled: false - interval: "5m" - exporter_failure_threshold: 5 - -service: - extensions: [health_check/1] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: +health_check: +health_check/1: + endpoint: "localhost:13" + tls: + ca_file: "/path/to/ca" + key_file: "/path/to/key" + cert_file: "/path/to/cert" + check_collector_pipeline: + enabled: false + interval: "5m" + exporter_failure_threshold: 5 +health_check/missingendpoint: + endpoint: "" + check_collector_pipeline: + enabled: false + interval: "5m" + exporter_failure_threshold: 5 +health_check/invalidthreshold: + endpoint: "localhost:13" + check_collector_pipeline: + enabled: false + interval: "5m" + exporter_failure_threshold: -1 +health_check/invalidpath: + endpoint: "localhost:13" + path: "invalid" + check_collector_pipeline: + enabled: false + interval: "5m" + exporter_failure_threshold: 5 diff --git a/extension/healthcheckextension/testdata/config_bad.yaml b/extension/healthcheckextension/testdata/config_bad.yaml deleted file mode 100644 index b419b73979b9..000000000000 --- a/extension/healthcheckextension/testdata/config_bad.yaml +++ /dev/null @@ -1,37 +0,0 @@ -extensions: - health_check: - health_check/missingendpoint: - endpoint: "" - check_collector_pipeline: - enabled: false - interval: "5m" - exporter_failure_threshold: 5 - health_check/invalidthreshold: - endpoint: "localhost:13" - check_collector_pipeline: - enabled: false - interval: "5m" - exporter_failure_threshold: -1 - health_check/invalidpath: - endpoint: "localhost:13" - path: "invalid" - check_collector_pipeline: - enabled: false - interval: "5m" - exporter_failure_threshold: 5 - -service: - extensions: [health_check/1] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: \ No newline at end of file diff --git a/extension/httpforwarder/config_test.go b/extension/httpforwarder/config_test.go new file mode 100644 index 000000000000..aa4ce687e9cb --- /dev/null +++ b/extension/httpforwarder/config_test.go @@ -0,0 +1,70 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package httpforwarder + +import ( + "path/filepath" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/config/confighttp" + "go.opentelemetry.io/collector/confmap/confmaptest" +) + +func TestLoadConfig(t *testing.T) { + t.Parallel() + + 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)), + Ingress: confighttp.HTTPServerSettings{ + Endpoint: "http://localhost:7070", + }, + Egress: confighttp.HTTPClientSettings{ + Endpoint: "http://target/", + Headers: map[string]string{ + "otel_http_forwarder": "dev", + }, + Timeout: 5 * time.Second, + }, + }, + }, + } + 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.NoError(t, cfg.Validate()) + assert.Equal(t, tt.expected, cfg) + }) + } +} diff --git a/extension/httpforwarder/testdata/config.yaml b/extension/httpforwarder/testdata/config.yaml index b0e17a98fbbf..1501cdd95903 100644 --- a/extension/httpforwarder/testdata/config.yaml +++ b/extension/httpforwarder/testdata/config.yaml @@ -1,26 +1,9 @@ -extensions: - http_forwarder: - http_forwarder/1: - ingress: - endpoint: http://localhost:7070 - egress: - endpoint: http://target/ - headers: - otel_http_forwarder: dev - timeout: 5s - -service: - extensions: [http_forwarder/1] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: +http_forwarder: +http_forwarder/1: + ingress: + endpoint: http://localhost:7070 + egress: + endpoint: http://target/ + headers: + otel_http_forwarder: dev + timeout: 5s diff --git a/extension/jaegerremotesampling/config_test.go b/extension/jaegerremotesampling/config_test.go index e89956fe768c..affd601cebf1 100644 --- a/extension/jaegerremotesampling/config_test.go +++ b/extension/jaegerremotesampling/config_test.go @@ -21,59 +21,65 @@ 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/configgrpc" "go.opentelemetry.io/collector/config/confighttp" "go.opentelemetry.io/collector/config/confignet" - "go.opentelemetry.io/collector/service/servicetest" + "go.opentelemetry.io/collector/confmap/confmaptest" ) 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.NoError(t, err) - require.NotNil(t, cfg) - - ext0 := cfg.Extensions[config.NewComponentID(typeStr)] - assert.Equal(t, - &Config{ - ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), - HTTPServerSettings: &confighttp.HTTPServerSettings{Endpoint: ":5778"}, - GRPCServerSettings: &configgrpc.GRPCServerSettings{NetAddr: confignet.NetAddr{ - Endpoint: ":14250", - Transport: "tcp", - }}, - Source: Source{ - Remote: &configgrpc.GRPCClientSettings{ - Endpoint: "jaeger-collector:14250", + tests := []struct { + id config.ComponentID + expected config.Extension + }{ + { + id: config.NewComponentID(typeStr), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + HTTPServerSettings: &confighttp.HTTPServerSettings{Endpoint: ":5778"}, + GRPCServerSettings: &configgrpc.GRPCServerSettings{NetAddr: confignet.NetAddr{ + Endpoint: ":14250", + Transport: "tcp", + }}, + Source: Source{ + Remote: &configgrpc.GRPCClientSettings{ + Endpoint: "jaeger-collector:14250", + }, }, }, }, - ext0) - - ext1 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "1")] - assert.Equal(t, - &Config{ - ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "1")), - HTTPServerSettings: &confighttp.HTTPServerSettings{Endpoint: ":5778"}, - GRPCServerSettings: &configgrpc.GRPCServerSettings{NetAddr: confignet.NetAddr{ - Endpoint: ":14250", - Transport: "tcp", - }}, - Source: Source{ - ReloadInterval: time.Second, - File: "/etc/otelcol/sampling_strategies.json", + { + id: config.NewComponentIDWithName(typeStr, "1"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + HTTPServerSettings: &confighttp.HTTPServerSettings{Endpoint: ":5778"}, + GRPCServerSettings: &configgrpc.GRPCServerSettings{NetAddr: confignet.NetAddr{ + Endpoint: ":14250", + Transport: "tcp", + }}, + Source: Source{ + ReloadInterval: time.Second, + File: "/etc/otelcol/sampling_strategies.json", + }, }, }, - ext1) - assert.Equal(t, 1, len(cfg.Service.Extensions)) - assert.Equal(t, config.NewComponentIDWithName(typeStr, "1"), cfg.Service.Extensions[0]) + } + 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.NoError(t, cfg.Validate()) + assert.Equal(t, tt.expected, cfg) + }) + } } func TestValidate(t *testing.T) { diff --git a/extension/jaegerremotesampling/testdata/config.yaml b/extension/jaegerremotesampling/testdata/config.yaml index 9f1587338ffb..ec8bd9d9814a 100644 --- a/extension/jaegerremotesampling/testdata/config.yaml +++ b/extension/jaegerremotesampling/testdata/config.yaml @@ -1,25 +1,8 @@ -extensions: - jaegerremotesampling: - source: - remote: - endpoint: jaeger-collector:14250 - jaegerremotesampling/1: - source: - reload_interval: 1s - file: /etc/otelcol/sampling_strategies.json - -service: - extensions: [jaegerremotesampling/1] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: +jaegerremotesampling: + source: + remote: + endpoint: jaeger-collector:14250 +jaegerremotesampling/1: + source: + reload_interval: 1s + file: /etc/otelcol/sampling_strategies.json diff --git a/extension/oauth2clientauthextension/config_test.go b/extension/oauth2clientauthextension/config_test.go index 4394b3e8b145..d3aea6c7d48e 100644 --- a/extension/oauth2clientauthextension/config_test.go +++ b/extension/oauth2clientauthextension/config_test.go @@ -22,99 +22,80 @@ 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/configtls" - "go.opentelemetry.io/collector/service/servicetest" + "go.opentelemetry.io/collector/confmap/confmaptest" ) 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) - - require.NoError(t, err) - require.NotNil(t, cfg) - - expected := factory.CreateDefaultConfig().(*Config) - expected.ClientSecret = "someclientsecret" - expected.ClientID = "someclientid" - expected.Scopes = []string{"api.metrics"} - expected.TokenURL = "https://example.com/oauth2/default/v1/token" - - ext := cfg.Extensions[config.NewComponentIDWithName(typeStr, "1")] - assert.Equal(t, - &Config{ - ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "1")), - ClientSecret: "someclientsecret", - ClientID: "someclientid", - EndpointParams: url.Values{"audience": []string{"someaudience"}}, - Scopes: []string{"api.metrics"}, - TokenURL: "https://example.com/oauth2/default/v1/token", - Timeout: time.Second, - }, - ext) - - assert.Equal(t, 2, len(cfg.Service.Extensions)) - assert.Equal(t, config.NewComponentIDWithName(typeStr, "1"), cfg.Service.Extensions[0]) -} - -func TestConfigTLSSettings(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) - - require.NoError(t, err) - require.NotNil(t, cfg) - - ext2 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "withtls")] - - cfg2 := ext2.(*Config) - assert.Equal(t, cfg2.TLSSetting, configtls.TLSClientSetting{ - TLSSetting: configtls.TLSSetting{ - CAFile: "cafile", - CertFile: "certfile", - KeyFile: "keyfile", - }, - Insecure: true, - InsecureSkipVerify: false, - ServerName: "", - }) -} - -func TestLoadConfigError(t *testing.T) { - factories, err := componenttest.NopFactories() - assert.NoError(t, err) + t.Parallel() tests := []struct { - configName string + id config.ComponentID + expected config.Extension expectedErr error }{ { - "missingurl", - errNoTokenURLProvided, + id: config.NewComponentID(typeStr), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + ClientSecret: "someclientsecret", + ClientID: "someclientid", + EndpointParams: url.Values{"audience": []string{"someaudience"}}, + Scopes: []string{"api.metrics"}, + TokenURL: "https://example.com/oauth2/default/v1/token", + Timeout: time.Second, + }, + }, + { + id: config.NewComponentIDWithName(typeStr, "withtls"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + ClientSecret: "someclientsecret2", + ClientID: "someclientid2", + Scopes: []string{"api.metrics"}, + TokenURL: "https://example2.com/oauth2/default/v1/token", + Timeout: time.Second, + TLSSetting: configtls.TLSClientSetting{ + TLSSetting: configtls.TLSSetting{ + CAFile: "cafile", + CertFile: "certfile", + KeyFile: "keyfile", + }, + Insecure: true, + InsecureSkipVerify: false, + ServerName: "", + }, + }, + }, + { + id: config.NewComponentIDWithName(typeStr, "missingurl"), + expectedErr: errNoTokenURLProvided, }, { - "missingid", - errNoClientIDProvided, + id: config.NewComponentIDWithName(typeStr, "missingid"), + expectedErr: errNoClientIDProvided, }, { - "missingsecret", - errNoClientSecretProvided, + id: config.NewComponentIDWithName(typeStr, "missingsecret"), + expectedErr: errNoClientSecretProvided, }, } for _, tt := range tests { - factory := NewFactory() - factories.Extensions[typeStr] = factory - cfg, _ := servicetest.LoadConfig(filepath.Join("testdata", "config_bad.yaml"), factories) - 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) + }) } } diff --git a/extension/oauth2clientauthextension/testdata/config.yaml b/extension/oauth2clientauthextension/testdata/config.yaml index be27f1a0102b..20cd3c8cc134 100644 --- a/extension/oauth2clientauthextension/testdata/config.yaml +++ b/extension/oauth2clientauthextension/testdata/config.yaml @@ -1,40 +1,36 @@ -extensions: - oauth2client/1: - client_id: someclientid - client_secret: someclientsecret - token_url: https://example.com/oauth2/default/v1/token - endpoint_params: - audience: someaudience - scopes: ["api.metrics"] - timeout: 1s +oauth2client: + client_id: someclientid + client_secret: someclientsecret + token_url: https://example.com/oauth2/default/v1/token + endpoint_params: + audience: someaudience + scopes: ["api.metrics"] + timeout: 1s - oauth2client/withtls: - client_id: someclientid2 - client_secret: someclientsecret2 - token_url: https://example2.com/oauth2/default/v1/token - scopes: ["api.metrics"] - timeout: 1s - # tls settings for the token client - tls: - insecure: true - ca_file: cafile - cert_file: certfile - key_file: keyfile +oauth2client/withtls: + client_id: someclientid2 + client_secret: someclientsecret2 + token_url: https://example2.com/oauth2/default/v1/token + scopes: ["api.metrics"] + timeout: 1s + # tls settings for the token client + tls: + insecure: true + ca_file: cafile + cert_file: certfile + key_file: keyfile +oauth2client/missingid: + client_secret: someclientsecret + token_url: https://example.com/oauth2/default/v1/token + scopes: ["api.metrics"] +oauth2client/missingsecret: + client_id: someclientid + token_url: https://example.com/oauth2/default/v1/token + scopes: ["api.metrics"] -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: - -service: - extensions: [oauth2client/1, oauth2client/withtls] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] +oauth2client/missingurl: + client_id: someclientid + client_secret: someclientsecret + scopes: ["api.metrics"] diff --git a/extension/oauth2clientauthextension/testdata/config_bad.yaml b/extension/oauth2clientauthextension/testdata/config_bad.yaml deleted file mode 100644 index 12044aa62817..000000000000 --- a/extension/oauth2clientauthextension/testdata/config_bad.yaml +++ /dev/null @@ -1,33 +0,0 @@ -extensions: - oauth2client/missingid: - client_secret: someclientsecret - token_url: https://example.com/oauth2/default/v1/token - scopes: ["api.metrics"] - - oauth2client/missingsecret: - client_id: someclientid - token_url: https://example.com/oauth2/default/v1/token - scopes: ["api.metrics"] - - oauth2client/missingurl: - client_id: someclientid - client_secret: someclientsecret - scopes: ["api.metrics"] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: - -service: - extensions: [oauth2client/missingid, - oauth2client/missingsecret, - oauth2client/missingurl] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] \ No newline at end of file diff --git a/extension/observer/dockerobserver/config_test.go b/extension/observer/dockerobserver/config_test.go index b5dfab0a242b..f9f9294fe099 100644 --- a/extension/observer/dockerobserver/config_test.go +++ b/extension/observer/dockerobserver/config_test.go @@ -21,41 +21,43 @@ 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" ) 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) - - require.Len(t, cfg.Extensions, 6) - - ext0 := cfg.Extensions[config.NewComponentID(typeStr)] - assert.Equal(t, factory.CreateDefaultConfig(), ext0) - - ext1 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "all_settings")] - assert.Equal(t, - &Config{ - Endpoint: "unix:///var/run/docker.sock", - ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "all_settings")), - CacheSyncInterval: 5 * time.Minute, - Timeout: 20 * time.Second, - ExcludedImages: []string{"excluded", "image"}, - UseHostnameIfPresent: true, - UseHostBindings: true, - IgnoreNonHostBindings: true, - DockerAPIVersion: 1.22, + tests := []struct { + id config.ComponentID + expected config.Extension + }{ + { + id: config.NewComponentID(typeStr), + expected: NewFactory().CreateDefaultConfig(), }, - ext1) + { + id: config.NewComponentIDWithName(typeStr, "all_settings"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + Endpoint: "unix:///var/run/docker.sock", + CacheSyncInterval: 5 * time.Minute, + Timeout: 20 * time.Second, + ExcludedImages: []string{"excluded", "image"}, + UseHostnameIfPresent: true, + UseHostBindings: true, + IgnoreNonHostBindings: true, + DockerAPIVersion: 1.22, + }, + }, + } + for _, tt := range tests { + t.Run(tt.id.String(), func(t *testing.T) { + cfg := loadConfig(t, tt.id) + assert.NoError(t, cfg.Validate()) + assert.Equal(t, tt.expected, cfg) + }) + } } func TestValidateConfig(t *testing.T) { @@ -74,3 +76,15 @@ func TestValidateConfig(t *testing.T) { cfg = &Config{Endpoint: "someEndpoint", DockerAPIVersion: 1.22, Timeout: 5 * time.Minute, CacheSyncInterval: 5 * time.Minute} assert.Nil(t, cfg.Validate()) } + +func loadConfig(t testing.TB, id config.ComponentID) *Config { + cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) + require.NoError(t, err) + factory := NewFactory() + cfg := factory.CreateDefaultConfig() + sub, err := cm.Sub(id.String()) + require.NoError(t, err) + require.NoError(t, config.UnmarshalExtension(sub, cfg)) + + return cfg.(*Config) +} diff --git a/extension/observer/dockerobserver/extension_test.go b/extension/observer/dockerobserver/extension_test.go index 6c6f7326b896..94eb1729ed9e 100644 --- a/extension/observer/dockerobserver/extension_test.go +++ b/extension/observer/dockerobserver/extension_test.go @@ -21,11 +21,8 @@ import ( "testing" dtypes "github.com/docker/docker/api/types" - "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.uber.org/zap" "github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer" @@ -107,19 +104,8 @@ func TestCollectEndpointsDefaultConfig(t *testing.T) { } func TestCollectEndpointsAllConfigSettings(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) - - require.Nil(t, err) - require.NotNil(t, cfg) - - extAllSettings := cfg.Extensions[config.NewComponentIDWithName(typeStr, "all_settings")] - - ext, err := newObserver(zap.NewNop(), extAllSettings.(*Config)) + extAllSettings := loadConfig(t, config.NewComponentIDWithName(typeStr, "all_settings")) + ext, err := newObserver(zap.NewNop(), extAllSettings) require.NoError(t, err) require.NotNil(t, ext) @@ -155,19 +141,8 @@ func TestCollectEndpointsAllConfigSettings(t *testing.T) { } func TestCollectEndpointsUseHostnameIfPresent(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) - - require.Nil(t, err) - require.NotNil(t, cfg) - - extUseHostname := cfg.Extensions[config.NewComponentIDWithName(typeStr, "use_hostname_if_present")] - - ext, err := newObserver(zap.NewNop(), extUseHostname.(*Config)) + extUseHostname := loadConfig(t, config.NewComponentIDWithName(typeStr, "use_hostname_if_present")) + ext, err := newObserver(zap.NewNop(), extUseHostname) require.NoError(t, err) require.NotNil(t, ext) @@ -203,19 +178,8 @@ func TestCollectEndpointsUseHostnameIfPresent(t *testing.T) { } func TestCollectEndpointsUseHostBindings(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) - - require.Nil(t, err) - require.NotNil(t, cfg) - - extHostBindings := cfg.Extensions[config.NewComponentIDWithName(typeStr, "use_host_bindings")] - - ext, err := newObserver(zap.NewNop(), extHostBindings.(*Config)) + extHostBindings := loadConfig(t, config.NewComponentIDWithName(typeStr, "use_host_bindings")) + ext, err := newObserver(zap.NewNop(), extHostBindings) require.NoError(t, err) require.NotNil(t, ext) @@ -251,19 +215,8 @@ func TestCollectEndpointsUseHostBindings(t *testing.T) { } func TestCollectEndpointsIgnoreNonHostBindings(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) - - require.Nil(t, err) - require.NotNil(t, cfg) - - extIgnoreHostBindings := cfg.Extensions[config.NewComponentIDWithName(typeStr, "ignore_non_host_bindings")] - - ext, err := newObserver(zap.NewNop(), extIgnoreHostBindings.(*Config)) + extIgnoreHostBindings := loadConfig(t, config.NewComponentIDWithName(typeStr, "ignore_non_host_bindings")) + ext, err := newObserver(zap.NewNop(), extIgnoreHostBindings) require.NoError(t, err) require.NotNil(t, ext) diff --git a/extension/observer/dockerobserver/testdata/config.yaml b/extension/observer/dockerobserver/testdata/config.yaml index f32d74326318..cb9440041ac8 100644 --- a/extension/observer/dockerobserver/testdata/config.yaml +++ b/extension/observer/dockerobserver/testdata/config.yaml @@ -1,34 +1,17 @@ -extensions: - docker_observer: - docker_observer/all_settings: - endpoint: "unix:///var/run/docker.sock" - timeout: 20s - excluded_images: ["excluded", "image"] - use_hostname_if_present: true - use_host_bindings: true - ignore_non_host_bindings: true - cache_sync_interval: 5m - docker_observer/use_hostname_if_present: - use_hostname_if_present: true - docker_observer/use_host_bindings: - use_host_bindings: true - docker_observer/ignore_non_host_bindings: - ignore_non_host_bindings: true - docker_observer/exclude_nginx: - excluded_images: ["nginx"] - -service: - extensions: [docker_observer/all_settings, docker_observer/exclude_nginx] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: +docker_observer: +docker_observer/all_settings: + endpoint: "unix:///var/run/docker.sock" + timeout: 20s + excluded_images: ["excluded", "image"] + use_hostname_if_present: true + use_host_bindings: true + ignore_non_host_bindings: true + cache_sync_interval: 5m +docker_observer/use_hostname_if_present: + use_hostname_if_present: true +docker_observer/use_host_bindings: + use_host_bindings: true +docker_observer/ignore_non_host_bindings: + ignore_non_host_bindings: true +docker_observer/exclude_nginx: + excluded_images: ["nginx"] diff --git a/extension/observer/ecsobserver/config.go b/extension/observer/ecsobserver/config.go index d903925e1252..27c87bc00d6a 100644 --- a/extension/observer/ecsobserver/config.go +++ b/extension/observer/ecsobserver/config.go @@ -98,13 +98,14 @@ func DefaultConfig() Config { // exampleConfig returns an example instance that matches testdata/config_example.yaml. // It can be used to validate if the struct tags like mapstructure, yaml are working properly. -func exampleConfig() Config { - return Config{ - ClusterName: "ecs-sd-test-1", - ClusterRegion: "us-west-2", - ResultFile: "/etc/ecs_sd_targets.yaml", - RefreshInterval: 15 * time.Second, - JobLabelName: defaultJobLabelName, +func exampleConfig() *Config { + return &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + ClusterName: "ecs-sd-test-1", + ClusterRegion: "us-west-2", + ResultFile: "/etc/ecs_sd_targets.yaml", + RefreshInterval: 15 * time.Second, + JobLabelName: defaultJobLabelName, Services: []ServiceConfig{ { NamePattern: "^retail-.*$", diff --git a/extension/observer/ecsobserver/config_test.go b/extension/observer/ecsobserver/config_test.go index e35b4bfc61bc..99c553cf21ab 100644 --- a/extension/observer/ecsobserver/config_test.go +++ b/extension/observer/ecsobserver/config_test.go @@ -20,63 +20,72 @@ 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" ) 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) - - require.Len(t, cfg.Extensions, 4) - - // Default - ext0 := cfg.Extensions[config.NewComponentID(typeStr)] - assert.Equal(t, factory.CreateDefaultConfig(), ext0) - - // Merge w/ Default - ext1 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "1")] - assert.Equal(t, DefaultConfig().ClusterName, ext1.(*Config).ClusterName) - assert.NotEqual(t, DefaultConfig().ClusterRegion, ext1.(*Config).ClusterRegion) - assert.Equal(t, "my_prometheus_job", ext1.(*Config).JobLabelName) - - // Example Config - ext2 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "2")] - ext2Expected := exampleConfig() - ext2Expected.ExtensionSettings = config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "2")) - assert.Equal(t, &ext2Expected, ext2) - - // Override docker label from default - ext3 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "3")] - ext3Expected := DefaultConfig() - ext3Expected.ExtensionSettings = config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "3")) - ext3Expected.DockerLabels = []DockerLabelConfig{ + tests := []struct { + id config.ComponentID + expected config.Extension + expectedErr bool + }{ { - PortLabel: "IS_NOT_DEFAULT", + id: config.NewComponentID(typeStr), + expected: NewFactory().CreateDefaultConfig(), + }, + { + id: config.NewComponentIDWithName(typeStr, "1"), + expected: func() config.Extension { + cfg := DefaultConfig() + cfg.ClusterRegion = "us-west-2" + cfg.JobLabelName = "my_prometheus_job" + return &cfg + }(), + }, + { + id: config.NewComponentIDWithName(typeStr, "2"), + expected: exampleConfig(), + }, + { + id: config.NewComponentIDWithName(typeStr, "3"), + expected: func() config.Extension { + cfg := DefaultConfig() + cfg.DockerLabels = []DockerLabelConfig{ + { + PortLabel: "IS_NOT_DEFAULT", + }, + } + return &cfg + }(), + }, + { + id: config.NewComponentIDWithName(typeStr, "invalid"), + expectedErr: true, }, } - assert.Equal(t, &ext3Expected, ext3) + 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)) + if tt.expectedErr { + assert.Error(t, cfg.Validate()) + return + } + assert.NoError(t, cfg.Validate()) + assert.Equal(t, tt.expected, cfg) + }) + } } func TestConfig_Validate(t *testing.T) { - t.Run("load", func(t *testing.T) { - factories, err := componenttest.NopFactories() - require.NoError(t, err) - - factory := NewFactory() - factories.Extensions[typeStr] = factory - _, err = servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config_invalid.yaml"), factories) - require.Error(t, err) - }) - cases := []struct { reason string cfg Config diff --git a/extension/observer/ecsobserver/testdata/config.yaml b/extension/observer/ecsobserver/testdata/config.yaml index 26b0f1c62416..1a541075afa6 100644 --- a/extension/observer/ecsobserver/testdata/config.yaml +++ b/extension/observer/ecsobserver/testdata/config.yaml @@ -1,40 +1,30 @@ -extensions: - ecs_observer: - ecs_observer/1: - cluster_region: 'us-west-2' - job_label_name: 'my_prometheus_job' - ecs_observer/2: - cluster_name: 'ecs-sd-test-1' - cluster_region: 'us-west-2' - result_file: '/etc/ecs_sd_targets.yaml' - refresh_interval: 15s - services: - - name_pattern: '^retail-.*$' - task_definitions: - - job_name: 'task_def_1' - metrics_path: '/not/metrics' - metrics_ports: - - 9113 - - 9090 - arn_pattern: '.*:task-definition/nginx:[0-9]+' - docker_labels: - - port_label: 'ECS_PROMETHEUS_EXPORTER_PORT' - ecs_observer/3: - docker_labels: - - port_label: 'IS_NOT_DEFAULT' - -service: - extensions: [ ecs_observer, ecs_observer/1 ] - pipelines: - traces: - receivers: [ nop ] - processors: [ nop ] - exporters: [ nop ] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: +ecs_observer: +ecs_observer/1: + cluster_region: 'us-west-2' + job_label_name: 'my_prometheus_job' +ecs_observer/2: + cluster_name: 'ecs-sd-test-1' + cluster_region: 'us-west-2' + result_file: '/etc/ecs_sd_targets.yaml' + refresh_interval: 15s + services: + - name_pattern: '^retail-.*$' + task_definitions: + - job_name: 'task_def_1' + metrics_path: '/not/metrics' + metrics_ports: + - 9113 + - 9090 + arn_pattern: '.*:task-definition/nginx:[0-9]+' + docker_labels: + - port_label: 'ECS_PROMETHEUS_EXPORTER_PORT' +ecs_observer/3: + docker_labels: + - port_label: 'IS_NOT_DEFAULT' +ecs_observer/invalid: + cluster_name: 'ecs-sd-test-1' + cluster_region: 'us-west-2' + result_file: '/etc/ecs_sd_targets.yaml' + refresh_interval: 15s + services: + - name_pattern: '*' # invalid regex diff --git a/extension/observer/ecsobserver/testdata/config_invalid.yaml b/extension/observer/ecsobserver/testdata/config_invalid.yaml deleted file mode 100644 index 1ab1f087152f..000000000000 --- a/extension/observer/ecsobserver/testdata/config_invalid.yaml +++ /dev/null @@ -1,24 +0,0 @@ -extensions: - ecs_observer: - cluster_name: 'ecs-sd-test-1' - cluster_region: 'us-west-2' - result_file: '/etc/ecs_sd_targets.yaml' - refresh_interval: 15s - services: - - name_pattern: '*' # invalid regex - -service: - extensions: [ ecs_observer ] - pipelines: - traces: - receivers: [ nop ] - processors: [ nop ] - exporters: [ nop ] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: diff --git a/extension/observer/ecstaskobserver/config_test.go b/extension/observer/ecstaskobserver/config_test.go index af985cf5cf8c..139e80c457e0 100644 --- a/extension/observer/ecstaskobserver/config_test.go +++ b/extension/observer/ecstaskobserver/config_test.go @@ -21,53 +21,62 @@ 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/confighttp" - "go.opentelemetry.io/collector/service/servicetest" + "go.opentelemetry.io/collector/confmap/confmaptest" ) func TestLoadConfig(t *testing.T) { - factories, err := componenttest.NopFactories() - assert.Nil(t, err) + t.Parallel() - factory := NewFactory() - factories.Extensions[typeStr] = factory - cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories) - - require.NoError(t, err) - require.NotNil(t, cfg) - - assert.Equal(t, len(cfg.Extensions), 3) - - dflt := cfg.Extensions[config.NewComponentID(typeStr)] - assert.Equal(t, dflt, factory.CreateDefaultConfig()) - - withEndpoint := cfg.Extensions[config.NewComponentIDWithName(typeStr, "with-endpoint")].(*Config) - assert.Equal(t, &Config{ - ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "with-endpoint")), - HTTPClientSettings: confighttp.HTTPClientSettings{ - Endpoint: "http://a.valid.url:1234/path", + tests := []struct { + id config.ComponentID + expected config.Extension + expectedErr string + }{ + { + id: config.NewComponentID(typeStr), + expected: NewFactory().CreateDefaultConfig(), }, - PortLabels: []string{"ECS_TASK_OBSERVER_PORT"}, - RefreshInterval: 100 * time.Second, - }, withEndpoint) - - withPortLabels := cfg.Extensions[config.NewComponentIDWithName(typeStr, "with-port-labels")].(*Config) - assert.Equal(t, &Config{ - ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "with-port-labels")), - PortLabels: []string{"A_PORT_LABEL", "ANOTHER_PORT_LABEL"}, - RefreshInterval: 30 * time.Second, - }, withPortLabels) -} - -func TestValidateConfig(t *testing.T) { - factories, err := componenttest.NopFactories() - assert.Nil(t, err) - - factory := NewFactory() - factories.Extensions[typeStr] = factory - _, err = servicetest.LoadConfigAndValidate(filepath.Join("testdata", "invalid_endpoint_config.yaml"), factories) - require.Error(t, err) - require.EqualError(t, err, `extension "ecs_task_observer/with-invalid-endpoint" has invalid configuration: failed to parse ecs task metadata endpoint "_:invalid": parse "_:invalid": first path segment in URL cannot contain colon`) + { + id: config.NewComponentIDWithName(typeStr, "with-endpoint"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + HTTPClientSettings: confighttp.HTTPClientSettings{ + Endpoint: "http://a.valid.url:1234/path", + }, + PortLabels: []string{"ECS_TASK_OBSERVER_PORT"}, + RefreshInterval: 100 * time.Second, + }, + }, + { + id: config.NewComponentIDWithName(typeStr, "with-port-labels"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + PortLabels: []string{"A_PORT_LABEL", "ANOTHER_PORT_LABEL"}, + RefreshInterval: 30 * time.Second, + }, + }, + { + id: config.NewComponentIDWithName(typeStr, "invalid"), + expectedErr: `failed to parse ecs task metadata endpoint "_:invalid": parse "_:invalid": first path segment in URL cannot contain colon`, + }, + } + 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)) + if tt.expectedErr != "" { + assert.EqualError(t, cfg.Validate(), tt.expectedErr) + return + } + assert.NoError(t, cfg.Validate()) + assert.Equal(t, tt.expected, cfg) + }) + } } diff --git a/extension/observer/ecstaskobserver/testdata/config.yaml b/extension/observer/ecstaskobserver/testdata/config.yaml index f048416b04be..6d1957839f76 100644 --- a/extension/observer/ecstaskobserver/testdata/config.yaml +++ b/extension/observer/ecstaskobserver/testdata/config.yaml @@ -1,31 +1,10 @@ -extensions: - ecs_task_observer: - ecs_task_observer/with-endpoint: - endpoint: http://a.valid.url:1234/path - refresh_interval: 100s - ecs_task_observer/with-port-labels: - port_labels: - - A_PORT_LABEL - - ANOTHER_PORT_LABEL - -receivers: - nop: -processors: - nop: -exporters: - nop: - -service: - extensions: - - ecs_task_observer - - ecs_task_observer/with-endpoint - - ecs_task_observer/with-port-labels - pipelines: - traces: - receivers: - - nop - processors: - - nop - exporters: - - nop - +ecs_task_observer: +ecs_task_observer/with-endpoint: + endpoint: http://a.valid.url:1234/path + refresh_interval: 100s +ecs_task_observer/with-port-labels: + port_labels: + - A_PORT_LABEL + - ANOTHER_PORT_LABEL +ecs_task_observer/invalid: + endpoint: _:invalid diff --git a/extension/observer/hostobserver/config_test.go b/extension/observer/hostobserver/config_test.go index 4803b2a94cc6..8f989eb7fe6b 100644 --- a/extension/observer/hostobserver/config_test.go +++ b/extension/observer/hostobserver/config_test.go @@ -21,32 +21,41 @@ 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" ) 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) - - require.Nil(t, err) - require.NotNil(t, cfg) - - require.Len(t, cfg.Extensions, 2) - - ext0 := cfg.Extensions[config.NewComponentID(typeStr)] - assert.Equal(t, factory.CreateDefaultConfig(), ext0) - - ext1 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "all_settings")] - assert.Equal(t, - &Config{ - ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "all_settings")), - RefreshInterval: 20 * time.Second, + t.Parallel() + + tests := []struct { + id config.ComponentID + expected config.Extension + }{ + { + id: config.NewComponentID(typeStr), + expected: NewFactory().CreateDefaultConfig(), + }, + { + id: config.NewComponentIDWithName(typeStr, "all_settings"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + RefreshInterval: 20 * time.Second, + }, }, - 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.NoError(t, cfg.Validate()) + assert.Equal(t, tt.expected, cfg) + }) + } } diff --git a/extension/observer/hostobserver/testdata/config.yaml b/extension/observer/hostobserver/testdata/config.yaml index f93bb11f9aef..585c1b17dadf 100644 --- a/extension/observer/hostobserver/testdata/config.yaml +++ b/extension/observer/hostobserver/testdata/config.yaml @@ -1,20 +1,3 @@ -extensions: - host_observer: - host_observer/all_settings: - refresh_interval: 20s - -service: - extensions: [host_observer, host_observer/all_settings] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: +host_observer: +host_observer/all_settings: + refresh_interval: 20s diff --git a/extension/observer/k8sobserver/config_test.go b/extension/observer/k8sobserver/config_test.go index 4400773a3bfb..04bcf3f70240 100644 --- a/extension/observer/k8sobserver/config_test.go +++ b/extension/observer/k8sobserver/config_test.go @@ -20,70 +20,67 @@ 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" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig" ) 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) - - require.NoError(t, err) - require.NotNil(t, cfg) - - require.Len(t, cfg.Extensions, 3) - - defaultConfig := cfg.Extensions[config.NewComponentID(typeStr)] - assert.EqualValues(t, factory.CreateDefaultConfig(), defaultConfig) - - ownNodeOnly := cfg.Extensions[config.NewComponentIDWithName(typeStr, "own-node-only")] - assert.EqualValues(t, - &Config{ - ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "own-node-only")), - Node: "node-1", - APIConfig: k8sconfig.APIConfig{AuthType: k8sconfig.AuthTypeKubeConfig}, - ObservePods: true, + t.Parallel() + + tests := []struct { + id config.ComponentID + expected config.Extension + expectedErr string + }{ + { + id: config.NewComponentID(typeStr), + expected: NewFactory().CreateDefaultConfig(), }, - ownNodeOnly) - - observeAll := cfg.Extensions[config.NewComponentIDWithName(typeStr, "observe-all")] - assert.EqualValues(t, - &Config{ - ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "observe-all")), - Node: "", - APIConfig: k8sconfig.APIConfig{AuthType: k8sconfig.AuthTypeNone}, - ObservePods: true, - ObserveNodes: true, + { + id: config.NewComponentIDWithName(typeStr, "own-node-only"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + Node: "node-1", + APIConfig: k8sconfig.APIConfig{AuthType: k8sconfig.AuthTypeKubeConfig}, + ObservePods: true, + }, }, - observeAll) - -} - -func TestInvalidAuth(t *testing.T) { - factories, err := componenttest.NopFactories() - assert.NoError(t, err) - - factory := NewFactory() - factories.Extensions[typeStr] = factory - cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "invalid_auth.yaml"), factories) - require.NotNil(t, cfg) - require.EqualError(t, err, `extension "k8s_observer" has invalid configuration: invalid authType for kubernetes: not a real auth type`) -} - -func TestInvalidNoObserving(t *testing.T) { - factories, err := componenttest.NopFactories() - assert.NoError(t, err) - - factory := NewFactory() - factories.Extensions[typeStr] = factory - cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "invalid_no_observing.yaml"), factories) - require.NotNil(t, cfg) - require.EqualError(t, err, `extension "k8s_observer" has invalid configuration: one of observe_pods and observe_nodes must be true`) + { + id: config.NewComponentIDWithName(typeStr, "observe-all"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + Node: "", + APIConfig: k8sconfig.APIConfig{AuthType: k8sconfig.AuthTypeNone}, + ObservePods: true, + ObserveNodes: true, + }, + }, + { + id: config.NewComponentIDWithName(typeStr, "invalid_auth"), + expectedErr: "invalid authType for kubernetes: not a real auth type", + }, + { + id: config.NewComponentIDWithName(typeStr, "invalid_no_observing"), + expectedErr: "one of observe_pods and observe_nodes must be true", + }, + } + 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)) + if tt.expectedErr != "" { + assert.EqualError(t, cfg.Validate(), tt.expectedErr) + return + } + assert.NoError(t, cfg.Validate()) + assert.Equal(t, tt.expected, cfg) + }) + } } diff --git a/extension/observer/k8sobserver/testdata/config.yaml b/extension/observer/k8sobserver/testdata/config.yaml index 596befd492a9..b1f1205cce8c 100644 --- a/extension/observer/k8sobserver/testdata/config.yaml +++ b/extension/observer/k8sobserver/testdata/config.yaml @@ -1,28 +1,13 @@ -extensions: - k8s_observer: - k8s_observer/own-node-only: - node: node-1 - auth_type: kubeConfig - k8s_observer/observe-all: - auth_type: none - observe_nodes: true - observe_pods: true - -service: - extensions: - - k8s_observer - - k8s_observer/own-node-only - - k8s_observer/observe-all - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: +k8s_observer: +k8s_observer/own-node-only: + node: node-1 + auth_type: kubeConfig +k8s_observer/observe-all: + auth_type: none + observe_nodes: true + observe_pods: true +k8s_observer/invalid_auth: + auth_type: not a real auth type +k8s_observer/invalid_no_observing: + observe_nodes: false + observe_pods: false diff --git a/extension/observer/k8sobserver/testdata/invalid_auth.yaml b/extension/observer/k8sobserver/testdata/invalid_auth.yaml deleted file mode 100644 index 89be4c222c7f..000000000000 --- a/extension/observer/k8sobserver/testdata/invalid_auth.yaml +++ /dev/null @@ -1,20 +0,0 @@ -extensions: - k8s_observer: - auth_type: not a real auth type - -service: - extensions: - - k8s_observer - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: diff --git a/extension/observer/k8sobserver/testdata/invalid_no_observing.yaml b/extension/observer/k8sobserver/testdata/invalid_no_observing.yaml deleted file mode 100644 index 4a185a863d4d..000000000000 --- a/extension/observer/k8sobserver/testdata/invalid_no_observing.yaml +++ /dev/null @@ -1,21 +0,0 @@ -extensions: - k8s_observer: - observe_nodes: false - observe_pods: false - -service: - extensions: - - k8s_observer - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: diff --git a/extension/pprofextension/config_test.go b/extension/pprofextension/config_test.go index e1d232701401..f8380abe54bb 100644 --- a/extension/pprofextension/config_test.go +++ b/extension/pprofextension/config_test.go @@ -20,36 +20,44 @@ 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/service/servicetest" + "go.opentelemetry.io/collector/confmap/confmaptest" ) 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) - - 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")), - TCPAddr: confignet.TCPAddr{Endpoint: "0.0.0.0:1777"}, - BlockProfileFraction: 3, - MutexProfileFraction: 5, + t.Parallel() + + tests := []struct { + id config.ComponentID + expected config.Extension + }{ + { + id: config.NewComponentID(typeStr), + expected: NewFactory().CreateDefaultConfig(), }, - ext1) - - assert.Equal(t, 1, len(cfg.Service.Extensions)) - assert.Equal(t, config.NewComponentIDWithName(typeStr, "1"), cfg.Service.Extensions[0]) + { + id: config.NewComponentIDWithName(typeStr, "1"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + TCPAddr: confignet.TCPAddr{Endpoint: "0.0.0.0:1777"}, + BlockProfileFraction: 3, + MutexProfileFraction: 5, + }, + }, + } + 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.NoError(t, cfg.Validate()) + assert.Equal(t, tt.expected, cfg) + }) + } } diff --git a/extension/pprofextension/testdata/config.yaml b/extension/pprofextension/testdata/config.yaml index 9f5cad07647f..84a7a9ad9dd7 100644 --- a/extension/pprofextension/testdata/config.yaml +++ b/extension/pprofextension/testdata/config.yaml @@ -1,22 +1,5 @@ -extensions: - pprof: - pprof/1: - endpoint: "0.0.0.0:1777" - block_profile_fraction: 3 - mutex_profile_fraction: 5 - -service: - extensions: [pprof/1] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: +pprof: +pprof/1: + endpoint: "0.0.0.0:1777" + block_profile_fraction: 3 + mutex_profile_fraction: 5 diff --git a/extension/sigv4authextension/config_test.go b/extension/sigv4authextension/config_test.go index e732ff3efe0a..51005b4f3d0e 100644 --- a/extension/sigv4authextension/config_test.go +++ b/extension/sigv4authextension/config_test.go @@ -16,14 +16,13 @@ package sigv4authextension import ( "context" - "path" + "path/filepath" "testing" "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" ) func TestLoadConfig(t *testing.T) { @@ -33,52 +32,34 @@ func TestLoadConfig(t *testing.T) { t.Setenv("AWS_ACCESS_KEY_ID", awsCreds.AccessKeyID) t.Setenv("AWS_SECRET_ACCESS_KEY", awsCreds.SecretAccessKey) - factories, err := componenttest.NopFactories() - assert.NoError(t, err) - + cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) + require.NoError(t, err) factory := NewFactory() - factories.Extensions[typeStr] = factory - cfg, err := servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories) - + cfg := factory.CreateDefaultConfig() + sub, err := cm.Sub(config.NewComponentID(typeStr).String()) require.NoError(t, err) - require.NotNil(t, cfg) + require.NoError(t, config.UnmarshalExtension(sub, cfg)) - expected := factory.CreateDefaultConfig().(*Config) - expected.Region = "region" - expected.Service = "service" - expected.AssumeRole.SessionName = "role_session_name" - - ext := cfg.Extensions[config.NewComponentID(typeStr)] - // Ensure creds are the same for load config test; tested in extension_test.go - expected.credsProvider = ext.(*Config).credsProvider - assert.Equal(t, expected, ext) - - assert.Equal(t, 1, len(cfg.Service.Extensions)) - assert.Equal(t, config.NewComponentID(typeStr), cfg.Service.Extensions[0]) + assert.NoError(t, cfg.Validate()) + assert.Equal(t, &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + Region: "region", + Service: "service", + AssumeRole: AssumeRole{ + SessionName: "role_session_name", + }, + // Ensure creds are the same for load config test; tested in extension_test.go + credsProvider: cfg.(*Config).credsProvider, + }, cfg) } func TestLoadConfigError(t *testing.T) { - factories, err := componenttest.NopFactories() - assert.NoError(t, err) - - tests := []struct { - name string - expectedErr error - }{ - { - "missing_credentials", - errBadCreds, - }, - } - for _, testcase := range tests { - t.Run(testcase.name, func(t *testing.T) { - factory := NewFactory() - factories.Extensions[typeStr] = factory - cfg, _ := servicetest.LoadConfig(path.Join(".", "testdata", "config_bad.yaml"), factories) - extension := cfg.Extensions[config.NewComponentIDWithName(typeStr, testcase.name)] - verr := extension.Validate() - require.ErrorIs(t, verr, testcase.expectedErr) - }) - - } + cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) + require.NoError(t, err) + factory := NewFactory() + cfg := factory.CreateDefaultConfig() + sub, err := cm.Sub(config.NewComponentIDWithName(typeStr, "missing_credentials").String()) + require.NoError(t, err) + require.NoError(t, config.UnmarshalExtension(sub, cfg)) + assert.ErrorIs(t, cfg.Validate(), errBadCreds) } diff --git a/extension/sigv4authextension/testdata/config.yaml b/extension/sigv4authextension/testdata/config.yaml index 7ab36602c3c2..36842f86181e 100644 --- a/extension/sigv4authextension/testdata/config.yaml +++ b/extension/sigv4authextension/testdata/config.yaml @@ -1,23 +1,8 @@ -extensions: - sigv4auth: - region: "region" - service: "service" - assume_role: - session_name: "role_session_name" - - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: - -service: - extensions: [sigv4auth] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] +sigv4auth: + region: "region" + service: "service" + assume_role: + session_name: "role_session_name" +sigv4auth/missing_credentials: + region: "region" + service: "service" diff --git a/extension/sigv4authextension/testdata/config_bad.yaml b/extension/sigv4authextension/testdata/config_bad.yaml deleted file mode 100644 index dfd5e540961d..000000000000 --- a/extension/sigv4authextension/testdata/config_bad.yaml +++ /dev/null @@ -1,20 +0,0 @@ -extensions: - sigv4auth/missing_credentials: - region: "region" - service: "service" - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: - -service: - extensions: [sigv4auth/missing_credentials] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] diff --git a/extension/storage/filestorage/config_test.go b/extension/storage/filestorage/config_test.go index c824404c79cd..1dbb226c6de4 100644 --- a/extension/storage/filestorage/config_test.go +++ b/extension/storage/filestorage/config_test.go @@ -23,51 +23,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/service/servicetest" + "go.opentelemetry.io/collector/confmap/confmaptest" ) 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.NoError(t, err) - require.NotNil(t, cfg) - - require.Len(t, cfg.Extensions, 2) - - ext0 := cfg.Extensions[config.NewComponentID(typeStr)] - defaultConfig := factory.CreateDefaultConfig() - defaultConfigFileStorage, ok := defaultConfig.(*Config) - require.True(t, ok) - // Specify a directory so that tests will pass because on some systems the - // default dir (e.g. on not windows: /var/lib/otelcol/file_storage) might not - // exist which will fail the test when config.Validate() will be called. - defaultConfigFileStorage.Directory = "." - assert.Equal(t, defaultConfig, ext0) - - ext1 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "all_settings")] - assert.Equal(t, - &Config{ - ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "all_settings")), - Directory: ".", - Compaction: &CompactionConfig{ - Directory: ".", - OnStart: true, - OnRebound: true, - MaxTransactionSize: 2048, - ReboundTriggerThresholdMiB: 16, - ReboundNeededThresholdMiB: 128, - CheckInterval: time.Second * 5, + tests := []struct { + id config.ComponentID + expected config.Extension + }{ + { + id: config.NewComponentID(typeStr), + expected: func() config.Extension { + ret := NewFactory().CreateDefaultConfig() + ret.(*Config).Directory = "." + return ret + }(), + }, + { + id: config.NewComponentIDWithName(typeStr, "all_settings"), + expected: &Config{ + ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), + Directory: ".", + Compaction: &CompactionConfig{ + Directory: ".", + OnStart: true, + OnRebound: true, + MaxTransactionSize: 2048, + ReboundTriggerThresholdMiB: 16, + ReboundNeededThresholdMiB: 128, + CheckInterval: time.Second * 5, + }, + Timeout: 2 * time.Second, }, - Timeout: 2 * time.Second, }, - 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.NoError(t, cfg.Validate()) + assert.Equal(t, tt.expected, cfg) + }) + } } func TestHandleNonExistingDirectoryWithAnError(t *testing.T) { diff --git a/extension/storage/filestorage/testdata/config.yaml b/extension/storage/filestorage/testdata/config.yaml index 06269ee7f990..dad1ef9bb50e 100644 --- a/extension/storage/filestorage/testdata/config.yaml +++ b/extension/storage/filestorage/testdata/config.yaml @@ -1,32 +1,15 @@ -extensions: - file_storage: - # Specify a directory so that tests will pass because on some systems the - # default dir (e.g. on not windows: /var/lib/otelcol/file_storage) might not - # exist which will fail the test when config.Validate() will be called. +file_storage: + # Specify a directory so that tests will pass because on some systems the + # default dir (e.g. on not windows: /var/lib/otelcol/file_storage) might not + # exist which will fail the test when config.Validate() will be called. + directory: . +file_storage/all_settings: + directory: . + compaction: directory: . - file_storage/all_settings: - directory: . - compaction: - directory: . - on_start: true - on_rebound: true - rebound_trigger_threshold_mib: 16 - rebound_needed_threshold_mib: 128 - max_transaction_size: 2048 - timeout: 2s - -service: - extensions: [file_storage, file_storage/all_settings] - pipelines: - traces: - receivers: [nop] - processors: [nop] - exporters: [nop] - -# Data pipeline is required to load the config. -receivers: - nop: -processors: - nop: -exporters: - nop: + on_start: true + on_rebound: true + rebound_trigger_threshold_mib: 16 + rebound_needed_threshold_mib: 128 + max_transaction_size: 2048 + timeout: 2s