Skip to content

Commit 64c2634

Browse files
committed
logging#Config: Also test with config.FromYAMLFile()
1 parent 91a6d37 commit 64c2634

File tree

1 file changed

+92
-40
lines changed

1 file changed

+92
-40
lines changed

logging/config_test.go

Lines changed: 92 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,84 @@
11
package logging
22

33
import (
4+
"fmt"
5+
"github.com/creasty/defaults"
46
"github.com/icinga/icinga-go-library/config"
7+
"github.com/icinga/icinga-go-library/testutils"
58
"github.com/stretchr/testify/require"
69
"go.uber.org/zap/zapcore"
10+
"os"
711
"testing"
812
"time"
913
)
1014

1115
func TestConfig(t *testing.T) {
12-
subtests := []struct {
13-
name string
14-
opts config.EnvOptions
15-
expected Config
16-
error bool
17-
}{
16+
var defaultConfig Config
17+
require.NoError(t, defaults.Set(&defaultConfig), "setting default config")
18+
// Validate also sets default values.
19+
require.NoError(t, defaultConfig.Validate(), "setting default config")
20+
21+
configTests := []testutils.TestCase[Config, testutils.ConfigTestData]{
22+
{
23+
Name: "Defaults",
24+
Data: testutils.ConfigTestData{
25+
// An empty YAML file causes an error,
26+
// so specify a valid key without a value to trigger fallback to the default.
27+
Yaml: `level:`,
28+
},
29+
Expected: defaultConfig,
30+
},
1831
{
19-
name: "empty",
20-
opts: config.EnvOptions{},
21-
expected: Config{
22-
Output: "console",
23-
Interval: 20 * time.Second,
32+
Name: "periodic logging interval must be positive",
33+
Data: testutils.ConfigTestData{
34+
Yaml: `interval: 0s`,
35+
Env: map[string]string{"INTERVAL": "0s"},
2436
},
37+
Error: testutils.ErrorContains("periodic logging interval must be positive"),
2538
},
2639
{
27-
name: "invalid-output",
28-
opts: config.EnvOptions{Environment: map[string]string{"OUTPUT": "☃"}},
29-
error: true,
40+
Name: "invalid logger output",
41+
Data: testutils.ConfigTestData{
42+
Yaml: `output: invalid`,
43+
Env: map[string]string{"OUTPUT": "invalid"},
44+
},
45+
Error: testutils.ErrorContains("invalid is not a valid logger output"),
3046
},
3147
{
32-
name: "customized",
33-
opts: config.EnvOptions{Environment: map[string]string{
34-
"LEVEL": zapcore.DebugLevel.String(),
35-
"OUTPUT": JOURNAL,
36-
"INTERVAL": "3m14s",
37-
}},
38-
expected: Config{
48+
Name: "Customized",
49+
Data: testutils.ConfigTestData{
50+
Yaml: fmt.Sprintf(
51+
`
52+
level: debug
53+
output: %s
54+
interval: 3m14s`,
55+
JOURNAL,
56+
),
57+
Env: map[string]string{
58+
"LEVEL": zapcore.DebugLevel.String(),
59+
"OUTPUT": JOURNAL,
60+
"INTERVAL": "3m14s",
61+
},
62+
},
63+
Expected: Config{
3964
Level: zapcore.DebugLevel,
4065
Output: JOURNAL,
4166
Interval: 3*time.Minute + 14*time.Second,
4267
},
4368
},
4469
{
45-
name: "options",
46-
opts: config.EnvOptions{Environment: map[string]string{"OPTIONS": "foo:debug,bar:info,buz:panic"}},
47-
expected: Config{
48-
Output: "console",
49-
Interval: 20 * time.Second,
70+
Name: "Options",
71+
Data: testutils.ConfigTestData{
72+
Yaml: `
73+
options:
74+
foo: debug
75+
bar: info
76+
buz: panic`,
77+
Env: map[string]string{"OPTIONS": "foo:debug,bar:info,buz:panic"},
78+
},
79+
Expected: Config{
80+
Output: defaultConfig.Output,
81+
Interval: defaultConfig.Interval,
5082
Options: map[string]zapcore.Level{
5183
"foo": zapcore.DebugLevel,
5284
"bar": zapcore.InfoLevel,
@@ -55,21 +87,41 @@ func TestConfig(t *testing.T) {
5587
},
5688
},
5789
{
58-
name: "options-invalid-levels",
59-
opts: config.EnvOptions{Environment: map[string]string{"OPTIONS": "foo:foo,bar:0"}},
60-
error: true,
90+
Name: "Options with invalid level",
91+
Data: testutils.ConfigTestData{
92+
Yaml: `
93+
options:
94+
foo: foo`,
95+
Env: map[string]string{"OPTIONS": "foo:foo"},
96+
},
97+
Error: testutils.ErrorContains(`unrecognized level: "foo"`),
6198
},
6299
}
63100

64-
for _, test := range subtests {
65-
t.Run(test.name, func(t *testing.T) {
66-
var out Config
67-
if err := config.FromEnv(&out, test.opts); test.error {
68-
require.Error(t, err)
69-
} else {
70-
require.NoError(t, err)
71-
require.Equal(t, test.expected, out)
72-
}
73-
})
74-
}
101+
t.Run("FromEnv", func(t *testing.T) {
102+
for _, tc := range configTests {
103+
t.Run(tc.Name, tc.F(func(data testutils.ConfigTestData) (Config, error) {
104+
var actual Config
105+
106+
err := config.FromEnv(&actual, config.EnvOptions{Environment: data.Env})
107+
108+
return actual, err
109+
}))
110+
}
111+
})
112+
113+
t.Run("FromYAMLFile", func(t *testing.T) {
114+
for _, tc := range configTests {
115+
t.Run(tc.Name+"/FromYAMLFile", tc.F(func(data testutils.ConfigTestData) (Config, error) {
116+
var actual Config
117+
118+
var err error
119+
testutils.WithYAMLFile(t, data.Yaml, func(file *os.File) {
120+
err = config.FromYAMLFile(file.Name(), &actual)
121+
})
122+
123+
return actual, err
124+
}))
125+
}
126+
})
75127
}

0 commit comments

Comments
 (0)