6
6
_ "embed"
7
7
"errors"
8
8
"io"
9
+ "net/http"
10
+ "net/http/httptest"
9
11
"os"
10
12
"os/exec"
11
13
"path/filepath"
@@ -35,18 +37,26 @@ func TestSMK6(t *testing.T) {
35
37
t .Fatalf ("sm-k6 binary does not seem to exist, must be compiled before running this test: %v" , err )
36
38
}
37
39
38
- ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
40
+ httpEndpoint , httpsEndpoint := testHTTPServer (t )
41
+
42
+ ctx , cancel := context .WithTimeout (context .Background (), time .Minute )
39
43
t .Cleanup (cancel )
40
44
41
45
outFile := filepath .Join (t .TempDir (), "metrics.txt" )
42
46
43
- cmd := exec .CommandContext (ctx , smk6 , "run" , "-" , "-o=sm=" + outFile )
47
+ cmd := exec .CommandContext (ctx , smk6 , "run" , "-" , "-o=sm=" + outFile , "--insecure-skip-tls-verify" )
44
48
cmd .Stdin = bytes .NewReader (testScript )
45
- err = cmd .Run ()
49
+ cmd .Env = []string {
50
+ "TEST_HTTP_HOST=" + httpEndpoint ,
51
+ "TEST_HTTPS_HOST=" + httpsEndpoint ,
52
+ }
53
+ k6out , err := cmd .CombinedOutput ()
46
54
if err != nil {
47
- t .Fatalf ("running sm-k6: %v" , err )
55
+ t .Fatalf ("running sm-k6: %v\n %s " , errors . Join ( err , ctx . Err ()), string ( k6out ) )
48
56
}
49
57
58
+ t .Log (string (k6out ))
59
+
50
60
out , err := os .Open (outFile )
51
61
if err != nil {
52
62
t .Fatalf ("reading output metrics: %v" , err )
@@ -218,44 +228,44 @@ func TestSMK6(t *testing.T) {
218
228
{
219
229
name : "HTTP duration second has a duration-ish value" ,
220
230
metricName : "probe_http_duration_seconds" ,
221
- metricLabels : map [string ]string {"phase" : "processing" , "url" : "https://test-api.k6.io /public/crocodiles/" },
231
+ metricLabels : map [string ]string {"phase" : "processing" , "url" : httpsEndpoint + " /public/crocodiles/" },
222
232
assertValue : nonZero ,
223
233
},
224
234
// Custom http phases. Check for each one individually as we use slightly different names than k6 uses.
225
235
{
226
236
name : "HTTP duration seconds has phase=resolve" ,
227
237
metricName : "probe_http_duration_seconds" ,
228
- metricLabels : map [string ]string {"phase" : "resolve" , "url" : "https://test-api.k6.io /public/crocodiles/" },
238
+ metricLabels : map [string ]string {"phase" : "resolve" , "url" : httpsEndpoint + " /public/crocodiles/" },
229
239
assertValue : any , // Just fail if not present.
230
240
},
231
241
{
232
242
name : "HTTP duration seconds has phase=connect" ,
233
243
metricName : "probe_http_duration_seconds" ,
234
- metricLabels : map [string ]string {"phase" : "connect" , "url" : "https://test-api.k6.io /public/crocodiles/" },
244
+ metricLabels : map [string ]string {"phase" : "connect" , "url" : httpsEndpoint + " /public/crocodiles/" },
235
245
assertValue : any , // Just fail if not present.
236
246
},
237
247
{
238
248
name : "HTTP duration seconds has phase=tls" ,
239
249
metricName : "probe_http_duration_seconds" ,
240
- metricLabels : map [string ]string {"phase" : "tls" , "url" : "https://test-api.k6.io /public/crocodiles/" },
250
+ metricLabels : map [string ]string {"phase" : "tls" , "url" : httpsEndpoint + " /public/crocodiles/" },
241
251
assertValue : any , // Just fail if not present.
242
252
},
243
253
{
244
254
name : "HTTP duration seconds has phase=processing" ,
245
255
metricName : "probe_http_duration_seconds" ,
246
- metricLabels : map [string ]string {"phase" : "processing" , "url" : "https://test-api.k6.io /public/crocodiles/" },
256
+ metricLabels : map [string ]string {"phase" : "processing" , "url" : httpsEndpoint + " /public/crocodiles/" },
247
257
assertValue : any , // Just fail if not present.
248
258
},
249
259
{
250
260
name : "HTTP duration seconds has phase=transfer" ,
251
261
metricName : "probe_http_duration_seconds" ,
252
- metricLabels : map [string ]string {"phase" : "transfer" , "url" : "https://test-api.k6.io /public/crocodiles/" },
262
+ metricLabels : map [string ]string {"phase" : "transfer" , "url" : httpsEndpoint + " /public/crocodiles/" },
253
263
assertValue : any , // Just fail if not present.
254
264
},
255
265
{
256
266
name : "Error code for request that should succeed" ,
257
267
metricName : "probe_http_error_code" ,
258
- metricLabels : map [string ]string {"url" : "https://test-api.k6.io /public/crocodiles/" },
268
+ metricLabels : map [string ]string {"url" : httpsEndpoint + " /public/crocodiles/" },
259
269
assertValue : equals (0 ),
260
270
},
261
271
{
@@ -267,56 +277,56 @@ func TestSMK6(t *testing.T) {
267
277
{
268
278
name : "HTTP status code for a request that should succeed" ,
269
279
metricName : "probe_http_status_code" ,
270
- metricLabels : map [string ]string {"url" : "https://test-api.k6.io /public/crocodiles/" },
280
+ metricLabels : map [string ]string {"url" : httpsEndpoint + " /public/crocodiles/" },
271
281
assertValue : equals (200 ),
272
282
},
273
283
{
274
284
name : "Expected response for a request that should succeed" ,
275
285
metricName : "probe_http_got_expected_response" ,
276
- metricLabels : map [string ]string {"url" : "https://test-api.k6.io /public/crocodiles/" },
286
+ metricLabels : map [string ]string {"url" : httpsEndpoint + " /public/crocodiles/" },
277
287
assertValue : equals (1 ),
278
288
},
279
289
{
280
290
name : "Expected response for a request that should fail" ,
281
291
metricName : "probe_http_got_expected_response" ,
282
- metricLabels : map [string ]string {"url" : "https://test-api.k6.io /public/crocodiles2/" },
292
+ metricLabels : map [string ]string {"url" : httpsEndpoint + " /public/crocodiles2/" },
283
293
assertValue : equals (0 ),
284
294
},
285
295
{
286
296
name : "Total requests for a URL accessed once" ,
287
297
metricName : "probe_http_requests_total" ,
288
- metricLabels : map [string ]string {"url" : "https://test-api.k6.io /public/crocodiles/" },
298
+ metricLabels : map [string ]string {"url" : httpsEndpoint + " /public/crocodiles/" },
289
299
assertValue : equals (1 ),
290
300
},
291
301
{
292
302
name : "Total requests for a URL accessed twice" ,
293
303
metricName : "probe_http_requests_total" ,
294
- metricLabels : map [string ]string {"url" : "https://test-api.k6.io /public/crocodiles4/" },
304
+ metricLabels : map [string ]string {"url" : httpsEndpoint + " /public/crocodiles4/" },
295
305
assertValue : equals (2 ),
296
306
},
297
307
{
298
308
name : "HTTP requests failed rate" ,
299
309
metricName : "probe_http_requests_failed" ,
300
- metricLabels : map [string ]string {"url" : "https://test-api.k6.io /public/crocodiles4/" },
310
+ metricLabels : map [string ]string {"url" : httpsEndpoint + " /public/crocodiles4/" },
301
311
assertValue : equals (1 ),
302
312
},
303
313
{
304
314
name : "HTTP requests failed ttoal" ,
305
315
metricName : "probe_http_requests_failed_total" ,
306
- metricLabels : map [string ]string {"url" : "https://test-api.k6.io /public/crocodiles4/" },
316
+ metricLabels : map [string ]string {"url" : httpsEndpoint + " /public/crocodiles4/" },
307
317
assertValue : equals (2 ),
308
318
},
309
319
{
310
320
name : "HTTP version" ,
311
321
metricName : "probe_http_version" ,
312
- metricLabels : map [string ]string {"url" : "https://test-api.k6.io /public/crocodiles/" },
322
+ metricLabels : map [string ]string {"url" : httpsEndpoint + " /public/crocodiles/" },
313
323
assertValue : func (f float64 ) bool { return f >= 1.1 },
314
324
},
315
325
{
316
326
name : "TLS version label value" ,
317
327
metricName : "probe_http_info" ,
318
328
// Test for a paticular URL to avoid matching a failed request, which has no TLS version.
319
- metricLabels : map [string ]string {"tls_version" : "1.3" , "url" : "https://test-api.k6.io /public/crocodiles/" },
329
+ metricLabels : map [string ]string {"tls_version" : "1.3" , "url" : httpsEndpoint + " /public/crocodiles/" },
320
330
assertValue : any , // Just fail if not present.
321
331
},
322
332
} {
@@ -383,3 +393,32 @@ func nonZero(v float64) bool {
383
393
func any (float64 ) bool {
384
394
return true
385
395
}
396
+
397
+ // testHTTPServer starts an HTTP and HTTPS servers that can be used to perform requests to.
398
+ // The HTTP server redirects all requests to the HTTPS one.
399
+ func testHTTPServer (t * testing.T ) (string , string ) {
400
+ t .Helper ()
401
+
402
+ mux := http .NewServeMux ()
403
+ mux .HandleFunc ("/{$}" , func (rw http.ResponseWriter , _ * http.Request ) {
404
+ // `/{$}` matches exactly `/`, and not `/foo`.
405
+ _ , _ = rw .Write ([]byte ("Hello world!" ))
406
+ })
407
+ mux .HandleFunc ("/public/crocodiles/" , func (rw http.ResponseWriter , _ * http.Request ) {
408
+ _ , _ = rw .Write ([]byte ("One, two, three, croc!" ))
409
+ })
410
+ tlsServer := httptest .NewTLSServer (mux )
411
+ t .Cleanup (func () {
412
+ tlsServer .Close ()
413
+ })
414
+
415
+ plainServer := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
416
+ rw .Header ().Add ("Location" , tlsServer .URL + "/" + strings .TrimPrefix (r .URL .Path , "/" ))
417
+ rw .WriteHeader (http .StatusPermanentRedirect )
418
+ }))
419
+ t .Cleanup (func () {
420
+ plainServer .Close ()
421
+ })
422
+
423
+ return plainServer .URL , tlsServer .URL
424
+ }
0 commit comments