github.com/thanos-io/thanos@v0.32.5/pkg/query/remote_engine_test.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package query
     5  
     6  import (
     7  	"math"
     8  	"testing"
     9  
    10  	"github.com/efficientgo/core/testutil"
    11  	"github.com/go-kit/log"
    12  	"github.com/prometheus/prometheus/model/labels"
    13  
    14  	"github.com/thanos-io/thanos/pkg/info/infopb"
    15  	"github.com/thanos-io/thanos/pkg/store/labelpb"
    16  )
    17  
    18  func TestRemoteEngine_LabelSets(t *testing.T) {
    19  	tests := []struct {
    20  		name          string
    21  		tsdbInfos     []infopb.TSDBInfo
    22  		replicaLabels []string
    23  		expected      []labels.Labels
    24  	}{
    25  		{
    26  			name:      "empty label sets",
    27  			tsdbInfos: []infopb.TSDBInfo{},
    28  			expected:  []labels.Labels{},
    29  		},
    30  		{
    31  			name:          "empty label sets with replica labels",
    32  			tsdbInfos:     []infopb.TSDBInfo{},
    33  			replicaLabels: []string{"replica"},
    34  			expected:      []labels.Labels{},
    35  		},
    36  		{
    37  			name: "non-empty label sets",
    38  			tsdbInfos: []infopb.TSDBInfo{{
    39  				Labels: zLabelSetFromStrings("a", "1"),
    40  			}},
    41  			expected: []labels.Labels{labels.FromStrings("a", "1")},
    42  		},
    43  		{
    44  			name: "non-empty label sets with replica labels",
    45  			tsdbInfos: []infopb.TSDBInfo{{
    46  				Labels: zLabelSetFromStrings("a", "1", "b", "2"),
    47  			}},
    48  			replicaLabels: []string{"a"},
    49  			expected:      []labels.Labels{labels.FromStrings("b", "2")},
    50  		},
    51  		{
    52  			name: "replica labels not in label sets",
    53  			tsdbInfos: []infopb.TSDBInfo{
    54  				{
    55  					Labels: zLabelSetFromStrings("a", "1", "c", "2"),
    56  				},
    57  			},
    58  			replicaLabels: []string{"a", "b"},
    59  			expected:      []labels.Labels{labels.FromStrings("c", "2")},
    60  		},
    61  	}
    62  
    63  	for _, testCase := range tests {
    64  		t.Run(testCase.name, func(t *testing.T) {
    65  			client := NewClient(nil, "", testCase.tsdbInfos)
    66  			engine := newRemoteEngine(log.NewNopLogger(), client, Opts{
    67  				ReplicaLabels: testCase.replicaLabels,
    68  			})
    69  
    70  			testutil.Equals(t, testCase.expected, engine.LabelSets())
    71  		})
    72  	}
    73  }
    74  
    75  func TestRemoteEngine_MinT(t *testing.T) {
    76  	tests := []struct {
    77  		name          string
    78  		tsdbInfos     []infopb.TSDBInfo
    79  		replicaLabels []string
    80  		expected      int64
    81  	}{
    82  		{
    83  			name:      "empty label sets",
    84  			tsdbInfos: []infopb.TSDBInfo{},
    85  			expected:  math.MaxInt64,
    86  		},
    87  		{
    88  			name:          "empty label sets with replica labels",
    89  			tsdbInfos:     []infopb.TSDBInfo{},
    90  			replicaLabels: []string{"replica"},
    91  			expected:      math.MaxInt64,
    92  		},
    93  		{
    94  			name: "non-empty label sets",
    95  			tsdbInfos: []infopb.TSDBInfo{{
    96  				Labels:  zLabelSetFromStrings("a", "1"),
    97  				MinTime: 30,
    98  			}},
    99  			expected: 30,
   100  		},
   101  		{
   102  			name: "non-empty label sets with replica labels",
   103  			tsdbInfos: []infopb.TSDBInfo{{
   104  				Labels:  zLabelSetFromStrings("a", "1", "b", "2"),
   105  				MinTime: 30,
   106  			}},
   107  			replicaLabels: []string{"a"},
   108  			expected:      30,
   109  		},
   110  		{
   111  			name: "replicated labelsets with different mint",
   112  			tsdbInfos: []infopb.TSDBInfo{
   113  				{
   114  					Labels:  zLabelSetFromStrings("a", "1", "replica", "1"),
   115  					MinTime: 30,
   116  				},
   117  				{
   118  					Labels:  zLabelSetFromStrings("a", "1", "replica", "2"),
   119  					MinTime: 60,
   120  				},
   121  			},
   122  			replicaLabels: []string{"replica"},
   123  			expected:      60,
   124  		},
   125  		{
   126  			name: "multiple replicated labelsets with different mint",
   127  			tsdbInfos: []infopb.TSDBInfo{
   128  				{
   129  					Labels:  zLabelSetFromStrings("a", "1", "replica", "1"),
   130  					MinTime: 30,
   131  				},
   132  				{
   133  					Labels:  zLabelSetFromStrings("a", "1", "replica", "2"),
   134  					MinTime: 60,
   135  				},
   136  				{
   137  					Labels:  zLabelSetFromStrings("a", "2", "replica", "1"),
   138  					MinTime: 80,
   139  				},
   140  				{
   141  					Labels:  zLabelSetFromStrings("a", "2", "replica", "2"),
   142  					MinTime: 120,
   143  				},
   144  			},
   145  			replicaLabels: []string{"replica"},
   146  			expected:      60,
   147  		},
   148  	}
   149  
   150  	for _, testCase := range tests {
   151  		t.Run(testCase.name, func(t *testing.T) {
   152  			client := NewClient(nil, "", testCase.tsdbInfos)
   153  			engine := newRemoteEngine(log.NewNopLogger(), client, Opts{
   154  				ReplicaLabels: testCase.replicaLabels,
   155  			})
   156  
   157  			testutil.Equals(t, testCase.expected, engine.MinT())
   158  		})
   159  	}
   160  }
   161  
   162  func zLabelSetFromStrings(ss ...string) labelpb.ZLabelSet {
   163  	return labelpb.ZLabelSet{
   164  		Labels: labelpb.ZLabelsFromPromLabels(labels.FromStrings(ss...)),
   165  	}
   166  }