Skip to content

Commit 316f0d8

Browse files
oxziAl2Klimov
authored andcommitted
database/logging/redis: Add TestConfig for envs
1 parent 692eb92 commit 316f0d8

File tree

3 files changed

+287
-0
lines changed

3 files changed

+287
-0
lines changed

database/config_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package database
2+
3+
import (
4+
"github.com/creasty/defaults"
5+
"github.com/icinga/icinga-go-library/config"
6+
"github.com/stretchr/testify/require"
7+
"testing"
8+
)
9+
10+
func TestConfig(t *testing.T) {
11+
var defaultOptions Options
12+
require.NoError(t, defaults.Set(&defaultOptions), "setting default options")
13+
14+
subtests := []struct {
15+
name string
16+
opts config.EnvOptions
17+
expected Config
18+
error bool
19+
}{
20+
{
21+
name: "empty-missing-fields",
22+
opts: config.EnvOptions{},
23+
error: true,
24+
},
25+
{
26+
name: "unknown-db-type",
27+
opts: config.EnvOptions{Environment: map[string]string{"TYPE": "☃"}},
28+
error: true,
29+
},
30+
{
31+
name: "minimal-config",
32+
opts: config.EnvOptions{Environment: map[string]string{
33+
"HOST": "db.example.com",
34+
"USER": "user",
35+
"DATABASE": "db",
36+
}},
37+
expected: Config{
38+
Type: "mysql",
39+
Host: "db.example.com",
40+
Database: "db",
41+
User: "user",
42+
Options: defaultOptions,
43+
},
44+
},
45+
{
46+
name: "tls",
47+
opts: config.EnvOptions{Environment: map[string]string{
48+
"HOST": "db.example.com",
49+
"USER": "user",
50+
"DATABASE": "db",
51+
"TLS": "true",
52+
"CERT": "/var/empty/db.crt",
53+
"CA": "/var/empty/ca.crt",
54+
}},
55+
expected: Config{
56+
Type: "mysql",
57+
Host: "db.example.com",
58+
Database: "db",
59+
User: "user",
60+
TlsOptions: config.TLS{
61+
Enable: true,
62+
Cert: "/var/empty/db.crt",
63+
Ca: "/var/empty/ca.crt",
64+
},
65+
Options: defaultOptions,
66+
},
67+
},
68+
{
69+
name: "options",
70+
opts: config.EnvOptions{Environment: map[string]string{
71+
"HOST": "db.example.com",
72+
"USER": "user",
73+
"DATABASE": "db",
74+
"OPTIONS_MAX_CONNECTIONS": "1",
75+
"OPTIONS_MAX_ROWS_PER_TRANSACTION": "65535",
76+
}},
77+
expected: Config{
78+
Type: "mysql",
79+
Host: "db.example.com",
80+
Database: "db",
81+
User: "user",
82+
Options: Options{
83+
MaxConnections: 1,
84+
MaxConnectionsPerTable: 8,
85+
MaxPlaceholdersPerStatement: 8192,
86+
MaxRowsPerTransaction: 65535,
87+
WsrepSyncWait: 7,
88+
},
89+
},
90+
},
91+
}
92+
93+
for _, test := range subtests {
94+
t.Run(test.name, func(t *testing.T) {
95+
var out Config
96+
if err := config.FromEnv(&out, test.opts); test.error {
97+
require.Error(t, err)
98+
} else {
99+
require.NoError(t, err)
100+
require.Equal(t, test.expected, out)
101+
}
102+
})
103+
}
104+
}

logging/config_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package logging
2+
3+
import (
4+
"github.com/icinga/icinga-go-library/config"
5+
"github.com/stretchr/testify/require"
6+
"go.uber.org/zap/zapcore"
7+
"testing"
8+
"time"
9+
)
10+
11+
func TestConfig(t *testing.T) {
12+
subtests := []struct {
13+
name string
14+
opts config.EnvOptions
15+
expected Config
16+
error bool
17+
}{
18+
{
19+
name: "empty",
20+
opts: config.EnvOptions{},
21+
expected: Config{
22+
Output: "console",
23+
Interval: 20 * time.Second,
24+
},
25+
},
26+
{
27+
name: "invalid-output",
28+
opts: config.EnvOptions{Environment: map[string]string{"OUTPUT": "☃"}},
29+
error: true,
30+
},
31+
{
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{
39+
Level: zapcore.DebugLevel,
40+
Output: JOURNAL,
41+
Interval: 3*time.Minute + 14*time.Second,
42+
},
43+
},
44+
{
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,
50+
Options: map[string]zapcore.Level{
51+
"foo": zapcore.DebugLevel,
52+
"bar": zapcore.InfoLevel,
53+
"buz": zapcore.PanicLevel,
54+
},
55+
},
56+
},
57+
{
58+
name: "options-as-ints",
59+
opts: config.EnvOptions{Environment: map[string]string{"OPTIONS": "foo:-1,bar:0,buz:4"}},
60+
expected: Config{
61+
Output: "console",
62+
Interval: 20 * time.Second,
63+
Options: map[string]zapcore.Level{
64+
"foo": zapcore.DebugLevel,
65+
"bar": zapcore.InfoLevel,
66+
"buz": zapcore.PanicLevel,
67+
},
68+
},
69+
},
70+
}
71+
72+
for _, test := range subtests {
73+
t.Run(test.name, func(t *testing.T) {
74+
var out Config
75+
if err := config.FromEnv(&out, test.opts); test.error {
76+
require.Error(t, err)
77+
} else {
78+
require.NoError(t, err)
79+
require.Equal(t, test.expected, out)
80+
}
81+
})
82+
}
83+
}

redis/config_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package redis
2+
3+
import (
4+
"github.com/creasty/defaults"
5+
"github.com/icinga/icinga-go-library/config"
6+
"github.com/stretchr/testify/require"
7+
"testing"
8+
"time"
9+
)
10+
11+
func TestConfig(t *testing.T) {
12+
var defaultOptions Options
13+
require.NoError(t, defaults.Set(&defaultOptions), "setting default options")
14+
15+
subtests := []struct {
16+
name string
17+
opts config.EnvOptions
18+
expected Config
19+
error bool
20+
}{
21+
{
22+
name: "empty-missing-host",
23+
opts: config.EnvOptions{},
24+
error: true,
25+
},
26+
{
27+
name: "minimal-config",
28+
opts: config.EnvOptions{Environment: map[string]string{"HOST": "kv.example.com"}},
29+
expected: Config{
30+
Host: "kv.example.com",
31+
Options: defaultOptions,
32+
},
33+
},
34+
{
35+
name: "customized-config",
36+
opts: config.EnvOptions{Environment: map[string]string{
37+
"HOST": "kv.example.com",
38+
"USERNAME": "user",
39+
"PASSWORD": "insecure",
40+
"DATABASE": "23",
41+
}},
42+
expected: Config{
43+
Host: "kv.example.com",
44+
Username: "user",
45+
Password: "insecure",
46+
Database: 23,
47+
Options: defaultOptions,
48+
},
49+
},
50+
{
51+
name: "tls",
52+
opts: config.EnvOptions{Environment: map[string]string{
53+
"HOST": "kv.example.com",
54+
"TLS": "true",
55+
"CERT": "/var/empty/db.crt",
56+
"CA": "/var/empty/ca.crt",
57+
}},
58+
expected: Config{
59+
Host: "kv.example.com",
60+
TlsOptions: config.TLS{
61+
Enable: true,
62+
Cert: "/var/empty/db.crt",
63+
Ca: "/var/empty/ca.crt",
64+
},
65+
Options: defaultOptions,
66+
},
67+
},
68+
{
69+
name: "options",
70+
opts: config.EnvOptions{Environment: map[string]string{
71+
"HOST": "kv.example.com",
72+
"OPTIONS_BLOCK_TIMEOUT": "1m",
73+
"OPTIONS_MAX_HMGET_CONNECTIONS": "1000",
74+
}},
75+
expected: Config{
76+
Host: "kv.example.com",
77+
Options: Options{
78+
BlockTimeout: time.Minute,
79+
HMGetCount: 4096,
80+
HScanCount: 4096,
81+
MaxHMGetConnections: 1000,
82+
Timeout: 30 * time.Second,
83+
XReadCount: 4096,
84+
},
85+
},
86+
},
87+
}
88+
89+
for _, test := range subtests {
90+
t.Run(test.name, func(t *testing.T) {
91+
var out Config
92+
if err := config.FromEnv(&out, test.opts); test.error {
93+
require.Error(t, err)
94+
} else {
95+
require.NoError(t, err)
96+
require.Equal(t, test.expected, out)
97+
}
98+
})
99+
}
100+
}

0 commit comments

Comments
 (0)