|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package googlecloudpubsubexporter |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + pubsub "cloud.google.com/go/pubsub/apiv1" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "google.golang.org/api/option" |
| 14 | +) |
| 15 | + |
| 16 | +func TestGenerateClientOptions(t *testing.T) { |
| 17 | + factory := NewFactory() |
| 18 | + |
| 19 | + t.Run("defaults", func(t *testing.T) { |
| 20 | + cfg := factory.CreateDefaultConfig().(*Config) |
| 21 | + cfg.ProjectID = "my-project" |
| 22 | + cfg.Topic = "projects/my-project/topics/otlp" |
| 23 | + |
| 24 | + require.NoError(t, cfg.Validate()) |
| 25 | + |
| 26 | + gotOptions, closeConnFn, err := generateClientOptions(cfg, "test-user-agent 6789") |
| 27 | + assert.NoError(t, err) |
| 28 | + assert.Empty(t, closeConnFn) |
| 29 | + |
| 30 | + expectedOptions := []option.ClientOption{ |
| 31 | + option.WithUserAgent("test-user-agent 6789"), |
| 32 | + } |
| 33 | + assert.ElementsMatch(t, expectedOptions, gotOptions) |
| 34 | + }) |
| 35 | + |
| 36 | + t.Run("secure custom endpoint", func(t *testing.T) { |
| 37 | + cfg := factory.CreateDefaultConfig().(*Config) |
| 38 | + cfg.ProjectID = "my-project" |
| 39 | + cfg.Topic = "projects/my-project/topics/otlp" |
| 40 | + cfg.Endpoint = "defg" |
| 41 | + |
| 42 | + require.NoError(t, cfg.Validate()) |
| 43 | + |
| 44 | + gotOptions, closeConnFn, err := generateClientOptions(cfg, "test-user-agent 4321") |
| 45 | + assert.NoError(t, err) |
| 46 | + assert.Empty(t, closeConnFn) |
| 47 | + |
| 48 | + expectedOptions := []option.ClientOption{ |
| 49 | + option.WithUserAgent("test-user-agent 4321"), |
| 50 | + option.WithEndpoint("defg"), |
| 51 | + } |
| 52 | + assert.ElementsMatch(t, expectedOptions, gotOptions) |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("insecure endpoint", func(t *testing.T) { |
| 56 | + cfg := factory.CreateDefaultConfig().(*Config) |
| 57 | + cfg.ProjectID = "my-project" |
| 58 | + cfg.Topic = "projects/my-project/topics/otlp" |
| 59 | + cfg.Endpoint = "abcd" |
| 60 | + cfg.Insecure = true |
| 61 | + |
| 62 | + require.NoError(t, cfg.Validate()) |
| 63 | + |
| 64 | + gotOptions, closeConnFn, err := generateClientOptions(cfg, "test-user-agent 1234") |
| 65 | + assert.NoError(t, err) |
| 66 | + assert.NotEmpty(t, closeConnFn) |
| 67 | + assert.NoError(t, closeConnFn()) |
| 68 | + |
| 69 | + require.Len(t, gotOptions, 2) |
| 70 | + assert.Equal(t, option.WithUserAgent("test-user-agent 1234"), gotOptions[0]) |
| 71 | + assert.IsType(t, option.WithGRPCConn(nil), gotOptions[1]) |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +func TestNewPublisherClient(t *testing.T) { |
| 76 | + // The publisher client checks for credentials during init |
| 77 | + t.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "testdata/gcp-fake-creds.json") |
| 78 | + |
| 79 | + ctx := context.Background() |
| 80 | + factory := NewFactory() |
| 81 | + |
| 82 | + t.Run("defaults", func(t *testing.T) { |
| 83 | + cfg := factory.CreateDefaultConfig().(*Config) |
| 84 | + cfg.ProjectID = "my-project" |
| 85 | + cfg.Topic = "projects/my-project/topics/otlp" |
| 86 | + |
| 87 | + require.NoError(t, cfg.Validate()) |
| 88 | + |
| 89 | + client, err := newPublisherClient(ctx, cfg, "test-user-agent 6789") |
| 90 | + assert.NoError(t, err) |
| 91 | + require.NotEmpty(t, client) |
| 92 | + assert.IsType(t, &pubsub.PublisherClient{}, client) |
| 93 | + assert.NoError(t, client.Close()) |
| 94 | + }) |
| 95 | + |
| 96 | + t.Run("secure custom endpoint", func(t *testing.T) { |
| 97 | + cfg := factory.CreateDefaultConfig().(*Config) |
| 98 | + cfg.ProjectID = "my-project" |
| 99 | + cfg.Topic = "projects/my-project/topics/otlp" |
| 100 | + cfg.Endpoint = "xyz" |
| 101 | + |
| 102 | + require.NoError(t, cfg.Validate()) |
| 103 | + |
| 104 | + client, err := newPublisherClient(ctx, cfg, "test-user-agent 6789") |
| 105 | + assert.NoError(t, err) |
| 106 | + require.NotEmpty(t, client) |
| 107 | + assert.IsType(t, &pubsub.PublisherClient{}, client) |
| 108 | + assert.NoError(t, client.Close()) |
| 109 | + }) |
| 110 | + |
| 111 | + t.Run("insecure endpoint", func(t *testing.T) { |
| 112 | + cfg := factory.CreateDefaultConfig().(*Config) |
| 113 | + cfg.ProjectID = "my-project" |
| 114 | + cfg.Topic = "projects/my-project/topics/otlp" |
| 115 | + cfg.Endpoint = "abc" |
| 116 | + cfg.Insecure = true |
| 117 | + |
| 118 | + require.NoError(t, cfg.Validate()) |
| 119 | + |
| 120 | + client, err := newPublisherClient(ctx, cfg, "test-user-agent 6789") |
| 121 | + assert.NoError(t, err) |
| 122 | + require.NotEmpty(t, client) |
| 123 | + assert.IsType(t, &wrappedPublisherClient{}, client) |
| 124 | + assert.NoError(t, client.Close()) |
| 125 | + }) |
| 126 | +} |
0 commit comments