@@ -17,9 +17,12 @@ package http
17
17
import (
18
18
"bytes"
19
19
"context"
20
+ "errors"
20
21
"fmt"
22
+ "io"
21
23
"io/ioutil"
22
24
"net/http"
25
+ "strings"
23
26
"testing"
24
27
"time"
25
28
@@ -32,7 +35,9 @@ import (
32
35
33
36
// The Transport is mocked instead of the Client because Client is not an
34
37
// interface, but RoundTripper (which Transport implements) is.
35
- type testTransport struct {}
38
+ type testTransport struct {
39
+ noBody io.ReadCloser
40
+ }
36
41
37
42
func newTestTransport () * testTransport {
38
43
return & testTransport {}
@@ -46,11 +51,20 @@ type testReadCloser struct {
46
51
func (trc * testReadCloser ) Read (p []byte ) (n int , err error ) {
47
52
return trc .b .Read (p )
48
53
}
54
+
49
55
func (trc * testReadCloser ) Close () error {
50
56
return nil
51
57
}
52
58
53
59
func (tt * testTransport ) RoundTrip (req * http.Request ) (* http.Response , error ) {
60
+ if req .URL .Host == "fail-test.com" {
61
+ return nil , errors .New ("failing for fail-target.com" )
62
+ }
63
+
64
+ if req .Body == nil {
65
+ return & http.Response {Body : http .NoBody }, nil
66
+ }
67
+
54
68
b , err := ioutil .ReadAll (req .Body )
55
69
if err != nil {
56
70
return nil , err
@@ -66,7 +80,7 @@ func (tt *testTransport) RoundTrip(req *http.Request) (*http.Response, error) {
66
80
67
81
func (tt * testTransport ) CancelRequest (req * http.Request ) {}
68
82
69
- func testProbe (opts * options.Options ) ([]* result , error ) {
83
+ func testProbe (opts * options.Options ) ([]* probeResult , error ) {
70
84
p := & Probe {}
71
85
err := p .Init ("http_test" , opts )
72
86
if err != nil {
@@ -76,23 +90,16 @@ func testProbe(opts *options.Options) ([]*result, error) {
76
90
77
91
p .runProbe (context .Background ())
78
92
79
- var results []* result
93
+ var results []* probeResult
80
94
for _ , target := range p .targets {
81
95
results = append (results , p .results [target .Name ])
82
96
}
83
97
return results , nil
84
98
}
85
99
86
- func TestRun (t * testing.T ) {
87
- methods := []configpb.ProbeConf_Method {
88
- configpb .ProbeConf_GET ,
89
- configpb .ProbeConf_POST ,
90
- configpb .ProbeConf_PUT ,
91
- configpb .ProbeConf_HEAD ,
92
- configpb .ProbeConf_DELETE ,
93
- configpb .ProbeConf_PATCH ,
94
- configpb .ProbeConf_OPTIONS ,
95
- 100 , // Should default to configpb.ProbeConf_GET
100
+ func TestProbeVariousMethods (t * testing.T ) {
101
+ mpb := func (s string ) * configpb.ProbeConf_Method {
102
+ return configpb .ProbeConf_Method (configpb .ProbeConf_Method_value [s ]).Enum ()
96
103
}
97
104
98
105
testBody := "Test HTTP Body"
@@ -105,39 +112,43 @@ func TestRun(t *testing.T) {
105
112
{& configpb.ProbeConf {}, "total: 1, success: 1" },
106
113
{& configpb.ProbeConf {Protocol : configpb .ProbeConf_HTTPS .Enum ()}, "total: 1, success: 1" },
107
114
{& configpb.ProbeConf {RequestsPerProbe : proto .Int32 (1 )}, "total: 1, success: 1" },
108
- {& configpb.ProbeConf {Method : & methods [ 0 ] }, "total: 1 , success: 1 " },
109
- {& configpb.ProbeConf {Method : & methods [ 1 ] }, "total: 1, success: 1" },
110
- {& configpb.ProbeConf {Method : & methods [ 1 ], Body : & testBody }, "total: 1, success: 1" },
111
- {& configpb.ProbeConf {Method : & methods [ 2 ] }, "total: 1, success: 1" },
112
- {& configpb.ProbeConf {Method : & methods [ 2 ], Body : & testBody }, "total: 1, success: 1" },
113
- {& configpb.ProbeConf {Method : & methods [ 3 ] }, "total: 1, success: 1" },
114
- {& configpb.ProbeConf {Method : & methods [ 4 ] }, "total: 1, success: 1" },
115
- {& configpb.ProbeConf {Method : & methods [ 5 ] }, "total: 1, success: 1" },
116
- {& configpb.ProbeConf {Method : & methods [ 6 ] }, "total: 1, success: 1" },
117
- {& configpb.ProbeConf {Method : & methods [ 7 ] }, "total: 1, success: 1" },
115
+ {& configpb.ProbeConf {RequestsPerProbe : proto . Int32 ( 4 ) }, "total: 4 , success: 4 " },
116
+ {& configpb.ProbeConf {Method : mpb ( "GET" ) }, "total: 1, success: 1" },
117
+ {& configpb.ProbeConf {Method : mpb ( "POST" ) }, "total: 1, success: 1" },
118
+ {& configpb.ProbeConf {Method : mpb ( "POST" ), Body : & testBody }, "total: 1, success: 1" },
119
+ {& configpb.ProbeConf {Method : mpb ( "PUT" ) }, "total: 1, success: 1" },
120
+ {& configpb.ProbeConf {Method : mpb ( "PUT" ), Body : & testBody }, "total: 1, success: 1" },
121
+ {& configpb.ProbeConf {Method : mpb ( "HEAD" ) }, "total: 1, success: 1" },
122
+ {& configpb.ProbeConf {Method : mpb ( "DELETE" ) }, "total: 1, success: 1" },
123
+ {& configpb.ProbeConf {Method : mpb ( "PATCH" ) }, "total: 1, success: 1" },
124
+ {& configpb.ProbeConf {Method : mpb ( "OPTIONS" ) }, "total: 1, success: 1" },
118
125
{& configpb.ProbeConf {Headers : []* configpb.ProbeConf_Header {{Name : & testHeaderName , Value : & testHeaderValue }}}, "total: 1, success: 1" },
119
126
}
120
127
121
- for _ , test := range tests {
122
- opts := & options.Options {
123
- Targets : targets .StaticTargets ("test.com" ),
124
- Interval : 2 * time .Second ,
125
- Timeout : time .Second ,
126
- ProbeConf : test .input ,
127
- }
128
- results , err := testProbe (opts )
129
- if err != nil {
130
- if fmt .Sprintf ("error: '%s'" , err .Error ()) != test .want {
131
- t .Errorf ("Unexpected initialization error: %v" , err )
128
+ for i , test := range tests {
129
+ t .Run (fmt .Sprintf ("Test_case(%d)_config(%v)" , i , test .input ), func (t * testing.T ) {
130
+ opts := & options.Options {
131
+ Targets : targets .StaticTargets ("test.com" ),
132
+ Interval : 2 * time .Second ,
133
+ Timeout : time .Second ,
134
+ ProbeConf : test .input ,
135
+ }
136
+
137
+ results , err := testProbe (opts )
138
+ if err != nil {
139
+ if fmt .Sprintf ("error: '%s'" , err .Error ()) != test .want {
140
+ t .Errorf ("Unexpected initialization error: %v" , err )
141
+ }
142
+ return
132
143
}
133
- } else {
144
+
134
145
for _ , result := range results {
135
146
got := fmt .Sprintf ("total: %d, success: %d" , result .total , result .success )
136
147
if got != test .want {
137
148
t .Errorf ("Mismatch got '%s', want '%s'" , got , test .want )
138
149
}
139
150
}
140
- }
151
+ })
141
152
}
142
153
}
143
154
@@ -181,3 +192,58 @@ func TestProbeWithBody(t *testing.T) {
181
192
t .Errorf ("response map: got=%s, expected=%s" , got , expected )
182
193
}
183
194
}
195
+
196
+ func TestMultipleTargetsMultipleRequests (t * testing.T ) {
197
+ testTargets := []string {"test.com" , "fail-test.com" }
198
+ reqPerProbe := int64 (6 )
199
+ opts := & options.Options {
200
+ Targets : targets .StaticTargets (strings .Join (testTargets , "," )),
201
+ Interval : 10 * time .Millisecond ,
202
+ ProbeConf : & configpb.ProbeConf {RequestsPerProbe : proto .Int32 (int32 (reqPerProbe ))},
203
+ }
204
+
205
+ p := & Probe {}
206
+ err := p .Init ("http_test" , opts )
207
+ if err != nil {
208
+ t .Errorf ("Unexpected error: %v" , err )
209
+ return
210
+ }
211
+ p .client .Transport = newTestTransport ()
212
+
213
+ // Verify that Init() created result struct for each target.
214
+ for _ , tgt := range testTargets {
215
+ if _ , ok := p .results [tgt ]; ! ok {
216
+ t .Errorf ("didn't find results for the target: %s" , tgt )
217
+ }
218
+ }
219
+
220
+ p .runProbe (context .Background ())
221
+
222
+ wantSuccess := map [string ]int64 {
223
+ "test.com" : reqPerProbe ,
224
+ "fail-test.com" : 0 , // Test transport is configured to fail this.
225
+ }
226
+
227
+ for _ , tgt := range testTargets {
228
+ if p .results [tgt ].total != reqPerProbe {
229
+ t .Errorf ("For target %s, total=%d, want=%d" , tgt , p .results [tgt ].total , reqPerProbe )
230
+ }
231
+ if p .results [tgt ].success != wantSuccess [tgt ] {
232
+ t .Errorf ("For target %s, success=%d, want=%d" , tgt , p .results [tgt ].success , wantSuccess [tgt ])
233
+ }
234
+ }
235
+
236
+ // Run again
237
+ p .runProbe (context .Background ())
238
+
239
+ wantSuccess ["test.com" ] += reqPerProbe
240
+
241
+ for _ , tgt := range testTargets {
242
+ if p .results [tgt ].total != 2 * reqPerProbe {
243
+ t .Errorf ("For target %s, total=%d, want=%d" , tgt , p .results [tgt ].total , reqPerProbe )
244
+ }
245
+ if p .results [tgt ].success != wantSuccess [tgt ] {
246
+ t .Errorf ("For target %s, success=%d, want=%d" , tgt , p .results [tgt ].success , wantSuccess [tgt ])
247
+ }
248
+ }
249
+ }
0 commit comments