cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.7.3

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/checks/base_test.go

101lines · modecode

1package checks_test
2
3import (
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
13type checkTest struct {
14 description string
15 content string
16 checker checks.RuleChecker
17 problems []checks.Problem
18}
19
20func 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.Errorf("Check() returned wrong problem list (-want +got):\n%s", diff)
33 return
34 }
35 }
36 })
37 t.Run(tc.description+" (bogus alerting rule)", func(t *testing.T) {
38 rules, err := p.Parse([]byte(`
39- alert: foo
40 expr: 'foo{}{} > 0'
41 annotations:
42 summary: '{{ $labels.job }} is incorrect'
43`))
44 if err != nil {
45 t.Fatal(err)
46 }
47 for _, rule := range rules {
48 _ = tc.checker.Check(ctx, rule)
49 }
50 })
51 t.Run(tc.description+" (bogus recording rule)", func(t *testing.T) {
52 rules, err := p.Parse([]byte(`
53- record: foo
54 expr: 'foo{}{}'
55`))
56 if err != nil {
57 t.Fatal(err)
58 }
59 for _, rule := range rules {
60 _ = tc.checker.Check(ctx, rule)
61 }
62 })
63 }
64}
65
66func TestParseSeverity(t *testing.T) {
67 type testCaseT struct {
68 input string
69 output string
70 shouldError bool
71 }
72
73 testCases := []testCaseT{
74 {"xxx", "", true},
75 {"Bug", "", true},
76 {"fatal", "Fatal", false},
77 {"bug", "Bug", false},
78 {"info", "Information", false},
79 {"warning", "Warning", false},
80 }
81
82 for _, tc := range testCases {
83 t.Run(tc.input, func(t *testing.T) {
84 sev, err := checks.ParseSeverity(tc.input)
85 hadError := err != nil
86
87 if hadError != tc.shouldError {
88 t.Errorf("checks.ParseSeverity() returned err=%v, expected=%v", err, tc.shouldError)
89 return
90 }
91
92 if hadError {
93 return
94 }
95
96 if sev.String() != tc.output {
97 t.Errorf("checks.ParseSeverity() returned severity=%q, expected=%q", sev, tc.output)
98 }
99 })
100 }
101}
102