| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package selector |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/prometheus/prometheus/model/labels" |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | "github.com/stretchr/testify/require" |
| 11 | ) |
| 12 | |
| 13 | func TestExpr_Empty(t *testing.T) { |
| 14 | tests := map[string]struct { |
| 15 | expr Expr |
| 16 | expected bool |
| 17 | }{ |
| 18 | "empty: both allow and deny": { |
| 19 | expr: Expr{Allow: []string{}, Deny: []string{}}, |
| 20 | expected: true, |
| 21 | }, |
| 22 | "nil: both allow and deny": { |
| 23 | expected: true, |
| 24 | }, |
| 25 | "nil, empty: allow, deny": { |
| 26 | expr: Expr{Deny: []string{""}}, |
| 27 | expected: false, |
| 28 | }, |
| 29 | "empty, nil: allow, deny": { |
| 30 | expr: Expr{Allow: []string{""}}, |
| 31 | expected: false, |
| 32 | }, |
| 33 | } |
| 34 | |
| 35 | for name, test := range tests { |
| 36 | t.Run(name, func(t *testing.T) { |
| 37 | assert.Equal(t, test.expected, test.expr.Empty()) |
| 38 | }) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestExpr_Parse(t *testing.T) { |
| 43 | tests := map[string]struct { |
| 44 | expr Expr |
| 45 | series labels.Labels |
| 46 | expected bool |
| 47 | expectedNil bool |
| 48 | expectedErr bool |
| 49 | }{ |
| 50 | "not set: both allow and deny": { |
| 51 | expr: Expr{}, |
| 52 | expectedNil: true, |
| 53 | }, |
| 54 | "set: both allow and deny": { |
| 55 | expr: Expr{ |
| 56 | Allow: []string{"go_memstats_*", "node_*"}, |
| 57 | Deny: []string{"go_memstats_frees_total", "node_cooling_*"}, |
| 58 | }, |
| 59 | expected: true, |
| 60 | series: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, |
| 61 | }, |
| 62 | "allow no match": { |
| 63 | expr: Expr{Allow: []string{"node_*"}}, |
| 64 | expected: false, |
| 65 | series: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, |
| 66 | }, |
| 67 | "invalid selector": { |
| 68 | expr: Expr{Allow: []string{`metric{label="x",}`}}, |
| 69 | expectedErr: true, |
| 70 | }, |
| 71 | } |
| 72 | |
| 73 | for name, test := range tests { |
| 74 | t.Run(name, func(t *testing.T) { |
| 75 | m, err := test.expr.Parse() |
| 76 | if test.expectedErr { |
| 77 | require.Error(t, err) |
| 78 | return |
| 79 | } |
| 80 | require.NoError(t, err) |
| 81 | if test.expectedNil { |
| 82 | assert.Nil(t, m) |
| 83 | return |
| 84 | } |
| 85 | require.NotNil(t, m) |
| 86 | assert.Equal(t, test.expected, m.Matches(test.series)) |
| 87 | }) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func TestExprSelector_Matches(t *testing.T) { |
| 92 | tests := map[string]struct { |
| 93 | expr Expr |
| 94 | lbs labels.Labels |
| 95 | expectedMatches bool |
| 96 | }{ |
| 97 | "allow matches": { |
| 98 | expr: Expr{Allow: []string{"go_*"}}, |
| 99 | lbs: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, |
| 100 | expectedMatches: true, |
| 101 | }, |
| 102 | "deny matches": { |
| 103 | expr: Expr{Deny: []string{"go_*"}}, |
| 104 | lbs: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, |
| 105 | expectedMatches: false, |
| 106 | }, |
| 107 | "allow and deny": { |
| 108 | expr: Expr{Allow: []string{"go_*"}, Deny: []string{"go_*"}}, |
| 109 | lbs: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, |
| 110 | expectedMatches: false, |
| 111 | }, |
| 112 | } |
| 113 | |
| 114 | for name, test := range tests { |
| 115 | t.Run(name, func(t *testing.T) { |
| 116 | sr, err := test.expr.Parse() |
| 117 | require.NoError(t, err) |
| 118 | assert.Equal(t, test.expectedMatches, sr.Matches(test.lbs)) |
| 119 | }) |
| 120 | } |
| 121 | } |