cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/checks/base_test.go
99lines · modecode
| 1 | package checks_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/google/go-cmp/cmp" |
| 8 | |
| 9 | "github.com/cloudflare/pint/internal/checks" |
| 10 | "github.com/cloudflare/pint/internal/parser" |
| 11 | ) |
| 12 | |
| 13 | type checkTest struct { |
| 14 | description string |
| 15 | content string |
| 16 | checker checks.RuleChecker |
| 17 | problems []checks.Problem |
| 18 | } |
| 19 | |
| 20 | func runTests(t *testing.T, testCases []checkTest, opts ...cmp.Option) { |
| 21 | p := parser.NewParser() |
| 22 | ctx := context.Background() |
| 23 | for _, tc := range testCases { |
| 24 | t.Run(tc.description, func(t *testing.T) { |
| 25 | rules, err := p.Parse([]byte(tc.content)) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | for _, rule := range rules { |
| 30 | problems := tc.checker.Check(ctx, rule) |
| 31 | if diff := cmp.Diff(tc.problems, problems, opts...); diff != "" { |
| 32 | t.Fatalf("Check() returned wrong problem list (-want +got):\n%s", diff) |
| 33 | } |
| 34 | } |
| 35 | }) |
| 36 | t.Run(tc.description+" (bogus alerting rule)", func(t *testing.T) { |
| 37 | rules, err := p.Parse([]byte(` |
| 38 | - alert: foo |
| 39 | expr: 'foo{}{} > 0' |
| 40 | annotations: |
| 41 | summary: '{{ $labels.job }} is incorrect' |
| 42 | `)) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | for _, rule := range rules { |
| 47 | _ = tc.checker.Check(ctx, rule) |
| 48 | } |
| 49 | }) |
| 50 | t.Run(tc.description+" (bogus recording rule)", func(t *testing.T) { |
| 51 | rules, err := p.Parse([]byte(` |
| 52 | - record: foo |
| 53 | expr: 'foo{}{}' |
| 54 | `)) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | for _, rule := range rules { |
| 59 | _ = tc.checker.Check(ctx, rule) |
| 60 | } |
| 61 | }) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestParseSeverity(t *testing.T) { |
| 66 | type testCaseT struct { |
| 67 | input string |
| 68 | output string |
| 69 | shouldError bool |
| 70 | } |
| 71 | |
| 72 | testCases := []testCaseT{ |
| 73 | {"xxx", "", true}, |
| 74 | {"Bug", "", true}, |
| 75 | {"fatal", "Fatal", false}, |
| 76 | {"bug", "Bug", false}, |
| 77 | {"info", "Information", false}, |
| 78 | {"warning", "Warning", false}, |
| 79 | } |
| 80 | |
| 81 | for _, tc := range testCases { |
| 82 | t.Run(tc.input, func(t *testing.T) { |
| 83 | sev, err := checks.ParseSeverity(tc.input) |
| 84 | hadError := err != nil |
| 85 | |
| 86 | if hadError != tc.shouldError { |
| 87 | t.Fatalf("checks.ParseSeverity() returned err=%v, expected=%v", err, tc.shouldError) |
| 88 | } |
| 89 | |
| 90 | if hadError { |
| 91 | return |
| 92 | } |
| 93 | |
| 94 | if sev.String() != tc.output { |
| 95 | t.Fatalf("checks.ParseSeverity() returned severity=%q, expected=%q", sev, tc.output) |
| 96 | } |
| 97 | }) |
| 98 | } |
| 99 | } |
| 100 | |