forked from scaleway/scaleway-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_config_test.go
186 lines (174 loc) · 4.93 KB
/
load_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package scw
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/scaleway/scaleway-sdk-go/internal/testhelpers"
)
// TestLoad tests all valid configuration files:
// - v2 config
// - v1 to v2 config migration
// - custom-path with v2 config
// - custom-path with v1 config
// - XDG config path with v2 config
// - Windows config path with v2 config
func TestLoad(t *testing.T) {
tests := []struct {
name string
env map[string]string
files map[string]string
expected *Config
expectedError string
expectedFiles map[string]string
}{
// valid config
{
name: "Custom-path config is empty", // custom config path
env: map[string]string{
scwConfigPathEnv: "{HOME}/valid1/test.conf",
},
files: map[string]string{
"valid1/test.conf": emptyFile,
},
expected: &Config{},
},
{
name: "Custom-path config with valid V2",
env: map[string]string{
scwConfigPathEnv: "{HOME}/valid3/test.conf",
},
files: map[string]string{
"valid3/test.conf": v2SimpleValidConfigFile,
},
expected: v2SimpleValidConfig,
},
{
name: "Default config with valid V2", // default config path
env: map[string]string{
"HOME": "{HOME}",
},
files: map[string]string{
".config/scw/config.yaml": v2SimpleValidConfigFile,
},
expected: v2SimpleValidConfig,
expectedFiles: map[string]string{
".config/scw/config.yaml": v2SimpleValidConfigFile,
},
},
{
name: "Default config with valid V2 and valid V1",
env: map[string]string{
"HOME": "{HOME}",
},
files: map[string]string{
".config/scw/config.yaml": v2SimpleValidConfigFile,
".scwrc": v1ValidConfigFile,
},
expected: v2SimpleValidConfig,
expectedFiles: map[string]string{
".config/scw/config.yaml": v2SimpleValidConfigFile,
},
},
{
name: "XDG config with valid V2",
env: map[string]string{
"HOME": "{HOME}",
xdgConfigDirEnv: "{HOME}/plop",
},
files: map[string]string{
"plop/scw/config.yaml": v2SimpleValidConfigFile,
},
expected: v2SimpleValidConfig,
},
{
name: "Windows config with valid V2",
env: map[string]string{
windowsHomeDirEnv: "{HOME}",
},
files: map[string]string{
".config/scw/config.yaml": v2SimpleValidConfigFile,
},
expected: v2SimpleValidConfig,
},
// errors
{
name: "Err: custom-path config does not exist",
env: map[string]string{
scwConfigPathEnv: "{HOME}/fake/test.conf",
},
expectedError: "scaleway-sdk-go: cannot read config file: open {HOME}/fake/test.conf: no such file or directory",
},
{
name: "Err: custom-path config with invalid V2",
env: map[string]string{
scwConfigPathEnv: "{HOME}/invalid1/test.conf",
},
files: map[string]string{
"invalid1/test.conf": v2SimpleInvalidConfigFile,
},
expectedError: "scaleway-sdk-go: content of config file {HOME}/invalid1/test.conf is invalid: yaml: found unexpected end of stream",
},
{
name: "Err: default config with invalid V2",
env: map[string]string{
"HOME": "{HOME}",
},
files: map[string]string{
".config/scw/config.yaml": v2SimpleInvalidConfigFile,
},
expectedError: "scaleway-sdk-go: content of config file {HOME}/.config/scw/config.yaml is invalid: yaml: found unexpected end of stream",
},
{
name: "Err: default config with invalid profile",
env: map[string]string{
"HOME": "{HOME}",
},
files: map[string]string{
".config/scw/config.yaml": v2SimpleConfigFileWithInvalidProfile,
},
expectedError: "scaleway-sdk-go: given profile flantier does not exist",
},
}
// create home dir
dir := initEnv(t)
// delete home dir and reset env variables
defer resetEnv(t, os.Environ(), dir)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// set up env and config file(s)
setEnv(t, test.env, test.files, dir)
test.expectedError = strings.Replace(test.expectedError, "{HOME}", dir, -1)
// remove config file(s)
defer cleanEnv(t, test.files, dir)
// load config
config, err := LoadConfig()
// test expected outputs
if test.expectedError != "" {
if err == nil {
_, tmpErr := config.GetActiveProfile()
if tmpErr != nil {
testhelpers.Equals(t, test.expectedError, tmpErr.Error())
return
}
}
testhelpers.Assert(t, err != nil, "error should not be nil")
testhelpers.Equals(t, test.expectedError, err.Error())
} else {
testhelpers.AssertNoError(t, err)
testhelpers.Equals(t, test.expected, config)
_, err = config.GetActiveProfile()
testhelpers.AssertNoError(t, err)
}
// test expected files
for path, expectedContent := range test.expectedFiles {
targetPath := filepath.Join(dir, path)
content, err := ioutil.ReadFile(targetPath)
testhelpers.AssertNoError(t, err)
testhelpers.Equals(t, expectedContent, string(content))
testhelpers.AssertNoError(t, os.RemoveAll(targetPath)) // delete at the end
}
})
}
}