cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.17.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/parser/utils/absent_test.go

137lines · modecode

1package utils_test
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/require"
7
8 "github.com/cloudflare/pint/internal/parser"
9 "github.com/cloudflare/pint/internal/parser/utils"
10)
11
12func TestHasOuterAbsent(t *testing.T) {
13 type callT struct {
14 call string
15 binExpr string
16 }
17
18 type testCaseT struct {
19 expr string
20 output []callT
21 }
22
23 testCases := []testCaseT{
24 {
25 expr: "foo",
26 },
27 {
28 expr: "absent(foo)",
29 output: []callT{{call: "absent(foo)"}},
30 },
31 {
32 expr: `absent(foo{job="bar"})`,
33 output: []callT{{call: `absent(foo{job="bar"})`}},
34 },
35 {
36 expr: `absent(foo{job="bar"}) AND on(job) bar`,
37 output: []callT{{
38 call: `absent(foo{job="bar"})`,
39 }},
40 },
41 {
42 expr: `vector(1) or absent(foo{job="bar"}) AND on(job) bar`,
43 output: []callT{
44 {call: `absent(foo{job="bar"})`},
45 },
46 },
47 {
48 expr: `up == 0 or absent(foo{job="bar"}) AND on(job) bar`,
49 output: []callT{{
50 call: `absent(foo{job="bar"})`,
51 }},
52 },
53 {
54 expr: `up == 0 or absent(foo{job="bar"}) or absent(bar)`,
55 output: []callT{
56 {call: `absent(foo{job="bar"})`},
57 {call: `absent(bar)`},
58 },
59 },
60 {
61 expr: `absent(sum(nonexistent{job="myjob"}))`,
62 output: []callT{
63 {call: `absent(sum(nonexistent{job="myjob"}))`},
64 },
65 },
66 {
67 expr: `up == 0 or absent(foo{job="bar"}) * on(job) group_left(xxx) bar`,
68 output: []callT{{
69 call: `absent(foo{job="bar"})`,
70 binExpr: `absent(foo{job="bar"}) * on(job) group_left(xxx) bar`,
71 }},
72 },
73 {
74 expr: `bar * on() group_left(xxx) absent(foo{job="bar"})`,
75 output: []callT{{
76 call: `absent(foo{job="bar"})`,
77 binExpr: `bar * on() group_left(xxx) absent(foo{job="bar"})`,
78 }},
79 },
80 {
81 expr: `up == 0 or absent(foo{job="bar"}) * on(job) group_left() bar`,
82 output: []callT{{
83 call: `absent(foo{job="bar"})`,
84 binExpr: `absent(foo{job="bar"}) * on(job) group_left() bar`,
85 }},
86 },
87 {
88 expr: `bar * on() group_right(xxx) absent(foo{job="bar"})`,
89 output: []callT{{
90 call: `absent(foo{job="bar"})`,
91 binExpr: `bar * on() group_right(xxx) absent(foo{job="bar"})`,
92 }},
93 },
94 {
95 expr: `absent(foo{job="bar"}) * on(job) group_right(xxx) bar`,
96 output: []callT{{
97 call: `absent(foo{job="bar"})`,
98 binExpr: `absent(foo{job="bar"}) * on(job) group_right(xxx) bar`,
99 }},
100 },
101 {
102 expr: `absent(foo{job="bar"}) OR bar`,
103 output: []callT{{
104 call: `absent(foo{job="bar"})`,
105 }},
106 },
107 }
108
109 for _, tc := range testCases {
110 t.Run(tc.expr, func(t *testing.T) {
111 n, err := parser.DecodeExpr(tc.expr)
112 if err != nil {
113 t.Error(err)
114 t.FailNow()
115 }
116 calls := utils.HasOuterAbsent(n)
117 if len(calls) == 0 {
118 if len(tc.output) > 0 {
119 t.Errorf("HasOuterAbsent() returned nil, expected %s", tc.output)
120 }
121 } else {
122 output := []callT{}
123 for _, a := range calls {
124 var c callT
125 if a.Fragment != nil {
126 c.call = a.Fragment.Node.String()
127 }
128 if a.BinExpr != nil {
129 c.binExpr = a.BinExpr.String()
130 }
131 output = append(output, c)
132 }
133 require.Equal(t, tc.output, output, "HasOuterAbsent() returned wrong output")
134 }
135 })
136 }
137}
138