@@ -328,46 +328,27 @@ func TestMetricNotFound(t *testing.T) {
328328}
329329
330330func  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  =  ` 
339332		# HELP some_total A value that represents a counter. 
340333		# TYPE some_total counter 
341334
342335		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+ 	` 
350337
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 )
359339
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 ()
361344
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+ }
364349
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  =  ` 
371352		# HELP some_total A value that represents a counter. 
372353		# TYPE some_total counter 
373354
@@ -377,78 +358,53 @@ func TestScrapeAndCompare(t *testing.T) {
377358		# TYPE some_total2 counter 
378359
379360		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+ 	` 
388362
389- 		some_total{ label1 = "value1" } 1  
363+ 	expectedReader   :=   strings . NewReader ( expected ) 
390364
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 ()
393369
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 )
416372	}
373+ }
417374
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+ } 
427384
428- 	 t . Run ( "bad status code" ,  func (t  * testing.T ) {
429- 		 const  expected  =  ` 
385+ func   TestScrapeAndCompareBadStatusCode (t  * testing.T ) {
386+ 	const  expected  =  ` 
430387		# HELP some_total A value that represents a counter. 
431388		# TYPE some_total counter 
432389
433390		some_total{ label1 = "value1" } 1 
434391	` 
435392
436- 		 expectedReader  :=  strings .NewReader (expected )
393+ 	expectedReader  :=  strings .NewReader (expected )
437394
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 ()
443400
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+ 	}
452408}
453409
454410func  TestCollectAndCount (t  * testing.T ) {
0 commit comments