forked from aerokube/selenoid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
254 lines (220 loc) · 7.13 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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"testing"
. "github.com/aandryashin/matchers"
"github.com/aerokube/selenoid/config"
"github.com/aerokube/selenoid/session"
)
const testLogConf = "config/container-logs.json"
func configfile(s string) string {
tmp, err := ioutil.TempFile("", "config")
if err != nil {
log.Fatal(err)
}
_, err = tmp.Write([]byte(s))
if err != nil {
log.Fatal(err)
}
err = tmp.Close()
if err != nil {
log.Fatal(err)
}
return tmp.Name()
}
func TestConfig(t *testing.T) {
confFile := configfile(`{}`)
defer os.Remove(confFile)
conf := config.NewConfig()
err := conf.Load(confFile, testLogConf)
AssertThat(t, err, Is{nil})
}
func TestConfigError(t *testing.T) {
confFile := configfile(`{}`)
os.Remove(confFile)
conf := config.NewConfig()
err := conf.Load(confFile, testLogConf)
AssertThat(t, err.Error(), EqualTo{fmt.Sprintf("browsers config: read error: open %s: no such file or directory", confFile)})
}
func TestLogConfigError(t *testing.T) {
confFile := configfile(`{}`)
defer os.Remove(confFile)
conf := config.NewConfig()
err := conf.Load(confFile, "some-missing-file")
AssertThat(t, err, Not{nil})
}
func TestConfigParseError(t *testing.T) {
confFile := configfile(`{`)
defer os.Remove(confFile)
var conf config.Config
err := conf.Load(confFile, testLogConf)
AssertThat(t, err.Error(), EqualTo{"browsers config: parse error: unexpected end of JSON input"})
}
func TestConfigEmptyState(t *testing.T) {
confFile := configfile(`{}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
state := conf.State(session.NewMap(), 0, 0, 0)
AssertThat(t, state.Total, EqualTo{0})
AssertThat(t, state.Queued, EqualTo{0})
AssertThat(t, state.Pending, EqualTo{0})
AssertThat(t, state.Used, EqualTo{0})
}
func TestConfigNonEmptyState(t *testing.T) {
confFile := configfile(`{}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
sessions := session.NewMap()
sessions.Put("0", &session.Session{Caps: session.Caps{Name: "firefox", Version: "49.0"}, Quota: "unknown"})
state := conf.State(sessions, 1, 0, 0)
AssertThat(t, state.Total, EqualTo{1})
AssertThat(t, state.Queued, EqualTo{0})
AssertThat(t, state.Pending, EqualTo{0})
AssertThat(t, state.Used, EqualTo{1})
AssertThat(t, state.Browsers["firefox"]["49.0"]["unknown"].Count, EqualTo{1})
}
func TestConfigEmptyVersions(t *testing.T) {
confFile := configfile(`{"firefox":{}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
sessions := session.NewMap()
sessions.Put("0", &session.Session{Caps: session.Caps{Name: "firefox", Version: "49.0"}, Quota: "unknown"})
state := conf.State(sessions, 1, 0, 0)
AssertThat(t, state.Total, EqualTo{1})
AssertThat(t, state.Queued, EqualTo{0})
AssertThat(t, state.Pending, EqualTo{0})
AssertThat(t, state.Used, EqualTo{1})
AssertThat(t, state.Browsers["firefox"]["49.0"]["unknown"].Count, EqualTo{1})
}
func TestConfigNonEmptyVersions(t *testing.T) {
confFile := configfile(`{"firefox":{"default":"49.0","versions":{"49.0":{}}}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
sessions := session.NewMap()
sessions.Put("0", &session.Session{Caps: session.Caps{Name: "firefox", Version: "49.0"}, Quota: "unknown"})
state := conf.State(sessions, 1, 0, 0)
AssertThat(t, state.Total, EqualTo{1})
AssertThat(t, state.Queued, EqualTo{0})
AssertThat(t, state.Pending, EqualTo{0})
AssertThat(t, state.Used, EqualTo{1})
AssertThat(t, state.Browsers["firefox"]["49.0"]["unknown"].Count, EqualTo{1})
}
func TestConfigFindMissingBrowser(t *testing.T) {
confFile := configfile(`{}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
_, _, ok := conf.Find("firefox", "")
AssertThat(t, ok, Is{false})
}
func TestConfigFindDefaultVersionError(t *testing.T) {
confFile := configfile(`{"firefox":{"default":""}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
_, _, ok := conf.Find("firefox", "")
AssertThat(t, ok, Is{false})
}
func TestConfigFindDefaultVersion(t *testing.T) {
confFile := configfile(`{"firefox":{"default":"49.0"}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
_, v, ok := conf.Find("firefox", "")
AssertThat(t, ok, Is{false})
AssertThat(t, v, EqualTo{"49.0"})
}
func TestConfigFindFoundByEmptyPrefix(t *testing.T) {
confFile := configfile(`{"firefox":{"default":"49.0","versions":{"49.0":{}}}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
_, v, ok := conf.Find("firefox", "")
AssertThat(t, ok, Is{true})
AssertThat(t, v, EqualTo{"49.0"})
}
func TestConfigFindFoundByPrefix(t *testing.T) {
confFile := configfile(`{"firefox":{"default":"49.0","versions":{"49.0":{}}}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
_, v, ok := conf.Find("firefox", "49")
AssertThat(t, ok, Is{true})
AssertThat(t, v, EqualTo{"49.0"})
}
func TestConfigFindFoundByMatch(t *testing.T) {
confFile := configfile(`{"firefox":{"default":"49.0","versions":{"49.0":{}}}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
_, v, ok := conf.Find("firefox", "49.0")
AssertThat(t, ok, Is{true})
AssertThat(t, v, EqualTo{"49.0"})
}
func TestConfigFindImage(t *testing.T) {
confFile := configfile(`{"firefox":{"default":"49.0","versions":{"49.0":{"image":"image","port":"5555", "path":"/"}}}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
b, v, ok := conf.Find("firefox", "49.0")
AssertThat(t, ok, Is{true})
AssertThat(t, v, EqualTo{"49.0"})
AssertThat(t, b.Image, EqualTo{"image"})
AssertThat(t, b.Port, EqualTo{"5555"})
AssertThat(t, b.Path, EqualTo{"/"})
}
func TestConfigConcurrentLoad(t *testing.T) {
confFile := configfile(`{"firefox":{"default":""}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
done := make(chan struct{})
go func() {
conf.Load(confFile, testLogConf)
done <- struct{}{}
}()
conf.Load(confFile, testLogConf)
<-done
}
func TestConfigConcurrentLoadAndRead(t *testing.T) {
confFile := configfile(`{"firefox":{"default":"49.0","versions":{"49.0":{"image":"image","port":"5555", "path":"/", "tmpfs": {"/tmp":"size=64k"}}}}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
err := conf.Load(confFile, testLogConf)
if err != nil {
t.Error(err)
}
done := make(chan string)
go func() {
browser, _, _ := conf.Find("firefox", "")
done <- browser.Tmpfs["/tmp"]
}()
conf.Load(confFile, testLogConf)
<-done
}
func TestConfigConcurrentRead(t *testing.T) {
confFile := configfile(`{"firefox":{"default":"49.0","versions":{"49.0":{"image":"image","port":"5555", "path":"/", "tmpfs": {"/tmp":"size=64k"}}}}}`)
defer os.Remove(confFile)
var conf config.Config
err := conf.Load(confFile, testLogConf)
if err != nil {
t.Error(err)
}
done := make(chan string)
go func() {
browser, _, _ := conf.Find("firefox", "")
done <- browser.Tmpfs["/tmp"]
}()
go func() {
browser, _, _ := conf.Find("firefox", "")
done <- browser.Tmpfs["/tmp"]
}()
<-done
<-done
}