From c2c5aeb16b9b3c7e2b6ce4a0ef03a213b8bcce21 Mon Sep 17 00:00:00 2001 From: misha Date: Wed, 3 Feb 2021 17:04:31 +0000 Subject: [PATCH] Add optional name property to testgroup for better test failure output Signed-off-by: misha --- cmd/promtool/testdata/failing.yml | 1 + cmd/promtool/unittest.go | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/promtool/testdata/failing.yml b/cmd/promtool/testdata/failing.yml index 8ef56eb37..b524b4521 100644 --- a/cmd/promtool/testdata/failing.yml +++ b/cmd/promtool/testdata/failing.yml @@ -1,6 +1,7 @@ tests: # Simple failing test. - interval: 1m + name: "Failing test" input_series: - series: test values: '0' diff --git a/cmd/promtool/unittest.go b/cmd/promtool/unittest.go index 551d7b4bf..e4e243b58 100644 --- a/cmd/promtool/unittest.go +++ b/cmd/promtool/unittest.go @@ -146,6 +146,7 @@ type testGroup struct { AlertRuleTests []alertTestCase `yaml:"alert_rule_test,omitempty"` PromqlExprTests []promqlTestCase `yaml:"promql_expr_test,omitempty"` ExternalLabels labels.Labels `yaml:"external_labels,omitempty"` + TestGroupName string `yaml:"name,omitempty"` } // test performs the unit tests. @@ -300,15 +301,25 @@ func (tg *testGroup) test(evalInterval time.Duration, groupOrderMap map[string]i } if gotAlerts.Len() != expAlerts.Len() { - errs = append(errs, errors.Errorf(" alertname:%s, time:%s, \n exp:%#v, \n got:%#v", - testcase.Alertname, testcase.EvalTime.String(), expAlerts.String(), gotAlerts.String())) + if tg.TestGroupName == "" { + errs = append(errs, errors.Errorf(" alertname:%s, time:%s, \n exp:%#v, \n got:%#v", + testcase.Alertname, testcase.EvalTime.String(), expAlerts.String(), gotAlerts.String())) + } else { + errs = append(errs, errors.Errorf(" name: %s,\n alertname:%s, time:%s, \n exp:%#v, \n got:%#v", + tg.TestGroupName, testcase.Alertname, testcase.EvalTime.String(), expAlerts.String(), gotAlerts.String())) + } } else { sort.Sort(gotAlerts) sort.Sort(expAlerts) if !reflect.DeepEqual(expAlerts, gotAlerts) { - errs = append(errs, errors.Errorf(" alertname:%s, time:%s, \n exp:%#v, \n got:%#v", - testcase.Alertname, testcase.EvalTime.String(), expAlerts.String(), gotAlerts.String())) + if tg.TestGroupName == "" { + errs = append(errs, errors.Errorf(" alertname:%s, time:%s, \n exp:%#v, \n got:%#v", + testcase.Alertname, testcase.EvalTime.String(), expAlerts.String(), gotAlerts.String())) + } else { + errs = append(errs, errors.Errorf(" name: %s,\n alertname:%s, time:%s, \n exp:%#v, \n got:%#v", + tg.TestGroupName, testcase.Alertname, testcase.EvalTime.String(), expAlerts.String(), gotAlerts.String())) + } } } }