-
Notifications
You must be signed in to change notification settings - Fork 31
/
aws_test.go
222 lines (170 loc) · 4.57 KB
/
aws_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
package awsping
import (
"errors"
"fmt"
"net"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
)
func TestAWSRegionError(t *testing.T) {
AWSErr := errors.New("something bad")
r := AWSRegion{Error: AWSErr}
got := r.GetLatencyStr()
want := AWSErr.Error()
if got != want {
t.Errorf("failed:\ngot=%q\nwant=%q", got, want)
}
}
type testTarget struct {
URL string
IP *net.TCPAddr
}
func (r *testTarget) GetURL() string {
return r.URL
}
// GetIP return IP for AWS target
func (r *testTarget) GetIP() (*net.TCPAddr, error) {
return r.IP, nil
}
func TestAWSRegionCheckLatencyHTTP(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(15 * time.Millisecond)
fmt.Fprintln(w, "X")
}))
defer ts.Close()
tt := testTarget{URL: ts.URL}
regions := GetRegions()
service := "ec2"
checkType := CheckTypeHTTP
regions.SetService(service)
regions.SetCheckType(checkType)
regions.SetTarget(func(r *AWSRegion) {
r.Target = &tt
})
var wg sync.WaitGroup
wg.Add(1)
regions[0].CheckLatency(&wg)
got := regions[0].GetLatency()
want := 15.0
if got < want || got > want*2 {
t.Errorf("failed:\ngot=%f\nwant=%f", got, want)
}
// check "error"
errTxt := "something bad"
regions[0].Request = &testRequest{err: errors.New(errTxt)}
wg.Add(1)
regions[0].CheckLatency(&wg)
if regions[0].Error == nil {
t.Errorf("failed: error should not be empty")
}
if regions[0].Error.Error() != errTxt {
t.Errorf("failed: error should be empty=%s", errTxt)
}
}
type testRequest struct {
duration time.Duration
err error
}
func (d *testRequest) Do(_, _ string, _ RequestType) (time.Duration, error) {
if d.err != nil {
return 0, d.err
}
return d.duration, nil
}
func TestAWSRegionCheckLatencyTCP(t *testing.T) {
// just random local IP
tt := testTarget{IP: &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 67890,
}}
regions := GetRegions()
service := "ec2"
checkType := CheckTypeTCP
regions.SetService(service)
regions.SetCheckType(checkType)
regions.SetTarget(func(r *AWSRegion) {
r.Target = &tt
})
regions[0].Request = &testRequest{duration: 15 * time.Millisecond}
var wg sync.WaitGroup
wg.Add(1)
regions[0].CheckLatency(&wg)
got := regions[0].GetLatency()
want := 15.0
if got < want || got > want+1 {
t.Errorf("failed:\ngot=%f\nwant=%f\nregion=%q", got, want, regions[0])
}
if regions[0].Error != nil {
t.Errorf("failed: error should be empty")
}
// check "error"
errTxt := "something bad"
regions[0].Request = &testRequest{err: errors.New(errTxt)}
wg.Add(1)
regions[0].CheckLatency(&wg)
if regions[0].Error == nil {
t.Errorf("failed: error should not be empty")
}
if regions[0].Error.Error() != errTxt {
t.Errorf("failed: error should be empty=%s", errTxt)
}
}
// ---------------------------------------------
func TestAWSRegionsLen(t *testing.T) {
regions := GetRegions()
got := regions.Len()
want := len(regions)
if got != want {
t.Errorf("failed:\ngot=%q\nwant=%q", got, want)
}
}
func TestAWSRegionsLess(t *testing.T) {
regions := GetRegions()
regions[0].Latencies = []time.Duration{15 * time.Millisecond}
regions[1].Latencies = []time.Duration{25 * time.Millisecond}
if !regions.Less(0, 1) {
t.Errorf("failed: not less, regions=%q", regions)
}
}
func TestAWSRegionsSwap(t *testing.T) {
regions := GetRegions()
regions[0].Latencies = []time.Duration{15 * time.Millisecond}
regions[1].Latencies = []time.Duration{25 * time.Millisecond}
regions.Swap(0, 3)
if len(regions[0].Latencies) != 0 {
t.Errorf("failed: not swapped, regions=%q", regions)
}
}
func TestAWSRegionsSetService(t *testing.T) {
regions := GetRegions()
service := "ec2"
regions.SetService(service)
if regions[0].Service != service || regions[len(regions)-1].Service != service {
t.Errorf("failed: not set, regions=%q, service=%s", regions, service)
}
}
func TestAWSRegionsSetCheckType(t *testing.T) {
regions := GetRegions()
checkType := CheckTypeHTTP
regions.SetCheckType(checkType)
if regions[0].CheckType != checkType || regions[len(regions)-1].CheckType != checkType {
t.Errorf("failed: not set, regions=%q, checkType=%d", regions, checkType)
}
}
func TestAWSRegionsSetDefaultTarget(t *testing.T) {
regions := GetRegions()
service := "ec2"
checkType := CheckTypeHTTPS
regions.SetService(service)
regions.SetCheckType(checkType)
regions.SetDefaultTarget()
got := regions[0].Target.GetURL()
want := fmt.Sprintf("https://ec2.%s.amazonaws.com/ping?x=", regions[0].Code)
if !strings.HasPrefix(got, want) {
t.Errorf("failed: wrong url\ngot=%s\nneed=%s", got, want)
}
}