-
Notifications
You must be signed in to change notification settings - Fork 293
/
config_test.go
93 lines (83 loc) · 2.95 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
// Copyright (c) 2018-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"flag"
"os"
"path/filepath"
"strings"
"testing"
)
func TestMain(m *testing.M) {
flag.Parse()
// Trim -test.* flags from the command line arguments list to allow
// go-flags tests to succeed.
os.Args = append([]string{os.Args[0]}, flag.Args()...)
os.Exit(m.Run())
}
// In order to test command line arguments and environment variables, append
// the flags to the os.Args variable like so:
// os.Args = append(os.Args, "--altdnsnames=\"hostname1,hostname2\"")
//
// For environment variables, use the following to set the variable before the
// func that loads the configuration is called:
// os.Setenv("DCRD_ALT_DNSNAMES", "hostname1,hostname2")
//
// These args and env variables will then get parsed during configuration load.
// TestLoadConfig ensures that basic configuration loading succeeds.
func TestLoadConfig(t *testing.T) {
appName := filepath.Base(os.Args[0])
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
_, _, err := loadConfig(appName)
if err != nil {
t.Fatalf("Failed to load dcrd config: %s", err)
}
}
// TestDefaultAltDNSNames ensures that there are no additional hostnames added
// by default during the configuration load phase.
func TestDefaultAltDNSNames(t *testing.T) {
appName := filepath.Base(os.Args[0])
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
cfg, _, err := loadConfig(appName)
if err != nil {
t.Fatalf("Failed to load dcrd config: %s", err)
}
if len(cfg.AltDNSNames) != 0 {
t.Fatalf("Invalid default value for altdnsnames: %s", cfg.AltDNSNames)
}
}
// TestAltDNSNamesWithEnv ensures the DCRD_ALT_DNSNAMES environment variable is
// parsed into a slice of additional hostnames as intended.
func TestAltDNSNamesWithEnv(t *testing.T) {
appName := filepath.Base(os.Args[0])
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
t.Setenv("DCRD_ALT_DNSNAMES", "hostname1,hostname2")
cfg, _, err := loadConfig(appName)
if err != nil {
t.Fatalf("Failed to load dcrd config: %s", err)
}
hostnames := strings.Join(cfg.AltDNSNames, ",")
if hostnames != "hostname1,hostname2" {
t.Fatalf("altDNSNames should be %s but was %s", "hostname1,hostname2",
hostnames)
}
}
// TestAltDNSNamesWithArg ensures the altdnsnames configuration option parses
// additional hostnames into a slice of hostnames as intended.
func TestAltDNSNamesWithArg(t *testing.T) {
appName := filepath.Base(os.Args[0])
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
old := os.Args
os.Args = append(os.Args, "--altdnsnames=\"hostname1,hostname2\"")
cfg, _, err := loadConfig(appName)
if err != nil {
t.Fatalf("Failed to load dcrd config: %s", err)
}
hostnames := strings.Join(cfg.AltDNSNames, ",")
if hostnames != "hostname1,hostname2" {
t.Fatalf("altDNSNames should be %s but was %s", "hostname1,hostname2",
hostnames)
}
os.Args = old
}