Skip to content

Commit

Permalink
add test cases for testFilter
Browse files Browse the repository at this point in the history
Signed-off-by: Mindaugas Niaura <mindaugas.niaura@vinted.com>
  • Loading branch information
niaurys committed Sep 30, 2024
1 parent 7398bc0 commit 4f8d78a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
55 changes: 55 additions & 0 deletions pkg/store/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/thanos-io/thanos/pkg/block"
"github.com/thanos-io/thanos/pkg/component"
"github.com/thanos-io/thanos/pkg/filter"
"github.com/thanos-io/thanos/pkg/info/infopb"
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/store/storepb"
Expand Down Expand Up @@ -1804,6 +1805,10 @@ func seriesEquals(t *testing.T, expected []rawSeries, got []*storepb.Series) {
}

func TestStoreMatches(t *testing.T) {
testMetricName := "test_metric"
testFilter := filter.NewCuckooFilterMetricNameFilter(10)
testFilter.ResetAddMetricName(testMetricName)

for _, c := range []struct {
s Client
mint, maxt int64
Expand Down Expand Up @@ -1925,6 +1930,35 @@ func TestStoreMatches(t *testing.T) {
maxt: 1,
expectedMatch: true,
},
{
s: &storetestutil.TestClient{
ExtLset: []labels.Labels{
labels.FromStrings("a", "b"),
},
MetricNameFilter: testFilter,
},
ms: []*labels.Matcher{
labels.MustNewMatcher(labels.MatchEqual, "a", "b"),
labels.MustNewMatcher(labels.MatchEqual, labels.MetricName, testMetricName),
},
maxt: 1,
expectedMatch: true,
},
{
s: &storetestutil.TestClient{
ExtLset: []labels.Labels{
labels.FromStrings("a", "b"),
},
MetricNameFilter: testFilter,
},
ms: []*labels.Matcher{
labels.MustNewMatcher(labels.MatchEqual, "a", "b"),
labels.MustNewMatcher(labels.MatchEqual, labels.MetricName, "some_other_metric"),
},
maxt: 1,
expectedMatch: false,
expectedReason: "metric name some_other_metric does not match filter",
},
} {
t.Run("", func(t *testing.T) {
ok, reason := storeMatches(context.TODO(), c.s, c.mint, c.maxt, c.ms...)
Expand Down Expand Up @@ -2270,6 +2304,27 @@ func TestProxyStore_storeMatchMetadata(t *testing.T) {
testutil.Equals(t, "", reason)
}

func TestProxyStore_MatchesMetricName(t *testing.T) {
c := storetestutil.TestClient{Name: "matchesmetricname"}
c.MetricNameFilter = filter.AllowAllMetricNameFilter{}
c.IsLocalStore = true

ok, reason := storeMatchDebugMetadata(c, [][]*labels.Matcher{{}})
testutil.Assert(t, !ok)
testutil.Equals(t, "the store is not remote, cannot match __address__", reason)

// Change client to remote.
c.IsLocalStore = false

ok, reason = storeMatchDebugMetadata(c, [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchEqual, "__address__", "wrong")}})
testutil.Assert(t, !ok)
testutil.Equals(t, "__address__ testaddr does not match debug store metadata matchers: [[__address__=\"wrong\"]]", reason)

ok, reason = storeMatchDebugMetadata(c, [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchEqual, "__address__", "testaddr")}})
testutil.Assert(t, ok)
testutil.Equals(t, "", reason)
}

func TestDedupRespHeap_Deduplication(t *testing.T) {
t.Parallel()

Expand Down
6 changes: 5 additions & 1 deletion pkg/store/storepb/testutil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package storetestutil
import (
"github.com/prometheus/prometheus/model/labels"

"github.com/thanos-io/thanos/pkg/filter"
"github.com/thanos-io/thanos/pkg/info/infopb"
"github.com/thanos-io/thanos/pkg/store/storepb"
)
Expand All @@ -21,6 +22,7 @@ type TestClient struct {
WithoutReplicaLabelsEnabled bool
IsLocalStore bool
StoreTSDBInfos []*infopb.TSDBInfo
MetricNameFilter filter.MetricNameFilter
}

func (c TestClient) LabelSets() []labels.Labels { return c.ExtLset }
Expand All @@ -30,4 +32,6 @@ func (c TestClient) SupportsSharding() bool { return c.Shardable }
func (c TestClient) SupportsWithoutReplicaLabels() bool { return c.WithoutReplicaLabelsEnabled }
func (c TestClient) String() string { return c.Name }
func (c TestClient) Addr() (string, bool) { return c.Name, c.IsLocalStore }
func (c TestClient) MatchesMetricName(_ string) bool { return true }
func (c TestClient) MatchesMetricName(metricName string) bool {
return c.MetricNameFilter.MatchesMetricName(metricName)
}

0 comments on commit 4f8d78a

Please sign in to comment.