Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Ryabov committed Apr 26, 2020
1 parent afeb1da commit 504a43b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/block/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ func (r *ReplicaLabelRemover) Modify(_ context.Context, metas map[ulid.ULID]*met
modified.WithLabelValues(replicaRemovedMeta).Inc()
}
}
if len(l) == 0 {
if len(l) == 0 && len(r.labelIfEmpty) == 2 {
l[r.labelIfEmpty[0]] = r.labelIfEmpty[1]
}
metas[u].Thanos.Labels = l
Expand Down
52 changes: 51 additions & 1 deletion pkg/block/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ func TestDeduplicateFilter_Filter(t *testing.T) {
func TestReplicaLabelRemover_Modify(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
rm := NewReplicaLabelRemover(log.NewNopLogger(), []string{"replica", "rule_replica"})
rm := NewReplicaLabelRemover(log.NewNopLogger(), []string{"replica", "rule_replica"}, []string{})

for _, tcase := range []struct {
name string
Expand Down Expand Up @@ -927,6 +927,56 @@ func TestReplicaLabelRemover_Modify(t *testing.T) {
}
}

func TestReplicaLabelRemover_ModifyEmpty(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
rm := NewReplicaLabelRemover(log.NewNopLogger(), []string{"replica"}, []string{"replica", "receive"})

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", "message": "something"}}},
ULID(4): {Thanos: metadata.Thanos{Labels: map[string]string{"replica": "cluster1"}}},
},
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{"replica": "receive"}}},
},
modified: 3.0,
},
} {
m := newTestFetcherMetrics()
testutil.Ok(t, rm.Modify(ctx, tcase.input, m.modified))

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) {
_, file, line, _ := runtime.Caller(1)
matching := true
Expand Down

0 comments on commit 504a43b

Please sign in to comment.