-
Notifications
You must be signed in to change notification settings - Fork 10
/
context_test.go
194 lines (153 loc) · 4.55 KB
/
context_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
package main
import (
"os"
"testing"
)
func TestLoadFromEnvironment(t *testing.T) {
c := Context{}
os.Setenv("PIPE_PORT", "1234")
os.Setenv("PIPE_LOGLEVEL", "debug")
os.Setenv("PIPE_LOGCOLORS", "true")
os.Setenv("PIPE_MONGOURL", "server.example.com")
os.Setenv("PIPE_ADMINNAME", "fake")
os.Setenv("PIPE_ADMINKEY", "12345")
os.Setenv("PIPE_POLL", "5000")
os.Setenv("PIPE_DEFAULTIMAGE", "cloudpipe/runner-trial")
os.Setenv("PIPE_DOCKERHOST", "tcp://1.2.3.4:4567/")
os.Setenv("PIPE_DOCKERTLS", "true")
os.Setenv("PIPE_CACERT", "/lockbox/ca.pem")
os.Setenv("PIPE_CERT", "/lockbox/cert.pem")
os.Setenv("PIPE_KEY", "/lockbox/key.pem")
os.Setenv("PIPE_AUTHSERVICE", "https://auth")
if err := c.Load(); err != nil {
t.Errorf("Error loading configuration: %v", err)
}
if c.Port != 1234 {
t.Errorf("Unexpected port: [%d]", c.Port)
}
if c.LogLevel != "debug" {
t.Errorf("Unexpected log level: [%s]", c.LogLevel)
}
if !c.LogColors {
t.Error("Expected log coloring to be enabled")
}
if c.MongoURL != "server.example.com" {
t.Errorf("Unexpected MongoDB URL: [%s]", c.MongoURL)
}
if c.Poll != 5000 {
t.Errorf("Unexpected polling interval: [%d]", c.Poll)
}
if c.DockerHost != "tcp://1.2.3.4:4567/" {
t.Errorf("Unexpected docker host: [%s]", c.DockerHost)
}
if !c.DockerTLS {
t.Errorf("Expected docker TLS to be enabled.")
}
if c.CACert != "/lockbox/ca.pem" {
t.Errorf("Unexpected docker CA cert: [%s]", c.CACert)
}
if c.Cert != "/lockbox/cert.pem" {
t.Errorf("Unexpected docker cert: [%s]", c.Cert)
}
if c.Key != "/lockbox/key.pem" {
t.Errorf("Unexpected docker key: [%s]", c.Key)
}
if c.DefaultImage != "cloudpipe/runner-trial" {
t.Errorf("Unexpected default image: [%s]", c.DefaultImage)
}
if c.AdminName != "fake" {
t.Errorf("Unexpected administrator name: [%s]", c.AdminName)
}
if c.AdminKey != "12345" {
t.Errorf("Unexpected administrator API key: [%s]", c.AdminKey)
}
if c.Settings.AuthService != "https://auth" {
t.Errorf("Unexpected authentication service URL: [%s]", c.AuthService)
}
}
func TestDefaultValues(t *testing.T) {
c := Context{}
os.Setenv("PIPE_PORT", "")
os.Setenv("PIPE_LOGLEVEL", "")
os.Setenv("PIPE_LOGCOLORS", "")
os.Setenv("PIPE_MONGOURL", "")
os.Setenv("PIPE_ADMINNAME", "")
os.Setenv("PIPE_ADMINKEY", "")
os.Setenv("PIPE_POLL", "")
os.Setenv("PIPE_DOCKERHOST", "")
os.Setenv("DOCKER_HOST", "")
os.Setenv("PIPE_DOCKERTLS", "")
os.Setenv("PIPE_CACERT", "")
os.Setenv("PIPE_CERT", "")
os.Setenv("PIPE_KEY", "")
os.Setenv("DOCKER_TLS_VERIFY", "")
os.Setenv("DOCKER_CERT_PATH", "")
os.Setenv("PIPE_DEFAULTIMAGE", "")
os.Setenv("PIPE_AUTHSERVICE", "")
if err := c.Load(); err != nil {
t.Errorf("Error loading configuration: %v", err)
}
if c.Port != 8000 {
t.Errorf("Unexpected port: [%d]", c.Port)
}
if c.LogLevel != "info" {
t.Errorf("Unexpected logging level: [%s]", c.LogLevel)
}
if c.LogColors {
t.Error("Expected logging colors to be disabled by default")
}
if c.MongoURL != "mongo" {
t.Errorf("Unexpected MongoDB connection URL: [%s]", c.MongoURL)
}
if c.Poll != 500 {
t.Errorf("Unexpected polling interval: [%d]", c.Poll)
}
if c.DockerHost != "unix:///var/run/docker.sock" {
t.Errorf("Unexpected docker host: [%s]", c.DockerHost)
}
if c.DockerTLS {
t.Errorf("Expected docker TLS to be disabled.")
}
if c.CACert != "/certificates/ca.pem" {
t.Errorf("Unexpected docker CA cert: [%s]", c.CACert)
}
if c.Cert != "/certificates/cloudpipe-cert.pem" {
t.Errorf("Unexpected docker cert: [%s]", c.Cert)
}
if c.Key != "/certificates/cloudpipe-key.pem" {
t.Errorf("Unexpected docker key: [%s]", c.Key)
}
if c.DefaultImage != "cloudpipe/runner-py2" {
t.Errorf("Unexpected default image: [%s]", c.DefaultImage)
}
if c.Settings.AuthService != "https://authstore:9001/v1" {
t.Errorf("Unexpected default auth service: [%s]", c.AuthService)
}
}
func TestUseDockerHost(t *testing.T) {
os.Setenv("PIPE_DOCKERHOST", "")
os.Setenv("DOCKER_HOST", "tcp://1.2.3.4:4567/")
c := Context{}
if err := c.Load(); err != nil {
t.Errorf("Error loading configuration: %v", err)
}
if c.DockerHost != "tcp://1.2.3.4:4567/" {
t.Errorf("Unexpected docker host: [%s]", c.DockerHost)
}
}
func TestAddressString(t *testing.T) {
c := Context{
Settings: Settings{Port: 1234},
}
if c.ListenAddr() != ":1234" {
t.Errorf("Unexpected listen address: %s", c.ListenAddr())
}
}
func TestValidateLogLevel(t *testing.T) {
c := Context{}
os.Setenv("PIPE_LOGLEVEL", "Walrus")
err := c.Load()
if err == nil {
t.Errorf("Expected an error when loading an invalid PIPE_LOG_LEVEL.")
}
}