-
Notifications
You must be signed in to change notification settings - Fork 101
Check for deprecated/unknown environment variables #1342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,10 @@ | |
| package config | ||
|
|
||
| import ( | ||
| "bytes" | ||
| _ "embed" | ||
| "errors" | ||
| "log/slog" | ||
| "os" | ||
| "path" | ||
| "sort" | ||
|
|
@@ -163,6 +165,100 @@ func TestNormalizeFunc(t *testing.T) { | |
| assert.Equal(t, expected, result) | ||
| } | ||
|
|
||
| type deprecatedEnvVarsTest struct { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be moved into the test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to define it outside as the linter gives error if we have more than 5 parameter in a function call. |
||
| name string | ||
| expectedLogContent string | ||
| unexpectedLogContent string | ||
| envVars map[string]string | ||
| viperKeys []string | ||
| expectWarning bool | ||
| } | ||
|
|
||
| func TestCheckDeprecatedEnvVars(t *testing.T) { | ||
| tests := []deprecatedEnvVarsTest{ | ||
| { | ||
| name: "Test 1: should log warning for deprecated env var", | ||
| envVars: map[string]string{ | ||
| "NGINX_AGENT_SERVER_HOST": "value", | ||
| }, | ||
| viperKeys: []string{"some_other_key"}, | ||
| expectedLogContent: "NGINX_AGENT_SERVER_HOST", | ||
| expectWarning: true, | ||
| }, | ||
| { | ||
| name: "Test 2: should not log warning for valid env var", | ||
| envVars: map[string]string{ | ||
| "NGINX_AGENT_LOG_LEVEL": "info", | ||
| }, | ||
| viperKeys: []string{"log_level"}, | ||
| unexpectedLogContent: "NGINX_AGENT_LOG_LEVEL", | ||
| expectWarning: false, | ||
| }, | ||
| { | ||
| name: "Test 3: should handle mixed valid and deprecated env vars", | ||
| envVars: map[string]string{ | ||
| "NGINX_AGENT_LOG_LEVEL": "info", | ||
| "NGINX_AGENT_DEPRECATED_VAR": "value", | ||
| }, | ||
| viperKeys: []string{"log_level"}, | ||
| expectedLogContent: "NGINX_AGENT_DEPRECATED_VAR", | ||
| unexpectedLogContent: "NGINX_AGENT_LOG_LEVEL", | ||
| expectWarning: true, | ||
| }, | ||
| { | ||
| name: "Test 4: should ignore non-agent env vars", | ||
| envVars: map[string]string{ | ||
| "NGINX_LICENSE": "value", | ||
| }, | ||
| viperKeys: []string{}, | ||
| expectWarning: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| runDeprecatedEnvVarsTest(t, tc) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func runDeprecatedEnvVarsTest(t *testing.T, tc deprecatedEnvVarsTest) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this instead just be done inside the unit tests instead of splitting it up There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That increases the Cyclomatic complexity of the Test function. |
||
| t.Helper() | ||
|
|
||
| originalViper := viperInstance | ||
| viperInstance = viper.NewWithOptions(viper.KeyDelimiter(KeyDelimiter)) | ||
| defer func() { viperInstance = originalViper }() | ||
|
|
||
| for key, value := range tc.envVars { | ||
| t.Setenv(key, value) | ||
| } | ||
|
|
||
| for _, key := range tc.viperKeys { | ||
| viperInstance.Set(key, "any-value") | ||
| } | ||
|
|
||
| var logBuffer bytes.Buffer | ||
| handler := slog.NewTextHandler(&logBuffer, nil) | ||
| slog.SetDefault(slog.New(handler)) | ||
|
|
||
| checkDeprecatedEnvVars() | ||
|
|
||
| logOutput := logBuffer.String() | ||
|
|
||
| if tc.expectWarning { | ||
| require.NotEmpty(t, logOutput, "Expected a warning log, but got none") | ||
| assert.Contains(t, logOutput, "Detected deprecated or unknown environment variables") | ||
| if tc.expectedLogContent != "" { | ||
| assert.Contains(t, logOutput, tc.expectedLogContent) | ||
| } | ||
| if tc.unexpectedLogContent != "" { | ||
| assert.NotContains(t, logOutput, tc.unexpectedLogContent) | ||
| } | ||
| } else { | ||
| assert.Empty(t, logOutput, "Expected no warning logs") | ||
| } | ||
| } | ||
|
|
||
| func TestResolveAllowedDirectories(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can revert this change, main branch will use the dev goproxy by default