/
config_test.go
276 lines (249 loc) · 5.72 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package config
import (
"os"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/jessevdk/go-flags"
log "github.com/sirupsen/logrus"
)
func TestSetLogging_debug(t *testing.T) {
c := Config{}
c.Log.Level = "debug"
c.Log.Format = "plain"
err := c.SetupLogging()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if log.GetLevel() != log.DebugLevel {
t.Fatalf("Expected debug level, got %v", log.GetLevel())
}
}
func TestLoadConfig(t *testing.T) {
var tmpConfig configFlags
parser := flags.NewParser(&tmpConfig, flags.Default)
if _, err := parser.ParseArgs([]string{"--db-host=test", "--port=1234"}); err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
}
log.Fatalf("Failed to parse flags: %s", err)
}
compareConfig := configFlags{
Log: LogConfig{
Level: "info",
Format: "plain",
},
ConfigFilePath: "",
DB: DBConfig{
Host: "test",
Port: 5432,
User: "gorm",
Password: "",
Name: "gorm",
SSLMode: "require",
NoSync: false,
SyncInterval: 1,
},
AWS: AWSConfig{
AccessKey: "",
SecretAccessKey: "",
DynamoDBTable: "",
},
S3: S3BucketConfig{
Bucket: "",
KeyPrefix: "",
FileExtension: []string{".tfstate"},
ForcePathStyle: false,
},
TFE: TFEConfig{
Address: "",
Token: "",
Organization: "",
},
GCP: GCPConfig{
GCSBuckets: nil,
GCPSAKey: "",
},
Gitlab: GitlabConfig{
Address: "https://gitlab.com",
Token: "",
},
Web: WebConfig{
Port: 1234,
SwaggerPort: 8081,
BaseURL: "/",
LogoutURL: "",
},
}
if !reflect.DeepEqual(tmpConfig, compareConfig) {
t.Errorf(
"TestLoadConfig() -> \n\ngot:\n%v,\n\nwant:\n%v",
spew.Sdump(tmpConfig),
spew.Sdump(compareConfig),
)
}
}
func TestLoadConfigFromYaml(t *testing.T) {
var config Config
os.Setenv("AWS_DEFAULT_REGION", "test-region")
defer os.Unsetenv("AWS_DEFAULT_REGION")
config.LoadConfigFromYaml("config_test.yml")
compareConfig := Config{
Log: LogConfig{
Level: "error",
Format: "json",
},
ConfigFilePath: "config_test.yml",
DB: DBConfig{
Host: "postgres",
Port: 15432,
User: "terraboard-user",
Password: "terraboard-pass",
Name: "terraboard-db",
SSLMode: "require",
NoSync: true,
SyncInterval: 1,
},
AWS: []AWSConfig{
{
AccessKey: "root",
SecretAccessKey: "mypassword",
DynamoDBTable: "terraboard-dynamodb",
Region: "test-region",
S3: []S3BucketConfig{{
Bucket: "terraboard-bucket",
KeyPrefix: "test/",
FileExtension: []string{".tfstate"},
ForcePathStyle: true,
}},
},
},
TFE: []TFEConfig{
{
Address: "https://tfe.example.com",
Token: "foo",
Organization: "bar",
},
},
GCP: []GCPConfig{
{
GCSBuckets: []string{"my-bucket-1", "my-bucket-2"},
GCPSAKey: "/path/to/key",
},
},
Gitlab: []GitlabConfig{
{
Address: "https://gitlab.example.com",
Token: "foo",
},
},
Web: WebConfig{
Port: 39090,
SwaggerPort: 8081,
BaseURL: "/test/",
LogoutURL: "/test-logout",
},
}
if !reflect.DeepEqual(config, compareConfig) {
t.Errorf(
"TestLoadConfig() -> \n\ngot:\n%v,\n\nwant:\n%v",
spew.Sdump(config),
spew.Sdump(compareConfig),
)
}
}
func TestSetLogging_info(t *testing.T) {
c := Config{}
c.Log.Level = "info"
c.Log.Format = "plain"
err := c.SetupLogging()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if log.GetLevel() != log.InfoLevel {
t.Fatalf("Expected info level, got %v", log.GetLevel())
}
}
func TestSetLogging_warn(t *testing.T) {
c := Config{}
c.Log.Level = "warn"
c.Log.Format = "plain"
err := c.SetupLogging()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if log.GetLevel() != log.WarnLevel {
t.Fatalf("Expected warn level, got %v", log.GetLevel())
}
}
func TestSetLogging_error(t *testing.T) {
c := Config{}
c.Log.Level = "error"
c.Log.Format = "plain"
err := c.SetupLogging()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if log.GetLevel() != log.ErrorLevel {
t.Fatalf("Expected error level, got %v", log.GetLevel())
}
}
func TestSetLogging_fatal(t *testing.T) {
c := Config{}
c.Log.Level = "fatal"
c.Log.Format = "plain"
err := c.SetupLogging()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if log.GetLevel() != log.FatalLevel {
t.Fatalf("Expected fatal level, got %v", log.GetLevel())
}
}
func TestSetLogging_panic(t *testing.T) {
c := Config{}
c.Log.Level = "panic"
c.Log.Format = "plain"
err := c.SetupLogging()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if log.GetLevel() != log.PanicLevel {
t.Fatalf("Expected panic level, got %v", log.GetLevel())
}
}
func TestSetLogging_wronglevel(t *testing.T) {
c := Config{}
c.Log.Level = "wrong"
c.Log.Format = "plain"
err := c.SetupLogging()
if err == nil {
t.Fatalf("Expected an error, got nil")
}
expectedError := "Wrong log level 'wrong'"
if err.Error() != expectedError {
t.Fatalf("Expected %s, got %s", expectedError, err.Error())
}
}
func TestSetLogging_json(t *testing.T) {
c := Config{}
c.Log.Level = "debug"
c.Log.Format = "json"
err := c.SetupLogging()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
}
func TestSetLogging_wrongformat(t *testing.T) {
c := Config{}
c.Log.Level = "debug"
c.Log.Format = "yaml"
err := c.SetupLogging()
if err == nil {
t.Fatalf("Expected an error, got nil")
}
expectedError := "Wrong log format 'yaml'"
if err.Error() != expectedError {
t.Fatalf("Expected %s, got %s", expectedError, err.Error())
}
}