From 49c277bc7cf1e5db7719d5269081235c1c998ffa Mon Sep 17 00:00:00 2001 From: Roger Coll Date: Wed, 29 May 2024 17:59:28 +0200 Subject: [PATCH] add provider's mock and configuration load test case --- processor/geoipprocessor/go.mod | 2 +- processor/geoipprocessor/go.sum | 13 +++- .../internal/provider/geoipprovider.go | 6 +- processor/geoipprocessor/provider_test.go | 70 +++++++++++++++++++ .../testdata/config-mockProvider.yaml | 17 +++++ 5 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 processor/geoipprocessor/provider_test.go create mode 100644 processor/geoipprocessor/testdata/config-mockProvider.yaml diff --git a/processor/geoipprocessor/go.mod b/processor/geoipprocessor/go.mod index bf6ffc2e7f50..b31c9489c272 100644 --- a/processor/geoipprocessor/go.mod +++ b/processor/geoipprocessor/go.mod @@ -7,6 +7,7 @@ require ( go.opentelemetry.io/collector/component v0.101.1-0.20240527192838-af4fdd4e342a go.opentelemetry.io/collector/confmap v0.101.1-0.20240527192838-af4fdd4e342a go.opentelemetry.io/collector/consumer v0.101.1-0.20240527192838-af4fdd4e342a + go.opentelemetry.io/collector/otelcol v0.101.0 go.opentelemetry.io/collector/pdata v1.8.1-0.20240527192838-af4fdd4e342a go.opentelemetry.io/collector/processor v0.101.1-0.20240527192838-af4fdd4e342a go.opentelemetry.io/otel v1.27.0 @@ -63,7 +64,6 @@ require ( go.opentelemetry.io/collector/exporter v0.101.0 // indirect go.opentelemetry.io/collector/extension v0.101.0 // indirect go.opentelemetry.io/collector/featuregate v1.8.0 // indirect - go.opentelemetry.io/collector/otelcol v0.101.0 // indirect go.opentelemetry.io/collector/pdata/testdata v0.101.0 // indirect go.opentelemetry.io/collector/receiver v0.101.0 // indirect go.opentelemetry.io/collector/semconv v0.101.0 // indirect diff --git a/processor/geoipprocessor/go.sum b/processor/geoipprocessor/go.sum index 77599024c659..e7033e919f34 100644 --- a/processor/geoipprocessor/go.sum +++ b/processor/geoipprocessor/go.sum @@ -100,13 +100,14 @@ github.com/prometheus/common v0.53.0 h1:U2pL9w9nmJwJDa4qqLQ3ZaePJ6ZTwt7cMD3AG3+a github.com/prometheus/common v0.53.0/go.mod h1:BrxBKv3FWBIGXw89Mg1AeBq7FSyRzXWI3l3e7W3RN5U= github.com/prometheus/procfs v0.15.0 h1:A82kmvXJq2jTu5YUhSGNlYoxh85zLnKgPz4bMZgI5Ek= github.com/prometheus/procfs v0.15.0/go.mod h1:Y0RJ/Y5g5wJpkTisOtqwDSo4HwhGmLB4VQSw2sQJLHk= -github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= -github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shirou/gopsutil/v3 v3.24.4 h1:dEHgzZXt4LMNm+oYELpzl9YCqV65Yr/6SfrvgRBtXeU= github.com/shirou/gopsutil/v3 v3.24.4/go.mod h1:lTd2mdiOspcqLgAnr9/nGi71NkeMpWKdmhuxm9GusH8= github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM= github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ= +github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU= github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= @@ -137,6 +138,10 @@ go.opentelemetry.io/collector v0.101.1-0.20240527192838-af4fdd4e342a h1:4qJ9yfH6 go.opentelemetry.io/collector v0.101.1-0.20240527192838-af4fdd4e342a/go.mod h1:/0r4YF1nBHmTBgaZYi9om171wt/c9zP7AU7Xqqr+a7A= go.opentelemetry.io/collector/component v0.101.1-0.20240527192838-af4fdd4e342a h1:8qOFfwhDNRcAobYZEy6VJ0EFzV8Rbn0jUR7DwYXS0T4= go.opentelemetry.io/collector/component v0.101.1-0.20240527192838-af4fdd4e342a/go.mod h1:WBWJoYh8Js5S8e+0twmQxSF/uqR1HHkDb3YNKzgKaWY= +go.opentelemetry.io/collector/config/confignet v0.101.0 h1:Mdb9e/EpCSac4Ccg7w4UchS/o4yY1WoIc9X5o7fTu9E= +go.opentelemetry.io/collector/config/confignet v0.101.0/go.mod h1:3naWoPss70RhDHhYjGACi7xh4NcVRvs9itzIRVWyu1k= +go.opentelemetry.io/collector/config/configretry v0.101.0 h1:5QggLq/lZiZXry1Ut52IOTbrdz1RbGoL29Io/wWdE4g= +go.opentelemetry.io/collector/config/configretry v0.101.0/go.mod h1:uRdmPeCkrW9Zsadh2WEbQ1AGXGYJ02vCfmmT+0g69nY= go.opentelemetry.io/collector/config/configtelemetry v0.101.1-0.20240527192838-af4fdd4e342a h1:EsbDY0mcCilr3LkM7XK5xRuOOZxn2qE6IdECWZ5xeFo= go.opentelemetry.io/collector/config/configtelemetry v0.101.1-0.20240527192838-af4fdd4e342a/go.mod h1:WxWKNVAQJg/Io1nA3xLgn/DWLE/W1QOB2+/Js3ACi40= go.opentelemetry.io/collector/confmap v0.101.1-0.20240527192838-af4fdd4e342a h1:rBVMMiax68gRFxZFA3pP6ZtCJoV6o+sqj/QUVApjbIA= @@ -161,6 +166,8 @@ go.opentelemetry.io/collector/exporter v0.101.0 h1:zAxQBfaWO+PEHL3nDglgMGaWsqLsj go.opentelemetry.io/collector/exporter v0.101.0/go.mod h1:ZFwUWCmnM2ZbEty71Q13qME9QhvIKMgyYrS3s8vJPM8= go.opentelemetry.io/collector/extension v0.101.0 h1:A4hq/aci9+/Pxi8sJfyYgbeHjSIL7JFZR81IlSOTla4= go.opentelemetry.io/collector/extension v0.101.0/go.mod h1:14gQMuybTcppfTTM9AwqeoFrNCLv/ds/c0A4Z0hWuLI= +go.opentelemetry.io/collector/extension/zpagesextension v0.101.0 h1:hZIkGTgKeVvVlqoPw72G/RkIhp0QSqY5PNRjf38mf2k= +go.opentelemetry.io/collector/extension/zpagesextension v0.101.0/go.mod h1:sllOMdEbNg2UnMxTO8jSx2OEfAcYX3ud6smuXhN6pbA= go.opentelemetry.io/collector/featuregate v1.8.0 h1:p/bAuk5LiSfdYS88yFl/Jzao9bHEYqCh7YvZJ+L+IZg= go.opentelemetry.io/collector/featuregate v1.8.0/go.mod h1:w7nUODKxEi3FLf1HslCiE6YWtMtOOrMnSwsDam8Mg9w= go.opentelemetry.io/collector/otelcol v0.101.0 h1:6kF2dcXpu5NjxK2j0ksCRzZhqigxCGrP/u7n57FSMOg= @@ -181,6 +188,8 @@ go.opentelemetry.io/contrib/config v0.6.0 h1:M1SRD1Z15XHPGk61tMLI1up77XT5FdrqQSR go.opentelemetry.io/contrib/config v0.6.0/go.mod h1:t+/kzmRWLN7J+4F/dD4fFvlYCmCO63WYwy/B00IC++c= go.opentelemetry.io/contrib/propagators/b3 v1.26.0 h1:wgFbVA+bK2k+fGVfDOCOG4cfDAoppyr5sI2dVlh8MWM= go.opentelemetry.io/contrib/propagators/b3 v1.26.0/go.mod h1:DDktFXxA+fyItAAM0Sbl5OBH7KOsCTjvbBdPKtoIf/k= +go.opentelemetry.io/contrib/zpages v0.51.0 h1:psVr4JTWd0qtISPj9EA6AODGJ09bvsOxWiuKqiGdSCA= +go.opentelemetry.io/contrib/zpages v0.51.0/go.mod h1:PKtp+NEp1gTTLmFHpynYgYCSkKtisPntOb9S1mQjFKg= go.opentelemetry.io/otel v1.27.0 h1:9BZoF3yMK/O1AafMiQTVu0YDj5Ea4hPhxCs7sGva+cg= go.opentelemetry.io/otel v1.27.0/go.mod h1:DMpAK8fzYRzs+bi3rS5REupisuqTheUlSZJ1WnZaPAQ= go.opentelemetry.io/otel/bridge/opencensus v1.26.0 h1:DZzxj9QjznMVoehskOJnFP2gsTCWtDTFBDvFhPAY7nc= diff --git a/processor/geoipprocessor/internal/provider/geoipprovider.go b/processor/geoipprocessor/internal/provider/geoipprovider.go index 23a2a3965c00..2a850617cef4 100644 --- a/processor/geoipprocessor/internal/provider/geoipprovider.go +++ b/processor/geoipprocessor/internal/provider/geoipprovider.go @@ -7,7 +7,6 @@ import ( "context" "net" - "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/processor" "go.opentelemetry.io/otel/attribute" ) @@ -19,10 +18,7 @@ type GeoIPProvider interface { } // Config is the configuration of a GeoIPProvider. -// It extends the component.ConfigValidator interface to include validation logic. -type Config interface { - component.ConfigValidator -} +type Config interface{} // GeoIPProviderFactory can create GeoIPProvider instances. type GeoIPProviderFactory interface { diff --git a/processor/geoipprocessor/provider_test.go b/processor/geoipprocessor/provider_test.go new file mode 100644 index 000000000000..e5ef6addc7e1 --- /dev/null +++ b/processor/geoipprocessor/provider_test.go @@ -0,0 +1,70 @@ +package geoipprocessor + +import ( + "context" + "net" + "path/filepath" + "testing" + + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/metadata" + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/provider" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/otelcol/otelcoltest" + "go.opentelemetry.io/collector/processor" + "go.opentelemetry.io/otel/attribute" +) + +type ProviderMock struct { + LocationF func(context.Context, net.IP) (attribute.Set, error) +} + +type ProviderFactoryMock struct { + CreateDefaultConfigF func() provider.Config + CreateGeoIPProviderF func(ctx context.Context, settings processor.CreateSettings, cfg provider.Config) (provider.GeoIPProvider, error) +} + +var ( + _ provider.GeoIPProvider = (*ProviderMock)(nil) + _ provider.GeoIPProviderFactory = (*ProviderFactoryMock)(nil) +) + +func (pm *ProviderMock) Location(ctx context.Context, ip net.IP) (attribute.Set, error) { + return pm.LocationF(ctx, ip) +} + +func (fm *ProviderFactoryMock) CreateDefaultConfig() provider.Config { + return fm.CreateDefaultConfigF() +} + +func (fm *ProviderFactoryMock) CreateGeoIPProvider(ctx context.Context, settings processor.CreateSettings, cfg provider.Config) (provider.GeoIPProvider, error) { + return fm.CreateGeoIPProviderF(ctx, settings, cfg) +} + +func TestLoadConfig_MockProvider(t *testing.T) { + mockProvider := ProviderMock{ + LocationF: func(context.Context, net.IP) (attribute.Set, error) { + return attribute.Set{}, nil + }, + } + mockFactory := ProviderFactoryMock{ + CreateDefaultConfigF: func() provider.Config { + type SampleConfig struct { + Database string `mapstructure:"database"` + } + return &SampleConfig{} + }, + CreateGeoIPProviderF: func(ctx context.Context, settings processor.CreateSettings, cfg provider.Config) (provider.GeoIPProvider, error) { + return &mockProvider, nil + }, + } + + factories, err := otelcoltest.NopFactories() + require.NoError(t, err) + + providerFactories["mock"] = &mockFactory + factory := NewFactory() + factories.Processors[metadata.Type] = factory + _, err = otelcoltest.LoadConfigAndValidate(filepath.Join("testdata", "config-mockProvider.yaml"), factories) + assert.NoError(t, err) +} diff --git a/processor/geoipprocessor/testdata/config-mockProvider.yaml b/processor/geoipprocessor/testdata/config-mockProvider.yaml new file mode 100644 index 000000000000..d6107cb3db01 --- /dev/null +++ b/processor/geoipprocessor/testdata/config-mockProvider.yaml @@ -0,0 +1,17 @@ +processors: + geoip: + providers: + mock: + database: "/tmp/geodata.csv" +receivers: + nop: + +exporters: + nop: + +service: + pipelines: + metrics: + receivers: [nop] + processors: [geoip] + exporters: [nop]