-
Notifications
You must be signed in to change notification settings - Fork 612
feat(chproxy): add unit tests for config.go #2975
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| chproxy | ||
| coverage.out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestLoadConfig(t *testing.T) { | ||
| // Setup and cleanup for all tests | ||
| setupEnv := func(t *testing.T, envs map[string]string) { | ||
| t.Helper() | ||
| for k, v := range envs { | ||
| os.Setenv(k, v) | ||
| } | ||
| } | ||
|
|
||
| cleanEnv := func(t *testing.T, keys []string) { | ||
| t.Helper() | ||
| for _, k := range keys { | ||
| os.Unsetenv(k) | ||
| } | ||
| } | ||
|
|
||
| // Required env vars that need to be cleaned up | ||
| envKeys := []string{ | ||
| "CLICKHOUSE_URL", | ||
| "BASIC_AUTH", | ||
| "PORT", | ||
| "OTEL_EXPORTER_LOG_DEBUG", | ||
| "OTEL_TRACE_SAMPLE_RATE", | ||
| } | ||
|
|
||
| // Cleanup after all tests in this function | ||
| t.Cleanup(func() { | ||
| cleanEnv(t, envKeys) | ||
| }) | ||
|
|
||
| // Test for valid config with default values | ||
| t.Run("ValidConfig", func(t *testing.T) { | ||
| // Set required environment variables | ||
| setupEnv(t, map[string]string{ | ||
| "CLICKHOUSE_URL": "http://localhost:8123", | ||
| "BASIC_AUTH": "user:password", | ||
| }) | ||
|
|
||
| config, err := LoadConfig() | ||
| if err != nil { | ||
| t.Fatalf("LoadConfig() error = %v, wantErr = false", err) | ||
| } | ||
|
|
||
| // Use a table-driven check for expected values | ||
| tests := []struct { | ||
| name string | ||
| got interface{} | ||
| expected interface{} | ||
| }{ | ||
| {"FlushInterval", config.FlushInterval, time.Second * 5}, | ||
| {"ListenerPort", config.ListenerPort, "7123"}, | ||
| {"LogDebug", config.LogDebug, false}, | ||
| {"ServiceName", config.ServiceName, "chproxy"}, | ||
| {"ServiceVersion", config.ServiceVersion, "1.3.1"}, | ||
| {"TraceMaxBatchSize", config.TraceMaxBatchSize, 512}, | ||
| {"TraceSampleRate", config.TraceSampleRate, 0.25}, | ||
| {"ClickhouseURL", config.ClickhouseURL, "http://localhost:8123"}, | ||
| {"BasicAuth", config.BasicAuth, "user:password"}, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| if tc.got != tc.expected { | ||
| t.Errorf("%s = %v, want %v", tc.name, tc.got, tc.expected) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| if config.Logger == nil { | ||
| t.Error("Logger is nil, want non-nil") | ||
| } | ||
| }) | ||
|
|
||
| // Test for error cases | ||
| t.Run("ErrorCases", func(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| envs map[string]string | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "MissingClickhouseURL", | ||
| envs: map[string]string{ | ||
| "BASIC_AUTH": "user:password", | ||
| }, | ||
| wantErr: "CLICKHOUSE_URL must be defined", | ||
| }, | ||
| { | ||
| name: "MissingBasicAuth", | ||
| envs: map[string]string{ | ||
| "CLICKHOUSE_URL": "http://localhost:8123", | ||
| }, | ||
| wantErr: "BASIC_AUTH must be defined", | ||
| }, | ||
| { | ||
| name: "InvalidSampleRate", | ||
| envs: map[string]string{ | ||
| "CLICKHOUSE_URL": "http://localhost:8123", | ||
| "BASIC_AUTH": "user:password", | ||
| "OTEL_TRACE_SAMPLE_RATE": "invalid", | ||
| }, | ||
| wantErr: "invalid TRACE_SAMPLE_RATE", | ||
| }, | ||
| { | ||
| name: "SampleRateTooLow", | ||
| envs: map[string]string{ | ||
| "CLICKHOUSE_URL": "http://localhost:8123", | ||
| "BASIC_AUTH": "user:password", | ||
| "OTEL_TRACE_SAMPLE_RATE": "-0.1", | ||
| }, | ||
| wantErr: "OTEL_TRACE_SAMPLE_RATE must be between 0.0 and 1.0", | ||
| }, | ||
| { | ||
| name: "SampleRateTooHigh", | ||
| envs: map[string]string{ | ||
| "CLICKHOUSE_URL": "http://localhost:8123", | ||
| "BASIC_AUTH": "user:password", | ||
| "OTEL_TRACE_SAMPLE_RATE": "1.1", | ||
| }, | ||
| wantErr: "OTEL_TRACE_SAMPLE_RATE must be between 0.0 and 1.0", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // Clean environment before each test case | ||
| cleanEnv(t, envKeys) | ||
|
|
||
| // Set up environment for this test case | ||
| setupEnv(t, tc.envs) | ||
|
|
||
| _, err := LoadConfig() | ||
| if err == nil { | ||
| t.Fatalf("LoadConfig() error = nil, wantErr = true") | ||
| } | ||
|
|
||
| if got := err.Error(); got != tc.wantErr && got[:len(tc.wantErr)] != tc.wantErr { | ||
| t.Errorf("LoadConfig() error = %v, want to contain %v", got, tc.wantErr) | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| // Test for custom configurations | ||
| t.Run("CustomConfigurations", func(t *testing.T) { | ||
| // Table-driven tests for custom configurations | ||
| tests := []struct { | ||
| name string | ||
| envs map[string]string | ||
| checkFunc func(*testing.T, *Config) | ||
| }{ | ||
| { | ||
| name: "CustomPort", | ||
| envs: map[string]string{ | ||
| "CLICKHOUSE_URL": "http://localhost:8123", | ||
| "BASIC_AUTH": "user:password", | ||
| "PORT": "8000", | ||
| }, | ||
| checkFunc: func(t *testing.T, c *Config) { | ||
| if c.ListenerPort != "8000" { | ||
| t.Errorf("ListenerPort = %s, want 8000", c.ListenerPort) | ||
| } | ||
| }, | ||
| }, | ||
| { | ||
| name: "DebugMode", | ||
| envs: map[string]string{ | ||
| "CLICKHOUSE_URL": "http://localhost:8123", | ||
| "BASIC_AUTH": "user:password", | ||
| "OTEL_EXPORTER_LOG_DEBUG": "true", | ||
| }, | ||
| checkFunc: func(t *testing.T, c *Config) { | ||
| if !c.LogDebug { | ||
| t.Errorf("LogDebug = %v, want true", c.LogDebug) | ||
| } | ||
| }, | ||
| }, | ||
| { | ||
| name: "CustomSampleRate", | ||
| envs: map[string]string{ | ||
| "CLICKHOUSE_URL": "http://localhost:8123", | ||
| "BASIC_AUTH": "user:password", | ||
| "OTEL_TRACE_SAMPLE_RATE": "0.5", | ||
| }, | ||
| checkFunc: func(t *testing.T, c *Config) { | ||
| if c.TraceSampleRate != 0.5 { | ||
| t.Errorf("TraceSampleRate = %f, want 0.5", c.TraceSampleRate) | ||
| } | ||
| }, | ||
| }, | ||
| { | ||
| name: "ZeroSampleRate", | ||
| envs: map[string]string{ | ||
| "CLICKHOUSE_URL": "http://localhost:8123", | ||
| "BASIC_AUTH": "user:password", | ||
| "OTEL_TRACE_SAMPLE_RATE": "0.0", | ||
| }, | ||
| checkFunc: func(t *testing.T, c *Config) { | ||
| if c.TraceSampleRate != 0.0 { | ||
| t.Errorf("TraceSampleRate = %f, want 0.0", c.TraceSampleRate) | ||
| } | ||
| }, | ||
| }, | ||
| { | ||
| name: "OneSampleRate", | ||
| envs: map[string]string{ | ||
| "CLICKHOUSE_URL": "http://localhost:8123", | ||
| "BASIC_AUTH": "user:password", | ||
| "OTEL_TRACE_SAMPLE_RATE": "1.0", | ||
| }, | ||
| checkFunc: func(t *testing.T, c *Config) { | ||
| if c.TraceSampleRate != 1.0 { | ||
| t.Errorf("TraceSampleRate = %f, want 1.0", c.TraceSampleRate) | ||
| } | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // Clean environment before each test case | ||
| cleanEnv(t, envKeys) | ||
|
|
||
| // Set up environment for this test case | ||
| setupEnv(t, tc.envs) | ||
|
|
||
| config, err := LoadConfig() | ||
| if err != nil { | ||
| t.Fatalf("LoadConfig() error = %v, wantErr = false", err) | ||
| } | ||
|
|
||
| tc.checkFunc(t, config) | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🛠️ Refactor suggestion
Potential index out of range error in error comparison
The current error check could panic if the actual error message is shorter than the expected error message.
Replace with a safer approach using
strings.Contains:📝 Committable suggestion