-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add provider's mock and configuration load test case
- Loading branch information
Showing
5 changed files
with
100 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
processor/geoipprocessor/testdata/config-mockProvider.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |