From 1c3e7b4241cfb6002bf75ae2ef4702555c8e52c8 Mon Sep 17 00:00:00 2001 From: misha Date: Fri, 5 Feb 2021 11:19:09 +0000 Subject: [PATCH] Use strings.Builder for neater error formatting Signed-off-by: misha --- cmd/promtool/unittest.go | 27 +++++++++++++----------- docs/configuration/unit_testing_rules.md | 3 +++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/cmd/promtool/unittest.go b/cmd/promtool/unittest.go index e4e243b58..64107f171 100644 --- a/cmd/promtool/unittest.go +++ b/cmd/promtool/unittest.go @@ -300,26 +300,29 @@ func (tg *testGroup) test(evalInterval time.Duration, groupOrderMap map[string]i }) } + var sb strings.Builder if gotAlerts.Len() != expAlerts.Len() { - 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())) + if tg.TestGroupName != "" { + fmt.Fprintf(&sb, " name: %s,\n", tg.TestGroupName) } + fmt.Fprintf(&sb, " alertname:%s, time:%s, \n", testcase.Alertname, testcase.EvalTime.String()) + fmt.Fprintf(&sb, " exp:%#v, \n", expAlerts.String()) + fmt.Fprintf(&sb, " got:%#v", gotAlerts.String()) + + errs = append(errs, errors.New(sb.String())) } else { sort.Sort(gotAlerts) sort.Sort(expAlerts) if !reflect.DeepEqual(expAlerts, gotAlerts) { - 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())) + if tg.TestGroupName != "" { + fmt.Fprintf(&sb, " name: %s,\n", tg.TestGroupName) } + fmt.Fprintf(&sb, " alertname:%s, time:%s, \n", testcase.Alertname, testcase.EvalTime.String()) + fmt.Fprintf(&sb, " exp:%#v, \n", expAlerts.String()) + fmt.Fprintf(&sb, " got:%#v", gotAlerts.String()) + + errs = append(errs, errors.New(sb.String())) } } } diff --git a/docs/configuration/unit_testing_rules.md b/docs/configuration/unit_testing_rules.md index f2bbbb664..3043b2edf 100644 --- a/docs/configuration/unit_testing_rules.md +++ b/docs/configuration/unit_testing_rules.md @@ -44,6 +44,9 @@ interval: input_series: [ - ] +# Name of the test group +[ name: ] + # Unit tests for the above data. # Unit tests for alerting rules. We consider the alerting rules from the input file.