github.com/thanos-io/thanos@v0.32.5/pkg/store/storepb/shard_info_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package storepb 5 6 import ( 7 "sync" 8 "testing" 9 10 "github.com/alecthomas/units" 11 12 "github.com/prometheus/prometheus/model/labels" 13 "github.com/thanos-io/thanos/pkg/store/labelpb" 14 ) 15 16 func TestShardInfo_MatchesSeries(t *testing.T) { 17 series := labelpb.ZLabelsFromPromLabels(labels.FromStrings( 18 "pod", "nginx", 19 "node", "node-1", 20 "container", "nginx", 21 )) 22 23 tests := []struct { 24 name string 25 shardInfo *ShardInfo 26 series []labelpb.ZLabel 27 matches bool 28 }{ 29 { 30 name: "nil shard info", 31 shardInfo: nil, 32 matches: true, 33 }, 34 { 35 name: "one shard only", 36 series: series, 37 shardInfo: &ShardInfo{ 38 ShardIndex: 0, 39 TotalShards: 1, 40 By: true, 41 Labels: nil, 42 }, 43 matches: true, 44 }, 45 { 46 name: "shard by empty sharding labels", 47 series: series, 48 shardInfo: &ShardInfo{ 49 ShardIndex: 0, 50 TotalShards: 2, 51 By: true, 52 Labels: nil, 53 }, 54 matches: false, 55 }, 56 { 57 name: "shard without empty sharding labels", 58 series: series, 59 shardInfo: &ShardInfo{ 60 ShardIndex: 0, 61 TotalShards: 2, 62 By: false, 63 Labels: nil, 64 }, 65 matches: true, 66 }, 67 { 68 name: "shard by labels for shard 0", 69 series: series, 70 shardInfo: &ShardInfo{ 71 ShardIndex: 0, 72 TotalShards: 2, 73 By: true, 74 Labels: []string{"pod", "node"}, 75 }, 76 matches: false, 77 }, 78 { 79 name: "shard by labels for shard 1", 80 series: series, 81 shardInfo: &ShardInfo{ 82 ShardIndex: 1, 83 TotalShards: 2, 84 By: true, 85 Labels: []string{"pod", "node"}, 86 }, 87 matches: true, 88 }, 89 { 90 name: "shard without labels for shard 0", 91 series: series, 92 shardInfo: &ShardInfo{ 93 ShardIndex: 0, 94 TotalShards: 2, 95 By: false, 96 Labels: []string{"node"}, 97 }, 98 matches: true, 99 }, 100 { 101 name: "shard without labels for shard 1", 102 series: series, 103 shardInfo: &ShardInfo{ 104 ShardIndex: 1, 105 TotalShards: 2, 106 By: false, 107 Labels: []string{"node"}, 108 }, 109 matches: false, 110 }, 111 } 112 113 buffers := sync.Pool{New: func() interface{} { 114 b := make([]byte, 0, 10*units.Kilobyte) 115 return &b 116 }} 117 for _, test := range tests { 118 t.Run(test.name, func(t *testing.T) { 119 matcher := test.shardInfo.Matcher(&buffers) 120 defer matcher.Close() 121 isMatch := matcher.MatchesZLabels(test.series) 122 if isMatch != test.matches { 123 t.Fatalf("invalid result, got %t, want %t", isMatch, test.matches) 124 } 125 }) 126 } 127 }