test_test.go 4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright 2019 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package promql

import (
	"math"
	"testing"
	"time"

21 22
	"github.com/prometheus/prometheus/pkg/labels"

23 24 25 26 27 28 29 30 31 32 33 34
	"github.com/prometheus/prometheus/util/testutil"
)

func TestLazyLoader_WithSamplesTill(t *testing.T) {
	type testCase struct {
		ts             time.Time
		series         []Series // Each series is checked separately. Need not mention all series here.
		checkOnlyError bool     // If this is true, series is not checked.
	}

	cases := []struct {
		loadString string
35
		// These testCases are run in sequence. So the testCase being run is dependent on the previous testCase.
36 37 38 39 40 41 42 43
		testCases []testCase
	}{
		{
			loadString: `
				load 10s
					metric1 1+1x10
			`,
			testCases: []testCase{
Matt Layher committed
44
				{
45 46
					ts: time.Unix(40, 0),
					series: []Series{
Matt Layher committed
47
						{
48 49 50 51 52 53 54
							Metric: labels.FromStrings("__name__", "metric1"),
							Points: []Point{
								{0, 1}, {10000, 2}, {20000, 3}, {30000, 4}, {40000, 5},
							},
						},
					},
				},
Matt Layher committed
55
				{
56 57
					ts: time.Unix(10, 0),
					series: []Series{
Matt Layher committed
58
						{
59 60 61 62 63 64 65
							Metric: labels.FromStrings("__name__", "metric1"),
							Points: []Point{
								{0, 1}, {10000, 2}, {20000, 3}, {30000, 4}, {40000, 5},
							},
						},
					},
				},
Matt Layher committed
66
				{
67 68
					ts: time.Unix(60, 0),
					series: []Series{
Matt Layher committed
69
						{
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
							Metric: labels.FromStrings("__name__", "metric1"),
							Points: []Point{
								{0, 1}, {10000, 2}, {20000, 3}, {30000, 4}, {40000, 5}, {50000, 6}, {60000, 7},
							},
						},
					},
				},
			},
		},
		{
			loadString: `
				load 10s
					metric1 1+0x5
					metric2 1+1x100
			`,
			testCases: []testCase{
Matt Layher committed
86
				{ // Adds all samples of metric1.
87 88
					ts: time.Unix(70, 0),
					series: []Series{
Matt Layher committed
89
						{
90 91 92 93 94
							Metric: labels.FromStrings("__name__", "metric1"),
							Points: []Point{
								{0, 1}, {10000, 1}, {20000, 1}, {30000, 1}, {40000, 1}, {50000, 1},
							},
						},
Matt Layher committed
95
						{
96 97 98 99 100 101 102
							Metric: labels.FromStrings("__name__", "metric2"),
							Points: []Point{
								{0, 1}, {10000, 2}, {20000, 3}, {30000, 4}, {40000, 5}, {50000, 6}, {60000, 7}, {70000, 8},
							},
						},
					},
				},
Matt Layher committed
103
				{ // This tests fix for https://github.com/prometheus/prometheus/issues/5064.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
					ts:             time.Unix(300, 0),
					checkOnlyError: true,
				},
			},
		},
	}

	for _, c := range cases {
		suite, err := NewLazyLoader(t, c.loadString)
		testutil.Ok(t, err)
		defer suite.Close()

		for _, tc := range c.testCases {
			suite.WithSamplesTill(tc.ts, func(err error) {
				testutil.Ok(t, err)
				if tc.checkOnlyError {
					return
				}

				// Check the series.
				queryable := suite.Queryable()
				querier, err := queryable.Querier(suite.Context(), math.MinInt64, math.MaxInt64)
				testutil.Ok(t, err)
				for _, s := range tc.series {
					var matchers []*labels.Matcher
					for _, label := range s.Metric {
						m, err := labels.NewMatcher(labels.MatchEqual, label.Name, label.Value)
						testutil.Ok(t, err)
						matchers = append(matchers, m)
					}

					// Get the series for the matcher.
136
					ss := querier.Select(false, nil, matchers...)
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
					testutil.Assert(t, ss.Next(), "")
					storageSeries := ss.At()
					testutil.Assert(t, !ss.Next(), "Expecting only 1 series")

					// Convert `storage.Series` to `promql.Series`.
					got := Series{
						Metric: storageSeries.Labels(),
					}
					it := storageSeries.Iterator()
					for it.Next() {
						t, v := it.At()
						got.Points = append(got.Points, Point{T: t, V: v})
					}
					testutil.Ok(t, it.Err())

					testutil.Equals(t, s, got)
				}
			})
		}
	}
}