Skip to content

Commit

Permalink
add provider's mock and configuration load test case
Browse files Browse the repository at this point in the history
  • Loading branch information
rogercoll committed May 29, 2024
1 parent ca50fa1 commit 49c277b
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 8 deletions.
2 changes: 1 addition & 1 deletion processor/geoipprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions processor/geoipprocessor/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions processor/geoipprocessor/internal/provider/geoipprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"net"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/otel/attribute"
)
Expand All @@ -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 {
Expand Down
70 changes: 70 additions & 0 deletions processor/geoipprocessor/provider_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
17 changes: 17 additions & 0 deletions processor/geoipprocessor/testdata/config-mockProvider.yaml
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 49c277b

Please sign in to comment.