-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
166 lines (136 loc) · 3.75 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestReadConfig(t *testing.T) {
t.Run("valid config", func(t *testing.T) {
file, err := os.CreateTemp(os.TempDir(), "connectbox-exporter.yml")
require.NoError(t, err)
defer os.Remove(file.Name())
_, err = file.WriteString(
"listen_addr: 0.0.0.0:9119\n" +
"timeout: 10s\n" +
"targets:\n" +
" - addr: 192.168.178.1\n" +
" username: NULL\n" +
" password: password",
)
require.NoError(t, err)
err = file.Close()
require.NoError(t, err)
conf, err := ReadConfig(file.Name())
require.NoError(t, err)
want := Config{
ListenAddr: "0.0.0.0:9119",
Timeout: 10 * time.Second,
Targets: []Target{{
Addr: "192.168.178.1",
Username: "NULL",
Password: "password",
}},
}
require.Equal(t, want, conf)
})
t.Run("empty listen address", func(t *testing.T) {
file, err := os.CreateTemp(os.TempDir(), "connectbox-exporter.yml")
require.NoError(t, err)
defer os.Remove(file.Name())
_, err = file.WriteString(
"targets:\n" +
" - addr: 192.168.178.1\n" +
" username: NULL\n" +
" password: password",
)
require.NoError(t, err)
err = file.Close()
require.NoError(t, err)
conf, err := ReadConfig(file.Name())
require.NoError(t, err)
want := Config{
ListenAddr: "0.0.0.0:9119",
Timeout: 30 * time.Second,
Targets: []Target{{
Addr: "192.168.178.1",
Username: "NULL",
Password: "password",
}},
}
require.Equal(t, want, conf)
})
t.Run("empty target address", func(t *testing.T) {
file, err := os.CreateTemp(os.TempDir(), "connectbox-exporter.yml")
require.NoError(t, err)
defer os.Remove(file.Name())
_, err = file.WriteString(
"listen_addr: 0.0.0.0:9119\n" +
"targets:\n" +
" - username: NULL\n" +
" password: password",
)
require.NoError(t, err)
err = file.Close()
require.NoError(t, err)
_, err = ReadConfig(file.Name())
require.ErrorContains(t, err, "found target with empty address")
})
t.Run("empty target username", func(t *testing.T) {
file, err := os.CreateTemp(os.TempDir(), "connectbox-exporter.yml")
require.NoError(t, err)
defer os.Remove(file.Name())
_, err = file.WriteString(
"listen_addr: 0.0.0.0:9119\n" +
"targets:\n" +
" - addr: 192.168.178.1\n" +
" password: password",
)
require.NoError(t, err)
err = file.Close()
require.NoError(t, err)
conf, err := ReadConfig(file.Name())
require.NoError(t, err)
want := Config{
ListenAddr: "0.0.0.0:9119",
Timeout: 30 * time.Second,
Targets: []Target{{
Addr: "192.168.178.1",
Username: "NULL",
Password: "password",
}},
}
require.Equal(t, want, conf)
})
t.Run("empty target password", func(t *testing.T) {
file, err := os.CreateTemp(os.TempDir(), "connectbox-exporter.yml")
require.NoError(t, err)
defer os.Remove(file.Name())
_, err = file.WriteString(
"listen_addr: 0.0.0.0:9119\n" +
"targets:\n" +
" - addr: 192.168.178.1\n" +
" username: NULL",
)
require.NoError(t, err)
err = file.Close()
require.NoError(t, err)
_, err = ReadConfig(file.Name())
require.ErrorContains(t, err, "found target with empty password")
})
t.Run("invalid yaml", func(t *testing.T) {
file, err := os.CreateTemp(os.TempDir(), "connectbox-exporter.yml")
require.NoError(t, err)
defer os.Remove(file.Name())
_, err = file.WriteString("hello: world: !")
require.NoError(t, err)
err = file.Close()
require.NoError(t, err)
_, err = ReadConfig(file.Name())
require.ErrorContains(t, err, "unmarshal file")
})
t.Run("non-existing config file", func(t *testing.T) {
_, err := ReadConfig("not-exists.yml")
require.ErrorContains(t, err, "no such file or directory")
})
}