package tracing import ( "crypto/x509" "os" "testing" "github.com/stretchr/testify/require" ) func TestConfig_validate(t *testing.T) { tests := []struct { name string config Config wantErr bool }{ { name: "disabled", wantErr: false, config: Config{ Enabled: false, }, }, { name: "stdout", wantErr: false, config: Config{ Enabled: true, Exporter: StdoutExporter, Service: "test", InstanceID: "s01", Version: "v0.0.1", }, }, { name: "noop", wantErr: false, config: Config{ Enabled: true, Exporter: StdoutExporter, Service: "test", InstanceID: "s01", Version: "v0.0.1", }, }, { name: "OTLP gRPC", wantErr: false, config: Config{ Enabled: true, Exporter: OTLPgRPCExporter, Service: "test", Endpoint: "localhost:4717", InstanceID: "s01", Version: "v0.0.1", }, }, { name: "unknown exporter", wantErr: true, config: Config{ Enabled: true, Exporter: "unknown", Service: "test", Endpoint: "localhost:4717", InstanceID: "s01", Version: "v0.0.1", }, }, { name: "no exporter", wantErr: true, config: Config{ Enabled: true, Service: "test", Endpoint: "localhost:4717", InstanceID: "s01", Version: "v0.0.1", }, }, { name: "no service", wantErr: true, config: Config{ Enabled: true, Exporter: OTLPgRPCExporter, Endpoint: "localhost:4717", InstanceID: "s01", Version: "v0.0.1", }, }, { name: "no endpoint for grpc", wantErr: true, config: Config{ Enabled: true, Exporter: OTLPgRPCExporter, Service: "test", InstanceID: "s01", Version: "v0.0.1", }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := tt.config.validate(); (err != nil) != tt.wantErr { t.Errorf("Config.validate() error = %v, wantErr %v", err, tt.wantErr) } }) } } func TestConfig_hasChange(t *testing.T) { tests := []struct { name string config Config other Config want bool }{ { name: "disabled configs always equal", want: false, config: Config{ Enabled: false, Exporter: StdoutExporter, Service: "test", InstanceID: "s01", Version: "v1.0.0", }, other: Config{ Enabled: false, Exporter: OTLPgRPCExporter, Endpoint: "localhost:4717", Service: "test", InstanceID: "s01", Version: "v1.0.0", }, }, { name: "enabled", want: true, config: Config{ Enabled: false, Exporter: OTLPgRPCExporter, Endpoint: "localhost:4717", Service: "test", InstanceID: "s01", Version: "v1.0.0", }, other: Config{ Enabled: true, Exporter: OTLPgRPCExporter, Endpoint: "localhost:4717", Service: "test", InstanceID: "s01", Version: "v1.0.0", }, }, { name: "disabled", want: true, config: Config{ Enabled: true, Exporter: OTLPgRPCExporter, Endpoint: "localhost:4717", Service: "test", InstanceID: "s01", Version: "v1.0.0", }, other: Config{ Enabled: false, Exporter: OTLPgRPCExporter, Endpoint: "localhost:4717", Service: "test", InstanceID: "s01", Version: "v1.0.0", }, }, { name: "do not use endpoint for stdout", want: false, config: Config{ Enabled: true, Exporter: StdoutExporter, Endpoint: "localhost:4717", Service: "test", InstanceID: "s01", Version: "v1.0.0", }, other: Config{ Enabled: true, Exporter: StdoutExporter, Endpoint: "otherhost:4717", Service: "test", InstanceID: "s01", Version: "v1.0.0", }, }, { name: "do not use endpoint for noop", want: false, config: Config{ Enabled: true, Exporter: NoOpExporter, Endpoint: "localhost:4717", Service: "test", InstanceID: "s01", Version: "v1.0.0", }, other: Config{ Enabled: true, Exporter: NoOpExporter, Endpoint: "otherhost:4717", Service: "test", InstanceID: "s01", Version: "v1.0.0", }, }, { name: "use endpoint for grpc", want: true, config: Config{ Enabled: true, Exporter: OTLPgRPCExporter, Endpoint: "localhost:4717", Service: "test", InstanceID: "s01", Version: "v1.0.0", }, other: Config{ Enabled: true, Exporter: OTLPgRPCExporter, Endpoint: "otherhost:4717", Service: "test", InstanceID: "s01", Version: "v1.0.0", }, }, { name: "use tls root ca certificate for grpc", want: true, config: Config{ Enabled: true, Exporter: OTLPgRPCExporter, Endpoint: "localhost:4717", Service: "test", InstanceID: "s01", Version: "v1.0.0", ServerCaCertPool: readCertPoolByPath(t, "../testdata/tracing/valid_google_globalsign_r4_rsa_root_ca.pem"), }, other: Config{ Enabled: true, Exporter: OTLPgRPCExporter, Endpoint: "localhost:4717", Service: "test", InstanceID: "s01", Version: "v1.0.0", ServerCaCertPool: readCertPoolByPath(t, "../testdata/tracing/valid_google_gts_r4_ecdsa_root_ca.pem"), }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := tt.config.hasChange(&tt.other); got != tt.want { t.Errorf("Config.equal() = %v, want %v", got, tt.want) } }) } } func readCertPoolByPath(t *testing.T, path string) *x509.CertPool { ca, err := os.ReadFile(path) require.NoError(t, err) roots := x509.NewCertPool() ok := roots.AppendCertsFromPEM(ca) require.True(t, ok) return roots }