Skip to content

Commit 8da7f29

Browse files
committed
test: lean towards table driven testing
To improve readability of the test. Signed-off-by: emreya <[email protected]>
1 parent 052f926 commit 8da7f29

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

notify/pagerduty/pagerduty_test.go

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -507,54 +507,60 @@ func TestPagerDutyTimeout(t *testing.T) {
507507
Links []pagerDutyLink
508508
}
509509

510-
latency := time.Millisecond * 50
511-
srv := httptest.NewServer(http.HandlerFunc(
512-
func(w http.ResponseWriter, r *http.Request) {
513-
decoder := json.NewDecoder(r.Body)
514-
var event pagerDutyEvent
515-
if err := decoder.Decode(&event); err != nil {
516-
panic(err)
517-
}
518-
519-
if event.RoutingKey == "" || event.EventAction == "" {
520-
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
521-
return
522-
}
523-
time.Sleep(latency)
524-
},
525-
))
526-
defer srv.Close()
527-
url, err := url.Parse(srv.URL)
528-
require.NoError(t, err)
510+
tests := map[string]struct {
511+
latency time.Duration
512+
timeout time.Duration
513+
wantErr bool
514+
}{
515+
"success": {latency: 100 * time.Millisecond, timeout: 120 * time.Millisecond, wantErr: false},
516+
"error": {latency: 100 * time.Millisecond, timeout: 80 * time.Millisecond, wantErr: true},
517+
}
529518

530-
f := func(ts time.Duration) error {
531-
cfg := config.PagerdutyConfig{
532-
HTTPConfig: &commoncfg.HTTPClientConfig{},
533-
RoutingKey: config.Secret("01234567890123456789012345678901"),
534-
URL: &config.URL{URL: url},
535-
Timeout: ts,
536-
}
519+
for name, tt := range tests {
520+
t.Run(name, func(t *testing.T) {
521+
522+
srv := httptest.NewServer(http.HandlerFunc(
523+
func(w http.ResponseWriter, r *http.Request) {
524+
decoder := json.NewDecoder(r.Body)
525+
var event pagerDutyEvent
526+
if err := decoder.Decode(&event); err != nil {
527+
panic(err)
528+
}
529+
530+
if event.RoutingKey == "" || event.EventAction == "" {
531+
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
532+
return
533+
}
534+
time.Sleep(tt.latency)
535+
},
536+
))
537+
defer srv.Close()
538+
u, err := url.Parse(srv.URL)
539+
require.NoError(t, err)
537540

538-
pd, err := New(&cfg, test.CreateTmpl(t), promslog.NewNopLogger())
539-
require.NoError(t, err)
541+
cfg := config.PagerdutyConfig{
542+
HTTPConfig: &commoncfg.HTTPClientConfig{},
543+
RoutingKey: config.Secret("01234567890123456789012345678901"),
544+
URL: &config.URL{URL: u},
545+
Timeout: tt.timeout,
546+
}
540547

541-
ctx := context.Background()
542-
ctx = notify.WithGroupKey(ctx, "1")
548+
pd, err := New(&cfg, test.CreateTmpl(t), promslog.NewNopLogger())
549+
require.NoError(t, err)
543550

544-
_, err = pd.Notify(ctx, &types.Alert{
545-
Alert: model.Alert{
546-
Labels: model.LabelSet{
547-
"lbl1": "val1",
551+
ctx := context.Background()
552+
ctx = notify.WithGroupKey(ctx, "1")
553+
alert := &types.Alert{
554+
Alert: model.Alert{
555+
Labels: model.LabelSet{
556+
"lbl1": "val1",
557+
},
558+
StartsAt: time.Now(),
559+
EndsAt: time.Now().Add(time.Hour),
548560
},
549-
StartsAt: time.Now(),
550-
EndsAt: time.Now().Add(time.Hour),
551-
}},
552-
)
553-
return err
561+
}
562+
_, err = pd.Notify(ctx, alert)
563+
require.Equal(t, tt.wantErr, err != nil)
564+
})
554565
}
555-
556-
err = f(latency - time.Millisecond*10)
557-
require.Error(t, err)
558-
err = f(latency + time.Millisecond*10)
559-
require.NoError(t, err)
560566
}

0 commit comments

Comments
 (0)