@@ -328,27 +328,46 @@ func TestMetricNotFound(t *testing.T) {
328
328
}
329
329
330
330
func TestScrapeAndCompare (t * testing.T ) {
331
- const expected = `
331
+ scenarios := map [string ]struct {
332
+ want string
333
+ metricNames []string
334
+ errPrefix string
335
+ fail bool
336
+ }{
337
+ "empty metric Names" : {
338
+ want : `
332
339
# HELP some_total A value that represents a counter.
333
340
# TYPE some_total counter
334
341
335
342
some_total{ label1 = "value1" } 1
336
- `
343
+ ` ,
344
+ metricNames : []string {},
345
+ },
346
+ "one metric" : {
347
+ want : `
348
+ # HELP some_total A value that represents a counter.
349
+ # TYPE some_total counter
337
350
338
- expectedReader := strings .NewReader (expected )
351
+ some_total{ label1 = "value1" } 1
352
+ ` ,
353
+ metricNames : []string {"some_total" },
354
+ },
355
+ "multiple expected" : {
356
+ want : `
357
+ # HELP some_total A value that represents a counter.
358
+ # TYPE some_total counter
339
359
340
- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
341
- fmt .Fprintln (w , expected )
342
- }))
343
- defer ts .Close ()
360
+ some_total{ label1 = "value1" } 1
344
361
345
- if err := ScrapeAndCompare (ts .URL , expectedReader , "some_total" ); err != nil {
346
- t .Errorf ("unexpected scraping result:\n %s" , err )
347
- }
348
- }
362
+ # HELP some_total2 A value that represents a counter.
363
+ # TYPE some_total2 counter
349
364
350
- func TestScrapeAndCompareWithMultipleExpected (t * testing.T ) {
351
- const expected = `
365
+ some_total2{ label2 = "value2" } 1
366
+ ` ,
367
+ metricNames : []string {"some_total2" },
368
+ },
369
+ "expected metric name is not scraped" : {
370
+ want : `
352
371
# HELP some_total A value that represents a counter.
353
372
# TYPE some_total counter
354
373
@@ -358,53 +377,80 @@ func TestScrapeAndCompareWithMultipleExpected(t *testing.T) {
358
377
# TYPE some_total2 counter
359
378
360
379
some_total2{ label2 = "value2" } 1
361
- `
362
-
363
- expectedReader := strings .NewReader (expected )
380
+ ` ,
381
+ metricNames : []string {"some_total3" },
382
+ errPrefix : "expected metrics name not found" ,
383
+ fail : true ,
384
+ },
385
+ "one of multiple expected metric names is not scraped" : {
386
+ want : `
387
+ # HELP some_total A value that represents a counter.
388
+ # TYPE some_total counter
364
389
365
- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
366
- fmt .Fprintln (w , expected )
367
- }))
368
- defer ts .Close ()
390
+ some_total{ label1 = "value1" } 1
369
391
370
- if err := ScrapeAndCompare (ts .URL , expectedReader , "some_total2" ); err != nil {
371
- t .Errorf ("unexpected scraping result:\n %s" , err )
372
- }
373
- }
392
+ # HELP some_total2 A value that represents a counter.
393
+ # TYPE some_total2 counter
374
394
375
- func TestScrapeAndCompareFetchingFail (t * testing.T ) {
376
- err := ScrapeAndCompare ("some_url" , strings .NewReader ("some expectation" ), "some_total" )
377
- if err == nil {
378
- t .Errorf ("expected an error but got nil" )
395
+ some_total2{ label2 = "value2" } 1
396
+ ` ,
397
+ metricNames : []string {"some_total1" , "some_total3" },
398
+ errPrefix : "expected metrics name not found" ,
399
+ fail : true ,
400
+ },
379
401
}
380
- if ! strings .HasPrefix (err .Error (), "scraping metrics failed" ) {
381
- t .Errorf ("unexpected error happened: %s" , err )
402
+ for name , scenario := range scenarios {
403
+ t .Run (name , func (t * testing.T ) {
404
+ expectedReader := strings .NewReader (scenario .want )
405
+
406
+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
407
+ fmt .Fprintln (w , scenario .want )
408
+ }))
409
+ defer ts .Close ()
410
+ if err := ScrapeAndCompare (ts .URL , expectedReader , scenario .metricNames ... ); err != nil {
411
+ if ! scenario .fail || ! strings .HasPrefix (err .Error (), scenario .errPrefix ) {
412
+ t .Errorf ("unexpected error happened: %s" , err )
413
+ }
414
+ } else if scenario .fail {
415
+ t .Errorf ("expected an error but got nil" )
416
+ }
417
+ })
382
418
}
383
- }
384
419
385
- func TestScrapeAndCompareBadStatusCode (t * testing.T ) {
386
- const expected = `
420
+ t .Run ("fetching fail" , func (t * testing.T ) {
421
+ err := ScrapeAndCompare ("some_url" , strings .NewReader ("some expectation" ), "some_total" )
422
+ if err == nil {
423
+ t .Errorf ("expected an error but got nil" )
424
+ }
425
+ if ! strings .HasPrefix (err .Error (), "scraping metrics failed" ) {
426
+ t .Errorf ("unexpected error happened: %s" , err )
427
+ }
428
+ })
429
+
430
+ t .Run ("bad status code" , func (t * testing.T ) {
431
+ const expected = `
387
432
# HELP some_total A value that represents a counter.
388
433
# TYPE some_total counter
389
434
390
435
some_total{ label1 = "value1" } 1
391
436
`
392
437
393
- expectedReader := strings .NewReader (expected )
438
+ expectedReader := strings .NewReader (expected )
394
439
395
- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
396
- w .WriteHeader (http .StatusBadGateway )
397
- fmt .Fprintln (w , expected )
398
- }))
399
- defer ts .Close ()
440
+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
441
+ w .WriteHeader (http .StatusBadGateway )
442
+ fmt .Fprintln (w , expected )
443
+ }))
444
+ defer ts .Close ()
400
445
401
- err := ScrapeAndCompare (ts .URL , expectedReader , "some_total" )
402
- if err == nil {
403
- t .Errorf ("expected an error but got nil" )
404
- }
405
- if ! strings .HasPrefix (err .Error (), "the scraping target returned a status code other than 200" ) {
406
- t .Errorf ("unexpected error happened: %s" , err )
407
- }
446
+ err := ScrapeAndCompare (ts .URL , expectedReader , "some_total" )
447
+ if err == nil {
448
+ t .Errorf ("expected an error but got nil" )
449
+ }
450
+ if ! strings .HasPrefix (err .Error (), "the scraping target returned a status code other than 200" ) {
451
+ t .Errorf ("unexpected error happened: %s" , err )
452
+ }
453
+ })
408
454
}
409
455
410
456
func TestCollectAndCount (t * testing.T ) {
0 commit comments