notifier: update use testutil for testing (#3695)

pull/4027/head
Solomon Van 7 years ago committed by Brian Brazil
parent daebf68ea2
commit 68e394a56e

@ -22,7 +22,6 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"reflect"
"testing" "testing"
"time" "time"
@ -64,9 +63,7 @@ func TestPostPath(t *testing.T) {
}, },
} }
for _, c := range cases { for _, c := range cases {
if res := postPath(c.in); res != c.out { testutil.Equals(t, c.out, postPath(c.in))
t.Errorf("Expected post path %q for %q but got %q", c.out, c.in, res)
}
} }
} }
@ -83,34 +80,23 @@ func TestHandlerNextBatch(t *testing.T) {
b := h.nextBatch() b := h.nextBatch()
if len(b) != maxBatchSize { testutil.Equals(t, maxBatchSize, len(b))
t.Fatalf("Expected first batch of length %d, but got %d", maxBatchSize, len(b))
} testutil.Assert(t, alertsEqual(expected[0:maxBatchSize], b), "First batch did not match")
if !alertsEqual(expected[0:maxBatchSize], b) {
t.Fatalf("First batch did not match")
}
b = h.nextBatch() b = h.nextBatch()
if len(b) != maxBatchSize { testutil.Equals(t, maxBatchSize, len(b))
t.Fatalf("Expected second batch of length %d, but got %d", maxBatchSize, len(b))
} testutil.Assert(t, alertsEqual(expected[maxBatchSize:2*maxBatchSize], b), "Second batch did not match")
if !alertsEqual(expected[maxBatchSize:2*maxBatchSize], b) {
t.Fatalf("Second batch did not match")
}
b = h.nextBatch() b = h.nextBatch()
if len(b) != 1 { testutil.Equals(t, 1, len(b))
t.Fatalf("Expected third batch of length %d, but got %d", 1, len(b))
}
if !alertsEqual(expected[2*maxBatchSize:], b) {
t.Fatalf("Third batch did not match")
}
if len(h.queue) != 0 { testutil.Assert(t, alertsEqual(expected[2*maxBatchSize:], b), "Third batch did not match")
t.Fatalf("Expected queue to be empty but got %d alerts", len(h.queue))
} testutil.Assert(t, len(h.queue) == 0, "Expected queue to be empty but got %d alerts", len(h.queue))
} }
func alertsEqual(a, b []*Alert) bool { func alertsEqual(a, b []*Alert) bool {
@ -137,29 +123,28 @@ func TestHandlerSendAll(t *testing.T) {
defer r.Body.Close() defer r.Body.Close()
var alerts []*Alert var alerts []*Alert
if err := json.NewDecoder(r.Body).Decode(&alerts); err != nil { testutil.Ok(t, json.NewDecoder(r.Body).Decode(&alerts))
t.Fatalf("Unexpected error on input decoding: %s", err)
}
if !alertsEqual(alerts, expected) { testutil.Assert(t, alertsEqual(alerts, expected), "Unexpected alerts received %v exp %v", alerts, expected)
t.Errorf("%#v %#v", *alerts[0], *expected[0])
t.Fatalf("Unexpected alerts received %v exp %v", alerts, expected)
}
} }
server1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { server1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, _ := r.BasicAuth() user, pass, _ := r.BasicAuth()
if user != "prometheus" || pass != "testing_password" { testutil.Assert(
t.Fatalf("Incorrect auth details for an alertmanager") t,
} user == "prometheus" || pass == "testing_password",
"Incorrect auth details for an alertmanager",
)
f(w, r) f(w, r)
w.WriteHeader(status1) w.WriteHeader(status1)
})) }))
server2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { server2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, _ := r.BasicAuth() user, pass, _ := r.BasicAuth()
if user != "" || pass != "" { testutil.Assert(
t.Fatalf("Incorrectly received auth details for an alertmanager") t,
} user == "" || pass == "",
"Incorrectly received auth details for an alertmanager",
)
f(w, r) f(w, r)
w.WriteHeader(status2) w.WriteHeader(status2)
@ -213,19 +198,13 @@ func TestHandlerSendAll(t *testing.T) {
status1 = http.StatusOK status1 = http.StatusOK
status2 = http.StatusOK status2 = http.StatusOK
if !h.sendAll(h.queue...) { testutil.Assert(t, h.sendAll(h.queue...), "all sends failed unexpectedly")
t.Fatalf("all sends failed unexpectedly")
}
status1 = http.StatusNotFound status1 = http.StatusNotFound
if !h.sendAll(h.queue...) { testutil.Assert(t, h.sendAll(h.queue...), "all sends failed unexpectedly")
t.Fatalf("all sends failed unexpectedly")
}
status2 = http.StatusInternalServerError status2 = http.StatusInternalServerError
if h.sendAll(h.queue...) { testutil.Assert(t, !h.sendAll(h.queue...), "all sends succeeded unexpectedly")
t.Fatalf("all sends succeeded unexpectedly")
}
} }
func TestCustomDo(t *testing.T) { func TestCustomDo(t *testing.T) {
@ -237,15 +216,13 @@ func TestCustomDo(t *testing.T) {
Do: func(ctx old_ctx.Context, client *http.Client, req *http.Request) (*http.Response, error) { Do: func(ctx old_ctx.Context, client *http.Client, req *http.Request) (*http.Response, error) {
received = true received = true
body, err := ioutil.ReadAll(req.Body) body, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatalf("Unable to read request body: %v", err) testutil.Ok(t, err)
}
if string(body) != testBody { testutil.Equals(t, testBody, string(body))
t.Fatalf("Unexpected body; want %v, got %v", testBody, string(body))
} testutil.Equals(t, testURL, req.URL.String())
if req.URL.String() != testURL {
t.Fatalf("Unexpected URL; want %v, got %v", testURL, req.URL.String())
}
return &http.Response{ return &http.Response{
Body: ioutil.NopCloser(nil), Body: ioutil.NopCloser(nil),
}, nil }, nil
@ -254,9 +231,7 @@ func TestCustomDo(t *testing.T) {
h.sendOne(context.Background(), nil, testURL, []byte(testBody)) h.sendOne(context.Background(), nil, testURL, []byte(testBody))
if !received { testutil.Assert(t, received, "Expected to receive an alert, but didn't")
t.Fatal("Expected to receive an alert, but didn't")
}
} }
func TestExternalLabels(t *testing.T) { func TestExternalLabels(t *testing.T) {
@ -290,9 +265,7 @@ func TestExternalLabels(t *testing.T) {
{Labels: labels.FromStrings("alertname", "externalrelabelthis", "a", "c")}, {Labels: labels.FromStrings("alertname", "externalrelabelthis", "a", "c")},
} }
if !alertsEqual(expected, h.queue) { testutil.Assert(t, alertsEqual(expected, h.queue), "Expected alerts %v, got %v", expected, h.queue)
t.Errorf("Expected alerts %v, got %v", expected, h.queue)
}
} }
func TestHandlerRelabel(t *testing.T) { func TestHandlerRelabel(t *testing.T) {
@ -328,9 +301,7 @@ func TestHandlerRelabel(t *testing.T) {
{Labels: labels.FromStrings("alertname", "renamed")}, {Labels: labels.FromStrings("alertname", "renamed")},
} }
if !alertsEqual(expected, h.queue) { testutil.Assert(t, alertsEqual(expected, h.queue), "Expected alerts %v, got %v", expected, h.queue)
t.Errorf("Expected alerts %v, got %v", expected, h.queue)
}
} }
func TestHandlerQueueing(t *testing.T) { func TestHandlerQueueing(t *testing.T) {
@ -347,13 +318,9 @@ func TestHandlerQueueing(t *testing.T) {
defer r.Body.Close() defer r.Body.Close()
var alerts []*Alert var alerts []*Alert
if err := json.NewDecoder(r.Body).Decode(&alerts); err != nil { testutil.Ok(t, json.NewDecoder(r.Body).Decode(&alerts))
t.Fatalf("Unexpected error on input decoding: %s", err)
}
if !alertsEqual(expected, alerts) { testutil.Assert(t, alertsEqual(expected, alerts), "Expected alerts %v, got %v", expected, alerts)
t.Errorf("Expected alerts %v, got %v", expected, alerts)
}
})) }))
h := NewManager(&Options{ h := NewManager(&Options{
@ -443,13 +410,11 @@ func (a alertmanagerMock) url() *url.URL {
func TestLabelSetNotReused(t *testing.T) { func TestLabelSetNotReused(t *testing.T) {
tg := makeInputTargetGroup() tg := makeInputTargetGroup()
_, _, err := alertmanagerFromGroup(tg, &config.AlertmanagerConfig{}) _, _, err := alertmanagerFromGroup(tg, &config.AlertmanagerConfig{})
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(tg, makeInputTargetGroup()) { testutil.Ok(t, err)
t.Fatal("Target modified during alertmanager extraction")
} // Target modified during alertmanager extraction
testutil.Equals(t, tg, makeInputTargetGroup())
} }
func TestReload(t *testing.T) { func TestReload(t *testing.T) {

Loading…
Cancel
Save