This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
config_test.go
209 lines (195 loc) · 5.33 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
package main
import (
"os"
"testing"
)
func Test_loadAllConfig_1(t *testing.T) {
if _, err := os.Stat("/etc/dockersh"); os.IsNotExist(err) {
t.Log("No /etc/dockersh, skipping test")
return
}
_, err := loadAllConfig("notexist", "/notexist")
if err != nil {
t.Errorf("Got error %v", err)
}
}
func Test_Dump_1(t *testing.T) {
exp := "ImageName busybox MountHomeTo %h ContainerUsername %u Shell /bin/ash DockerSocket /var/run/docker.sock"
str := defaultConfig.Dump()
if str != exp {
t.Errorf("Got '%s' expected '%s'", str, exp)
}
}
func Test_DefaultConfig_1(t *testing.T) {
if defaultConfig.ImageName == "busybox" {
t.Log("default ImageName passed.")
} else {
t.Errorf("default ImageName failed: expected busybox got %s", defaultConfig.ImageName)
}
}
func Test_SimpleConfig_1(t *testing.T) {
c, err := loadConfigFromString([]byte(``), "fred")
if err != nil {
t.Error(err)
}
c, err = loadConfigFromString([]byte(`[dockersh]
imagename = testimage`), "fred")
if err != nil {
t.Error(err)
}
if c.ImageName == "testimage" {
t.Log("set ImageName passed.")
} else {
t.Errorf("Expected ImageName testimage got %s", c.ImageName)
}
}
func Test_UserConfig_1(t *testing.T) {
c, err := loadConfigFromString([]byte(`[dockersh]
imagename = testimage
shell = someshell
[user "fred"]
imagename = fredsimage
containerusername = bill`), "fred")
if err != nil {
t.Error(err)
}
if c.Shell == "someshell" {
t.Log("set Shell in dockersh config passed.")
} else {
t.Errorf("Expected Shell dockersg got %s", c.Shell)
}
if c.ContainerUsername == "bill" {
t.Log("set ContainerUserName in user config passed.")
} else {
t.Errorf("Expected ContainerUserName bill got %s", c.ContainerUsername)
}
if c.ImageName == "fredsimage" {
t.Log("set ImageName in user config passed.")
} else {
t.Errorf("Expected ImageName fredsimage got %s", c.ImageName)
}
}
func Test_IniConfig_2(t *testing.T) {
c := Configuration{ContainerUsername: "default_contun", ImageName: "default", EnableUserConfig: true, EnableUserImageName: true}
n, err := loadConfigFromString([]byte(`[dockersh]
imagename = testimage
containerusername = shouldbeblacklisted`), "fred")
c = mergeConfigs(c, n, true)
if err != nil {
t.Error(err)
}
if c.ImageName == "testimage" {
t.Log("set ImageName passed.")
} else {
t.Errorf("Expected ImageName testimage got %s", c.ImageName)
}
if c.ContainerUsername == "default_contun" {
t.Log("blacklising worked, value not changed")
} else {
t.Error("blacklisting failed")
}
}
func Test_Config_3(t *testing.T) {
c := Configuration{ContainerUsername: "default_contun", Shell: "default_shell"}
c, err := loadConfigFromString([]byte(`[dockersh]
shell = global_default
containerusername = global_default
mounthometo = somewhere
enableusershell
`), "fred")
if err != nil {
t.Error(err)
}
if c.Shell != "global_default" {
t.Error("Set shell to global_default failed")
}
if c.ContainerUsername != "global_default" {
t.Error("Set un to global default failed")
}
if c.MountHomeTo != "somewhere" {
t.Error("Set mounthome to global default failed")
}
newc, err := loadConfigFromString([]byte(`[dockersh]
shell = user_value
containerusername = user_value
mounthometo = somewhere_else`), "fred")
if err != nil {
t.Error(err)
}
c = mergeConfigs(c, newc, true)
if c.Shell != "user_value" {
t.Error("Local defaults not applying over global defaults")
} else {
t.Log("c.shell not overridden")
}
if c.ContainerUsername != "global_default" {
t.Error("Blacklist of container_username in global config failed")
}
if c.MountHomeTo != "somewhere" {
t.Error("Blacklist mounthome in global config failed")
}
}
func Test_IniConfig_4(t *testing.T) {
c, err := loadConfigFromString([]byte(`[dockersh]
containerusername = default_contun
imagename = default
enableuserconfig
enableuserimagename`), "fred")
newc, err := loadConfigFromString([]byte(`[dockersh]
imagename = testimage
containerusername = shouldbeblacklisted`), "fred")
if err != nil {
t.Error(err)
}
c = mergeConfigs(c, newc, true)
if c.ImageName == "testimage" {
t.Log("set ImageName passed.")
} else {
t.Errorf("Expected ImageName testimage got %s", c.ImageName)
}
if c.ContainerUsername != "default_contun" {
t.Error("blacklising disabled, value changed")
} else {
t.Log("blacklisting enabled, value has not changed")
}
}
func Test_IniConfig_5(t *testing.T) {
c, err := loadConfigFromString([]byte(`[dockersh]
containerusername = default_contun
imagename = default
enableuserconfig
`), "fred")
newc, err := loadConfigFromString([]byte(`[dockersh]
imagename = testimage
containerusername = shouldbeblacklisted`), "fred")
if err != nil {
t.Error(err)
}
c = mergeConfigs(c, newc, true)
if c.ImageName == "default" {
t.Log("set ImageName passed.")
} else {
t.Errorf("Expected ImageName default got %s", c.ImageName)
}
if c.ContainerUsername != "default_contun" {
t.Error("blacklising disabled, value changed")
} else {
t.Log("blacklisting enabled, value has not changed")
}
}
func Test_IniConfig_6(t *testing.T) {
c, err := loadConfigFromString([]byte(`[dockersh]
imagename = default
enableuserimagename
[user "fred"]
imagename = testimage
`), "fred")
if err != nil {
t.Error(err)
}
if c.ImageName == "testimage" {
t.Log("set ImageName in user section when blacklisted in [dockersh] passed.")
} else {
t.Errorf("Expected ImageName testimage got %s", c.ImageName)
}
}