This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
ggr-ui_test.go
346 lines (321 loc) · 7.13 KB
/
ggr-ui_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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"context"
"crypto/md5"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"testing"
"time"
"github.com/aerokube/ggr/config"
)
var _ = func() bool {
testing.Init()
return true
}()
func init() {
gitRevision = "test-revision"
}
type Selenoid struct {
*httptest.Server
Host *config.Host
Sum string
}
func NewSelenoid(mux http.Handler) *Selenoid {
s := httptest.NewServer(mux)
u, _ := url.Parse(s.URL)
host, p, _ := net.SplitHostPort(u.Host)
port, _ := strconv.ParseInt(p, 10, 32)
return &Selenoid{
Server: s,
Host: &config.Host{
Name: host,
Port: int(port),
},
Sum: fmt.Sprintf("%x", md5.Sum([]byte(s.URL))),
}
}
type GGR struct {
*httptest.Server
}
func NewGGR() *GGR {
return &GGR{httptest.NewServer(mux())}
}
func CheckPath(p string) (*url.URL, error) {
u, err := url.ParseRequestURI(p)
if err != nil {
return nil, fmt.Errorf("check path: %v", err)
}
return u, nil
}
func ReadResponseBody(r io.Reader) ([]byte, error) {
buf, err := io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("read response body: %v", err)
}
return buf, nil
}
func CheckResponseCode(code int) error {
if code != http.StatusOK {
return fmt.Errorf("bad response code: %s", http.StatusText(code))
}
return nil
}
func DecodeResponse(r []byte) (map[string]interface{}, error) {
var v map[string]interface{}
err := json.Unmarshal(r, &v)
if err != nil {
return nil, fmt.Errorf("decode response: %v", err)
}
return v, nil
}
func QueryError(err error, p string) error {
return fmt.Errorf("query %s: %v", p, err)
}
func (ggr *GGR) Query(ctx context.Context, p string) (map[string]interface{}, error) {
u, err := CheckPath(p)
if err != nil {
return nil, QueryError(err, p)
}
if err != nil {
return nil, QueryError(err, p)
}
req, err := http.NewRequest(http.MethodGet, ggr.URL+u.Path, nil)
if err != nil {
return nil, QueryError(err, p)
}
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, QueryError(err, p)
}
err = CheckResponseCode(resp.StatusCode)
if err != nil {
return nil, QueryError(err, p)
}
buf, err := ReadResponseBody(resp.Body)
if err != nil {
return nil, QueryError(err, p)
}
r, err := DecodeResponse(buf)
if err != nil {
return nil, QueryError(err, p)
}
return r, nil
}
func (ggr *GGR) Status(ctx context.Context) (Status, error) {
r, err := ggr.Query(ctx, "/status")
if err != nil {
return nil, fmt.Errorf("status: %v", err)
}
return Status(r), nil
}
func (ggr *GGR) Ping(ctx context.Context) (map[string]interface{}, error) {
r, err := ggr.Query(ctx, "/ping")
if err != nil {
return nil, fmt.Errorf("ping: %v", err)
}
return r, nil
}
func TestPing(t *testing.T) {
ggr := NewGGR()
defer ggr.Close()
data, err := ggr.Ping(context.Background())
if err != nil {
t.Fatal(err)
}
_, ok := data["uptime"]
if !ok {
t.Error("there is no uptime")
}
version := data["version"]
if version != "test-revision" {
t.Errorf("version\n")
}
}
func TestBrokenConfig(t *testing.T) {
m := map[string]map[string]*config.Host{
"unknown": map[string]*config.Host{
"md5sum": &config.Host{Name: "://localhost"},
}}
lock.Lock()
hosts = m
lock.Unlock()
ggr := NewGGR()
defer ggr.Close()
s, err := ggr.Status(context.Background())
if err != nil {
t.Fatal(err)
}
if len(s) != 0 {
t.Errorf("uexpected nonempty status: %v\n", s)
}
}
func TestQueue(t *testing.T) {
tempLimit := limit
limit = 0
defer func(l int) {
limit = l
}(tempLimit)
tempTimeout := timeout
timeout = 100 * time.Millisecond
defer func(t time.Duration) {
timeout = t
}(tempTimeout)
ggr := NewGGR()
defer ggr.Close()
s, err := ggr.Status(context.Background())
if err != nil {
t.Fatal(err)
}
if len(s) != 0 {
t.Errorf("uexpected nonempty status: %v\n", s)
}
}
func TestResponseTime(t *testing.T) {
temp := responseTime
responseTime = 100 * time.Millisecond
defer func(tmp time.Duration) {
responseTime = tmp
}(temp)
selenoid := NewSelenoid(silent)
m := map[string]map[string]*config.Host{
"unknown": map[string]*config.Host{
selenoid.Sum: &config.Host{Name: selenoid.URL},
}}
lock.Lock()
hosts = m
lock.Unlock()
ggr := NewGGR()
defer ggr.Close()
s, err := ggr.Status(context.Background())
if err != nil {
t.Fatal(err)
}
if len(s) != 0 {
t.Errorf("uexpected nonempty status: %v\n", s)
}
}
var (
silent = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-r.Context().Done()
}))
empty = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, `{"total":1,"used":0,"queued":0,"pending":0,"browsers":{"chrome":{"60.0":{}},"firefox":{"59.0":{}}}}`)
}))
broken = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, `{"total":1,"used":0,"queued":0,"pending":0,`)
}))
)
type Case struct {
Name string
Expected float64
Timeout time.Duration
ContextTimeout time.Duration
Handlers []http.Handler
}
func TestStatus(t *testing.T) {
cases := []Case{
{
Name: "ConnectionRefused",
Expected: 0,
Timeout: 100 * time.Millisecond,
Handlers: []http.Handler{nil, nil, nil},
},
{
Name: "Timeout",
Expected: 0,
Timeout: 100 * time.Millisecond,
Handlers: []http.Handler{silent},
},
{
Name: "ClientDisconnected",
Expected: 0,
ContextTimeout: 100 * time.Millisecond,
Handlers: []http.Handler{silent},
},
{
Name: "TwoHostsDown",
Expected: 1,
Handlers: []http.Handler{nil, nil, empty},
},
{
Name: "TwoHostsBroken",
Expected: 1,
Handlers: []http.Handler{broken, broken, empty},
},
{
Name: "TwoHostsNoAnswer",
Expected: 1,
Timeout: 100 * time.Millisecond,
Handlers: []http.Handler{silent, silent, empty},
},
{
Name: "AllHostsUpAndRunning",
Expected: 3,
Handlers: []http.Handler{empty, empty, empty},
},
}
for i, c := range cases {
m := map[string]map[string]*config.Host{
"unknown": {}}
for _, handler := range c.Handlers {
selenoid := NewSelenoid(handler)
if handler != nil {
defer selenoid.Close()
} else {
selenoid.Close()
}
m["unknown"][selenoid.Sum] = selenoid.Host
}
lock.Lock()
hosts = m
lock.Unlock()
name := strconv.Itoa(i)
if c.Name != "" {
name = c.Name
}
t.Run(name, func(t *testing.T) {
if c.Timeout != 0 {
temp := timeout
timeout = c.Timeout
defer func(t time.Duration) {
timeout = t
}(temp)
}
ggr := NewGGR()
defer ggr.Close()
expectTimeout := false
ctx := context.Background()
if c.ContextTimeout != 0 {
expectTimeout = true
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, c.ContextTimeout)
defer cancel()
}
s, err := ggr.Status(ctx)
if expectTimeout && err == nil {
t.Fatal("unexpected success")
}
if !expectTimeout && err != nil {
t.Fatal(err)
}
if c.Expected != 0 {
if s["total"] != c.Expected {
t.Errorf("uexpected total: %g\n", s["total"])
}
} else {
if len(s) != 0 {
t.Errorf("uexpected nonempty status: %v\n", s)
}
}
})
}
}