|
5 | 5 | package config |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "bytes" |
8 | 9 | _ "embed" |
9 | 10 | "errors" |
| 11 | + "log/slog" |
10 | 12 | "os" |
11 | 13 | "path" |
12 | 14 | "sort" |
@@ -164,6 +166,100 @@ func TestNormalizeFunc(t *testing.T) { |
164 | 166 | assert.Equal(t, expected, result) |
165 | 167 | } |
166 | 168 |
|
| 169 | +type deprecatedEnvVarsTest struct { |
| 170 | + name string |
| 171 | + expectedLogContent string |
| 172 | + unexpectedLogContent string |
| 173 | + envVars map[string]string |
| 174 | + viperKeys []string |
| 175 | + expectWarning bool |
| 176 | +} |
| 177 | + |
| 178 | +func TestCheckDeprecatedEnvVars(t *testing.T) { |
| 179 | + tests := []deprecatedEnvVarsTest{ |
| 180 | + { |
| 181 | + name: "Test 1: should log warning for deprecated env var", |
| 182 | + envVars: map[string]string{ |
| 183 | + "NGINX_AGENT_SERVER_HOST": "value", |
| 184 | + }, |
| 185 | + viperKeys: []string{"some_other_key"}, |
| 186 | + expectedLogContent: "NGINX_AGENT_SERVER_HOST", |
| 187 | + expectWarning: true, |
| 188 | + }, |
| 189 | + { |
| 190 | + name: "Test 2: should not log warning for valid env var", |
| 191 | + envVars: map[string]string{ |
| 192 | + "NGINX_AGENT_LOG_LEVEL": "info", |
| 193 | + }, |
| 194 | + viperKeys: []string{"log_level"}, |
| 195 | + unexpectedLogContent: "NGINX_AGENT_LOG_LEVEL", |
| 196 | + expectWarning: false, |
| 197 | + }, |
| 198 | + { |
| 199 | + name: "Test 3: should handle mixed valid and deprecated env vars", |
| 200 | + envVars: map[string]string{ |
| 201 | + "NGINX_AGENT_LOG_LEVEL": "info", |
| 202 | + "NGINX_AGENT_DEPRECATED_VAR": "value", |
| 203 | + }, |
| 204 | + viperKeys: []string{"log_level"}, |
| 205 | + expectedLogContent: "NGINX_AGENT_DEPRECATED_VAR", |
| 206 | + unexpectedLogContent: "NGINX_AGENT_LOG_LEVEL", |
| 207 | + expectWarning: true, |
| 208 | + }, |
| 209 | + { |
| 210 | + name: "Test 4: should ignore non-agent env vars", |
| 211 | + envVars: map[string]string{ |
| 212 | + "NGINX_LICENSE": "value", |
| 213 | + }, |
| 214 | + viperKeys: []string{}, |
| 215 | + expectWarning: false, |
| 216 | + }, |
| 217 | + } |
| 218 | + |
| 219 | + for _, tc := range tests { |
| 220 | + t.Run(tc.name, func(t *testing.T) { |
| 221 | + runDeprecatedEnvVarsTest(t, tc) |
| 222 | + }) |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +func runDeprecatedEnvVarsTest(t *testing.T, tc deprecatedEnvVarsTest) { |
| 227 | + t.Helper() |
| 228 | + |
| 229 | + originalViper := viperInstance |
| 230 | + viperInstance = viper.NewWithOptions(viper.KeyDelimiter(KeyDelimiter)) |
| 231 | + defer func() { viperInstance = originalViper }() |
| 232 | + |
| 233 | + for key, value := range tc.envVars { |
| 234 | + t.Setenv(key, value) |
| 235 | + } |
| 236 | + |
| 237 | + for _, key := range tc.viperKeys { |
| 238 | + viperInstance.Set(key, "any-value") |
| 239 | + } |
| 240 | + |
| 241 | + var logBuffer bytes.Buffer |
| 242 | + handler := slog.NewTextHandler(&logBuffer, nil) |
| 243 | + slog.SetDefault(slog.New(handler)) |
| 244 | + |
| 245 | + checkDeprecatedEnvVars() |
| 246 | + |
| 247 | + logOutput := logBuffer.String() |
| 248 | + |
| 249 | + if tc.expectWarning { |
| 250 | + require.NotEmpty(t, logOutput, "Expected a warning log, but got none") |
| 251 | + assert.Contains(t, logOutput, "Detected deprecated or unknown environment variables") |
| 252 | + if tc.expectedLogContent != "" { |
| 253 | + assert.Contains(t, logOutput, tc.expectedLogContent) |
| 254 | + } |
| 255 | + if tc.unexpectedLogContent != "" { |
| 256 | + assert.NotContains(t, logOutput, tc.unexpectedLogContent) |
| 257 | + } |
| 258 | + } else { |
| 259 | + assert.Empty(t, logOutput, "Expected no warning logs") |
| 260 | + } |
| 261 | +} |
| 262 | + |
167 | 263 | func TestResolveAllowedDirectories(t *testing.T) { |
168 | 264 | tests := []struct { |
169 | 265 | name string |
@@ -1237,7 +1333,7 @@ func createConfig() *Config { |
1237 | 1333 | { |
1238 | 1334 | Action: "insert", |
1239 | 1335 | Key: "label1", |
1240 | | - Value: "label 1", |
| 1336 | + Value: "label-1", |
1241 | 1337 | }, |
1242 | 1338 | { |
1243 | 1339 | Action: "insert", |
@@ -1317,7 +1413,7 @@ func createConfig() *Config { |
1317 | 1413 | }, |
1318 | 1414 | }, |
1319 | 1415 | Labels: map[string]any{ |
1320 | | - "label1": "label 1", |
| 1416 | + "label1": "label-1", |
1321 | 1417 | "label2": "new-value", |
1322 | 1418 | "label3": 123, |
1323 | 1419 | }, |
@@ -1413,3 +1509,59 @@ func createDefaultCollectorConfig() *Collector { |
1413 | 1509 | }, |
1414 | 1510 | } |
1415 | 1511 | } |
| 1512 | + |
| 1513 | +func TestValidateLabel(t *testing.T) { |
| 1514 | + tests := []struct { |
| 1515 | + name string |
| 1516 | + input string |
| 1517 | + expected bool |
| 1518 | + }{ |
| 1519 | + { |
| 1520 | + name: "Test 1: Valid label - simple", |
| 1521 | + input: "label123", |
| 1522 | + expected: true, |
| 1523 | + }, |
| 1524 | + { |
| 1525 | + name: "Test 2: Valid label - with dash and underscore", |
| 1526 | + input: "label-123_abc", |
| 1527 | + expected: true, |
| 1528 | + }, |
| 1529 | + { |
| 1530 | + name: "Test 3: Invalid label - too long", |
| 1531 | + input: strings.Repeat("a", 257), |
| 1532 | + expected: false, |
| 1533 | + }, |
| 1534 | + { |
| 1535 | + name: "Test 4: Invalid label - special char", |
| 1536 | + input: "label$", |
| 1537 | + expected: false, |
| 1538 | + }, |
| 1539 | + { |
| 1540 | + name: "Test 5: Invalid label - starts with dash", |
| 1541 | + input: "-label", |
| 1542 | + expected: false, |
| 1543 | + }, |
| 1544 | + { |
| 1545 | + name: "Test 6: Invalid label - ends with dash", |
| 1546 | + input: "label-", |
| 1547 | + expected: false, |
| 1548 | + }, |
| 1549 | + { |
| 1550 | + name: "Test 7: Invalid label - empty", |
| 1551 | + input: "", |
| 1552 | + expected: false, |
| 1553 | + }, |
| 1554 | + { |
| 1555 | + name: "Test 8: Invalid label - contains spaces", |
| 1556 | + input: "label 123", |
| 1557 | + expected: false, |
| 1558 | + }, |
| 1559 | + } |
| 1560 | + |
| 1561 | + for _, tt := range tests { |
| 1562 | + t.Run(tt.name, func(t *testing.T) { |
| 1563 | + actual := validateLabel(tt.input) |
| 1564 | + assert.Equal(t, tt.expected, actual) |
| 1565 | + }) |
| 1566 | + } |
| 1567 | +} |
0 commit comments