Refactor scrape tests to use testify.

Signed-off-by: Paweł Szulik <paul.szulik@gmail.com>
This commit is contained in:
Paweł Szulik 2021-09-04 14:35:03 +02:00 committed by Bryan Boreham
parent 6e9cca8158
commit b0c538787d
3 changed files with 98 additions and 186 deletions

View file

@ -458,9 +458,9 @@ func loadConfiguration(t testing.TB, c string) *config.Config {
t.Helper() t.Helper()
cfg := &config.Config{} cfg := &config.Config{}
if err := yaml.UnmarshalStrict([]byte(c), cfg); err != nil { err := yaml.UnmarshalStrict([]byte(c), cfg)
t.Fatalf("Unable to load YAML config: %s", err) require.NoError(t, err, "Unable to load YAML config.")
}
return cfg return cfg
} }
@ -533,42 +533,38 @@ scrape_configs:
} }
// Apply the initial configuration. // Apply the initial configuration.
if err := scrapeManager.ApplyConfig(cfg1); err != nil { err = scrapeManager.ApplyConfig(cfg1)
t.Fatalf("unable to apply configuration: %s", err) require.NoError(t, err, "Unable to apply configuration.")
}
select { select {
case <-ch: case <-ch:
t.Fatal("reload happened") require.FailNow(t, "Reload happened.")
default: default:
} }
// Apply a configuration for which the reload fails. // Apply a configuration for which the reload fails.
if err := scrapeManager.ApplyConfig(cfg2); err == nil { err = scrapeManager.ApplyConfig(cfg2)
t.Fatalf("expecting error but got none") require.Error(t, err, "Expecting error but got none.")
}
select { select {
case <-ch: case <-ch:
t.Fatal("reload happened") require.FailNow(t, "Reload happened.")
default: default:
} }
// Apply a configuration for which the reload succeeds. // Apply a configuration for which the reload succeeds.
if err := scrapeManager.ApplyConfig(cfg3); err != nil { err = scrapeManager.ApplyConfig(cfg3)
t.Fatalf("unable to apply configuration: %s", err) require.NoError(t, err, "Unable to apply configuration.")
}
select { select {
case <-ch: case <-ch:
default: default:
t.Fatal("reload didn't happen") require.FailNow(t, "Reload didn't happen.")
} }
// Re-applying the same configuration shouldn't trigger a reload. // Re-applying the same configuration shouldn't trigger a reload.
if err := scrapeManager.ApplyConfig(cfg3); err != nil { err = scrapeManager.ApplyConfig(cfg3)
t.Fatalf("unable to apply configuration: %s", err) require.NoError(t, err, "Unable to apply configuration.")
}
select { select {
case <-ch: case <-ch:
t.Fatal("reload happened") require.FailNow(t, "Reload happened.")
default: default:
} }
} }
@ -595,7 +591,7 @@ func TestManagerTargetsUpdates(t *testing.T) {
select { select {
case ts <- tgSent: case ts <- tgSent:
case <-time.After(10 * time.Millisecond): case <-time.After(10 * time.Millisecond):
t.Error("Scrape manager's channel remained blocked after the set threshold.") require.Fail(t, "Scrape manager's channel remained blocked after the set threshold.")
} }
} }
@ -609,7 +605,7 @@ func TestManagerTargetsUpdates(t *testing.T) {
select { select {
case <-m.triggerReload: case <-m.triggerReload:
default: default:
t.Error("No scrape loops reload was triggered after targets update.") require.Fail(t, "No scrape loops reload was triggered after targets update.")
} }
} }
@ -622,9 +618,8 @@ global:
` `
cfg := &config.Config{} cfg := &config.Config{}
if err := yaml.UnmarshalStrict([]byte(cfgText), cfg); err != nil { err := yaml.UnmarshalStrict([]byte(cfgText), cfg)
t.Fatalf("Unable to load YAML config cfgYaml: %s", err) require.NoError(t, err, "Unable to load YAML config cfgYaml.")
}
return cfg return cfg
} }
@ -636,25 +631,18 @@ global:
// Load the first config. // Load the first config.
cfg1 := getConfig("ha1") cfg1 := getConfig("ha1")
if err := scrapeManager.setOffsetSeed(cfg1.GlobalConfig.ExternalLabels); err != nil { err = scrapeManager.setOffsetSeed(cfg1.GlobalConfig.ExternalLabels)
t.Error(err) require.NoError(t, err)
}
offsetSeed1 := scrapeManager.offsetSeed offsetSeed1 := scrapeManager.offsetSeed
if offsetSeed1 == 0 { require.NotZero(t, offsetSeed1, "Offset seed has to be a hash of uint64.")
t.Error("Offset seed has to be a hash of uint64")
}
// Load the first config. // Load the first config.
cfg2 := getConfig("ha2") cfg2 := getConfig("ha2")
if err := scrapeManager.setOffsetSeed(cfg2.GlobalConfig.ExternalLabels); err != nil { require.NoError(t, scrapeManager.setOffsetSeed(cfg2.GlobalConfig.ExternalLabels))
t.Error(err)
}
offsetSeed2 := scrapeManager.offsetSeed offsetSeed2 := scrapeManager.offsetSeed
if offsetSeed1 == offsetSeed2 { require.NotEqual(t, offsetSeed1, offsetSeed2, "Offset seed should not be the same on different set of external labels.")
t.Error("Offset seed should not be the same on different set of external labels")
}
} }
func TestManagerScrapePools(t *testing.T) { func TestManagerScrapePools(t *testing.T) {

View file

@ -72,15 +72,11 @@ func TestNewScrapePool(t *testing.T) {
sp, _ = newScrapePool(cfg, app, 0, nil, nil, &Options{}, newTestScrapeMetrics(t)) sp, _ = newScrapePool(cfg, app, 0, nil, nil, &Options{}, newTestScrapeMetrics(t))
) )
if a, ok := sp.appendable.(*nopAppendable); !ok || a != app { a, ok := sp.appendable.(*nopAppendable)
t.Fatalf("Wrong sample appender") require.True(t, ok, "Failure to append.")
} require.Equal(t, app, a, "Wrong sample appender.")
if sp.config != cfg { require.Equal(t, cfg, sp.config, "Wrong scrape config.")
t.Fatalf("Wrong scrape config") require.NotNil(t, sp.newLoop, "newLoop function not initialized.")
}
if sp.newLoop == nil {
t.Fatalf("newLoop function not initialized")
}
} }
func TestDroppedTargetsList(t *testing.T) { func TestDroppedTargetsList(t *testing.T) {
@ -233,12 +229,10 @@ func TestScrapePoolStop(t *testing.T) {
select { select {
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Fatalf("scrapeLoop.stop() did not return as expected") require.Fail(t, "scrapeLoop.stop() did not return as expected")
case <-done: case <-done:
// This should have taken at least as long as the last target slept. // This should have taken at least as long as the last target slept.
if time.Since(stopTime) < time.Duration(numTargets*20)*time.Millisecond { require.GreaterOrEqual(t, time.Since(stopTime), time.Duration(numTargets*20)*time.Millisecond, "scrapeLoop.stop() exited before all targets stopped")
t.Fatalf("scrapeLoop.stop() exited before all targets stopped")
}
} }
mtx.Lock() mtx.Lock()
@ -324,12 +318,10 @@ func TestScrapePoolReload(t *testing.T) {
select { select {
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Fatalf("scrapeLoop.reload() did not return as expected") require.FailNow(t, "scrapeLoop.reload() did not return as expected")
case <-done: case <-done:
// This should have taken at least as long as the last target slept. // This should have taken at least as long as the last target slept.
if time.Since(reloadTime) < time.Duration(numTargets*20)*time.Millisecond { require.GreaterOrEqual(t, time.Since(reloadTime), time.Duration(numTargets*20)*time.Millisecond, "scrapeLoop.stop() exited before all targets stopped")
t.Fatalf("scrapeLoop.stop() exited before all targets stopped")
}
} }
mtx.Lock() mtx.Lock()
@ -703,13 +695,13 @@ func TestScrapeLoopStopBeforeRun(t *testing.T) {
select { select {
case <-stopDone: case <-stopDone:
t.Fatalf("Stopping terminated before run exited successfully") require.FailNow(t, "Stopping terminated before run exited successfully.")
case <-time.After(500 * time.Millisecond): case <-time.After(500 * time.Millisecond):
} }
// Running the scrape loop must exit before calling the scraper even once. // Running the scrape loop must exit before calling the scraper even once.
scraper.scrapeFunc = func(context.Context, io.Writer) error { scraper.scrapeFunc = func(context.Context, io.Writer) error {
t.Fatalf("scraper was called for terminated scrape loop") require.FailNow(t, "Scraper was called for terminated scrape loop.")
return nil return nil
} }
@ -722,13 +714,13 @@ func TestScrapeLoopStopBeforeRun(t *testing.T) {
select { select {
case <-runDone: case <-runDone:
case <-time.After(1 * time.Second): case <-time.After(1 * time.Second):
t.Fatalf("Running terminated scrape loop did not exit") require.FailNow(t, "Running terminated scrape loop did not exit.")
} }
select { select {
case <-stopDone: case <-stopDone:
case <-time.After(1 * time.Second): case <-time.After(1 * time.Second):
t.Fatalf("Stopping did not terminate after running exited") require.FailNow(t, "Stopping did not terminate after running exited.")
} }
} }
@ -765,14 +757,13 @@ func TestScrapeLoopStop(t *testing.T) {
select { select {
case <-signal: case <-signal:
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Fatalf("Scrape wasn't stopped.") require.FailNow(t, "Scrape wasn't stopped.")
} }
// We expected 1 actual sample for each scrape plus 5 for report samples. // We expected 1 actual sample for each scrape plus 5 for report samples.
// At least 2 scrapes were made, plus the final stale markers. // At least 2 scrapes were made, plus the final stale markers.
if len(appender.resultFloats) < 6*3 || len(appender.resultFloats)%6 != 0 { require.GreaterOrEqual(t, len(appender.resultFloats), 6*3, "Expected at least 3 scrapes with 6 samples each.")
t.Fatalf("Expected at least 3 scrapes with 6 samples each, got %d samples", len(appender.resultFloats)) require.Zero(t, len(appender.resultFloats)%6, "There is a scrape with missing samples.")
}
// All samples in a scrape must have the same timestamp. // All samples in a scrape must have the same timestamp.
var ts int64 var ts int64
for i, s := range appender.resultFloats { for i, s := range appender.resultFloats {
@ -785,9 +776,7 @@ func TestScrapeLoopStop(t *testing.T) {
} }
// All samples from the last scrape must be stale markers. // All samples from the last scrape must be stale markers.
for _, s := range appender.resultFloats[len(appender.resultFloats)-5:] { for _, s := range appender.resultFloats[len(appender.resultFloats)-5:] {
if !value.IsStaleNaN(s.f) { require.True(t, value.IsStaleNaN(s.f), "Appended last sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(s.f))
t.Fatalf("Appended last sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(s.f))
}
} }
} }
@ -843,9 +832,9 @@ func TestScrapeLoopRun(t *testing.T) {
select { select {
case <-signal: case <-signal:
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Fatalf("Cancellation during initial offset failed") require.FailNow(t, "Cancellation during initial offset failed.")
case err := <-errc: case err := <-errc:
t.Fatalf("Unexpected error: %s", err) require.FailNow(t, "Unexpected error: %s", err)
} }
// The provided timeout must cause cancellation of the context passed down to the // The provided timeout must cause cancellation of the context passed down to the
@ -873,11 +862,9 @@ func TestScrapeLoopRun(t *testing.T) {
select { select {
case err := <-errc: case err := <-errc:
if !errors.Is(err, context.DeadlineExceeded) { require.ErrorIs(t, err, context.DeadlineExceeded)
t.Fatalf("Expected timeout error but got: %s", err)
}
case <-time.After(3 * time.Second): case <-time.After(3 * time.Second):
t.Fatalf("Expected timeout error but got none") require.FailNow(t, "Expected timeout error but got none.")
} }
// We already caught the timeout error and are certainly in the loop. // We already caught the timeout error and are certainly in the loop.
@ -890,9 +877,9 @@ func TestScrapeLoopRun(t *testing.T) {
case <-signal: case <-signal:
// Loop terminated as expected. // Loop terminated as expected.
case err := <-errc: case err := <-errc:
t.Fatalf("Unexpected error: %s", err) require.FailNow(t, "Unexpected error: %s", err)
case <-time.After(3 * time.Second): case <-time.After(3 * time.Second):
t.Fatalf("Loop did not terminate on context cancellation") require.FailNow(t, "Loop did not terminate on context cancellation")
} }
} }
@ -912,7 +899,7 @@ func TestScrapeLoopForcedErr(t *testing.T) {
sl.setForcedError(forcedErr) sl.setForcedError(forcedErr)
scraper.scrapeFunc = func(context.Context, io.Writer) error { scraper.scrapeFunc = func(context.Context, io.Writer) error {
t.Fatalf("should not be scraped") require.FailNow(t, "Should not be scraped.")
return nil return nil
} }
@ -923,18 +910,16 @@ func TestScrapeLoopForcedErr(t *testing.T) {
select { select {
case err := <-errc: case err := <-errc:
if !errors.Is(err, forcedErr) { require.ErrorIs(t, err, forcedErr)
t.Fatalf("Expected forced error but got: %s", err)
}
case <-time.After(3 * time.Second): case <-time.After(3 * time.Second):
t.Fatalf("Expected forced error but got none") require.FailNow(t, "Expected forced error but got none.")
} }
cancel() cancel()
select { select {
case <-signal: case <-signal:
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Fatalf("Scrape not stopped") require.FailNow(t, "Scrape not stopped.")
} }
} }
@ -1141,7 +1126,7 @@ func TestScrapeLoopRunCreatesStaleMarkersOnFailedScrape(t *testing.T) {
select { select {
case <-signal: case <-signal:
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Fatalf("Scrape wasn't stopped.") require.FailNow(t, "Scrape wasn't stopped.")
} }
// 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for // 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for
@ -1188,7 +1173,7 @@ func TestScrapeLoopRunCreatesStaleMarkersOnParseFailure(t *testing.T) {
select { select {
case <-signal: case <-signal:
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Fatalf("Scrape wasn't stopped.") require.FailNow(t, "Scrape wasn't stopped.")
} }
// 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for // 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for
@ -1220,19 +1205,15 @@ func TestScrapeLoopCache(t *testing.T) {
scraper.scrapeFunc = func(ctx context.Context, w io.Writer) error { scraper.scrapeFunc = func(ctx context.Context, w io.Writer) error {
switch numScrapes { switch numScrapes {
case 1, 2: case 1, 2:
if _, ok := sl.cache.series["metric_a"]; !ok { _, ok := sl.cache.series["metric_a"]
t.Errorf("metric_a missing from cache after scrape %d", numScrapes) require.True(t, ok, "metric_a missing from cache after scrape %d", numScrapes)
} _, ok = sl.cache.series["metric_b"]
if _, ok := sl.cache.series["metric_b"]; !ok { require.True(t, ok, "metric_b missing from cache after scrape %d", numScrapes)
t.Errorf("metric_b missing from cache after scrape %d", numScrapes)
}
case 3: case 3:
if _, ok := sl.cache.series["metric_a"]; !ok { _, ok := sl.cache.series["metric_a"]
t.Errorf("metric_a missing from cache after scrape %d", numScrapes) require.True(t, ok, "metric_a missing from cache after scrape %d", numScrapes)
} _, ok = sl.cache.series["metric_b"]
if _, ok := sl.cache.series["metric_b"]; ok { require.False(t, ok, "metric_b present in cache after scrape %d", numScrapes)
t.Errorf("metric_b present in cache after scrape %d", numScrapes)
}
} }
numScrapes++ numScrapes++
@ -1257,7 +1238,7 @@ func TestScrapeLoopCache(t *testing.T) {
select { select {
case <-signal: case <-signal:
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Fatalf("Scrape wasn't stopped.") require.FailNow(t, "Scrape wasn't stopped.")
} }
// 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for // 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for
@ -1305,12 +1286,10 @@ func TestScrapeLoopCacheMemoryExhaustionProtection(t *testing.T) {
select { select {
case <-signal: case <-signal:
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Fatalf("Scrape wasn't stopped.") require.FailNow(t, "Scrape wasn't stopped.")
} }
if len(sl.cache.series) > 2000 { require.LessOrEqual(t, len(sl.cache.series), 2000, "More than 2000 series cached.")
t.Fatalf("More than 2000 series cached. Got: %d", len(sl.cache.series))
}
} }
func TestScrapeLoopAppend(t *testing.T) { func TestScrapeLoopAppend(t *testing.T) {
@ -1541,9 +1520,7 @@ func TestScrapeLoopAppendSampleLimit(t *testing.T) {
now := time.Now() now := time.Now()
slApp := sl.appender(context.Background()) slApp := sl.appender(context.Background())
total, added, seriesAdded, err := sl.append(app, []byte("metric_a 1\nmetric_b 1\nmetric_c 1\n"), "", now) total, added, seriesAdded, err := sl.append(app, []byte("metric_a 1\nmetric_b 1\nmetric_c 1\n"), "", now)
if !errors.Is(err, errSampleLimit) { require.ErrorIs(t, err, errSampleLimit)
t.Fatalf("Did not see expected sample limit error: %s", err)
}
require.NoError(t, slApp.Rollback()) require.NoError(t, slApp.Rollback())
require.Equal(t, 3, total) require.Equal(t, 3, total)
require.Equal(t, 3, added) require.Equal(t, 3, added)
@ -1572,9 +1549,7 @@ func TestScrapeLoopAppendSampleLimit(t *testing.T) {
now = time.Now() now = time.Now()
slApp = sl.appender(context.Background()) slApp = sl.appender(context.Background())
total, added, seriesAdded, err = sl.append(slApp, []byte("metric_a 1\nmetric_b 1\nmetric_c{deleteme=\"yes\"} 1\nmetric_d 1\nmetric_e 1\nmetric_f 1\nmetric_g 1\nmetric_h{deleteme=\"yes\"} 1\nmetric_i{deleteme=\"yes\"} 1\n"), "", now) total, added, seriesAdded, err = sl.append(slApp, []byte("metric_a 1\nmetric_b 1\nmetric_c{deleteme=\"yes\"} 1\nmetric_d 1\nmetric_e 1\nmetric_f 1\nmetric_g 1\nmetric_h{deleteme=\"yes\"} 1\nmetric_i{deleteme=\"yes\"} 1\n"), "", now)
if !errors.Is(err, errSampleLimit) { require.ErrorIs(t, err, errSampleLimit)
t.Fatalf("Did not see expected sample limit error: %s", err)
}
require.NoError(t, slApp.Rollback()) require.NoError(t, slApp.Rollback())
require.Equal(t, 9, total) require.Equal(t, 9, total)
require.Equal(t, 6, added) require.Equal(t, 6, added)
@ -2357,15 +2332,12 @@ func TestTargetScraperScrapeOK(t *testing.T) {
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if protobufParsing { if protobufParsing {
accept := r.Header.Get("Accept") accept := r.Header.Get("Accept")
if !strings.HasPrefix(accept, "application/vnd.google.protobuf;") { require.True(t, strings.HasPrefix(accept, "application/vnd.google.protobuf;"),
t.Errorf("Expected Accept header to prefer application/vnd.google.protobuf, got %q", accept) "Expected Accept header to prefer application/vnd.google.protobuf.")
}
} }
timeout := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds") timeout := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds")
if timeout != expectedTimeout { require.Equal(t, expectedTimeout, timeout, "Expected scrape timeout header.")
t.Errorf("Expected scrape timeout header %q, got %q", expectedTimeout, timeout)
}
w.Header().Set("Content-Type", `text/plain; version=0.0.4`) w.Header().Set("Content-Type", `text/plain; version=0.0.4`)
w.Write([]byte("metric_a 1\nmetric_b 2\n")) w.Write([]byte("metric_a 1\nmetric_b 2\n"))
@ -2453,7 +2425,7 @@ func TestTargetScrapeScrapeCancel(t *testing.T) {
select { select {
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Fatalf("Scrape function did not return unexpectedly") require.FailNow(t, "Scrape function did not return unexpectedly.")
case err := <-errc: case err := <-errc:
require.NoError(t, err) require.NoError(t, err)
} }
@ -3053,7 +3025,7 @@ func TestScrapeReportSingleAppender(t *testing.T) {
select { select {
case <-signal: case <-signal:
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Fatalf("Scrape wasn't stopped.") require.FailNow(t, "Scrape wasn't stopped.")
} }
} }

View file

@ -77,9 +77,7 @@ func TestTargetOffset(t *testing.T) {
buckets := make([]int, interval/bucketSize) buckets := make([]int, interval/bucketSize)
for _, offset := range offsets { for _, offset := range offsets {
if offset < 0 || offset >= interval { require.InDelta(t, time.Duration(0), offset, float64(interval), "Offset %v out of bounds.", offset)
t.Fatalf("Offset %v out of bounds", offset)
}
bucket := offset / bucketSize bucket := offset / bucketSize
buckets[bucket]++ buckets[bucket]++
@ -98,9 +96,7 @@ func TestTargetOffset(t *testing.T) {
diff = -diff diff = -diff
} }
if float64(diff)/float64(avg) > tolerance { require.LessOrEqual(t, float64(diff)/float64(avg), tolerance, "Bucket out of tolerance bounds.")
t.Fatalf("Bucket out of tolerance bounds")
}
} }
} }
@ -150,9 +146,7 @@ func TestNewHTTPBearerToken(t *testing.T) {
func(w http.ResponseWriter, r *http.Request) { func(w http.ResponseWriter, r *http.Request) {
expected := "Bearer 1234" expected := "Bearer 1234"
received := r.Header.Get("Authorization") received := r.Header.Get("Authorization")
if expected != received { require.Equal(t, expected, received, "Authorization header was not set correctly.")
t.Fatalf("Authorization header was not set correctly: expected '%v', got '%v'", expected, received)
}
}, },
), ),
) )
@ -162,13 +156,9 @@ func TestNewHTTPBearerToken(t *testing.T) {
BearerToken: "1234", BearerToken: "1234",
} }
c, err := config_util.NewClientFromConfig(cfg, "test") c, err := config_util.NewClientFromConfig(cfg, "test")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
_, err = c.Get(server.URL) _, err = c.Get(server.URL)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
} }
func TestNewHTTPBearerTokenFile(t *testing.T) { func TestNewHTTPBearerTokenFile(t *testing.T) {
@ -177,9 +167,7 @@ func TestNewHTTPBearerTokenFile(t *testing.T) {
func(w http.ResponseWriter, r *http.Request) { func(w http.ResponseWriter, r *http.Request) {
expected := "Bearer 12345" expected := "Bearer 12345"
received := r.Header.Get("Authorization") received := r.Header.Get("Authorization")
if expected != received { require.Equal(t, expected, received, "Authorization header was not set correctly.")
t.Fatalf("Authorization header was not set correctly: expected '%v', got '%v'", expected, received)
}
}, },
), ),
) )
@ -189,13 +177,9 @@ func TestNewHTTPBearerTokenFile(t *testing.T) {
BearerTokenFile: "testdata/bearertoken.txt", BearerTokenFile: "testdata/bearertoken.txt",
} }
c, err := config_util.NewClientFromConfig(cfg, "test") c, err := config_util.NewClientFromConfig(cfg, "test")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
_, err = c.Get(server.URL) _, err = c.Get(server.URL)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
} }
func TestNewHTTPBasicAuth(t *testing.T) { func TestNewHTTPBasicAuth(t *testing.T) {
@ -203,9 +187,9 @@ func TestNewHTTPBasicAuth(t *testing.T) {
http.HandlerFunc( http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) { func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth() username, password, ok := r.BasicAuth()
if !(ok && username == "user" && password == "password123") { require.True(t, ok, "Basic authorization header was not set correctly.")
t.Fatalf("Basic authorization header was not set correctly: expected '%v:%v', got '%v:%v'", "user", "password123", username, password) require.Equal(t, "user", username)
} require.Equal(t, "password123", password)
}, },
), ),
) )
@ -218,13 +202,9 @@ func TestNewHTTPBasicAuth(t *testing.T) {
}, },
} }
c, err := config_util.NewClientFromConfig(cfg, "test") c, err := config_util.NewClientFromConfig(cfg, "test")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
_, err = c.Get(server.URL) _, err = c.Get(server.URL)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
} }
func TestNewHTTPCACert(t *testing.T) { func TestNewHTTPCACert(t *testing.T) {
@ -246,13 +226,9 @@ func TestNewHTTPCACert(t *testing.T) {
}, },
} }
c, err := config_util.NewClientFromConfig(cfg, "test") c, err := config_util.NewClientFromConfig(cfg, "test")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
_, err = c.Get(server.URL) _, err = c.Get(server.URL)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
} }
func TestNewHTTPClientCert(t *testing.T) { func TestNewHTTPClientCert(t *testing.T) {
@ -279,13 +255,9 @@ func TestNewHTTPClientCert(t *testing.T) {
}, },
} }
c, err := config_util.NewClientFromConfig(cfg, "test") c, err := config_util.NewClientFromConfig(cfg, "test")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
_, err = c.Get(server.URL) _, err = c.Get(server.URL)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
} }
func TestNewHTTPWithServerName(t *testing.T) { func TestNewHTTPWithServerName(t *testing.T) {
@ -308,13 +280,9 @@ func TestNewHTTPWithServerName(t *testing.T) {
}, },
} }
c, err := config_util.NewClientFromConfig(cfg, "test") c, err := config_util.NewClientFromConfig(cfg, "test")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
_, err = c.Get(server.URL) _, err = c.Get(server.URL)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
} }
func TestNewHTTPWithBadServerName(t *testing.T) { func TestNewHTTPWithBadServerName(t *testing.T) {
@ -337,31 +305,23 @@ func TestNewHTTPWithBadServerName(t *testing.T) {
}, },
} }
c, err := config_util.NewClientFromConfig(cfg, "test") c, err := config_util.NewClientFromConfig(cfg, "test")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
_, err = c.Get(server.URL) _, err = c.Get(server.URL)
if err == nil { require.Error(t, err)
t.Fatal("Expected error, got nil.")
}
} }
func newTLSConfig(certName string, t *testing.T) *tls.Config { func newTLSConfig(certName string, t *testing.T) *tls.Config {
tlsConfig := &tls.Config{} tlsConfig := &tls.Config{}
caCertPool := x509.NewCertPool() caCertPool := x509.NewCertPool()
caCert, err := os.ReadFile(caCertPath) caCert, err := os.ReadFile(caCertPath)
if err != nil { require.NoError(t, err, "Couldn't read CA cert.")
t.Fatalf("Couldn't set up TLS server: %v", err)
}
caCertPool.AppendCertsFromPEM(caCert) caCertPool.AppendCertsFromPEM(caCert)
tlsConfig.RootCAs = caCertPool tlsConfig.RootCAs = caCertPool
tlsConfig.ServerName = "127.0.0.1" tlsConfig.ServerName = "127.0.0.1"
certPath := fmt.Sprintf("testdata/%s.cer", certName) certPath := fmt.Sprintf("testdata/%s.cer", certName)
keyPath := fmt.Sprintf("testdata/%s.key", certName) keyPath := fmt.Sprintf("testdata/%s.key", certName)
cert, err := tls.LoadX509KeyPair(certPath, keyPath) cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil { require.NoError(t, err, "Unable to use specified server cert (%s) & key (%v).", certPath, keyPath)
t.Errorf("Unable to use specified server cert (%s) & key (%v): %s", certPath, keyPath, err)
}
tlsConfig.Certificates = []tls.Certificate{cert} tlsConfig.Certificates = []tls.Certificate{cert}
return tlsConfig return tlsConfig
} }
@ -375,9 +335,7 @@ func TestNewClientWithBadTLSConfig(t *testing.T) {
}, },
} }
_, err := config_util.NewClientFromConfig(cfg, "test") _, err := config_util.NewClientFromConfig(cfg, "test")
if err == nil { require.Error(t, err)
t.Fatalf("Expected error, got nil.")
}
} }
func TestTargetsFromGroup(t *testing.T) { func TestTargetsFromGroup(t *testing.T) {
@ -389,15 +347,9 @@ func TestTargetsFromGroup(t *testing.T) {
} }
lb := labels.NewBuilder(labels.EmptyLabels()) lb := labels.NewBuilder(labels.EmptyLabels())
targets, failures := TargetsFromGroup(&targetgroup.Group{Targets: []model.LabelSet{{}, {model.AddressLabel: "localhost:9090"}}}, &cfg, false, nil, lb) targets, failures := TargetsFromGroup(&targetgroup.Group{Targets: []model.LabelSet{{}, {model.AddressLabel: "localhost:9090"}}}, &cfg, false, nil, lb)
if len(targets) != 1 { require.Len(t, targets, 1)
t.Fatalf("Expected 1 target, got %v", len(targets)) require.Len(t, failures, 1)
} require.EqualError(t, failures[0], expectedError)
if len(failures) != 1 {
t.Fatalf("Expected 1 failure, got %v", len(failures))
}
if failures[0].Error() != expectedError {
t.Fatalf("Expected error %s, got %s", expectedError, failures[0])
}
} }
func BenchmarkTargetsFromGroup(b *testing.B) { func BenchmarkTargetsFromGroup(b *testing.B) {