Skip to content

Commit

Permalink
Add config tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pavol Loffay <[email protected]>
  • Loading branch information
pavolloffay committed May 13, 2020
1 parent 5a299a1 commit 8222410
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 13 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector/exporter/jaegerexporter"
"github.com/spf13/viper"

"github.com/jaegertracing/jaeger/cmd/agent/app/reporter/grpc"
grpcRep "github.com/jaegertracing/jaeger/cmd/agent/app/reporter/grpc"
)

// Factory wraps jaegerexporter.Factory and makes the default config configurable via viper.
Expand All @@ -44,7 +44,7 @@ func (f Factory) Type() configmodels.Type {
// CreateDefaultConfig returns default configuration of Factory.
// This function implements OTEL component.ExporterFactoryBase interface.
func (f Factory) CreateDefaultConfig() configmodels.Exporter {
repCfg := grpc.ConnBuilder{}
repCfg := grpcRep.ConnBuilder{}
repCfg.InitFromViper(f.Viper)
cfg := f.Wrapped.CreateDefaultConfig().(*jaegerexporter.Config)
if len(repCfg.CollectorHostPorts) > 0 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package jaegerexporter

import (
"context"
jConfig "github.com/jaegertracing/jaeger/pkg/config"
"github.com/open-telemetry/opentelemetry-collector/component"
"github.com/open-telemetry/opentelemetry-collector/config"
"github.com/open-telemetry/opentelemetry-collector/config/configerror"
"github.com/open-telemetry/opentelemetry-collector/config/configmodels"
"github.com/open-telemetry/opentelemetry-collector/exporter/jaegerexporter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"path"
"testing"

grpcRep "github.com/jaegertracing/jaeger/cmd/agent/app/reporter/grpc"
)

func TestDefaultValues(t *testing.T) {
v, c := jConfig.Viperize(grpcRep.AddFlags)
err := c.ParseFlags([]string{})
require.NoError(t, err)

factory := &Factory{Viper: v, Wrapped: &jaegerexporter.Factory{}}
cfg := factory.CreateDefaultConfig().(*jaegerexporter.Config)
assert.Empty(t, cfg.GRPCSettings.Endpoint)
tlsConf := cfg.TLSConfig
assert.False(t, tlsConf.UseSecure)
assert.Empty(t, tlsConf.CaCert)
assert.Empty(t, tlsConf.ClientKey)
assert.Empty(t, tlsConf.ClientCert)
assert.Empty(t, tlsConf.ServerNameOverride)
}

func TestDefaultValueFromViper(t *testing.T) {
v, c := jConfig.Viperize(grpcRep.AddFlags)
err := c.ParseFlags([]string{"--reporter.grpc.host-port=foo", "--reporter.grpc.tls.enabled=true", "--reporter.grpc.tls.ca=ca.crt"})
require.NoError(t, err)

f := &Factory{
Wrapped: &jaegerexporter.Factory{},
Viper: v,
}

cfg := f.CreateDefaultConfig().(*jaegerexporter.Config)
assert.Equal(t, "foo", cfg.GRPCSettings.Endpoint)
tlsConfig := cfg.TLSConfig
assert.Equal(t, true, tlsConfig.UseSecure)
assert.Equal(t, "ca.crt", tlsConfig.CaCert)
}

func TestLoadConfigAndFlags(t *testing.T) {
factories, err := config.ExampleComponents()
require.NoError(t, err)

v, c := jConfig.Viperize(grpcRep.AddFlags)
err = c.ParseFlags([]string{"--reporter.grpc.host-port=foo"})
require.NoError(t, err)

factory := &Factory{Viper: v, Wrapped: &jaegerexporter.Factory{}}
assert.Equal(t, "foo", factory.CreateDefaultConfig().(*jaegerexporter.Config).GRPCSettings.Endpoint)

factories.Exporters["jaeger"] = factory
colConfig, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
require.NoError(t, err)
require.NotNil(t, colConfig)

cfg := colConfig.Exporters["jaeger"].(*jaegerexporter.Config)
assert.Equal(t, "bar", cfg.GRPCSettings.Endpoint)
}

func TestType(t *testing.T) {
f := &Factory{
Wrapped: &jaegerexporter.Factory{},
}
assert.Equal(t, configmodels.Type("jaeger"), f.Type())
}

func TestCreateMetricsExporter(t *testing.T) {
f := &Factory{
Wrapped: &jaegerexporter.Factory{},
}
mReceiver, err := f.CreateMetricsExporter(context.Background(), component.ExporterCreateParams{}, nil)
assert.Equal(t, configerror.ErrDataTypeIsNotSupported, err)
assert.Nil(t, mReceiver)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
receivers:
examplereceiver:

processors:
exampleprocessor:

exporters:
jaeger:
endpoint: bar

service:
pipelines:
traces:
receivers: [examplereceiver]
processors: [exampleprocessor]
exporters: [jaeger]
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ package jaegerreceiver

import (
"context"
"fmt"
"path"
"testing"

"github.com/open-telemetry/opentelemetry-collector/component"
"github.com/open-telemetry/opentelemetry-collector/config"
"github.com/open-telemetry/opentelemetry-collector/config/configerror"
"github.com/open-telemetry/opentelemetry-collector/config/configmodels"
"github.com/open-telemetry/opentelemetry-collector/receiver"
"github.com/open-telemetry/opentelemetry-collector/receiver/jaegerreceiver"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

agentApp "github.com/jaegertracing/jaeger/cmd/agent/app"
grpcRep "github.com/jaegertracing/jaeger/cmd/agent/app/reporter/grpc"
collectorApp "github.com/jaegertracing/jaeger/cmd/collector/app"
jConfig "github.com/jaegertracing/jaeger/pkg/config"
"github.com/jaegertracing/jaeger/plugin/sampling/strategystore/static"
)
Expand All @@ -40,19 +44,87 @@ func TestDefaultValues(t *testing.T) {
factory := &Factory{Viper: v, Wrapped: &jaegerreceiver.Factory{}}
cfg := factory.CreateDefaultConfig().(*jaegerreceiver.Config)
assert.Nil(t, cfg.RemoteSampling)
assert.Empty(t, cfg.Protocols)
}

func TestDefaultValueFromViper(t *testing.T) {
v := viper.New()
v.Set(static.SamplingStrategiesFile, "config.json")

f := &Factory{
Wrapped: &jaegerreceiver.Factory{},
Viper: v,
tests := []struct {
name string
flags []string
expected *jaegerreceiver.Config
}{
{
name: "samplingStrategyFile",
flags: []string{fmt.Sprintf("--%s=%s", static.SamplingStrategiesFile, "conf.json")},
expected: &jaegerreceiver.Config{
RemoteSampling: &jaegerreceiver.RemoteSamplingConfig{
StrategyFile: "conf.json",
},
Protocols: map[string]*receiver.SecureReceiverSettings{},
},
},
{
name: "thriftCompact",
flags: []string{fmt.Sprintf("--%s=%s", thriftCompactHostPort, "localhost:9999")},
expected: &jaegerreceiver.Config{
Protocols: map[string]*receiver.SecureReceiverSettings{
"thrift_compact": {ReceiverSettings: configmodels.ReceiverSettings{Endpoint: "localhost:9999"}},
},
},
},
{
name: "thriftBinary",
flags: []string{fmt.Sprintf("--%s=%s", thriftBinaryHostPort, "localhost:8888")},
expected: &jaegerreceiver.Config{
Protocols: map[string]*receiver.SecureReceiverSettings{
"thrift_binary": {ReceiverSettings: configmodels.ReceiverSettings{Endpoint: "localhost:8888"}},
},
},
},
{
name: "grpc",
flags: []string{fmt.Sprintf("--%s=%s", collectorApp.CollectorGRPCHostPort, "localhost:7894")},
expected: &jaegerreceiver.Config{
Protocols: map[string]*receiver.SecureReceiverSettings{
"grpc": {ReceiverSettings: configmodels.ReceiverSettings{Endpoint: "localhost:7894"}},
},
},
},
{
name: "thriftHttp",
flags: []string{fmt.Sprintf("--%s=%s", collectorApp.CollectorHTTPHostPort, "localhost:8080")},
expected: &jaegerreceiver.Config{
Protocols: map[string]*receiver.SecureReceiverSettings{
"thrift_http": {ReceiverSettings: configmodels.ReceiverSettings{Endpoint: "localhost:8080"}},
},
},
},
{
name: "thriftHttpAndThriftBinary",
flags: []string{fmt.Sprintf("--%s=%s", collectorApp.CollectorHTTPHostPort, "localhost:8089"), fmt.Sprintf("--%s=%s", thriftBinaryHostPort, "localhost:2222")},
expected: &jaegerreceiver.Config{
Protocols: map[string]*receiver.SecureReceiverSettings{
"thrift_http": {ReceiverSettings: configmodels.ReceiverSettings{Endpoint: "localhost:8089"}},
"thrift_binary": {ReceiverSettings: configmodels.ReceiverSettings{Endpoint: "localhost:2222"}},
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
v, c := jConfig.Viperize(static.AddFlags, grpcRep.AddFlags, agentApp.AddFlags, collectorApp.AddFlags)
err := c.ParseFlags(test.flags)
require.NoError(t, err)
f := &Factory{
Wrapped: &jaegerreceiver.Factory{},
Viper: v,
}
cfg := f.CreateDefaultConfig().(*jaegerreceiver.Config)
test.expected.TypeVal = "jaeger"
test.expected.NameVal = "jaeger"
assert.Equal(t, test.expected, cfg)
})
}

cfg := f.CreateDefaultConfig().(*jaegerreceiver.Config)
assert.Equal(t, "config.json", cfg.RemoteSampling.StrategyFile)
}

func TestLoadConfigAndFlags(t *testing.T) {
Expand Down

0 comments on commit 8222410

Please sign in to comment.