cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.21.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/discovery/glob_test.go

135lines · modecode

1package discovery_test
2
3import (
4 "errors"
5 "fmt"
6 "io/ioutil"
7 "os"
8 "path"
9 "path/filepath"
10 "regexp"
11 "strconv"
12 "strings"
13 "testing"
14
15 "github.com/prometheus/prometheus/model/rulefmt"
16 "github.com/stretchr/testify/require"
17 "gopkg.in/yaml.v3"
18
19 "github.com/cloudflare/pint/internal/discovery"
20 "github.com/cloudflare/pint/internal/parser"
21)
22
23func TestGlobPathFinder(t *testing.T) {
24 type testCaseT struct {
25 files map[string]string
26 finder discovery.GlobFinder
27 entries []discovery.Entry
28 err error
29 }
30
31 p := parser.NewParser()
32 testRuleBody := "# pint file/owner bob\n\n- record: foo\n expr: sum(foo)\n"
33 testRules, err := p.Parse([]byte(testRuleBody))
34 require.NoError(t, err)
35
36 var r rulefmt.RuleGroups
37 strictErr := yaml.Unmarshal([]byte(testRuleBody), &r)
38
39 testCases := []testCaseT{
40 {
41 files: map[string]string{},
42 finder: discovery.NewGlobFinder([]string{"[]"}, nil),
43 err: filepath.ErrBadPattern,
44 },
45 {
46 files: map[string]string{},
47 finder: discovery.NewGlobFinder([]string{"*"}, nil),
48 err: fmt.Errorf("no matching files"),
49 },
50 {
51 files: map[string]string{},
52 finder: discovery.NewGlobFinder([]string{"*"}, nil),
53 err: fmt.Errorf("no matching files"),
54 },
55 {
56 files: map[string]string{},
57 finder: discovery.NewGlobFinder([]string{"foo/*"}, nil),
58 err: fmt.Errorf("no matching files"),
59 },
60 {
61 files: map[string]string{"bar.yml": testRuleBody},
62 finder: discovery.NewGlobFinder([]string{"foo/*"}, nil),
63 err: fmt.Errorf("no matching files"),
64 },
65 {
66 files: map[string]string{"bar.yml": testRuleBody},
67 finder: discovery.NewGlobFinder([]string{"*"}, []*regexp.Regexp{regexp.MustCompile(".*")}),
68 entries: []discovery.Entry{
69 {
70 Path: "bar.yml",
71 Rule: testRules[0],
72 ModifiedLines: testRules[0].Lines(),
73 Owner: "bob",
74 },
75 },
76 },
77 {
78 files: map[string]string{"foo/bar.yml": testRuleBody + "\n\n# pint file/owner alice\n"},
79 finder: discovery.NewGlobFinder([]string{"*"}, []*regexp.Regexp{regexp.MustCompile(".*")}),
80 entries: []discovery.Entry{
81 {
82 Path: "foo/bar.yml",
83 Rule: testRules[0],
84 ModifiedLines: testRules[0].Lines(),
85 Owner: "alice",
86 },
87 },
88 },
89 {
90 files: map[string]string{"bar.yml": testRuleBody},
91 finder: discovery.NewGlobFinder([]string{"*"}, nil),
92 entries: []discovery.Entry{
93 {
94 Path: "bar.yml",
95 PathError: strictErr,
96 ModifiedLines: []int{1, 2, 3, 4},
97 Owner: "bob",
98 },
99 },
100 },
101 {
102 files: map[string]string{"bar.yml": "record:::{}\n expr: sum(foo)\n\n# pint file/owner bob\n"},
103 finder: discovery.NewGlobFinder([]string{"*"}, []*regexp.Regexp{regexp.MustCompile(".*")}),
104 entries: []discovery.Entry{
105 {
106 Path: "bar.yml",
107 PathError: errors.New("yaml: line 2: mapping values are not allowed in this context"),
108 ModifiedLines: []int{1, 2, 3, 4},
109 Owner: "bob",
110 },
111 },
112 },
113 }
114
115 for i, tc := range testCases {
116 t.Run(strconv.Itoa(i), func(t *testing.T) {
117 workdir := t.TempDir()
118 err := os.Chdir(workdir)
119 require.NoError(t, err)
120
121 for p, content := range tc.files {
122 if strings.Contains(p, "/") {
123 err = os.MkdirAll(path.Dir(p), 0o755)
124 require.NoError(t, err)
125 }
126 err = ioutil.WriteFile(p, []byte(content), 0o644)
127 require.NoError(t, err)
128 }
129
130 entries, err := tc.finder.Find()
131 require.Equal(t, tc.err, err)
132 require.Equal(t, tc.entries, entries)
133 })
134 }
135}