| 224 | } |
| 225 | |
| 226 | func TestGetPostingForLabels(t *testing.T) { |
| 227 | ctx := context.Background() |
| 228 | testErr := errors.New("err") |
| 229 | for _, tc := range []struct { |
| 230 | name string |
| 231 | ir tsdb.IndexReader |
| 232 | lbls labels.Labels |
| 233 | postings []storage.SeriesRef |
| 234 | err error |
| 235 | }{ |
| 236 | { |
| 237 | name: "single label", |
| 238 | ir: &mockIndexReader{postings: map[string]map[string]index.Postings{ |
| 239 | "foo": {"bar": index.NewListPostings([]storage.SeriesRef{1, 2, 3, 4, 5})}, |
| 240 | }}, |
| 241 | lbls: labels.FromStrings("foo", "bar"), |
| 242 | postings: []storage.SeriesRef{1, 2, 3, 4, 5}, |
| 243 | }, |
| 244 | { |
| 245 | name: "intersect", |
| 246 | ir: &mockIndexReader{postings: map[string]map[string]index.Postings{ |
| 247 | "foo": {"bar": index.NewListPostings([]storage.SeriesRef{1, 2, 3, 4, 5})}, |
| 248 | "job": {"prometheus": index.NewListPostings([]storage.SeriesRef{1, 3, 5})}, |
| 249 | }}, |
| 250 | lbls: labels.FromStrings("foo", "bar", "job", "prometheus"), |
| 251 | postings: []storage.SeriesRef{1, 3, 5}, |
| 252 | }, |
| 253 | { |
| 254 | name: "error", |
| 255 | ir: &mockIndexReader{postings: map[string]map[string]index.Postings{ |
| 256 | "foo": {"bar": index.ErrPostings(testErr)}, |
| 257 | "job": {"prometheus": index.NewListPostings([]storage.SeriesRef{1, 3, 5})}, |
| 258 | }}, |
| 259 | lbls: labels.FromStrings("foo", "bar", "job", "prometheus"), |
| 260 | postings: []storage.SeriesRef{1, 3, 5}, |
| 261 | err: testErr, |
| 262 | }, |
| 263 | } { |
| 264 | t.Run(tc.name, func(t *testing.T) { |
| 265 | p, err := getPostingForLabels(ctx, tc.ir, tc.lbls) |
| 266 | if err != nil { |
| 267 | require.EqualError(t, err, tc.err.Error()) |
| 268 | } else { |
| 269 | require.NoError(t, err) |
| 270 | series := make([]storage.SeriesRef, 0) |
| 271 | for p.Next() { |
| 272 | series = append(series, p.At()) |
| 273 | } |
| 274 | if tc.err != nil { |
| 275 | require.EqualError(t, p.Err(), tc.err.Error()) |
| 276 | } else { |
| 277 | require.Equal(t, tc.postings, series) |
| 278 | } |
| 279 | } |
| 280 | }) |
| 281 | } |
| 282 | } |
| 283 | |