Skip to content

Commit

Permalink
Improve fetcher unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
  • Loading branch information
kakkoyun committed Mar 10, 2020
1 parent d99f86c commit 9c4effe
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions pkg/block/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,27 +865,52 @@ func TestDeduplicateFilter_Filter(t *testing.T) {
}
}

func TestReplicaLabel_Filter(t *testing.T) {
func TestReplicaLabelRemover_Modify(t *testing.T) {
rm := NewReplicaLabelRemover([]string{"replica", "rule_replica"})

input := map[ulid.ULID]*metadata.Meta{
ULID(1): {Thanos: metadata.Thanos{Labels: map[string]string{"message": "something"}}},
ULID(2): {Thanos: metadata.Thanos{Labels: map[string]string{"replica": "cluster1", "message": "something"}}},
ULID(3): {Thanos: metadata.Thanos{Labels: map[string]string{"replica": "cluster1", "rule_replica": "rule1", "message": "something"}}},
ULID(4): {Thanos: metadata.Thanos{Labels: map[string]string{"replica": "cluster1", "rule_replica": "rule1"}}},
}
expected := map[ulid.ULID]*metadata.Meta{
ULID(1): {Thanos: metadata.Thanos{Labels: map[string]string{"message": "something"}}},
ULID(2): {Thanos: metadata.Thanos{Labels: map[string]string{"message": "something"}}},
ULID(3): {Thanos: metadata.Thanos{Labels: map[string]string{"message": "something"}}},
ULID(4): {Thanos: metadata.Thanos{Labels: map[string]string{}}},
}

m := newTestSyncMetrics()
rm.Modify(input, m, false)
for _, tcase := range []struct {
name string
input map[ulid.ULID]*metadata.Meta
expected map[ulid.ULID]*metadata.Meta
modified float64
}{
{
name: "without replica labels",
input: map[ulid.ULID]*metadata.Meta{
ULID(1): {Thanos: metadata.Thanos{Labels: map[string]string{"message": "something"}}},
ULID(2): {Thanos: metadata.Thanos{Labels: map[string]string{"message": "something"}}},
ULID(3): {Thanos: metadata.Thanos{Labels: map[string]string{"message": "something1"}}},
},
expected: map[ulid.ULID]*metadata.Meta{
ULID(1): {Thanos: metadata.Thanos{Labels: map[string]string{"message": "something"}}},
ULID(2): {Thanos: metadata.Thanos{Labels: map[string]string{"message": "something"}}},
ULID(3): {Thanos: metadata.Thanos{Labels: map[string]string{"message": "something1"}}},
},
modified: 0,
},
{
name: "with replica labels",
input: map[ulid.ULID]*metadata.Meta{
ULID(1): {Thanos: metadata.Thanos{Labels: map[string]string{"message": "something"}}},
ULID(2): {Thanos: metadata.Thanos{Labels: map[string]string{"replica": "cluster1", "message": "something"}}},
ULID(3): {Thanos: metadata.Thanos{Labels: map[string]string{"replica": "cluster1", "rule_replica": "rule1", "message": "something"}}},
ULID(4): {Thanos: metadata.Thanos{Labels: map[string]string{"replica": "cluster1", "rule_replica": "rule1"}}},
},
expected: map[ulid.ULID]*metadata.Meta{
ULID(1): {Thanos: metadata.Thanos{Labels: map[string]string{"message": "something"}}},
ULID(2): {Thanos: metadata.Thanos{Labels: map[string]string{"message": "something"}}},
ULID(3): {Thanos: metadata.Thanos{Labels: map[string]string{"message": "something"}}},
ULID(4): {Thanos: metadata.Thanos{Labels: map[string]string{}}},
},
modified: 5.0,
},
} {
m := newTestSyncMetrics()
rm.Modify(tcase.input, m, false)

testutil.Equals(t, 5.0, promtest.ToFloat64(m.modified.WithLabelValues(replicaRemovedMeta)))
testutil.Equals(t, expected, input)
testutil.Equals(t, tcase.modified, promtest.ToFloat64(m.modified.WithLabelValues(replicaRemovedMeta)))
testutil.Equals(t, tcase.expected, tcase.input)
}
}

func compareSliceWithMapKeys(tb testing.TB, m map[ulid.ULID]*metadata.Meta, s []ulid.ULID) {
Expand Down

0 comments on commit 9c4effe

Please sign in to comment.