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