github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/configs/legacy_promql/printer_test.go (about) 1 // Copyright 2015 The Prometheus Authors 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package promql 15 16 import ( 17 "testing" 18 "time" 19 20 "github.com/prometheus/prometheus/pkg/labels" 21 ) 22 23 func TestStatementString(t *testing.T) { 24 in := &AlertStmt{ 25 Name: "FooAlert", 26 Expr: &BinaryExpr{ 27 Op: itemGTR, 28 LHS: &VectorSelector{ 29 Name: "foo", 30 LabelMatchers: []*labels.Matcher{ 31 {Type: labels.MatchEqual, Name: labels.MetricName, Value: "bar"}, 32 }, 33 }, 34 RHS: &NumberLiteral{10}, 35 }, 36 Duration: 5 * time.Minute, 37 Labels: labels.FromStrings("foo", "bar"), 38 Annotations: labels.FromStrings("notify", "team-a"), 39 } 40 41 expected := `ALERT FooAlert 42 IF foo > 10 43 FOR 5m 44 LABELS {foo="bar"} 45 ANNOTATIONS {notify="team-a"}` 46 47 if in.String() != expected { 48 t.Fatalf("expected:\n%s\ngot:\n%s\n", expected, in.String()) 49 } 50 } 51 52 func TestExprString(t *testing.T) { 53 // A list of valid expressions that are expected to be 54 // returned as out when calling String(). If out is empty the output 55 // is expected to equal the input. 56 inputs := []struct { 57 in, out string 58 }{ 59 { 60 in: `sum by() (task:errors:rate10s{job="s"})`, 61 out: `sum(task:errors:rate10s{job="s"})`, 62 }, 63 { 64 in: `sum by(code) (task:errors:rate10s{job="s"})`, 65 }, 66 { 67 in: `sum without() (task:errors:rate10s{job="s"})`, 68 }, 69 { 70 in: `sum without(instance) (task:errors:rate10s{job="s"})`, 71 }, 72 { 73 in: `topk(5, task:errors:rate10s{job="s"})`, 74 }, 75 { 76 in: `count_values("value", task:errors:rate10s{job="s"})`, 77 }, 78 { 79 in: `a - on() c`, 80 }, 81 { 82 in: `a - on(b) c`, 83 }, 84 { 85 in: `a - on(b) group_left(x) c`, 86 }, 87 { 88 in: `a - on(b) group_left(x, y) c`, 89 }, 90 { 91 in: `a - on(b) group_left c`, 92 out: `a - on(b) group_left() c`, 93 }, 94 { 95 in: `a - on(b) group_left() (c)`, 96 }, 97 { 98 in: `a - ignoring(b) c`, 99 }, 100 { 101 in: `a - ignoring() c`, 102 out: `a - c`, 103 }, 104 { 105 in: `up > bool 0`, 106 }, 107 { 108 in: `a offset 1m`, 109 }, 110 { 111 in: `a{c="d"}[5m] offset 1m`, 112 }, 113 { 114 in: `a[5m] offset 1m`, 115 }, 116 } 117 118 for _, test := range inputs { 119 expr, err := ParseExpr(test.in) 120 if err != nil { 121 t.Fatalf("parsing error for %q: %s", test.in, err) 122 } 123 exp := test.in 124 if test.out != "" { 125 exp = test.out 126 } 127 if expr.String() != exp { 128 t.Fatalf("expected %q to be returned as:\n%s\ngot:\n%s\n", test.in, exp, expr.String()) 129 } 130 } 131 } 132 133 func TestStmtsString(t *testing.T) { 134 // A list of valid statements that are expected to be returned as out when 135 // calling String(). If out is empty the output is expected to equal the 136 // input. 137 inputs := []struct { 138 in, out string 139 }{ 140 { 141 in: `ALERT foo IF up == 0 FOR 1m`, 142 out: "ALERT foo\n\tIF up == 0\n\tFOR 1m", 143 }, 144 { 145 in: `ALERT foo IF up == 0 FOR 1m ANNOTATIONS {summary="foo"}`, 146 out: "ALERT foo\n\tIF up == 0\n\tFOR 1m\n\tANNOTATIONS {summary=\"foo\"}", 147 }, 148 } 149 150 for _, test := range inputs { 151 expr, err := ParseStmts(test.in) 152 if err != nil { 153 t.Fatalf("parsing error for %q: %s", test.in, err) 154 } 155 exp := test.in 156 if test.out != "" { 157 exp = test.out 158 } 159 if expr.String() != exp { 160 t.Fatalf("expected %q to be returned as:\n%s\ngot:\n%s\n", test.in, exp, expr.String()) 161 } 162 } 163 }