// Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 package googlecloudpubsubreceiver import ( "context" "testing" "github.com/stretchr/testify/assert" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/consumer/consumertest" "go.opentelemetry.io/collector/receiver/receivertest" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/googlecloudpubsubreceiver/internal/metadata" ) func TestCreateDefaultConfig(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() assert.NotNil(t, cfg, "failed to create default config") assert.NoError(t, componenttest.CheckConfigStruct(cfg)) } func TestType(t *testing.T) { factory := NewFactory() assert.Equal(t, component.Type(metadata.Type), factory.Type()) } func TestCreateTracesReceiver(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() cfg.(*Config).Subscription = "projects/my-project/subscriptions/my-subscription" params := receivertest.NewNopCreateSettings() tReceiver, err := factory.CreateTracesReceiver(context.Background(), params, cfg, consumertest.NewNop()) assert.NoError(t, err) assert.NotNil(t, tReceiver, "traces receiver creation failed") _, err = factory.CreateTracesReceiver(context.Background(), params, cfg, nil) assert.Error(t, err) } func TestCreateMetricsReceiver(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() cfg.(*Config).Subscription = "projects/my-project/subscriptions/my-subscription" params := receivertest.NewNopCreateSettings() tReceiver, err := factory.CreateMetricsReceiver(context.Background(), params, cfg, consumertest.NewNop()) assert.NoError(t, err) assert.NotNil(t, tReceiver, "metrics receiver creation failed") _, err = factory.CreateMetricsReceiver(context.Background(), params, cfg, nil) assert.Error(t, err) } func TestCreateLogsReceiver(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() cfg.(*Config).Subscription = "projects/my-project/subscriptions/my-subscription" params := receivertest.NewNopCreateSettings() tReceiver, err := factory.CreateLogsReceiver(context.Background(), params, cfg, consumertest.NewNop()) assert.NoError(t, err) assert.NotNil(t, tReceiver, "logs receiver creation failed") _, err = factory.CreateLogsReceiver(context.Background(), params, cfg, nil) assert.Error(t, err) } func TestEnsureReceiver(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() cfg.(*Config).Subscription = "projects/my-project/subscriptions/my-subscription" tReceiver, err := factory.CreateTracesReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, consumertest.NewNop()) assert.NoError(t, err) mReceiver, err := factory.CreateMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, consumertest.NewNop()) assert.NoError(t, err) assert.Equal(t, tReceiver, mReceiver) lReceiver, err := factory.CreateLogsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, consumertest.NewNop()) assert.NoError(t, err) assert.Equal(t, mReceiver, lReceiver) }