forked from camptocamp/terraboard
/
config_test.go
196 lines (172 loc) · 3.96 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
package config
import (
"reflect"
"testing"
log "github.com/sirupsen/logrus"
)
func TestLoadConfigFromYaml(t *testing.T) {
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",
NoSync: true,
},
AWS: []AWSConfig{
{
AccessKey: "root",
SecretAccessKey: "mypassword",
DynamoDBTable: "terraboard-dynamodb",
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,
BaseURL: "/test/",
LogoutURL: "/test-logout",
},
}
c := Config{ConfigFilePath: "config_test.yml"}
c.LoadConfigFromYaml()
if !reflect.DeepEqual(c, compareConfig) {
t.Fatalf("Expected: %v\nGot: %v", compareConfig, c)
}
}
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 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())
}
}