|
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 |
|
0 commit comments