forked from AntoineAugusti/updown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
155 lines (125 loc) · 3.83 KB
/
client_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
package updown
import (
"net"
"net/http"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const TQToken = "s7su"
func newClient() *Client {
apiKey := os.Getenv("UPDOWN_API_KEY")
if apiKey == "" {
panic("API key is not set")
}
return NewClient(apiKey, nil)
}
func TestTokenForAlias(t *testing.T) {
client := newClient()
// Cache miss + alias not found
token, err := client.Check.TokenForAlias("foo")
assert.Equal(t, "", token)
assert.Equal(t, ErrTokenNotFound, err)
// - Cache miss + match found after request
// - Cache hit
for i := 0; i < 2; i++ {
token, err = client.Check.TokenForAlias("Teen Quotes")
assert.Nil(t, err)
assert.Equal(t, TQToken, token)
}
}
func TestList(t *testing.T) {
client := newClient()
checks, resp, _ := client.Check.List()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.True(t, len(checks) > 0)
found := false
for _, element := range checks {
if element.Alias == "Teen Quotes" {
found = true
break
}
}
assert.True(t, found, "Cannot found the Teen Quotes check")
}
func TestGet(t *testing.T) {
client := newClient()
check, resp, _ := client.Check.Get(TQToken)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "Teen Quotes", check.Alias)
check, resp, err := client.Check.Get("aaaaaa")
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
assert.Equal(t, "GET https://updown.io/api/checks/aaaaaa: 404 ", err.Error())
}
func TestListDowntimes(t *testing.T) {
client := newClient()
// Page should be set to 1 automatically
downs, resp, _ := client.Downtime.List(TQToken, -1)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.True(t, len(downs) > 1)
// Page with no downtimes
downs, resp, _ = client.Downtime.List(TQToken, 200)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, 0, len(downs))
}
func TestAddUpdateRemoveCheck(t *testing.T) {
client := newClient()
res, resp, _ := client.Check.Add(CheckItem{URL: "https://google.fr"})
assert.Equal(t, http.StatusCreated, resp.StatusCode)
assert.Equal(t, "https://google.fr", res.URL)
res, resp, _ = client.Check.Update(res.Token, CheckItem{URL: "https://google.com"})
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "https://google.com", res.URL)
result, resp, _ := client.Check.Remove(res.Token)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.True(t, result)
}
func TestListMetrics(t *testing.T) {
client := newClient()
now := time.Now()
timeFormat := "2006-01-02 15:04:05 -0700"
from, to := now.AddDate(0, 0, -1).Format(timeFormat), now.Format(timeFormat)
metricRes, resp, _ := client.Metric.List(TQToken, "host", from, to)
assert.Equal(t, http.StatusOK, resp.StatusCode)
for _, location := range updownLocations() {
assert.Contains(t, metricRes, location)
}
assert.True(t, len(metricRes) > 1)
}
func TestListNodes(t *testing.T) {
client := newClient()
nodeRes, resp, _ := client.Node.List()
assert.Equal(t, http.StatusOK, resp.StatusCode)
for _, location := range updownLocations() {
assert.Contains(t, nodeRes, location)
}
assert.True(t, len(nodeRes) > 1)
}
func TestListIPv4(t *testing.T) {
client := newClient()
IPs, resp, _ := client.Node.ListIPv4()
for _, ip := range IPs {
assert.True(t, isIPv4(net.ParseIP(ip)))
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, len(IPs), len(updownLocations()))
}
func TestListIPv6(t *testing.T) {
client := newClient()
IPs, resp, _ := client.Node.ListIPv6()
for _, ip := range IPs {
assert.True(t, isIPv6(net.ParseIP(ip)))
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, len(IPs), len(updownLocations()))
}
func updownLocations() []string {
return []string{"lan", "mia", "bhs", "gra", "fra", "sin", "tok", "syd"}
}
func isIPv4(ip net.IP) bool {
return ip.To4().String() == ip.String()
}
func isIPv6(ip net.IP) bool {
return ip.To16().String() == ip.String()
}