From 2d1e92513b4b66df2619b4cc02add379cbe29f06 Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Wed, 14 Jun 2017 13:21:32 +0530 Subject: [PATCH] Add tests Signed-off-by: Goutham Veeramachaneni --- pkg/rulefmt/rulefmt.go | 5 ++ pkg/rulefmt/rulefmt_test.go | 50 ++++++++++++++++++- pkg/rulefmt/testdata/bad_expr.bad.yaml | 6 +++ pkg/rulefmt/testdata/duplicate_grp.bad.yaml | 4 ++ pkg/rulefmt/testdata/no_rec_alert.bad.yaml | 5 ++ pkg/rulefmt/testdata/noexpr.bad.yaml | 5 ++ pkg/rulefmt/testdata/noversion.bad.yaml | 2 + .../testdata/record_and_alert.bad.yaml | 7 +++ pkg/rulefmt/testdata/test.yaml | 2 +- 9 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 pkg/rulefmt/testdata/bad_expr.bad.yaml create mode 100644 pkg/rulefmt/testdata/duplicate_grp.bad.yaml create mode 100644 pkg/rulefmt/testdata/no_rec_alert.bad.yaml create mode 100644 pkg/rulefmt/testdata/noexpr.bad.yaml create mode 100644 pkg/rulefmt/testdata/noversion.bad.yaml create mode 100644 pkg/rulefmt/testdata/record_and_alert.bad.yaml diff --git a/pkg/rulefmt/rulefmt.go b/pkg/rulefmt/rulefmt.go index 777da84c2..97e7bd8e9 100644 --- a/pkg/rulefmt/rulefmt.go +++ b/pkg/rulefmt/rulefmt.go @@ -33,6 +33,10 @@ func (g *RuleGroups) Validate() (errs []error) { set := map[string]struct{}{} for _, g := range g.Groups { + if g.Name == "" { + errs = append(errs, errors.Errorf("Groupname should not be empty")) + } + if _, ok := set[g.Name]; ok { errs = append( errs, @@ -80,6 +84,7 @@ func (r *Rule) Validate() (errs []error) { if r.Record == "" && r.Alert == "" { errs = append(errs, errors.Errorf("one of 'record' or 'alert' must be set")) } + if r.Expr == "" { errs = append(errs, errors.Errorf("field 'expr' must be set in rule")) } else if _, err := promql.ParseExpr(r.Expr); err != nil { diff --git a/pkg/rulefmt/rulefmt_test.go b/pkg/rulefmt/rulefmt_test.go index 494a8ee50..693956599 100644 --- a/pkg/rulefmt/rulefmt_test.go +++ b/pkg/rulefmt/rulefmt_test.go @@ -1,6 +1,10 @@ package rulefmt -import "testing" +import ( + "path/filepath" + "strings" + "testing" +) func TestParseFileSuccess(t *testing.T) { if _, errs := ParseFile("testdata/test.yaml"); len(errs) > 0 { @@ -10,3 +14,47 @@ func TestParseFileSuccess(t *testing.T) { } } } + +func TestParseFileFailure(t *testing.T) { + table := []struct { + filename string + errMsg string + }{ + { + filename: "duplicate_grp.bad.yaml", + errMsg: "groupname: \"yolo\" is repeated in the same file", + }, + { + filename: "noversion.bad.yaml", + errMsg: "invalid rule group version 0", + }, + { + filename: "bad_expr.bad.yaml", + errMsg: "parse error", + }, + { + filename: "record_and_alert.bad.yaml", + errMsg: "only one of 'record' and 'alert' must be set", + }, + { + filename: "no_rec_alert.bad.yaml", + errMsg: "one of 'record' or 'alert' must be set", + }, + { + filename: "noexpr.bad.yaml", + errMsg: "field 'expr' must be set in rule", + }, + } + + for _, c := range table { + _, errs := ParseFile(filepath.Join("testdata", c.filename)) + if errs == nil { + t.Errorf("Expected error parsing %s but got none", c.filename) + continue + } + if !strings.Contains(errs[0].Error(), c.errMsg) { + t.Errorf("Expected error for %s to contain %q but got: %s", c.filename, c.errMsg, errs) + } + } + +} diff --git a/pkg/rulefmt/testdata/bad_expr.bad.yaml b/pkg/rulefmt/testdata/bad_expr.bad.yaml new file mode 100644 index 000000000..df9a8cac1 --- /dev/null +++ b/pkg/rulefmt/testdata/bad_expr.bad.yaml @@ -0,0 +1,6 @@ +Version: 1 +Groups: +- name: yolo + rules: + - record: yolo + expr: rate(hi) diff --git a/pkg/rulefmt/testdata/duplicate_grp.bad.yaml b/pkg/rulefmt/testdata/duplicate_grp.bad.yaml new file mode 100644 index 000000000..dc2239121 --- /dev/null +++ b/pkg/rulefmt/testdata/duplicate_grp.bad.yaml @@ -0,0 +1,4 @@ +Version: 1 +Groups: +- name: yolo +- name: yolo diff --git a/pkg/rulefmt/testdata/no_rec_alert.bad.yaml b/pkg/rulefmt/testdata/no_rec_alert.bad.yaml new file mode 100644 index 000000000..5806275aa --- /dev/null +++ b/pkg/rulefmt/testdata/no_rec_alert.bad.yaml @@ -0,0 +1,5 @@ +Version: 1 +Groups: + - name: yolo + rules: + - expr: 1 diff --git a/pkg/rulefmt/testdata/noexpr.bad.yaml b/pkg/rulefmt/testdata/noexpr.bad.yaml new file mode 100644 index 000000000..99c502796 --- /dev/null +++ b/pkg/rulefmt/testdata/noexpr.bad.yaml @@ -0,0 +1,5 @@ +Version: 1 +Groups: + - name: yolo + rules: + - record: ylo diff --git a/pkg/rulefmt/testdata/noversion.bad.yaml b/pkg/rulefmt/testdata/noversion.bad.yaml new file mode 100644 index 000000000..8b4ed9ce7 --- /dev/null +++ b/pkg/rulefmt/testdata/noversion.bad.yaml @@ -0,0 +1,2 @@ +Groups: +- name: yolo diff --git a/pkg/rulefmt/testdata/record_and_alert.bad.yaml b/pkg/rulefmt/testdata/record_and_alert.bad.yaml new file mode 100644 index 000000000..98063960c --- /dev/null +++ b/pkg/rulefmt/testdata/record_and_alert.bad.yaml @@ -0,0 +1,7 @@ +Version: 1 +Groups: +- name: yolo + rules: + - record: Hi + alert: Hello + expr: 1 diff --git a/pkg/rulefmt/testdata/test.yaml b/pkg/rulefmt/testdata/test.yaml index 321b0268f..70bb267ee 100644 --- a/pkg/rulefmt/testdata/test.yaml +++ b/pkg/rulefmt/testdata/test.yaml @@ -32,7 +32,7 @@ groups: annotations: description: "stuff's happening with {{ $.labels.service }}" -- name: my-group-name +- name: my-another-name interval: 30s # defaults to global interval rules: - alert: HighErrors