Debian Codegraph @

internal/config/config_test.go

raw symbols SCIP
1 package config
2
3 import (
4 "errors"
5 "testing"
6 "time"
7
8 "github.com/google/go-cmp/cmp"
9 "github.com/xperimental/nextcloud-exporter/internal/testutil"
10 )
11
12 func testEnv(env map[string]string) func(string) string {
13 return func(key string) string {
14 return env[key]
15 }
16 }
17
18 func TestConfig(t *testing.T) {
19 defaults := defaultConfig()
20 tt := []struct {
21 desc string
22 args []string
23 env map[string]string
24 wantErr error
25 wantConfig Config
26 }{
27 {
28 desc: "flags",
29 args: []string{
30 "test",
31 "--addr",
32 "127.0.0.1:9205",
33 "--timeout",
34 "30s",
35 "--server",
36 "http://localhost",
37 "--username",
38 "testuser",
39 "--password",
40 "testpass",
41 },
42 env: map[string]string{},
43 wantErr: nil,
44 wantConfig: Config{
45 ListenAddr: "127.0.0.1:9205",
46 Timeout: 30 * time.Second,
47 ServerURL: "http://localhost",
48 Username: "testuser",
49 Password: "testpass",
50 TLSSkipVerify: false,
51 },
52 },
53 {
54 desc: "password from file",
55 args: []string{
56 "test",
57 "--server",
58 "http://localhost",
59 "--username",
60 "testuser",
61 "--password",
62 "@testdata/password",
63 },
64 env: map[string]string{},
65 wantErr: nil,
66 wantConfig: Config{
67 ListenAddr: defaults.ListenAddr,
68 Timeout: defaults.Timeout,
69 ServerURL: "http://localhost",
70 Username: "testuser",
71 Password: "testpass",
72 TLSSkipVerify: false,
73 },
74 },
75 {
76 desc: "config from file",
77 args: []string{
78 "test",
79 "--config-file",
80 "testdata/all.yml",
81 },
82 env: map[string]string{},
83 wantErr: nil,
84 wantConfig: Config{
85 ListenAddr: "127.0.0.10:9205",
86 Timeout: 10 * time.Second,
87 ServerURL: "http://localhost",
88 Username: "testuser",
89 Password: "testpass",
90 TLSSkipVerify: false,
91 },
92 },
93 {
94 desc: "config and password from file",
95 args: []string{
96 "test",
97 "--config-file",
98 "testdata/passwordfile.yml",
99 },
100 env: map[string]string{},
101 wantErr: nil,
102 wantConfig: Config{
103 ListenAddr: "127.0.0.10:9205",
104 Timeout: 10 * time.Second,
105 ServerURL: "http://localhost",
106 Username: "testuser",
107 Password: "testpass",
108 TLSSkipVerify: false,
109 },
110 },
111 {
112 desc: "don't check tls certificates",
113 args: []string{
114 "test",
115 "--tls-skip-verify",
116 "true",
117 },
118 env: map[string]string{},
119 wantErr: nil,
120 wantConfig: Config{
121 ListenAddr: ":9205",
122 Timeout: 5 * time.Second,
123 ServerURL: "",
124 Username: "",
125 Password: "",
126 TLSSkipVerify: true,
127 },
128 },
129 {
130 desc: "env config",
131 args: []string{
132 "test",
133 },
134 env: map[string]string{
135 envListenAddress: "127.0.0.11:9205",
136 envTimeout: "15s",
137 envServerURL: "http://localhost",
138 envUsername: "testuser",
139 envPassword: "testpass",
140 envTLSSkipVerify: "true",
141 },
142 wantErr: nil,
143 wantConfig: Config{
144 ListenAddr: "127.0.0.11:9205",
145 Timeout: 15 * time.Second,
146 ServerURL: "http://localhost",
147 Username: "testuser",
148 Password: "testpass",
149 TLSSkipVerify: true,
150 },
151 },
152 {
153 desc: "minimal env",
154 args: []string{
155 "test",
156 },
157 env: map[string]string{
158 envServerURL: "http://localhost",
159 envUsername: "testuser",
160 envPassword: "testpass",
161 },
162 wantErr: nil,
163 wantConfig: Config{
164 ListenAddr: defaults.ListenAddr,
165 Timeout: defaults.Timeout,
166 ServerURL: "http://localhost",
167 Username: "testuser",
168 Password: "testpass",
169 TLSSkipVerify: false,
170 },
171 },
172 {
173 desc: "auth token env, skip apps",
174 args: []string{
175 "test",
176 },
177 env: map[string]string{
178 envServerURL: "http://localhost",
179 envAuthToken: "auth-token",
180 envInfoApps: "true",
181 },
182 wantErr: nil,
183 wantConfig: Config{
184 ListenAddr: defaults.ListenAddr,
185 Timeout: defaults.Timeout,
186 ServerURL: "http://localhost",
187 AuthToken: "auth-token",
188 Info: InfoConfig{
189 Apps: true,
190 },
191 },
192 },
193 {
194 desc: "token file",
195 args: []string{
196 "test",
197 "--config-file",
198 "testdata/authtoken.yml",
199 },
200 env: map[string]string{},
201 wantErr: nil,
202 wantConfig: Config{
203 ListenAddr: defaults.ListenAddr,
204 Timeout: defaults.Timeout,
205 ServerURL: "http://localhost",
206 Username: "",
207 Password: "",
208 AuthToken: "auth-token",
209 TLSSkipVerify: false,
210 },
211 },
212 {
213 desc: "show help",
214 args: []string{
215 "test",
216 "--help",
217 },
218 env: map[string]string{},
219 wantErr: nil,
220 wantConfig: Config{
221 RunMode: RunModeHelp,
222 },
223 },
224 {
225 desc: "show version",
226 args: []string{
227 "test",
228 "--version",
229 },
230 env: map[string]string{},
231 wantErr: nil,
232 wantConfig: Config{
233 RunMode: RunModeVersion,
234 },
235 },
236 {
237 desc: "login mode",
238 args: []string{
239 "test",
240 "--login",
241 "--server",
242 "http://localhost",
243 },
244 env: map[string]string{},
245 wantErr: nil,
246 wantConfig: Config{
247 ListenAddr: defaults.ListenAddr,
248 Timeout: defaults.Timeout,
249 ServerURL: "http://localhost",
250 RunMode: RunModeLogin,
251 },
252 },
253 {
254 desc: "wrongflag",
255 args: []string{
256 "test",
257 "--unknown",
258 },
259 env: map[string]string{},
260 wantErr: errors.New("error parsing flags: unknown flag: --unknown"),
261 },
262 {
263 desc: "env wrong duration",
264 args: []string{
265 "test",
266 },
267 env: map[string]string{
268 "NEXTCLOUD_TIMEOUT": "unknown",
269 },
270 wantErr: errors.New("error reading environment variables: time: invalid duration \"unknown\""),
271 },
272 {
273 desc: "password from file error",
274 args: []string{
275 "test",
276 "--server",
277 "http://localhost",
278 "--password",
279 "@testdata/notfound",
280 },
281 env: map[string]string{},
282 wantErr: errors.New("can not read password file: open testdata/notfound: no such file or directory"),
283 },
284 {
285 desc: "config from file error",
286 args: []string{
287 "test",
288 "--config-file",
289 "testdata/notfound.yml",
290 },
291 env: map[string]string{},
292 wantErr: errors.New("error reading configuration file: open testdata/notfound.yml: no such file or directory"),
293 },
294 {
295 desc: "fail parsing tlsSkipVerify env",
296 args: []string{
297 "test",
298 },
299 env: map[string]string{
300 envTLSSkipVerify: "invalid",
301 },
302 wantErr: errors.New(`error reading environment variables: can not parse value for "NEXTCLOUD_TLS_SKIP_VERIFY": invalid`),
303 },
304 {
305 desc: "fail parsing infoSkipApps env",
306 args: []string{
307 "test",
308 },
309 env: map[string]string{
310 envInfoApps: "invalid",
311 },
312 wantErr: errors.New(`error reading environment variables: can not parse value for "NEXTCLOUD_INFO_APPS": invalid`),
313 },
314 }
315
316 for _, tc := range tt {
317 tc := tc
318 t.Run(tc.desc, func(t *testing.T) {
319 t.Parallel()
320
321 config, err := parseConfig(tc.args, testEnv(tc.env))
322
323 if !testutil.EqualErrorMessage(err, tc.wantErr) {
324 t.Errorf("got error %q, want %q", err, tc.wantErr)
325 }
326
327 if err != nil {
328 return
329 }
330
331 if diff := cmp.Diff(config, tc.wantConfig); diff != "" {
332 t.Errorf("config differs: -got +want\n%s", diff)
333 }
334 })
335 }
336 }
337
338 func TestConfigValidate(t *testing.T) {
339 tt := []struct {
340 desc string
341 config Config
342 wantErr error
343 }{
344 {
345 desc: "minimal",
346 config: Config{
347 ServerURL: "https://example.com",
348 Username: "exporter",
349 Password: "testpass",
350 },
351 wantErr: nil,
352 },
353 {
354 desc: "auth token",
355 config: Config{
356 ServerURL: "https://example.com",
357 AuthToken: "auth-token",
358 },
359 wantErr: nil,
360 },
361 {
362 desc: "no url",
363 config: Config{
364 Username: "exporter",
365 Password: "testpass",
366 },
367 wantErr: errValidateNoServerURL,
368 },
369 {
370 desc: "auth help",
371 config: Config{
372 ServerURL: "https://example.com",
373 },
374 wantErr: errValidateNoAuth,
375 },
376 {
377 desc: "no username",
378 config: Config{
379 ServerURL: "https://example.com",
380 Password: "testpass",
381 },
382 wantErr: errValidateNoUsername,
383 },
384 {
385 desc: "no password",
386 config: Config{
387 ServerURL: "https://example.com",
388 Username: "exporter",
389 },
390 wantErr: errValidateNoPassword,
391 },
392 }
393
394 for _, tc := range tt {
395 tc := tc
396 t.Run(tc.desc, func(t *testing.T) {
397 t.Parallel()
398
399 err := tc.config.Validate()
400
401 if !testutil.EqualErrorMessage(err, tc.wantErr) {
402 t.Errorf("got error %q, want %q", err, tc.wantErr)
403 }
404 })
405 }
406 }