mirror of
https://github.com/prometheus/prometheus.git
synced 2025-02-21 03:16:00 -08:00
Refactor web tests to use testify.
Signed-off-by: Paweł Szulik <paul.szulik@gmail.com>
This commit is contained in:
parent
d5eb636a89
commit
6e9cca8158
|
@ -216,18 +216,11 @@ type rulesRetrieverMock struct {
|
|||
|
||||
func (m *rulesRetrieverMock) CreateAlertingRules() {
|
||||
expr1, err := parser.ParseExpr(`absent(test_metric3) != 1`)
|
||||
if err != nil {
|
||||
m.testing.Fatalf("unable to parse alert expression: %s", err)
|
||||
}
|
||||
require.NoError(m.testing, err)
|
||||
expr2, err := parser.ParseExpr(`up == 1`)
|
||||
if err != nil {
|
||||
m.testing.Fatalf("Unable to parse alert expression: %s", err)
|
||||
}
|
||||
|
||||
require.NoError(m.testing, err)
|
||||
expr3, err := parser.ParseExpr(`vector(1)`)
|
||||
if err != nil {
|
||||
m.testing.Fatalf("Unable to parse alert expression: %s", err)
|
||||
}
|
||||
require.NoError(m.testing, err)
|
||||
|
||||
rule1 := rules.NewAlertingRule(
|
||||
"test_metric3",
|
||||
|
@ -302,9 +295,7 @@ func (m *rulesRetrieverMock) CreateRuleGroups() {
|
|||
}
|
||||
|
||||
recordingExpr, err := parser.ParseExpr(`vector(1)`)
|
||||
if err != nil {
|
||||
m.testing.Fatalf("unable to parse alert expression: %s", err)
|
||||
}
|
||||
require.NoError(m.testing, err, "unable to parse alert expression: %s", err)
|
||||
recordingRule := rules.NewRecordingRule("recording-rule-1", recordingExpr, labels.Labels{})
|
||||
r = append(r, recordingRule)
|
||||
|
||||
|
@ -714,9 +705,7 @@ func TestQueryExemplars(t *testing.T) {
|
|||
for _, te := range tc.exemplars {
|
||||
for _, e := range te.Exemplars {
|
||||
_, err := es.AppendExemplar(0, te.SeriesLabels, e)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2832,9 +2821,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
|
|||
}
|
||||
|
||||
req, err := request(method, test.query)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
tr.ResetMetadataStore()
|
||||
for _, tm := range test.metadata {
|
||||
|
@ -2844,9 +2831,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
|
|||
for _, te := range test.exemplars {
|
||||
for _, e := range te.Exemplars {
|
||||
_, err := es.AppendExemplar(0, te.SeriesLabels, e)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2882,17 +2867,11 @@ func describeAPIFunc(f apiFunc) string {
|
|||
func assertAPIError(t *testing.T, got *apiError, exp errorType) {
|
||||
t.Helper()
|
||||
|
||||
if got != nil {
|
||||
if exp == errorNone {
|
||||
t.Fatalf("Unexpected error: %s", got)
|
||||
}
|
||||
if exp != got.typ {
|
||||
t.Fatalf("Expected error of type %q but got type %q (%q)", exp, got.typ, got)
|
||||
}
|
||||
return
|
||||
}
|
||||
if exp != errorNone {
|
||||
t.Fatalf("Expected error of type %q but got none", exp)
|
||||
require.Nil(t, got)
|
||||
} else {
|
||||
require.NotNil(t, got)
|
||||
require.Equal(t, exp, got.typ, "(%q)", got)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2906,13 +2885,7 @@ func assertAPIResponseLength(t *testing.T, got interface{}, expLen int) {
|
|||
t.Helper()
|
||||
|
||||
gotLen := reflect.ValueOf(got).Len()
|
||||
if gotLen != expLen {
|
||||
t.Fatalf(
|
||||
"Response length does not match, expected:\n%d\ngot:\n%d",
|
||||
expLen,
|
||||
gotLen,
|
||||
)
|
||||
}
|
||||
require.Equal(t, expLen, gotLen, "Response length does not match")
|
||||
}
|
||||
|
||||
func assertAPIResponseMetadataLen(t *testing.T, got interface{}, expLen int) {
|
||||
|
@ -2924,13 +2897,7 @@ func assertAPIResponseMetadataLen(t *testing.T, got interface{}, expLen int) {
|
|||
gotLen += len(m)
|
||||
}
|
||||
|
||||
if gotLen != expLen {
|
||||
t.Fatalf(
|
||||
"Amount of metadata in the response does not match, expected:\n%d\ngot:\n%d",
|
||||
expLen,
|
||||
gotLen,
|
||||
)
|
||||
}
|
||||
require.Equal(t, expLen, gotLen, "Amount of metadata in the response does not match")
|
||||
}
|
||||
|
||||
type fakeDB struct {
|
||||
|
@ -3271,26 +3238,18 @@ func TestRespondError(t *testing.T) {
|
|||
defer s.Close()
|
||||
|
||||
resp, err := http.Get(s.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Error on test request: %s", err)
|
||||
}
|
||||
require.NoError(t, err, "Error on test request: %s", err)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
defer resp.Body.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("Error reading response body: %s", err)
|
||||
}
|
||||
|
||||
if want, have := http.StatusServiceUnavailable, resp.StatusCode; want != have {
|
||||
t.Fatalf("Return code %d expected in error response but got %d", want, have)
|
||||
}
|
||||
if h := resp.Header.Get("Content-Type"); h != "application/json" {
|
||||
t.Fatalf("Expected Content-Type %q but got %q", "application/json", h)
|
||||
}
|
||||
require.NoError(t, err, "Error reading response body: %s", err)
|
||||
want, have := http.StatusServiceUnavailable, resp.StatusCode
|
||||
require.Equal(t, want, have, "Return code %d expected in error response but got %d", want, have)
|
||||
h := resp.Header.Get("Content-Type")
|
||||
require.Equal(t, "application/json", h, "Expected Content-Type %q but got %q", "application/json", h)
|
||||
|
||||
var res Response
|
||||
if err = json.Unmarshal(body, &res); err != nil {
|
||||
t.Fatalf("Error unmarshaling JSON body: %s", err)
|
||||
}
|
||||
err = json.Unmarshal(body, &res)
|
||||
require.NoError(t, err, "Error unmarshaling JSON body: %s", err)
|
||||
|
||||
exp := &Response{
|
||||
Status: statusError,
|
||||
|
@ -3419,17 +3378,13 @@ func TestParseTime(t *testing.T) {
|
|||
|
||||
for _, test := range tests {
|
||||
ts, err := parseTime(test.input)
|
||||
if err != nil && !test.fail {
|
||||
t.Errorf("Unexpected error for %q: %s", test.input, err)
|
||||
if !test.fail {
|
||||
require.NoError(t, err, "Unexpected error for %q: %s", test.input, err)
|
||||
require.NotNil(t, ts)
|
||||
require.True(t, ts.Equal(test.result), "Expected time %v for input %q but got %v", test.result, test.input, ts)
|
||||
continue
|
||||
}
|
||||
if err == nil && test.fail {
|
||||
t.Errorf("Expected error for %q but got none", test.input)
|
||||
continue
|
||||
}
|
||||
if !test.fail && !ts.Equal(test.result) {
|
||||
t.Errorf("Expected time %v for input %q but got %v", test.result, test.input, ts)
|
||||
}
|
||||
require.Error(t, err, "Expected error for %q but got none", test.input)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3473,17 +3428,12 @@ func TestParseDuration(t *testing.T) {
|
|||
|
||||
for _, test := range tests {
|
||||
d, err := parseDuration(test.input)
|
||||
if err != nil && !test.fail {
|
||||
t.Errorf("Unexpected error for %q: %s", test.input, err)
|
||||
if !test.fail {
|
||||
require.NoError(t, err, "Unexpected error for %q: %s", test.input, err)
|
||||
require.Equal(t, test.result, d, "Expected duration %v for input %q but got %v", test.result, test.input, d)
|
||||
continue
|
||||
}
|
||||
if err == nil && test.fail {
|
||||
t.Errorf("Expected error for %q but got none", test.input)
|
||||
continue
|
||||
}
|
||||
if !test.fail && d != test.result {
|
||||
t.Errorf("Expected duration %v for input %q but got %v", test.result, test.input, d)
|
||||
}
|
||||
require.Error(t, err, "Expected error for %q but got none", test.input)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3496,18 +3446,11 @@ func TestOptionsMethod(t *testing.T) {
|
|||
defer s.Close()
|
||||
|
||||
req, err := http.NewRequest("OPTIONS", s.URL+"/any_path", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating OPTIONS request: %s", err)
|
||||
}
|
||||
require.NoError(t, err, "Error creating OPTIONS request")
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Error executing OPTIONS request: %s", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusNoContent {
|
||||
t.Fatalf("Expected status %d, got %d", http.StatusNoContent, resp.StatusCode)
|
||||
}
|
||||
require.NoError(t, err, "Error executing OPTIONS request")
|
||||
require.Equal(t, http.StatusNoContent, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestTSDBStatus(t *testing.T) {
|
||||
|
@ -3546,9 +3489,7 @@ func TestTSDBStatus(t *testing.T) {
|
|||
api := &API{db: tc.db, gatherer: prometheus.DefaultGatherer}
|
||||
endpoint := tc.endpoint(api)
|
||||
req, err := http.NewRequest(tc.method, fmt.Sprintf("?%s", tc.values.Encode()), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Error when creating test request: %s", err)
|
||||
}
|
||||
require.NoError(t, err, "Error when creating test request")
|
||||
res := endpoint(req)
|
||||
assertAPIError(t, res.err, tc.errType)
|
||||
})
|
||||
|
|
|
@ -440,7 +440,7 @@ func TestShutdownWithStaleConnection(t *testing.T) {
|
|||
select {
|
||||
case <-closed:
|
||||
case <-time.After(timeout + 5*time.Second):
|
||||
t.Fatalf("Server still running after read timeout.")
|
||||
require.FailNow(t, "Server still running after read timeout.")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -502,7 +502,7 @@ func TestHandleMultipleQuitRequests(t *testing.T) {
|
|||
select {
|
||||
case <-closed:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatalf("Server still running after 5 seconds.")
|
||||
require.FailNow(t, "Server still running after 5 seconds.")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue