cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.73.7

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/parser/fuzz_test.go

287lines · modecode

1package parser_test
2
3import (
4 "strings"
5 "testing"
6
7 "github.com/prometheus/common/model"
8
9 "github.com/cloudflare/pint/internal/parser"
10)
11
12func FuzzParse(f *testing.F) {
13 testcases := []string{
14 `# head comment
15- record: foo # record comment
16 expr: foo offset 10m # expr comment
17 # pre-labels comment
18 labels:
19 # pre-foo comment
20 foo: bar
21 # post-foo comment
22 bob: alice
23# foot comment
24`,
25 `
26- alert: foo
27 annotations: {}
28 expr: bar
29 annotations: {}
30`,
31 `- record: name
32expr: sum(foo)
33labels:
34 foo: bar
35 bob: alice
36`,
37 `
38groups:
39- name: custom_rules
40 rules:
41 - record: name
42 expr: sum(foo)
43 labels:
44 foo: bar
45 bob: alice
46`,
47 `- alert: Down
48 expr: |
49 up == 0
50 for: |+
51 11m
52 labels:
53 severity: critical
54 annotations:
55 uri: https://docs.example.com/down.html
56
57- record: >
58 foo
59 expr: |-
60 bar
61 /
62 baz > 1
63 labels: {}
64`,
65 `- alert: Foo
66expr:
67 (
68 xxx
69 -
70 yyy
71 ) * bar > 0
72 and on(instance, device) baz
73for: 30m
74`,
75 `
76# pint ignore/begin
77{%- set foo = 1 %}
78{% set bar = 2 -%}
79{# comment #}
80{#
81 comment
82#}
83# pint ignore/end
84
85- record: colo_job:up:count
86 expr: sum(foo) without(job)
87
88- record: invalid
89 expr: sum(foo) by ())
90
91# pint ignore/begin
92- record: colo_job:down:count
93 expr: up == {{ foo }}
94# pint ignore/end
95
96- record: colo:multiline
97 expr: |
98 sum(
99 multiline
100 ) without(job, instance)
101
102- record: colo:multiline:sum
103 expr: |
104 sum(sum) without(job)
105 +
106 sum(sum) without(job)
107
108- record: colo:multiline2
109 expr: >-
110 sum(
111 multiline2
112 ) without(job, instance)
113
114- record: colo_job:up:byinstance
115 expr: sum(byinstance) by(instance)
116
117- record: instance_mode:node_cpu:rate4m
118 expr: sum(rate(node_cpu_seconds_total[4m])) without (cpu)
119
120- record: instance_mode:node_cpu:rate4m
121 expr: sum(rate(node_cpu_seconds_total[5m])) without (cpu)
122
123- record: instance_mode:node_cpu:rate5min
124 expr: sum(irate(node_cpu_seconds_total[5m])) without (cpu)
125
126- alert: Instance Is Down
127 expr: up == 0
128`,
129 `
130- record: colo_job:down:count
131 expr: up{job=~"foo"} == 0
132
133- record: colo_job:down:count
134 expr: up{job!~"foo"} == 0
135`,
136 `
137- record: colo_job:fl_cf_html_bytes_in:rate10m
138 expr: sum(rate(fl_cf_html_bytes_in[10m])) WITHOUT (colo_id, instance, node_type, region, node_status, job, colo_name)
139- record: colo_job:foo:rate1m
140 expr: sum(rate(foo[1m])) WITHOUT (instance)
141- record: colo_job:foo:irate3m
142 expr: sum(irate(foo[3m])) WITHOUT (colo_id)
143`,
144 `
145xxx:
146 xxx:
147 xxx:
148
149- xx
150- yyy
151`,
152 `
153- record: "colo:test1"
154 expr: topk(6, sum(rate(edgeworker_subrequest_errorCount{cordon="free"}[5m])) BY (zoneId,job))
155- record: "colo:test2"
156 expr: topk(6, sum(rate(edgeworker_subrequest_errorCount{cordon="free"}[10m])) without (instance))
157`,
158 `- alert: Always
159 expr: up
160- alert: AlwaysIgnored
161 expr: up # pint disable alerts/comparison
162 labels:
163 severity: warning
164 annotations:
165 url: "https://wiki.example.com/page/ServiceIsDown.html"
166- alert: ServiceIsDown
167 expr: up == 0
168- alert: ServiceIsDown
169 expr: up == 0
170 labels:
171 severity: bad
172 annotations:
173 url: bad
174- alert: ServiceIsDown
175 expr: up == 0
176 labels:
177 severity: warning
178 annotations:
179 url: "https://wiki.example.com/page/ServiceIsDown.html"
180`,
181 `
182- alert: Foo Is Down
183 expr: up{job="foo"} == 0
184 annotations:
185 url: "https://wiki.example.com/page/ServiceIsDown.html"
186 summary: 'Instance {{ $label.instance }} down'
187 func: '{{ $valuexx | xxx }}'
188 labels:
189 severity: warning
190 summary: 'Instance {{ $label.instance }} down'
191 func: '{{ $value | xxx }}'
192 bar: 'Some {{$value}} value'
193 val: '{{ .Value|humanizeDuration }}'
194 ignore: '$value is not a variable'
195`,
196 `groups:
197- name: example
198 rules:
199
200 # Alert for any instance that is unreachable for >5 minutes.
201 - alert: InstanceDown
202 expr: up == 0
203 for: 5m
204 labels:
205 severity: page
206 annotations:
207 summary: "Instance {{ $labels.instance }} down"
208 description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."
209
210 # Alert for any instance that has a median request latency >1s.
211 - alert: APIHighRequestLatency
212 expr: sum by (instance) (http_inprogress_requests) > 0
213 for: 10m
214 annotations:
215 summary: "High request latency on {{ $labels.instance }}"
216 description: "{{ $labels.instance }} has a median request latency above 1s (current value: {{ $value }}s)"
217`,
218 `- alert: Good
219expr: up == 0
220for: 2m
221labels:
222 component: foo
223
224alert: Bad
225expr: up == 0
226for: 2m
227labels:
228 component: foo
229`,
230 `
231- record: disabled
232 expr: sum(errors_total) by ) # pint disable promql/syntax
233
234- record: active
235 expr: sum(errors_total) by )
236
237- record: disabled
238 # pint disable promql/aggregate(job:true)
239 expr: sum(errors_total) without(job)
240
241- record: disabled
242 # pint disable promql/aggregate
243 expr: sum(errors_total) without(job)
244
245- record: active
246 expr: sum(errors_total) without(job)
247
248- alert: disabled
249 expr: sum(errors_total) by ) # pint disable promql/syntax
250
251- alert: active
252 expr: sum(errors_total) by )
253
254- alert: disabled
255 # pint disable promql/aggregate(job:true)
256 expr: sum(errors_total) without(job) > 0
257
258- alert: disabled
259 # pint disable promql/aggregate
260 expr: sum(errors_total) without(job) > 0
261
262- alert: active
263 expr: sum(errors_total) without(job)
264`,
265 `groups:
266- name: "haproxy.api_server.rules"
267 rules:
268 - alert: HaproxyServerHealthcheckFailure
269 expr: increase(haproxy_server_check_failures_total[15m]) > 100
270 for: 5m
271 labels:
272 severity: 24x7
273 annotations:
274 summary: "HAProxy server healthcheck failure (instance {{ $labels.instance }})"
275 description: "Some server healthcheck are failing on {{ $labels.server }}\n VALUE = {{ $value }}\n LABELS: {{ $labels }}"
276
277`,
278 }
279 for _, tc := range testcases {
280 f.Add(tc)
281 }
282 p := parser.NewParser(false, parser.PrometheusSchema, model.UTF8Validation)
283 f.Fuzz(func(t *testing.T, s string) {
284 t.Logf("Parsing: [%s]\n", s)
285 _ = p.Parse(strings.NewReader(s))
286 })
287}
288