cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.4.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/checks/base_test.go

98lines · modecode

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