cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.22.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

cmd/pint/lint_test.go

156lines · modecode

1package main
2
3import (
4 "fmt"
5 "os"
6 "path"
7 "strings"
8 "testing"
9)
10
11func mockRules(dir string, filesCount, rulesPerFile int) error {
12 var rulePath, c string
13 var err error
14 var content strings.Builder
15 for i := 1; i <= filesCount; i++ {
16 content.Reset()
17 rulePath = path.Join(dir, fmt.Sprintf("%d_rules.yaml", i))
18 for j := 1; j <= rulesPerFile; j++ {
19 c = fmt.Sprintf("- record: %d_rule\n expr: sum(foo) without(instance)\n labels:\n foo: bar\n\n", j)
20 if _, err = content.WriteString(c); err != nil {
21 return err
22 }
23 }
24
25 if err = os.WriteFile(rulePath, []byte(content.String()), 0o644); err != nil {
26 return err
27 }
28 }
29 return nil
30}
31
32func mockConfig(configPath string) error {
33 content := `
34parser {
35 relaxed = ["(.*)"]
36}
37rule {
38 reject ".* +.*" {
39 label_keys = true
40 annotation_keys = true
41 }
42
43 reject "https?://.+" {
44 label_keys = true
45 label_values = true
46 }
47}
48
49rule {
50 match {
51 kind = "alerting"
52 }
53
54 annotation "summary" {
55 severity = "bug"
56 required = true
57 }
58
59 annotation "dashboard" {
60 severity = "bug"
61 value = "https://grafana.example.com/(.+)"
62 }
63
64 label "priority" {
65 severity = "bug"
66 value = "(info|warning|critical)"
67 required = true
68 }
69
70 label "notify" {
71 severity = "bug"
72 required = true
73 }
74
75 label "component" {
76 severity = "bug"
77 required = true
78 }
79
80 alerts {
81 range = "1d"
82 step = "1m"
83 resolve = "5m"
84 }
85}
86
87rule {
88 match {
89 kind = "alerting"
90
91 label "notify" {
92 value = "(?:.*\\s+)?(chat|pagerduty|jira)(?:\\s+.*)?"
93 }
94 }
95
96 annotation "link" {
97 severity = "bug"
98 value = "https://alert-references.(?:s3.)?cfdata.org/(.+)"
99 required = true
100 }
101}
102
103rule {
104 match {
105 kind = "recording"
106 }
107
108 aggregate ".+" {
109 severity = "bug"
110 keep = ["job"]
111 }
112
113 cost {
114 bytesPerSample = 4036
115 }
116}
117
118rule {
119 match {
120 kind = "recording"
121 path = ".*"
122 }
123
124 aggregate "dc(?:_.+)?:.+" {
125 severity = "bug"
126 strip = ["instance"]
127 }
128}
129`
130 return os.WriteFile(configPath, []byte(content), 0o644)
131}
132
133func BenchmarkLint(b *testing.B) {
134 var err error
135
136 rulesDir := b.TempDir()
137 if err = mockRules(rulesDir, 100, 50); err != nil {
138 b.Error(err)
139 b.FailNow()
140 }
141
142 configPath := path.Join(rulesDir, ".pint.hcl")
143 if err = mockConfig(configPath); err != nil {
144 b.Error(err)
145 b.FailNow()
146 }
147
148 app := newApp()
149 args := []string{"pint", "-c", configPath, "-l", "error", "--offline", "lint", rulesDir + "/*.yaml"}
150 for n := 0; n < b.N; n++ {
151 if err = app.Run(args); err != nil {
152 b.Error(err)
153 b.FailNow()
154 }
155 }
156}
157