-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcanaryd_test.go
134 lines (100 loc) · 2.57 KB
/
canaryd_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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"github.com/vmihailenco/redis/v2"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func formatFloat(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 64)
}
type MockRedis struct {
mock.Mock
}
func (mr *MockRedis) ZAdd(key string, members ...redis.Z) *redis.IntCmd {
args := mr.Mock.Called(key, members)
return args.Get(0).(*redis.IntCmd)
}
func (mr *MockRedis) ZRemRangeByScore(key, min, max string) *redis.IntCmd {
args := mr.Mock.Called(key, min, max)
return args.Get(0).(*redis.IntCmd)
}
func (mr *MockRedis) ZRevRangeByScore(key string, opt redis.ZRangeByScore) *redis.StringSliceCmd {
args := mr.Mock.Called(key, opt)
return args.Get(0).(*redis.StringSliceCmd)
}
func (mr *MockRedis) Publish(channel, message string) *redis.IntCmd {
args := mr.Mock.Called(channel, message)
return args.Get(0).(*redis.IntCmd)
}
func TestGetHealth(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(healthHandler))
defer ts.Close()
res, err := http.Get(ts.URL + "/health")
assert.Nil(t, err)
assert.Equal(t, 200, res.StatusCode)
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
assert.Nil(t, err)
assert.Equal(t, "OK\n", string(body))
}
func TestRecordMeasurement(t *testing.T) {
measurement := Measurement{
Check: Check{"FOO", "http://localhost"},
ID: "ee338667-42ac-4a4f-49cc-00a6a16a384b",
Location: "test-location",
T: 1403604335,
ExitStatus: 0,
}
s, _ := json.Marshal(measurement)
mockRedis := new(MockRedis)
resp := redis.NewIntCmd()
mockRedis.On(
"ZAdd",
"measurements:FOO",
[]redis.Z{
redis.Z{
Score: float64(measurement.T),
Member: string(s),
},
},
).Return(resp)
recorder := NewRecorder(mockRedis, false)
recorder.record(&measurement)
mockRedis.AssertExpectations(t)
}
func TestPublishMeasurement(t *testing.T) {
measurement := Measurement{
Check: Check{"FOO", "http://localhost"},
ID: "ee338667-42ac-4a4f-49cc-00a6a16a384b",
Location: "test-location",
T: 1403604335,
ExitStatus: 0,
}
s, _ := json.Marshal(measurement)
mockRedis := new(MockRedis)
resp := redis.NewIntCmd()
mockRedis.On(
"ZAdd",
"measurements:FOO",
[]redis.Z{
redis.Z{
Score: float64(measurement.T),
Member: string(s),
},
},
).Return(resp)
mockRedis.On(
"Publish",
"measurements:FOO",
string(s),
).Return(resp)
recorder := NewRecorder(mockRedis, true)
recorder.record(&measurement)
mockRedis.AssertExpectations(t)
}