-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconfig_test.go
133 lines (123 loc) · 2.9 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
package main
import (
"testing"
)
const ConfigExample = `
marathon:
host: www.example.com
image:
repository: index.docker.io
tagTemplate: "{{ .GitRevCount }}-{{ .GitRevShort }}"
environments:
prod:
marathon:
file: prod.yaml
images:
hello:
name: library/hello-world
staging:
marathon:
file: staging.yaml
images:
hello:
name: library/hello-world
`
func TestConfigLoad(t *testing.T) {
tests := []struct {
Data string
Flags flags
ExpectMarathonHost string
ExpectMarathonHeaders map[string]string
ExpectError string
}{
// Basic case
{
Data: ConfigExample,
Flags: flags{
env: "prod",
},
ExpectMarathonHost: "www.example.com",
ExpectMarathonHeaders: map[string]string{},
},
// Override marathon host
{
Data: ConfigExample,
Flags: flags{
env: "prod",
marathonHost: "test.example.com",
},
ExpectMarathonHost: "test.example.com",
ExpectMarathonHeaders: map[string]string{},
},
// Override marathon curl opts with single header
{
Data: ConfigExample,
Flags: flags{
env: "prod",
marathonCurlOpts: "-H \"OauthEmail: [email protected]\"",
},
ExpectMarathonHost: "www.example.com",
ExpectMarathonHeaders: map[string]string{
"OauthEmail": "[email protected]",
},
},
// Override marathon curl opts with multiple headers
{
Data: ConfigExample,
Flags: flags{
env: "prod",
marathonCurlOpts: "-H \"OauthEmail: [email protected]\" -H \"OauthExpires: 1501617700\"",
},
ExpectMarathonHost: "www.example.com",
ExpectMarathonHeaders: map[string]string{
"OauthEmail": "[email protected]",
"OauthExpires": "1501617700",
},
},
}
for i, test := range tests {
// Test loading
config, err := configLoad(
[]byte(test.Data),
test.Flags,
)
if test.ExpectError != "" && err == nil {
t.Errorf(
"(%d) Expected error loading config YAML but did not get: %s",
i,
test.ExpectError,
)
} else if test.ExpectError == "" && err != nil {
t.Errorf("(%d) Unexpected error loading config YAML: %s", i, err)
} else if err != nil && test.ExpectError != err.Error() {
t.Errorf(
"(%d) Unexpected error loading config YAML.\nExpected: %s\nGot: %s\n",
i,
test.ExpectError,
err,
)
}
// Test marathon host override
if config.Marathon.Host != test.ExpectMarathonHost {
t.Errorf(
"(%d) Error parsing config YAML. Expect marathon.host '%s', got '%s'",
i,
test.ExpectMarathonHost,
config.Marathon.Host,
)
}
// Test marathon headers
for hKey, hVal := range test.ExpectMarathonHeaders {
got := config.Marathon.Headers.Get(hKey)
if got != hVal {
t.Errorf(
"(%d) Error parsing Marathon headers. Expect '%s' = '%s', got '%s'",
i,
hKey,
hVal,
got,
)
}
}
}
}