github.com/thanos-io/thanos@v0.32.5/pkg/exemplars/tsdb_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package exemplars 5 6 import ( 7 "testing" 8 9 "github.com/prometheus/prometheus/model/labels" 10 11 "github.com/efficientgo/core/testutil" 12 ) 13 14 func TestSelectorsMatchExternalLabels(t *testing.T) { 15 t.Parallel() 16 17 tests := map[string]struct { 18 selectors [][]*labels.Matcher 19 extLabels labels.Labels 20 shouldMatch bool 21 expectedSelectors [][]*labels.Matcher 22 }{ 23 "should return true for matching labels": { 24 selectors: [][]*labels.Matcher{ 25 { 26 labels.MustNewMatcher(labels.MatchEqual, "code", "200"), 27 labels.MustNewMatcher(labels.MatchEqual, "receive", "true"), 28 }, 29 }, 30 extLabels: labels.FromStrings("receive", "true"), 31 shouldMatch: true, 32 expectedSelectors: [][]*labels.Matcher{ 33 { 34 labels.MustNewMatcher(labels.MatchEqual, "code", "200"), 35 }, 36 }, 37 }, 38 "should return true when the external labels are not present in input at all": { 39 selectors: [][]*labels.Matcher{ 40 { 41 labels.MustNewMatcher(labels.MatchEqual, "code", "200"), 42 }, 43 }, 44 extLabels: labels.FromStrings("receive", "true"), 45 shouldMatch: true, 46 expectedSelectors: [][]*labels.Matcher{ 47 { 48 labels.MustNewMatcher(labels.MatchEqual, "code", "200"), 49 }, 50 }, 51 }, 52 "should return true when only some of matchers slice are matching": { 53 selectors: [][]*labels.Matcher{ 54 { 55 labels.MustNewMatcher(labels.MatchEqual, "code", "200"), 56 labels.MustNewMatcher(labels.MatchRegexp, "receive", "false"), 57 }, 58 { 59 labels.MustNewMatcher(labels.MatchEqual, "code", "500"), 60 labels.MustNewMatcher(labels.MatchRegexp, "receive", "true"), 61 }, 62 }, 63 extLabels: labels.FromStrings("receive", "true", "replica", "0"), 64 shouldMatch: true, 65 expectedSelectors: [][]*labels.Matcher{ 66 { 67 labels.MustNewMatcher(labels.MatchEqual, "code", "500"), 68 }, 69 }, 70 }, 71 "should return false when the external labels are not matching": { 72 selectors: [][]*labels.Matcher{ 73 { 74 labels.MustNewMatcher(labels.MatchEqual, "code", "200"), 75 labels.MustNewMatcher(labels.MatchNotEqual, "replica", "1"), 76 }, 77 { 78 labels.MustNewMatcher(labels.MatchEqual, "op", "get"), 79 labels.MustNewMatcher(labels.MatchEqual, "replica", "0"), 80 }, 81 }, 82 extLabels: labels.FromStrings("replica", "1"), 83 shouldMatch: false, 84 expectedSelectors: nil, 85 }, 86 } 87 88 for name, tdata := range tests { 89 t.Run(name, func(t *testing.T) { 90 match, selectors := selectorsMatchesExternalLabels(tdata.selectors, tdata.extLabels) 91 testutil.Equals(t, tdata.shouldMatch, match) 92 93 testutil.Equals(t, tdata.expectedSelectors, selectors) 94 }) 95 } 96 }