-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_test.go
More file actions
233 lines (194 loc) · 6.92 KB
/
config_test.go
File metadata and controls
233 lines (194 loc) · 6.92 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"flag"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/picfight/pfcd/dcrutil"
)
var tempConfigFile *os.File
var tempAppDataDir string
func TestMain(m *testing.M) {
// Temp config file is used to ensure there are no external influences
// from previously set env variables or default config files.
tempConfigFile, _ = ioutil.TempFile("", "pfcdata_test_file.cfg")
defer os.Remove(tempConfigFile.Name())
os.Setenv("PFCDATA_CONFIG_FILE", tempConfigFile.Name())
// Make an empty folder for appdata tests.
tempAppDataDir, _ = ioutil.TempDir("", "pfcdata_test_appdata")
defer os.RemoveAll(tempAppDataDir)
// Parse the -test.* flags before removing them from the command line
// arguments list, which we do to allow go-flags to succeed.
flag.Parse()
os.Args = os.Args[:1]
// Run the tests now that the testing package flags have been parsed.
m.Run()
os.Unsetenv("PFCDATA_CONFIG_FILE")
}
// disableConfigFileEnv checks if the PFCDATA_CONFIG_FILE environment variable
// is set, unsets it, and returns a function that will return
// PFCDATA_CONFIG_FILE to its state before calling disableConfigFileEnv.
func disableConfigFileEnv() func() {
loc, wasSet := os.LookupEnv("PFCDATA_CONFIG_FILE")
if wasSet {
os.Unsetenv("PFCDATA_CONFIG_FILE")
return func() { os.Setenv("PFCDATA_CONFIG_FILE", loc) }
}
return func() {}
}
func TestLoadCustomConfigPresent(t *testing.T) {
// Load using the empty config file set via environment variable in
// TestMain. Since the file exists, it should not cause an error.
_, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load pfcdata config: %v", err)
}
}
func TestLoadDefaultConfigMissing(t *testing.T) {
// Unset the custom config file.
restoreConfigFileLoc := disableConfigFileEnv()
defer restoreConfigFileLoc()
// Use the empty appdata dir.
os.Setenv("PFCDATA_APPDATA_DIR", tempAppDataDir)
defer os.Unsetenv("PFCDATA_APPDATA_DIR")
// Load using the the empty appdata directory (with no config file). Since
// this is the default config file, it should not cause an error.
_, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load pfcdata config: %v", err)
}
}
func TestLoadCustomConfigMissing(t *testing.T) {
// Unset the custom config file.
restoreConfigFileLoc := disableConfigFileEnv()
defer restoreConfigFileLoc()
// Set a path to a non-existent config file. Use TempFile followed by Remove
// to guarantee the file does not exist.
goneFile, _ := ioutil.TempFile("", "blah")
os.Remove(goneFile.Name())
os.Setenv("PFCDATA_CONFIG_FILE", goneFile.Name())
// Attempt to load using the non-existent non-default config file, which
// should return an error.
_, err := loadConfig()
if err == nil {
t.Errorf("Loaded pfcdata config, but the explicitly set config file"+
"%s does not exist.", goneFile.Name())
}
}
// TestLoadDefaultConfigPathCustomAppdata ensures that setting appdata while the
// config file is not explicitly set will change the default config file
// location, and that there is no error if this new default config file does not
// exist as missing config files are only an error when explicitly set.
func TestLoadDefaultConfigPathCustomAppdata(t *testing.T) {
// Unset the custom config file.
restoreConfigFileLoc := disableConfigFileEnv()
defer restoreConfigFileLoc()
// Use the empty appdata dir.
os.Setenv("PFCDATA_APPDATA_DIR", tempAppDataDir)
defer os.Unsetenv("PFCDATA_APPDATA_DIR")
// Load using the the empty appdata directory (with no config file). Since
// this is the default config file, it should not cause an error.
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load pfcdata config: %v", err)
}
// Verify that the default config file is located in the specified appdata
// directory rather than the default appdata directory.
expected := filepath.Join(tempAppDataDir, defaultConfigFilename)
if cfg.ConfigFile != expected {
t.Errorf("Default config file expected at %s, got %s", expected, cfg.ConfigFile)
}
}
func TestDefaultConfigAPIListen(t *testing.T) {
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load pfcdata config: %v", err)
}
if cfg.APIListen != defaultAPIListen {
t.Errorf("Expected API listen URL %s, got %s", defaultAPIListen, cfg.APIListen)
}
}
func TestDefaultConfigAPIListenWithEnv(t *testing.T) {
customListenPath := "0.0.0.0:7777"
os.Setenv("PFCDATA_LISTEN_URL", customListenPath)
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load pfcdata config: %v", err)
}
if cfg.APIListen != customListenPath {
t.Errorf("Expected API listen URL %s, got %s", customListenPath, cfg.APIListen)
}
}
func TestDefaultConfigAppDataDir(t *testing.T) {
expected := dcrutil.AppDataDir("pfcdata", false)
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load pfcdata config: %v", err)
}
if cfg.HomeDir != expected {
t.Errorf("Expected appdata directory %s, got %s", expected, cfg.HomeDir)
}
}
func TestCustomHomeDirWithEnv(t *testing.T) {
// Do not override config file as appdata changes its location.
restoreConfigFileLoc := disableConfigFileEnv()
defer restoreConfigFileLoc()
// Use the empty appdata dir made for the tests.
os.Setenv("PFCDATA_APPDATA_DIR", tempAppDataDir)
defer os.Unsetenv("PFCDATA_APPDATA_DIR")
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load pfcdata config: %v", err)
}
if cfg.HomeDir != tempAppDataDir {
t.Errorf("Expected appdata directory %s, got %s", tempAppDataDir, cfg.HomeDir)
}
}
// Ensure that command line flags override env variables.
func TestDefaultConfigHomeDirWithEnvAndFlag(t *testing.T) {
tmp2 := "pfcdata_test_appdata2"
cliOverride, err := ioutil.TempDir("", tmp2)
if err != nil {
t.Fatalf("Unable to create temporary folder %s: %v", tmp2, err)
}
defer os.RemoveAll(cliOverride)
os.Args = append(os.Args, "--appdata="+cliOverride)
os.Setenv("PFCDATA_APPDATA_DIR", cliOverride)
defer os.Unsetenv("PFCDATA_APPDATA_DIR")
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load pfcdata config: %v", err)
}
if cfg.HomeDir != cliOverride {
t.Errorf("Expected appdata directory %s, got %s", cliOverride, cfg.HomeDir)
}
}
func TestDefaultConfigNetwork(t *testing.T) {
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load pfcdata config: %v", err)
}
if cfg.TestNet || cfg.SimNet {
t.Errorf("Default config should be for mainnet but was not.")
}
}
func TestDefaultConfigTestNetWithEnv(t *testing.T) {
os.Setenv("PFCDATA_USE_TESTNET", "true")
defer os.Unsetenv("PFCDATA_USE_TESTNET")
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load pfcdata config: %v", err)
}
if !cfg.TestNet {
t.Errorf("Testnet was specified via environment variable, but not using testnet.")
}
}
func TestDefaultConfigTestNetWithEnvAndBadValue(t *testing.T) {
os.Setenv("PFCDATA_USE_TESTNET", "no")
defer os.Unsetenv("PFCDATA_USE_TESTNET")
_, err := loadConfig()
if err == nil {
t.Errorf("Invalid boolean value for PFCDATA_USE_TESTNET did not cause an error.")
}
}