-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver_test.go
68 lines (61 loc) · 1.17 KB
/
server_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
package patrol
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"testing"
"time"
"github.com/karimsa/patrol/internal/checker"
"github.com/karimsa/patrol/internal/history"
)
func TestServer(t *testing.T) {
os.Remove("server-test.db")
historyFile, err := history.New(history.NewOptions{
File: "server-test.db",
})
if err != nil {
t.Error(err)
return
}
p, err := New(CreatePatrolOptions{
Port: 8081,
Checkers: []*checker.Checker{
checker.New(&checker.Checker{
Group: "foo",
Name: "bar",
Cmd: "ping -c1 localhost",
History: historyFile,
Interval: 1 * time.Minute,
}),
checker.New(&checker.Checker{
Group: "foo",
Name: "unbar",
Cmd: "ping -c1 localhost",
History: historyFile,
Interval: 1 * time.Minute,
}),
},
}, historyFile)
if err != nil {
t.Error(err)
return
}
p.Start()
<-time.After(1 * time.Second)
res, err := http.Get("http://localhost:8081")
if err != nil {
t.Error(err)
return
}
if res.StatusCode != 200 {
t.Error(fmt.Errorf("Server returned non-200 status: %#v", res))
return
}
_, err = ioutil.ReadAll(res.Body)
if err != nil {
t.Error(err)
return
}
p.Close()
}