Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
santileira committed Jan 18, 2025
1 parent e502c90 commit 4b55295
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
20 changes: 19 additions & 1 deletion pkg/querier/distributor_queryable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,13 +640,14 @@ func TestDistributorQuerier_Select_CounterResets(t *testing.T) {
func TestDistributorQuerier_LabelNames(t *testing.T) {
const mint, maxt = 0, 10

hints := &storage.LabelHints{Limit: 1}
someMatchers := []*labels.Matcher{labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")}
labelNames := []string{"foo", "job"}

t.Run("with matchers", func(t *testing.T) {
t.Run("queryLabelNamesWithMatchers=true", func(t *testing.T) {
d := &mockDistributor{}
d.On("LabelNames", mock.Anything, model.Time(mint), model.Time(maxt), someMatchers).
d.On("LabelNames", mock.Anything, model.Time(mint), model.Time(maxt), &storage.LabelHints{}, someMatchers).
Return(labelNames, nil)
ctx := user.InjectOrgID(context.Background(), "0")
queryable := NewDistributorQueryable(d, newMockConfigProvider(0), nil, log.NewNopLogger())
Expand All @@ -659,6 +660,23 @@ func TestDistributorQuerier_LabelNames(t *testing.T) {
assert.Equal(t, labelNames, names)
})
})

t.Run("with hints", func(t *testing.T) {
t.Run("queryLabelNamesWithMatchers=true", func(t *testing.T) {
d := &mockDistributor{}
d.On("LabelNames", mock.Anything, model.Time(mint), model.Time(maxt), hints, []*labels.Matcher{}).
Return(labelNames[:1], nil)
ctx := user.InjectOrgID(context.Background(), "0")
queryable := NewDistributorQueryable(d, newMockConfigProvider(0), nil, log.NewNopLogger())
querier, err := queryable.Querier(mint, maxt)
require.NoError(t, err)

names, warnings, err := querier.LabelNames(ctx, hints, []*labels.Matcher{}...)
require.NoError(t, err)
assert.Empty(t, warnings)
assert.Equal(t, labelNames[:1], names)
})
})
}

func BenchmarkDistributorQuerier_Select(b *testing.B) {
Expand Down
18 changes: 11 additions & 7 deletions pkg/querier/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,16 +965,17 @@ func TestQuerier_ValidateQueryTimeRange_MaxQueryLookback(t *testing.T) {
matchers := []*labels.Matcher{
labels.MustNewMatcher(labels.MatchNotEqual, "route", "get_user"),
}
hints := &storage.LabelHints{Limit: 1}
distributor := &mockDistributor{}
distributor.On("LabelNames", mock.Anything, mock.Anything, mock.Anything, matchers).Return([]string{}, nil)
distributor.On("LabelNames", mock.Anything, mock.Anything, mock.Anything, hints, matchers).Return([]string{}, nil)

queryable, _, _, err := New(cfg, overrides, distributor, nil, nil, logger, nil)
require.NoError(t, err)

q, err := queryable.Querier(util.TimeToMillis(testData.queryStartTime), util.TimeToMillis(testData.queryEndTime))
require.NoError(t, err)

_, _, err = q.LabelNames(ctx, &storage.LabelHints{}, matchers...)
_, _, err = q.LabelNames(ctx, hints, matchers...)
require.NoError(t, err)

if !testData.expectedSkipped {
Expand All @@ -985,24 +986,26 @@ func TestQuerier_ValidateQueryTimeRange_MaxQueryLookback(t *testing.T) {
args := distributor.Calls[0].Arguments
assert.InDelta(t, util.TimeToMillis(testData.expectedMetadataStartTime), int64(args.Get(1).(model.Time)), delta)
assert.InDelta(t, util.TimeToMillis(testData.expectedMetadataEndTime), int64(args.Get(2).(model.Time)), delta)
assert.Equal(t, matchers, args.Get(3).([]*labels.Matcher))
assert.Equal(t, hints, args.Get(3).(*storage.LabelHints))
assert.Equal(t, matchers, args.Get(4).([]*labels.Matcher))
} else {
// Ensure no query has been executed (because skipped).
assert.Len(t, distributor.Calls, 0)
}
})

t.Run("label values", func(t *testing.T) {
hints := &storage.LabelHints{Limit: 1}
distributor := &mockDistributor{}
distributor.On("LabelValuesForLabelName", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]string{}, nil)
distributor.On("LabelValuesForLabelName", mock.Anything, mock.Anything, mock.Anything, mock.Anything, hints, mock.Anything).Return([]string{}, nil)

queryable, _, _, err := New(cfg, overrides, distributor, nil, nil, logger, nil)
require.NoError(t, err)

q, err := queryable.Querier(util.TimeToMillis(testData.queryStartTime), util.TimeToMillis(testData.queryEndTime))
require.NoError(t, err)

_, _, err = q.LabelValues(ctx, labels.MetricName, &storage.LabelHints{})
_, _, err = q.LabelValues(ctx, labels.MetricName, hints)
require.NoError(t, err)

if !testData.expectedSkipped {
Expand All @@ -1012,6 +1015,7 @@ func TestQuerier_ValidateQueryTimeRange_MaxQueryLookback(t *testing.T) {
assert.Equal(t, "LabelValuesForLabelName", distributor.Calls[0].Method)
assert.InDelta(t, util.TimeToMillis(testData.expectedMetadataStartTime), int64(distributor.Calls[0].Arguments.Get(1).(model.Time)), delta)
assert.InDelta(t, util.TimeToMillis(testData.expectedMetadataEndTime), int64(distributor.Calls[0].Arguments.Get(2).(model.Time)), delta)
assert.Equal(t, hints, distributor.Calls[0].Arguments.Get(4).(*storage.LabelHints))
} else {
// Ensure no query has been executed (because skipped).
assert.Len(t, distributor.Calls, 0)
Expand Down Expand Up @@ -1213,11 +1217,11 @@ func (d *emptyDistributor) QueryExemplars(context.Context, model.Time, model.Tim
return nil, nil
}

func (d *emptyDistributor) LabelValuesForLabelName(context.Context, model.Time, model.Time, model.LabelName, ...*labels.Matcher) ([]string, error) {
func (d *emptyDistributor) LabelValuesForLabelName(context.Context, model.Time, model.Time, model.LabelName, *storage.LabelHints, ...*labels.Matcher) ([]string, error) {
return nil, nil
}

func (d *emptyDistributor) LabelNames(context.Context, model.Time, model.Time, ...*labels.Matcher) ([]string, error) {
func (d *emptyDistributor) LabelNames(context.Context, model.Time, model.Time, *storage.LabelHints, ...*labels.Matcher) ([]string, error) {
return nil, nil
}

Expand Down

0 comments on commit 4b55295

Please sign in to comment.