|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package kafkatopicsobserver |
| 5 | + |
| 6 | +import ( |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "go.opentelemetry.io/collector/component" |
| 14 | + "go.opentelemetry.io/collector/confmap" |
| 15 | + "go.opentelemetry.io/collector/confmap/confmaptest" |
| 16 | + "go.opentelemetry.io/collector/confmap/xconfmap" |
| 17 | + |
| 18 | + "github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer/kafkatopicsobserver/internal/metadata" |
| 19 | + "github.com/open-telemetry/opentelemetry-collector-contrib/internal/kafka" |
| 20 | +) |
| 21 | + |
| 22 | +func TestLoadConfig(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + |
| 25 | + tests := []struct { |
| 26 | + id component.ID |
| 27 | + expected component.Config |
| 28 | + expectedError string |
| 29 | + }{ |
| 30 | + { |
| 31 | + id: component.NewID(metadata.Type), |
| 32 | + expected: NewFactory().CreateDefaultConfig(), |
| 33 | + expectedError: "protocol_version must be specified; topic_regex must be specified", |
| 34 | + }, |
| 35 | + { |
| 36 | + id: component.NewIDWithName(metadata.Type, "all_settings"), |
| 37 | + expected: &Config{ |
| 38 | + ProtocolVersion: "3.7.0", |
| 39 | + Brokers: []string{"1.2.3.4:9092", "2.3.4.5:9092"}, |
| 40 | + TopicRegex: "^topic[0-9]$", |
| 41 | + TopicsSyncInterval: 5 * time.Second, |
| 42 | + ResolveCanonicalBootstrapServersOnly: false, |
| 43 | + SessionTimeout: 30 * time.Second, |
| 44 | + HeartbeatInterval: 20 * time.Second, |
| 45 | + Authentication: kafka.Authentication{ |
| 46 | + PlainText: &kafka.PlainTextConfig{ |
| 47 | + Username: "fooUser", |
| 48 | + Password: "fooPassword", |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + for _, tt := range tests { |
| 55 | + t.Run(tt.id.String(), func(t *testing.T) { |
| 56 | + cfg := loadConfig(t, tt.id) |
| 57 | + if tt.expectedError != "" { |
| 58 | + assert.EqualError(t, xconfmap.Validate(cfg), tt.expectedError) |
| 59 | + } else { |
| 60 | + assert.NoError(t, xconfmap.Validate(cfg)) |
| 61 | + } |
| 62 | + assert.Equal(t, tt.expected, cfg) |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestValidateConfig(t *testing.T) { |
| 68 | + cfg := &Config{ |
| 69 | + Brokers: []string{}, |
| 70 | + ProtocolVersion: "3.7.0", |
| 71 | + TopicRegex: "^test[0-9]$", |
| 72 | + } |
| 73 | + assert.Equal(t, "brokers list must be specified; topics_sync_interval must be greater than 0; session_timeout must be greater than 0; heartbeat_interval must be greater than 0", xconfmap.Validate(cfg).Error()) |
| 74 | + |
| 75 | + cfg = &Config{ |
| 76 | + Brokers: []string{"1.2.3.4:9092"}, |
| 77 | + ProtocolVersion: "", |
| 78 | + TopicRegex: "^topic[0-9]$", |
| 79 | + TopicsSyncInterval: 1 * time.Second, |
| 80 | + SessionTimeout: 1 * time.Second, |
| 81 | + HeartbeatInterval: 1 * time.Second, |
| 82 | + } |
| 83 | + assert.Equal(t, "protocol_version must be specified", xconfmap.Validate(cfg).Error()) |
| 84 | + |
| 85 | + cfg = &Config{ |
| 86 | + Brokers: []string{"1.2.3.4:9092"}, |
| 87 | + ProtocolVersion: "3.7.0", |
| 88 | + TopicRegex: "", |
| 89 | + TopicsSyncInterval: 1 * time.Second, |
| 90 | + SessionTimeout: 1 * time.Second, |
| 91 | + HeartbeatInterval: 1 * time.Second, |
| 92 | + } |
| 93 | + assert.Equal(t, "topic_regex must be specified", xconfmap.Validate(cfg).Error()) |
| 94 | + |
| 95 | + cfg = &Config{ |
| 96 | + Brokers: []string{"1.2.3.4:9092"}, |
| 97 | + ProtocolVersion: "3.7.0", |
| 98 | + TopicRegex: "^topic[0-9]$", |
| 99 | + TopicsSyncInterval: 0 * time.Second, |
| 100 | + SessionTimeout: 1 * time.Second, |
| 101 | + HeartbeatInterval: 1 * time.Second, |
| 102 | + } |
| 103 | + assert.Equal(t, "topics_sync_interval must be greater than 0", xconfmap.Validate(cfg).Error()) |
| 104 | + |
| 105 | + cfg = &Config{ |
| 106 | + Brokers: []string{"1.2.3.4:9092"}, |
| 107 | + ProtocolVersion: "3.7.0", |
| 108 | + TopicRegex: "^topic[0-9]$", |
| 109 | + TopicsSyncInterval: 1 * time.Second, |
| 110 | + SessionTimeout: 0 * time.Second, |
| 111 | + HeartbeatInterval: 1 * time.Second, |
| 112 | + } |
| 113 | + assert.Equal(t, "session_timeout must be greater than 0", xconfmap.Validate(cfg).Error()) |
| 114 | + |
| 115 | + cfg = &Config{ |
| 116 | + Brokers: []string{"1.2.3.4:9092"}, |
| 117 | + ProtocolVersion: "3.7.0", |
| 118 | + TopicRegex: "^topic[0-9]$", |
| 119 | + TopicsSyncInterval: 1 * time.Second, |
| 120 | + SessionTimeout: 1 * time.Second, |
| 121 | + HeartbeatInterval: 0 * time.Second, |
| 122 | + } |
| 123 | + assert.Equal(t, "heartbeat_interval must be greater than 0", xconfmap.Validate(cfg).Error()) |
| 124 | + |
| 125 | + cfg = &Config{ |
| 126 | + Brokers: []string{"1.2.3.4:9092"}, |
| 127 | + ProtocolVersion: "3.7.0", |
| 128 | + TopicRegex: "^topic[0-9]$", |
| 129 | + TopicsSyncInterval: 1 * time.Second, |
| 130 | + SessionTimeout: 1 * time.Second, |
| 131 | + HeartbeatInterval: 1 * time.Second, |
| 132 | + } |
| 133 | + assert.NoError(t, xconfmap.Validate(cfg)) |
| 134 | +} |
| 135 | + |
| 136 | +func loadConf(tb testing.TB, path string, id component.ID) *confmap.Conf { |
| 137 | + cm, err := confmaptest.LoadConf(filepath.Join("testdata", path)) |
| 138 | + require.NoError(tb, err) |
| 139 | + sub, err := cm.Sub(id.String()) |
| 140 | + require.NoError(tb, err) |
| 141 | + return sub |
| 142 | +} |
| 143 | + |
| 144 | +func loadConfig(tb testing.TB, id component.ID) *Config { |
| 145 | + factory := NewFactory() |
| 146 | + cfg := factory.CreateDefaultConfig() |
| 147 | + sub := loadConf(tb, "config.yaml", id) |
| 148 | + require.NoError(tb, sub.Unmarshal(cfg)) |
| 149 | + return cfg.(*Config) |
| 150 | +} |
0 commit comments