cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.71.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/parser/fuzz_test.go

286lines · modecode

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