github.com/netdata/go.d.plugin@v0.58.1/pkg/prometheus/selector/expr_test.go (about) 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{ 20 Allow: []string{}, 21 Deny: []string{}, 22 }, 23 expected: true, 24 }, 25 "nil: both allow and deny": { 26 expected: true, 27 }, 28 "nil, empty: allow, deny": { 29 expr: Expr{ 30 Deny: []string{""}, 31 }, 32 expected: false, 33 }, 34 "empty, nil: allow, deny": { 35 expr: Expr{ 36 Allow: []string{""}, 37 }, 38 expected: false, 39 }, 40 } 41 42 for name, test := range tests { 43 t.Run(name, func(t *testing.T) { 44 if test.expected { 45 assert.True(t, test.expr.Empty()) 46 } else { 47 assert.False(t, test.expr.Empty()) 48 } 49 }) 50 } 51 } 52 53 func TestExpr_Parse(t *testing.T) { 54 tests := map[string]struct { 55 expr Expr 56 expectedSr Selector 57 expectedErr bool 58 }{ 59 "not set: both allow and deny": { 60 expr: Expr{}, 61 }, 62 "set: both allow and deny": { 63 expr: Expr{ 64 Allow: []string{ 65 "go_memstats_*", 66 "node_*", 67 }, 68 Deny: []string{ 69 "go_memstats_frees_total", 70 "node_cooling_*", 71 }, 72 }, 73 expectedSr: andSelector{ 74 lhs: orSelector{ 75 lhs: mustSPName("go_memstats_*"), 76 rhs: mustSPName("node_*"), 77 }, 78 rhs: Not(orSelector{ 79 lhs: mustSPName("go_memstats_frees_total"), 80 rhs: mustSPName("node_cooling_*"), 81 }), 82 }, 83 }, 84 "set: only includes": { 85 expr: Expr{ 86 Allow: []string{ 87 "go_memstats_*", 88 "node_*", 89 }, 90 }, 91 expectedSr: andSelector{ 92 lhs: orSelector{ 93 lhs: mustSPName("go_memstats_*"), 94 rhs: mustSPName("node_*"), 95 }, 96 rhs: Not(falseSelector{}), 97 }, 98 }, 99 "set: only excludes": { 100 expr: Expr{ 101 Deny: []string{ 102 "go_memstats_frees_total", 103 "node_cooling_*", 104 }, 105 }, 106 expectedSr: andSelector{ 107 lhs: trueSelector{}, 108 rhs: Not(orSelector{ 109 lhs: mustSPName("go_memstats_frees_total"), 110 rhs: mustSPName("node_cooling_*"), 111 }), 112 }, 113 }, 114 } 115 116 for name, test := range tests { 117 t.Run(name, func(t *testing.T) { 118 m, err := test.expr.Parse() 119 120 if test.expectedErr { 121 assert.Error(t, err) 122 } else { 123 assert.Equal(t, test.expectedSr, m) 124 } 125 }) 126 } 127 } 128 129 func TestExprSelector_Matches(t *testing.T) { 130 tests := map[string]struct { 131 expr Expr 132 lbs labels.Labels 133 expectedMatches bool 134 }{ 135 "allow matches: single pattern": { 136 expr: Expr{ 137 Allow: []string{"go_*"}, 138 }, 139 lbs: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, 140 expectedMatches: true, 141 }, 142 "allow matches: several patterns": { 143 expr: Expr{ 144 Allow: []string{"node_*", "go_*"}, 145 }, 146 lbs: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, 147 expectedMatches: true, 148 }, 149 "allow not matches": { 150 expr: Expr{ 151 Allow: []string{"node_*"}, 152 }, 153 lbs: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, 154 expectedMatches: false, 155 }, 156 "deny matches: single pattern": { 157 expr: Expr{ 158 Deny: []string{"go_*"}, 159 }, 160 lbs: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, 161 expectedMatches: false, 162 }, 163 "deny matches: several patterns": { 164 expr: Expr{ 165 Deny: []string{"node_*", "go_*"}, 166 }, 167 lbs: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, 168 expectedMatches: false, 169 }, 170 "deny not matches": { 171 expr: Expr{ 172 Deny: []string{"node_*"}, 173 }, 174 lbs: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, 175 expectedMatches: true, 176 }, 177 "allow and deny matches: single pattern": { 178 expr: Expr{ 179 Allow: []string{"go_*"}, 180 Deny: []string{"go_*"}, 181 }, 182 lbs: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, 183 expectedMatches: false, 184 }, 185 "allow and deny matches: several patterns": { 186 expr: Expr{ 187 Allow: []string{"node_*", "go_*"}, 188 Deny: []string{"node_*", "go_*"}, 189 }, 190 lbs: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, 191 expectedMatches: false, 192 }, 193 "allow matches and deny not matches": { 194 expr: Expr{ 195 Allow: []string{"go_*"}, 196 Deny: []string{"node_*"}, 197 }, 198 lbs: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, 199 expectedMatches: true, 200 }, 201 "allow not matches and deny matches": { 202 expr: Expr{ 203 Allow: []string{"node_*"}, 204 Deny: []string{"go_*"}, 205 }, 206 lbs: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, 207 expectedMatches: false, 208 }, 209 "allow not matches and deny not matches": { 210 expr: Expr{ 211 Allow: []string{"node_*"}, 212 Deny: []string{"node_*"}, 213 }, 214 lbs: []labels.Label{{Name: labels.MetricName, Value: "go_memstats_alloc_bytes"}}, 215 expectedMatches: false, 216 }, 217 } 218 219 for name, test := range tests { 220 t.Run(name, func(t *testing.T) { 221 sr, err := test.expr.Parse() 222 require.NoError(t, err) 223 224 if test.expectedMatches { 225 assert.True(t, sr.Matches(test.lbs)) 226 } else { 227 assert.False(t, sr.Matches(test.lbs)) 228 } 229 }) 230 } 231 }