-
Notifications
You must be signed in to change notification settings - Fork 0
Tests for Introduce config.FromEnv() #58
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,104 @@ | ||
| package database | ||
|
|
||
| import ( | ||
| "github.com/creasty/defaults" | ||
| "github.com/icinga/icinga-go-library/config" | ||
| "github.com/stretchr/testify/require" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestConfig(t *testing.T) { | ||
| var defaultOptions Options | ||
| require.NoError(t, defaults.Set(&defaultOptions), "setting default options") | ||
|
|
||
| subtests := []struct { | ||
| name string | ||
| opts config.EnvOptions | ||
| expected Config | ||
| error bool | ||
| }{ | ||
| { | ||
| name: "empty-missing-fields", | ||
| opts: config.EnvOptions{}, | ||
| error: true, | ||
| }, | ||
| { | ||
| name: "unknown-db-type", | ||
| opts: config.EnvOptions{Environment: map[string]string{"TYPE": "☃"}}, | ||
| error: true, | ||
| }, | ||
| { | ||
| name: "minimal-config", | ||
| opts: config.EnvOptions{Environment: map[string]string{ | ||
| "HOST": "db.example.com", | ||
| "USER": "user", | ||
| "DATABASE": "db", | ||
| }}, | ||
| expected: Config{ | ||
| Type: "mysql", | ||
| Host: "db.example.com", | ||
| Database: "db", | ||
| User: "user", | ||
| Options: defaultOptions, | ||
| }, | ||
| }, | ||
| { | ||
| name: "tls", | ||
| opts: config.EnvOptions{Environment: map[string]string{ | ||
| "HOST": "db.example.com", | ||
| "USER": "user", | ||
| "DATABASE": "db", | ||
| "TLS": "true", | ||
| "CERT": "/var/empty/db.crt", | ||
| "CA": "/var/empty/ca.crt", | ||
| }}, | ||
| expected: Config{ | ||
| Type: "mysql", | ||
| Host: "db.example.com", | ||
| Database: "db", | ||
| User: "user", | ||
| TlsOptions: config.TLS{ | ||
| Enable: true, | ||
| Cert: "/var/empty/db.crt", | ||
| Ca: "/var/empty/ca.crt", | ||
| }, | ||
| Options: defaultOptions, | ||
| }, | ||
| }, | ||
| { | ||
| name: "options", | ||
| opts: config.EnvOptions{Environment: map[string]string{ | ||
| "HOST": "db.example.com", | ||
| "USER": "user", | ||
| "DATABASE": "db", | ||
| "OPTIONS_MAX_CONNECTIONS": "1", | ||
| "OPTIONS_MAX_ROWS_PER_TRANSACTION": "65535", | ||
| }}, | ||
| expected: Config{ | ||
| Type: "mysql", | ||
| Host: "db.example.com", | ||
| Database: "db", | ||
| User: "user", | ||
| Options: Options{ | ||
| MaxConnections: 1, | ||
| MaxConnectionsPerTable: 8, | ||
| MaxPlaceholdersPerStatement: 8192, | ||
| MaxRowsPerTransaction: 65535, | ||
| WsrepSyncWait: 7, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range subtests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| var out Config | ||
| if err := config.FromEnv(&out, test.opts); test.error { | ||
| require.Error(t, err) | ||
| } else { | ||
| require.NoError(t, err) | ||
| require.Equal(t, test.expected, 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,75 @@ | ||
| package logging | ||
|
|
||
| import ( | ||
| "github.com/icinga/icinga-go-library/config" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/zap/zapcore" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestConfig(t *testing.T) { | ||
| subtests := []struct { | ||
| name string | ||
| opts config.EnvOptions | ||
| expected Config | ||
| error bool | ||
| }{ | ||
| { | ||
| name: "empty", | ||
| opts: config.EnvOptions{}, | ||
| expected: Config{ | ||
| Output: "console", | ||
| Interval: 20 * time.Second, | ||
| }, | ||
| }, | ||
| { | ||
| name: "invalid-output", | ||
| opts: config.EnvOptions{Environment: map[string]string{"OUTPUT": "☃"}}, | ||
| error: true, | ||
| }, | ||
| { | ||
| name: "customized", | ||
| opts: config.EnvOptions{Environment: map[string]string{ | ||
| "LEVEL": zapcore.DebugLevel.String(), | ||
| "OUTPUT": JOURNAL, | ||
| "INTERVAL": "3m14s", | ||
| }}, | ||
| expected: Config{ | ||
| Level: zapcore.DebugLevel, | ||
| Output: JOURNAL, | ||
| Interval: 3*time.Minute + 14*time.Second, | ||
| }, | ||
| }, | ||
| { | ||
| name: "options", | ||
| opts: config.EnvOptions{Environment: map[string]string{"OPTIONS": "foo:debug,bar:info,buz:panic"}}, | ||
| expected: Config{ | ||
| Output: "console", | ||
| Interval: 20 * time.Second, | ||
| Options: map[string]zapcore.Level{ | ||
| "foo": zapcore.DebugLevel, | ||
| "bar": zapcore.InfoLevel, | ||
| "buz": zapcore.PanicLevel, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "options-invalid-levels", | ||
| opts: config.EnvOptions{Environment: map[string]string{"OPTIONS": "foo:foo,bar:0"}}, | ||
| error: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range subtests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| var out Config | ||
| if err := config.FromEnv(&out, test.opts); test.error { | ||
| require.Error(t, err) | ||
| } else { | ||
| require.NoError(t, err) | ||
| require.Equal(t, test.expected, 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package redis | ||
|
|
||
| import ( | ||
| "github.com/creasty/defaults" | ||
| "github.com/icinga/icinga-go-library/config" | ||
| "github.com/stretchr/testify/require" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestConfig(t *testing.T) { | ||
| var defaultOptions Options | ||
| require.NoError(t, defaults.Set(&defaultOptions), "setting default options") | ||
|
|
||
| subtests := []struct { | ||
| name string | ||
| opts config.EnvOptions | ||
| expected Config | ||
| error bool | ||
| }{ | ||
| { | ||
| name: "empty-missing-host", | ||
| opts: config.EnvOptions{}, | ||
| error: true, | ||
| }, | ||
| { | ||
| name: "minimal-config", | ||
| opts: config.EnvOptions{Environment: map[string]string{"HOST": "kv.example.com"}}, | ||
| expected: Config{ | ||
| Host: "kv.example.com", | ||
| Options: defaultOptions, | ||
| }, | ||
| }, | ||
| { | ||
| name: "customized-config", | ||
| opts: config.EnvOptions{Environment: map[string]string{ | ||
| "HOST": "kv.example.com", | ||
| "USERNAME": "user", | ||
| "PASSWORD": "insecure", | ||
| "DATABASE": "23", | ||
| }}, | ||
| expected: Config{ | ||
| Host: "kv.example.com", | ||
| Username: "user", | ||
| Password: "insecure", | ||
| Database: 23, | ||
| Options: defaultOptions, | ||
| }, | ||
| }, | ||
| { | ||
| name: "tls", | ||
| opts: config.EnvOptions{Environment: map[string]string{ | ||
| "HOST": "kv.example.com", | ||
| "TLS": "true", | ||
| "CERT": "/var/empty/db.crt", | ||
| "CA": "/var/empty/ca.crt", | ||
| }}, | ||
| expected: Config{ | ||
| Host: "kv.example.com", | ||
| TlsOptions: config.TLS{ | ||
| Enable: true, | ||
| Cert: "/var/empty/db.crt", | ||
| Ca: "/var/empty/ca.crt", | ||
| }, | ||
| Options: defaultOptions, | ||
| }, | ||
| }, | ||
| { | ||
| name: "options", | ||
| opts: config.EnvOptions{Environment: map[string]string{ | ||
| "HOST": "kv.example.com", | ||
| "OPTIONS_BLOCK_TIMEOUT": "1m", | ||
| "OPTIONS_MAX_HMGET_CONNECTIONS": "1000", | ||
| }}, | ||
| expected: Config{ | ||
| Host: "kv.example.com", | ||
| Options: Options{ | ||
| BlockTimeout: time.Minute, | ||
| HMGetCount: 4096, | ||
| HScanCount: 4096, | ||
| MaxHMGetConnections: 1000, | ||
| Timeout: 30 * time.Second, | ||
| XReadCount: 4096, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range subtests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| var out Config | ||
| if err := config.FromEnv(&out, test.opts); test.error { | ||
| require.Error(t, err) | ||
| } else { | ||
| require.NoError(t, err) | ||
| require.Equal(t, test.expected, out) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
It turns out the lib already supports
[]encoding.TextUnmarshaler, so why notmap[T]encoding.TextUnmarshaler?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.
Thanks for trying to solve this issue upstream. I have updated the PR to reflect this potentially upcoming change in the method's signature, https://github.com/Icinga/icinga-go-library/compare/7d7fd247cd9a69fddf46f237a6ae54ba475cb7e5..2ba2d8b19d9a9460f7f58cc2cd8d31561c03edf3.
However, I am against waiting for this PR to get merged and a new env release to get drafted. If this will be supported in the future, the custom
encoding.TextUnmarshalercan just be removed on our end.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.
As written over at your env PR, it works fine. Thanks again.
However, I would still say to merge it in the current state, eventually removing the
encoding.TextUnmarshalerimplementation later on. We have no "influence" on the upstream, resulting in blocking this PR, resulting in blocking your PR, resulting in having less tests (your PR would be nice to have in #60) and also resulting in blocking the icinga-go-library release.