|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +func TestLoadConfig(t *testing.T) { |
| 10 | + // Setup and cleanup for all tests |
| 11 | + setupEnv := func(t *testing.T, envs map[string]string) { |
| 12 | + t.Helper() |
| 13 | + for k, v := range envs { |
| 14 | + os.Setenv(k, v) |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + cleanEnv := func(t *testing.T, keys []string) { |
| 19 | + t.Helper() |
| 20 | + for _, k := range keys { |
| 21 | + os.Unsetenv(k) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // Required env vars that need to be cleaned up |
| 26 | + envKeys := []string{ |
| 27 | + "CLICKHOUSE_URL", |
| 28 | + "BASIC_AUTH", |
| 29 | + "PORT", |
| 30 | + "OTEL_EXPORTER_LOG_DEBUG", |
| 31 | + "OTEL_TRACE_SAMPLE_RATE", |
| 32 | + } |
| 33 | + |
| 34 | + // Cleanup after all tests in this function |
| 35 | + t.Cleanup(func() { |
| 36 | + cleanEnv(t, envKeys) |
| 37 | + }) |
| 38 | + |
| 39 | + // Test for valid config with default values |
| 40 | + t.Run("ValidConfig", func(t *testing.T) { |
| 41 | + // Set required environment variables |
| 42 | + setupEnv(t, map[string]string{ |
| 43 | + "CLICKHOUSE_URL": "http://localhost:8123", |
| 44 | + "BASIC_AUTH": "user:password", |
| 45 | + }) |
| 46 | + |
| 47 | + config, err := LoadConfig() |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("LoadConfig() error = %v, wantErr = false", err) |
| 50 | + } |
| 51 | + |
| 52 | + // Use a table-driven check for expected values |
| 53 | + tests := []struct { |
| 54 | + name string |
| 55 | + got interface{} |
| 56 | + expected interface{} |
| 57 | + }{ |
| 58 | + {"FlushInterval", config.FlushInterval, time.Second * 5}, |
| 59 | + {"ListenerPort", config.ListenerPort, "7123"}, |
| 60 | + {"LogDebug", config.LogDebug, false}, |
| 61 | + {"ServiceName", config.ServiceName, "chproxy"}, |
| 62 | + {"ServiceVersion", config.ServiceVersion, "1.3.1"}, |
| 63 | + {"TraceMaxBatchSize", config.TraceMaxBatchSize, 512}, |
| 64 | + {"TraceSampleRate", config.TraceSampleRate, 0.25}, |
| 65 | + {"ClickhouseURL", config.ClickhouseURL, "http://localhost:8123"}, |
| 66 | + {"BasicAuth", config.BasicAuth, "user:password"}, |
| 67 | + } |
| 68 | + |
| 69 | + for _, tc := range tests { |
| 70 | + t.Run(tc.name, func(t *testing.T) { |
| 71 | + if tc.got != tc.expected { |
| 72 | + t.Errorf("%s = %v, want %v", tc.name, tc.got, tc.expected) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + if config.Logger == nil { |
| 78 | + t.Error("Logger is nil, want non-nil") |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + // Test for error cases |
| 83 | + t.Run("ErrorCases", func(t *testing.T) { |
| 84 | + tests := []struct { |
| 85 | + name string |
| 86 | + envs map[string]string |
| 87 | + wantErr string |
| 88 | + }{ |
| 89 | + { |
| 90 | + name: "MissingClickhouseURL", |
| 91 | + envs: map[string]string{ |
| 92 | + "BASIC_AUTH": "user:password", |
| 93 | + }, |
| 94 | + wantErr: "CLICKHOUSE_URL must be defined", |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "MissingBasicAuth", |
| 98 | + envs: map[string]string{ |
| 99 | + "CLICKHOUSE_URL": "http://localhost:8123", |
| 100 | + }, |
| 101 | + wantErr: "BASIC_AUTH must be defined", |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "InvalidSampleRate", |
| 105 | + envs: map[string]string{ |
| 106 | + "CLICKHOUSE_URL": "http://localhost:8123", |
| 107 | + "BASIC_AUTH": "user:password", |
| 108 | + "OTEL_TRACE_SAMPLE_RATE": "invalid", |
| 109 | + }, |
| 110 | + wantErr: "invalid TRACE_SAMPLE_RATE", |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "SampleRateTooLow", |
| 114 | + envs: map[string]string{ |
| 115 | + "CLICKHOUSE_URL": "http://localhost:8123", |
| 116 | + "BASIC_AUTH": "user:password", |
| 117 | + "OTEL_TRACE_SAMPLE_RATE": "-0.1", |
| 118 | + }, |
| 119 | + wantErr: "OTEL_TRACE_SAMPLE_RATE must be between 0.0 and 1.0", |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "SampleRateTooHigh", |
| 123 | + envs: map[string]string{ |
| 124 | + "CLICKHOUSE_URL": "http://localhost:8123", |
| 125 | + "BASIC_AUTH": "user:password", |
| 126 | + "OTEL_TRACE_SAMPLE_RATE": "1.1", |
| 127 | + }, |
| 128 | + wantErr: "OTEL_TRACE_SAMPLE_RATE must be between 0.0 and 1.0", |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + for _, tc := range tests { |
| 133 | + t.Run(tc.name, func(t *testing.T) { |
| 134 | + // Clean environment before each test case |
| 135 | + cleanEnv(t, envKeys) |
| 136 | + |
| 137 | + // Set up environment for this test case |
| 138 | + setupEnv(t, tc.envs) |
| 139 | + |
| 140 | + _, err := LoadConfig() |
| 141 | + if err == nil { |
| 142 | + t.Fatalf("LoadConfig() error = nil, wantErr = true") |
| 143 | + } |
| 144 | + |
| 145 | + if got := err.Error(); got != tc.wantErr && got[:len(tc.wantErr)] != tc.wantErr { |
| 146 | + t.Errorf("LoadConfig() error = %v, want to contain %v", got, tc.wantErr) |
| 147 | + } |
| 148 | + }) |
| 149 | + } |
| 150 | + }) |
| 151 | + |
| 152 | + // Test for custom configurations |
| 153 | + t.Run("CustomConfigurations", func(t *testing.T) { |
| 154 | + // Table-driven tests for custom configurations |
| 155 | + tests := []struct { |
| 156 | + name string |
| 157 | + envs map[string]string |
| 158 | + checkFunc func(*testing.T, *Config) |
| 159 | + }{ |
| 160 | + { |
| 161 | + name: "CustomPort", |
| 162 | + envs: map[string]string{ |
| 163 | + "CLICKHOUSE_URL": "http://localhost:8123", |
| 164 | + "BASIC_AUTH": "user:password", |
| 165 | + "PORT": "8000", |
| 166 | + }, |
| 167 | + checkFunc: func(t *testing.T, c *Config) { |
| 168 | + if c.ListenerPort != "8000" { |
| 169 | + t.Errorf("ListenerPort = %s, want 8000", c.ListenerPort) |
| 170 | + } |
| 171 | + }, |
| 172 | + }, |
| 173 | + { |
| 174 | + name: "DebugMode", |
| 175 | + envs: map[string]string{ |
| 176 | + "CLICKHOUSE_URL": "http://localhost:8123", |
| 177 | + "BASIC_AUTH": "user:password", |
| 178 | + "OTEL_EXPORTER_LOG_DEBUG": "true", |
| 179 | + }, |
| 180 | + checkFunc: func(t *testing.T, c *Config) { |
| 181 | + if !c.LogDebug { |
| 182 | + t.Errorf("LogDebug = %v, want true", c.LogDebug) |
| 183 | + } |
| 184 | + }, |
| 185 | + }, |
| 186 | + { |
| 187 | + name: "CustomSampleRate", |
| 188 | + envs: map[string]string{ |
| 189 | + "CLICKHOUSE_URL": "http://localhost:8123", |
| 190 | + "BASIC_AUTH": "user:password", |
| 191 | + "OTEL_TRACE_SAMPLE_RATE": "0.5", |
| 192 | + }, |
| 193 | + checkFunc: func(t *testing.T, c *Config) { |
| 194 | + if c.TraceSampleRate != 0.5 { |
| 195 | + t.Errorf("TraceSampleRate = %f, want 0.5", c.TraceSampleRate) |
| 196 | + } |
| 197 | + }, |
| 198 | + }, |
| 199 | + { |
| 200 | + name: "ZeroSampleRate", |
| 201 | + envs: map[string]string{ |
| 202 | + "CLICKHOUSE_URL": "http://localhost:8123", |
| 203 | + "BASIC_AUTH": "user:password", |
| 204 | + "OTEL_TRACE_SAMPLE_RATE": "0.0", |
| 205 | + }, |
| 206 | + checkFunc: func(t *testing.T, c *Config) { |
| 207 | + if c.TraceSampleRate != 0.0 { |
| 208 | + t.Errorf("TraceSampleRate = %f, want 0.0", c.TraceSampleRate) |
| 209 | + } |
| 210 | + }, |
| 211 | + }, |
| 212 | + { |
| 213 | + name: "OneSampleRate", |
| 214 | + envs: map[string]string{ |
| 215 | + "CLICKHOUSE_URL": "http://localhost:8123", |
| 216 | + "BASIC_AUTH": "user:password", |
| 217 | + "OTEL_TRACE_SAMPLE_RATE": "1.0", |
| 218 | + }, |
| 219 | + checkFunc: func(t *testing.T, c *Config) { |
| 220 | + if c.TraceSampleRate != 1.0 { |
| 221 | + t.Errorf("TraceSampleRate = %f, want 1.0", c.TraceSampleRate) |
| 222 | + } |
| 223 | + }, |
| 224 | + }, |
| 225 | + } |
| 226 | + |
| 227 | + for _, tc := range tests { |
| 228 | + t.Run(tc.name, func(t *testing.T) { |
| 229 | + // Clean environment before each test case |
| 230 | + cleanEnv(t, envKeys) |
| 231 | + |
| 232 | + // Set up environment for this test case |
| 233 | + setupEnv(t, tc.envs) |
| 234 | + |
| 235 | + config, err := LoadConfig() |
| 236 | + if err != nil { |
| 237 | + t.Fatalf("LoadConfig() error = %v, wantErr = false", err) |
| 238 | + } |
| 239 | + |
| 240 | + tc.checkFunc(t, config) |
| 241 | + }) |
| 242 | + } |
| 243 | + }) |
| 244 | +} |
0 commit comments