test

package
v0.0.0-...-a0de49b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MyTokenFederateScenarios = map[string]Scenario{
	"empty": {
		Queries:  url.Values{},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"match nothing": {
		Queries: url.Values{
			"match[]": []string{"does_not_match_anything"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"invalid Params from the beginning": {
		Queries: url.Values{
			"match[]": []string{"-not-a-valid-metric-name"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: `1:1: parse error: unexpected <op:->
`,
	},
	"invalid Params somewhere in the middle": {
		Queries: url.Values{
			"match[]": []string{"not-a-valid-metric-name"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: `1:4: parse error: unexpected <op:->
`,
	},
	"test_metric1": {
		Queries: url.Values{
			"match[]": []string{"test_metric1"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_metric1 untyped
test_metric1{foo="bar",namespace="ns-a",instance="",prometheus="cluster-level/test"} 10000 6000000
test_metric1{foo="boo",namespace="ns-c",instance="",prometheus="cluster-level/test"} 1 6000000
`,
	},
	"test_metric2": {
		Queries: url.Values{
			"match[]": []string{"test_metric2"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_metric2 untyped
test_metric2{foo="boo",instance="",prometheus="cluster-level/test"} 1 6000000
`,
	},
	"test_metric_without_labels": {
		Queries: url.Values{
			"match[]": []string{"test_metric_without_labels"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_metric_without_labels untyped
test_metric_without_labels{instance="",prometheus="cluster-level/test"} 1001 6000000
`,
	},
	"test_stale_metric": {
		Queries: url.Values{
			"match[]": []string{"test_metric_stale"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"test_old_metric": {
		Queries: url.Values{
			"match[]": []string{"test_metric_old"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_metric_old untyped
test_metric_old{instance="",prometheus="cluster-level/test"} 981 5880000
`,
	},
	"{foo='boo'}": {
		Queries: url.Values{
			"match[]": []string{"{foo='boo'}"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_metric1 untyped
test_metric1{foo="boo",namespace="ns-c",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric2 untyped
test_metric2{foo="boo",instance="",prometheus="cluster-level/test"} 1 6000000
`,
	},
	"{namespace='ns-c'}": {
		Queries: url.Values{
			"match[]": []string{"{namespace='ns-c'}"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_metric1 untyped
test_metric1{foo="boo",namespace="ns-c",instance="",prometheus="cluster-level/test"} 1 6000000
`,
	},
	"two matchers": {
		Queries: url.Values{
			"match[]": []string{"test_metric1", "test_metric2"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_metric1 untyped
test_metric1{foo="bar",namespace="ns-a",instance="",prometheus="cluster-level/test"} 10000 6000000
test_metric1{foo="boo",namespace="ns-c",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric2 untyped
test_metric2{foo="boo",instance="",prometheus="cluster-level/test"} 1 6000000
`,
	},
	"everything": {
		Queries: url.Values{
			"match[]": []string{"{__name__=~'.+'}"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_cluster_wide_metric untyped
test_cluster_wide_metric{namespace="caasglobal",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric1 untyped
test_metric1{foo="bar",namespace="ns-a",instance="",prometheus="cluster-level/test"} 10000 6000000
test_metric1{foo="boo",namespace="ns-c",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric2 untyped
test_metric2{foo="boo",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric_old untyped
test_metric_old{instance="",prometheus="cluster-level/test"} 981 5880000
# TYPE test_metric_without_labels untyped
test_metric_without_labels{instance="",prometheus="cluster-level/test"} 1001 6000000
`,
	},
	"empty existing label value matches everything that doesn't have that label": {
		Queries: url.Values{
			"match[]": []string{"{foo='',__name__=~'.+'}"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_cluster_wide_metric untyped
test_cluster_wide_metric{namespace="caasglobal",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric_old untyped
test_metric_old{instance="",prometheus="cluster-level/test"} 981 5880000
# TYPE test_metric_without_labels untyped
test_metric_without_labels{instance="",prometheus="cluster-level/test"} 1001 6000000
`,
	},
	"empty none-existing label value matches everything": {
		Queries: url.Values{
			"match[]": []string{"{bar='',__name__=~'.+'}"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_cluster_wide_metric untyped
test_cluster_wide_metric{namespace="caasglobal",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric1 untyped
test_metric1{foo="bar",namespace="ns-a",instance="",prometheus="cluster-level/test"} 10000 6000000
test_metric1{foo="boo",namespace="ns-c",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric2 untyped
test_metric2{foo="boo",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric_old untyped
test_metric_old{instance="",prometheus="cluster-level/test"} 981 5880000
# TYPE test_metric_without_labels untyped
test_metric_without_labels{instance="",prometheus="cluster-level/test"} 1001 6000000
`,
	},
	"empty `namespace` label value matches everything that doesn't have `namespace` label": {
		Queries: url.Values{
			"match[]": []string{"{namespace='',__name__=~'.+'}"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_metric2 untyped
test_metric2{foo="boo",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric_old untyped
test_metric_old{instance="",prometheus="cluster-level/test"} 981 5880000
# TYPE test_metric_without_labels untyped
test_metric_without_labels{instance="",prometheus="cluster-level/test"} 1001 6000000
`,
	},
}
View Source
var MyTokenLabelScenarios = map[string]Scenario{
	"bad value `invalid][query`": {
		Params: map[string]string{
			"name": "invalid][query",
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `invalid label name: "invalid][query"`,
		},
	},
	"__name__": {
		Params: map[string]string{
			"name": "__name__",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []string{
				"test_cluster_wide_metric",
				"test_metric1",
				"test_metric2",
				"test_metric_old",
				"test_metric_stale",
				"test_metric_without_labels",
			},
		},
	},
	"namespace": {
		Params: map[string]string{
			"name": "namespace",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []string{
				"caasglobal",
				"ns-a",
				"ns-c",
			},
		},
	},
	"foo": {
		Params: map[string]string{
			"name": "foo",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []string{
				"bar",
				"boo",
			},
		},
	},
	"does_not_match_anything": {
		Params: map[string]string{
			"name": "does_not_match_anything",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []string{},
		},
	},
	"test_metric_without_labels": {
		Params: map[string]string{
			"name": "test_metric_without_labels",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []string{},
		},
	},
}
View Source
var MyTokenQueryScenarios = map[string]Scenario{
	"query - none expression with time 1": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"2"},
			"time":  []string{"123.4"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeScalar,
				Result: promql.Scalar{
					V: 2,
					T: timestamp.FromTime(start.Add(123*time.Second + 400*time.Millisecond)),
				},
			},
		},
	},
	"query - none expression with time 2": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"0.333"},
			"time":  []string{"1970-01-01T00:02:03Z"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeScalar,
				Result: promql.Scalar{
					V: 0.333,
					T: timestamp.FromTime(start.Add(123 * time.Second)),
				},
			},
		},
	},
	"query - bad query `invalid][query`": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"invalid][query"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `invalid parameter "query": 1:8: parse error: unexpected right bracket ']'`,
		},
	},
	"query - test_metric1": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"test_metric1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result: promql.Vector{
					{
						Metric: []labels.Label{
							{
								Name:  "__name__",
								Value: "test_metric1",
							},
							{
								Name:  "foo",
								Value: "bar",
							},
							{
								Name:  "namespace",
								Value: "ns-a",
							},
						},
						F: 0,
						T: timestamp.FromTime(start),
					},
					{
						Metric: []labels.Label{
							{
								Name:  "__name__",
								Value: "test_metric1",
							},
							{
								Name:  "foo",
								Value: "boo",
							},
							{
								Name:  "namespace",
								Value: "ns-c",
							},
						},
						F: 1,
						T: timestamp.FromTime(start),
					},
				},
			},
		},
	},
	"query - test_metric1{namespace='ns-c'}": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"test_metric1{namespace='ns-c'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result: promql.Vector{
					promql.Sample{
						Metric: []labels.Label{
							{
								Name:  "__name__",
								Value: "test_metric1",
							},
							{
								Name:  "foo",
								Value: "boo",
							},
							{
								Name:  "namespace",
								Value: "ns-c",
							},
						},

						F: 1,
						T: timestamp.FromTime(start),
					},
				},
			},
		},
	},
	"query - test_metric2{foo='boo'}": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"test_metric2{foo='boo'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result: promql.Vector{
					promql.Sample{
						Metric: []labels.Label{
							{
								Name:  "__name__",
								Value: "test_metric2",
							},
							{
								Name:  "foo",
								Value: "boo",
							},
						},

						F: 1,
						T: timestamp.FromTime(start),
					},
				},
			},
		},
	},
	"query - test_metric1[5m]": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"test_metric1[5m]"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result: promql.Matrix{
					promql.Series{
						Floats: []promql.FPoint{
							{F: 0, T: timestamp.FromTime(start)},
						},
						Metric: []labels.Label{
							{
								Name:  "__name__",
								Value: "test_metric1",
							},
							{
								Name:  "foo",
								Value: "bar",
							},
							{
								Name:  "namespace",
								Value: "ns-a",
							},
						},
					},
					promql.Series{
						Floats: []promql.FPoint{
							{F: 1, T: timestamp.FromTime(start)},
						},
						Metric: []labels.Label{
							{
								Name:  "__name__",
								Value: "test_metric1",
							},
							{
								Name:  "foo",
								Value: "boo",
							},
							{
								Name:  "namespace",
								Value: "ns-c",
							},
						},
					},
				},
			},
		},
	},
	"query - test_metric_without_labels": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"test_metric_without_labels"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result: promql.Vector{promql.Sample{
					Metric: []labels.Label{
						{
							Name:  "__name__",
							Value: "test_metric_without_labels",
						},
					},

					F: 1,
					T: timestamp.FromTime(start),
				}},
			},
		},
	},
	"query - does_not_match_anything": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"does_not_match_anything"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result:     promql.Vector{},
			},
		},
	},
	"query_range - query=time()&start=0&end=2&step=1": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result: promql.Matrix{
					promql.Series{
						Floats: []promql.FPoint{
							{F: 0, T: timestamp.FromTime(start)},
							{F: 1, T: timestamp.FromTime(start.Add(1 * time.Second))},
							{F: 2, T: timestamp.FromTime(start.Add(2 * time.Second))},
						},
						Metric: nil,
					},
				},
			},
		},
	},
	"query_range - query=time()&end=2&step=1": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `invalid parameter "start": cannot parse "" to a valid timestamp`,
		},
	},
	"query_range - bad query `invalid][query`": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"invalid][query"},
			"start": []string{"0"},
			"end":   []string{"100"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `invalid parameter "query": 1:8: parse error: unexpected right bracket ']'`,
		},
	},
	"query_range - invalid step": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"start": []string{"1"},
			"end":   []string{"2"},
			"step":  []string{"0"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `invalid parameter "step": zero or negative query resolution step widths are not accepted. Try a positive integer`,
		},
	},
	"query_range - start after end": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"start": []string{"2"},
			"end":   []string{"1"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `invalid parameter "end": end timestamp must not be before start time`,
		},
	},
	"query_range - start overflows int64 internally": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"start": []string{"148966367200.372"},
			"end":   []string{"1489667272.372"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `invalid parameter "end": end timestamp must not be before start time`,
		},
	},
	"query_range - test_metric1": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"test_metric1"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result: promql.Matrix{
					promql.Series{
						Metric: []labels.Label{
							{
								Name:  "__name__",
								Value: "test_metric1",
							},
							{
								Name:  "foo",
								Value: "bar",
							},
							{
								Name:  "namespace",
								Value: "ns-a",
							},
						},
						Floats: []promql.FPoint{
							{F: 0, T: timestamp.FromTime(start)},
							{F: 0, T: timestamp.FromTime(start.Add(1 * time.Second))},
							{F: 0, T: timestamp.FromTime(start.Add(2 * time.Second))},
						},
					},
					promql.Series{
						Metric: []labels.Label{
							{
								Name:  "__name__",
								Value: "test_metric1",
							},
							{
								Name:  "foo",
								Value: "boo",
							},
							{
								Name:  "namespace",
								Value: "ns-c",
							},
						},
						Floats: []promql.FPoint{
							{F: 1, T: timestamp.FromTime(start)},
							{F: 1, T: timestamp.FromTime(start.Add(1 * time.Second))},
							{F: 1, T: timestamp.FromTime(start.Add(2 * time.Second))},
						},
					},
				},
			},
		},
	},
	"query_range - test_metric1{namespace='ns-c'}": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"test_metric1{namespace='ns-c'}"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result: promql.Matrix{
					promql.Series{
						Metric: []labels.Label{
							{
								Name:  "__name__",
								Value: "test_metric1",
							},
							{
								Name:  "foo",
								Value: "boo",
							},
							{
								Name:  "namespace",
								Value: "ns-c",
							},
						},
						Floats: []promql.FPoint{
							{F: 1, T: timestamp.FromTime(start)},
							{F: 1, T: timestamp.FromTime(start.Add(1 * time.Second))},
							{F: 1, T: timestamp.FromTime(start.Add(2 * time.Second))},
						},
					},
				},
			},
		},
	},
	"query_range - test_metric2{foo='boo'}": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"test_metric2{foo='boo'}"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result: promql.Matrix{
					promql.Series{
						Metric: []labels.Label{
							{
								Name:  "__name__",
								Value: "test_metric2",
							},
							{
								Name:  "foo",
								Value: "boo",
							},
						},
						Floats: []promql.FPoint{
							{F: 1, T: timestamp.FromTime(start)},
							{F: 1, T: timestamp.FromTime(start.Add(1 * time.Second))},
							{F: 1, T: timestamp.FromTime(start.Add(2 * time.Second))},
						},
					},
				},
			},
		},
	},
	"query_range - test_metric_without_labels": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"test_metric_without_labels"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result: promql.Matrix{
					promql.Series{
						Metric: []labels.Label{
							{
								Name:  "__name__",
								Value: "test_metric_without_labels",
							},
						},
						Floats: []promql.FPoint{
							{F: 1, T: timestamp.FromTime(start)},
							{F: 1, T: timestamp.FromTime(start.Add(1 * time.Second))},
							{F: 1, T: timestamp.FromTime(start.Add(2 * time.Second))},
						},
					},
				},
			},
		},
	},
	"query_range - does_not_match_anything": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"does_not_match_anything"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result:     promql.Vector{},
			},
		},
	},
}
View Source
var MyTokenSeriesScenarios = map[string]Scenario{
	"missing match[] query Params in series requests": {
		Queries:  url.Values{},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     "no match[] parameter provided",
		},
	},
	"bad match[] `invalid][query`": {
		Queries: url.Values{
			"match[]": []string{"invalid][query"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `invalid parameter "match[]": 1:8: parse error: unexpected right bracket ']'`,
		},
	},
	"test_metric1": {
		Queries: url.Values{
			"match[]": []string{"test_metric1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []labels.Labels{
				labels.FromStrings("__name__", "test_metric1", "foo", "bar", "namespace", "ns-a"),
				labels.FromStrings("__name__", "test_metric1", "foo", "boo", "namespace", "ns-c"),
			},
		},
	},
	"test_metric1{namespace='ns-c'}": {
		Queries: url.Values{
			"match[]": []string{"test_metric1{namespace='ns-c'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []labels.Labels{
				labels.FromStrings("__name__", "test_metric1", "foo", "boo", "namespace", "ns-c"),
			},
		},
	},
	"test_metric2{foo='boo'}": {
		Queries: url.Values{
			"match[]": []string{"test_metric2{foo='boo'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []labels.Labels{
				labels.FromStrings("__name__", "test_metric2", "foo", "boo"),
			},
		},
	},
	"{foo='boo'}": {
		Queries: url.Values{
			"match[]": []string{"{foo='boo'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []labels.Labels{
				labels.FromStrings("__name__", "test_metric1", "foo", "boo", "namespace", "ns-c"),
				labels.FromStrings("__name__", "test_metric2", "foo", "boo"),
			},
		},
	},
	"two matches": {
		Queries: url.Values{
			"match[]": []string{`test_metric1{foo=~".+o$"}`, `test_metric1{foo=~".+o"}`},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []labels.Labels{
				labels.FromStrings("__name__", "test_metric1", "foo", "boo", "namespace", "ns-c"),
			},
		},
	},
	"two matches, but one is `none`": {
		Queries: url.Values{
			"match[]": []string{`test_metric2{foo=~".+o"}`, `none`},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []labels.Labels{
				labels.FromStrings("__name__", "test_metric2", "foo", "boo"),
			},
		},
	},
	"test_metric_without_labels": {
		Queries: url.Values{
			"match[]": []string{"test_metric_without_labels"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []labels.Labels{
				labels.FromStrings("__name__", "test_metric_without_labels"),
			},
		},
	},
	"does_not_match_anything": {
		Queries: url.Values{
			"match[]": []string{"does_not_match_anything"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"start and end before series starts": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"-2"},
			"end":     []string{"-1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"start and end after series ends": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"100000"},
			"end":     []string{"100001"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"start and end within series": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"1"},
			"end":     []string{"100"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []labels.Labels{
				labels.FromStrings("__name__", "test_metric1", "foo", "bar", "namespace", "ns-a"),
				labels.FromStrings("__name__", "test_metric1", "foo", "boo", "namespace", "ns-c"),
			},
		},
	},
	"start within series, end after": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"1"},
			"end":     []string{"100000"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []labels.Labels{
				labels.FromStrings("__name__", "test_metric1", "foo", "bar", "namespace", "ns-a"),
				labels.FromStrings("__name__", "test_metric1", "foo", "boo", "namespace", "ns-c"),
			},
		},
	},
	"start before series, end within series": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"-1"},
			"end":     []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []labels.Labels{
				labels.FromStrings("__name__", "test_metric1", "foo", "bar", "namespace", "ns-a"),
				labels.FromStrings("__name__", "test_metric1", "foo", "boo", "namespace", "ns-c"),
			},
		},
	},
}
View Source
var NoneNamespacesTokenFederateScenarios = map[string]Scenario{
	"empty": {
		Queries:  url.Values{},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"match nothing": {
		Queries: url.Values{
			"match[]": []string{"does_not_match_anything"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"invalid Params from the beginning": {
		Queries: url.Values{
			"match[]": []string{"-not-a-valid-metric-name"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: `1:1: parse error: unexpected <op:->
`,
	},
	"invalid Params somewhere in the middle": {
		Queries: url.Values{
			"match[]": []string{"not-a-valid-metric-name"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: `1:4: parse error: unexpected <op:->
`,
	},
	"test_metric1": {
		Queries: url.Values{
			"match[]": []string{"test_metric1"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"test_metric2": {
		Queries: url.Values{
			"match[]": []string{"test_metric2"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"test_metric_without_labels": {
		Queries: url.Values{
			"match[]": []string{"test_metric_without_labels"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"test_stale_metric": {
		Queries: url.Values{
			"match[]": []string{"test_metric_stale"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"test_old_metric": {
		Queries: url.Values{
			"match[]": []string{"test_metric_old"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"{foo='boo'}": {
		Queries: url.Values{
			"match[]": []string{"{foo='boo'}"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"{namespace='ns-c'}": {
		Queries: url.Values{
			"match[]": []string{"{namespace='ns-c'}"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"two matchers": {
		Queries: url.Values{
			"match[]": []string{"test_metric1", "test_metric2"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"everything": {
		Queries: url.Values{
			"match[]": []string{"{__name__=~'.+'}"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"empty existing label value matches everything that doesn't have that label": {
		Queries: url.Values{
			"match[]": []string{"{foo='',__name__=~'.+'}"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"empty non-existing label value matches everything": {
		Queries: url.Values{
			"match[]": []string{"{bar='',__name__=~'.+'}"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"empty `namespace` label value matches everything": {
		Queries: url.Values{
			"match[]": []string{"{namespace='',__name__=~'.+'}"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
}
View Source
var NoneNamespacesTokenLabelScenarios = map[string]Scenario{
	"bad value `invalid][query`": {
		Params: map[string]string{
			"name": "invalid][query",
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `invalid label name: "invalid][query"`,
		},
	},
	"__name__": {
		Params: map[string]string{
			"name": "__name__",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []string{},
		},
	},
	"namespace": {
		Params: map[string]string{
			"name": "namespace",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []string{},
		},
	},
	"foo": {
		Params: map[string]string{
			"name": "foo",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []string{
				"bar",
				"boo",
			},
		},
	},
	"does_not_match_anything": {
		Params: map[string]string{
			"name": "does_not_match_anything",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []string{},
		},
	},
	"test_metric_without_labels": {
		Params: map[string]string{
			"name": "test_metric_without_labels",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []string{},
		},
	},
}
View Source
var NoneNamespacesTokenQueryScenarios = map[string]Scenario{
	"query - none expression with time 1": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"2"},
			"time":  []string{"123.4"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeScalar,
				Result: promql.Scalar{
					V: 2,
					T: timestamp.FromTime(start.Add(123*time.Second + 400*time.Millisecond)),
				},
			},
		},
	},
	"query - none expression with time 2": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"0.333"},
			"time":  []string{"1970-01-01T00:02:03Z"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeScalar,
				Result: promql.Scalar{
					V: 0.333,
					T: timestamp.FromTime(start.Add(123 * time.Second)),
				},
			},
		},
	},
	"query - bad query `invalid][query`": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"invalid][query"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `1:8: parse error: unexpected right bracket ']'`,
		},
	},
	"query - test_metric1": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"test_metric1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result:     promql.Vector{},
			},
		},
	},
	"query - test_metric1{namespace='ns-c'}": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"test_metric1{namespace='ns-c'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result:     promql.Vector{},
			},
		},
	},
	"query - test_metric2{foo='boo'}": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"test_metric2{foo='boo'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result:     promql.Vector{},
			},
		},
	},
	"query - test_metric_without_labels": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"test_metric_without_labels"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result:     promql.Vector{},
			},
		},
	},
	"query - does_not_match_anything": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"does_not_match_anything"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result:     promql.Vector{},
			},
		},
	},
	"query_range - query=time()&start=0&end=2&step=1": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result: promql.Matrix{
					promql.Series{
						Floats: []promql.FPoint{
							{F: 0, T: timestamp.FromTime(start)},
							{F: 1, T: timestamp.FromTime(start.Add(1 * time.Second))},
							{F: 2, T: timestamp.FromTime(start.Add(2 * time.Second))},
						},
						Metric: nil,
					},
				},
			},
		},
	},
	"query_range - query=time()&end=2&step=1": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `cannot parse "" to a valid timestamp`,
		},
	},
	"query_range - bad query `invalid][query`": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"invalid][query"},
			"start": []string{"0"},
			"end":   []string{"100"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `1:8: parse error: unexpected right bracket ']'`,
		},
	},
	"query_range - invalid step": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"start": []string{"1"},
			"end":   []string{"2"},
			"step":  []string{"0"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `zero or negative query resolution step widths are not accepted. Try a positive integer`,
		},
	},
	"query_range - start after end": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"start": []string{"2"},
			"end":   []string{"1"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `end timestamp must not be before start time`,
		},
	},
	"query_range - start overflows int64 internally": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"start": []string{"148966367200.372"},
			"end":   []string{"1489667272.372"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     "end timestamp must not be before start time",
		},
	},
	"query_range - test_metric1": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"test_metric1"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result:     promql.Matrix{},
			},
		},
	},
	"query_range - test_metric1{namespace='ns-c'}": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"test_metric1{namespace='ns-c'}"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result:     promql.Matrix{},
			},
		},
	},
	"query_range - test_metric2{foo='boo'}": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"test_metric2{foo='boo'}"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result:     promql.Matrix{},
			},
		},
	},
	"query_range - test_metric_without_labels": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"test_metric_without_labels"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result:     promql.Matrix{},
			},
		},
	},
	"query_range - does_not_match_anything": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"does_not_match_anything"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result:     promql.Vector{},
			},
		},
	},
}
View Source
var NoneNamespacesTokenSeriesScenarios = map[string]Scenario{
	"missing match[] query Params in series requests": {
		Queries:  url.Values{},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     "no match[] parameter provided",
		},
	},
	"bad match[] `invalid][query`": {
		Queries: url.Values{
			"match[]": []string{"invalid][query"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `1:8: parse error: unexpected right bracket ']'`,
		},
	},
	"test_metric1": {
		Queries: url.Values{
			"match[]": []string{"test_metric1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"test_metric1{namespace='ns-c'}": {
		Queries: url.Values{
			"match[]": []string{"test_metric1{namespace='ns-c'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"test_metric2{foo='boo'}": {
		Queries: url.Values{
			"match[]": []string{"test_metric2{foo='boo'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"{foo='boo'}": {
		Queries: url.Values{
			"match[]": []string{"{foo='boo'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"two matches": {
		Queries: url.Values{
			"match[]": []string{`test_metric1{foo=~".+o$"}`, `test_metric1{foo=~".+o"}`},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"two matches, but one is `none`": {
		Queries: url.Values{
			"match[]": []string{`test_metric2{foo=~".+o"}`, `none`},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"test_metric_without_labels": {
		Queries: url.Values{
			"match[]": []string{"test_metric_without_labels"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"does_not_match_anything": {
		Queries: url.Values{
			"match[]": []string{"does_not_match_anything"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"start and end before series starts": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"-2"},
			"end":     []string{"-1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"start and end after series ends": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"100000"},
			"end":     []string{"100001"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"start and end within series": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"1"},
			"end":     []string{"100"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"start within series, end after": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"1"},
			"end":     []string{"100000"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"start before series, end within series": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"-1"},
			"end":     []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
}
View Source
var SomeNamespacesTokenFederateScenarios = map[string]Scenario{
	"empty": {
		Queries:  url.Values{},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"match nothing": {
		Queries: url.Values{
			"match[]": []string{"does_not_match_anything"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"invalid Params from the beginning": {
		Queries: url.Values{
			"match[]": []string{"-not-a-valid-metric-name"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: `1:1: parse error: unexpected <op:->
`,
	},
	"invalid Params somewhere in the middle": {
		Queries: url.Values{
			"match[]": []string{"not-a-valid-metric-name"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: `1:4: parse error: unexpected <op:->
`,
	},
	"test_metric1": {
		Queries: url.Values{
			"match[]": []string{"test_metric1"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_metric1 untyped
test_metric1{foo="bar",namespace="ns-a",instance="",prometheus="cluster-level/test"} 10000 6000000
`,
	},
	"test_metric2": {
		Queries: url.Values{
			"match[]": []string{"test_metric2"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"test_metric_without_labels": {
		Queries: url.Values{
			"match[]": []string{"test_metric_without_labels"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"test_stale_metric": {
		Queries: url.Values{
			"match[]": []string{"test_metric_stale"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"test_old_metric": {
		Queries: url.Values{
			"match[]": []string{"test_metric_old"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"{foo='boo'}": {
		Queries: url.Values{
			"match[]": []string{"{foo='boo'}"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"{namespace='ns-c'}": {
		Queries: url.Values{
			"match[]": []string{"{namespace='ns-c'}"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
	"two matchers": {
		Queries: url.Values{
			"match[]": []string{"test_metric1", "test_metric2"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_metric1 untyped
test_metric1{foo="bar",namespace="ns-a",instance="",prometheus="cluster-level/test"} 10000 6000000
`,
	},
	"everything": {
		Queries: url.Values{
			"match[]": []string{"{__name__=~'.+'}"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_cluster_wide_metric untyped
test_cluster_wide_metric{namespace="caasglobal",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric1 untyped
test_metric1{foo="bar",namespace="ns-a",instance="",prometheus="cluster-level/test"} 10000 6000000
`,
	},
	"empty existing label value matches everything that doesn't have that label": {
		Queries: url.Values{
			"match[]": []string{"{foo='',__name__=~'.+'}"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_cluster_wide_metric untyped
test_cluster_wide_metric{namespace="caasglobal",instance="",prometheus="cluster-level/test"} 1 6000000
`,
	},
	"empty none-existing label value matches everything": {
		Queries: url.Values{
			"match[]": []string{"{bar='',__name__=~'.+'}"},
		},
		RespCode: http.StatusOK,
		RespBody: `# TYPE test_cluster_wide_metric untyped
test_cluster_wide_metric{namespace="caasglobal",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric1 untyped
test_metric1{foo="bar",namespace="ns-a",instance="",prometheus="cluster-level/test"} 10000 6000000
`,
	},
	"empty `namespace` label value matches everything that doesn't have `namespace` label": {
		Queries: url.Values{
			"match[]": []string{"{namespace='',__name__=~'.+'}"},
		},
		RespCode: http.StatusOK,
		RespBody: ``,
	},
}
View Source
var SomeNamespacesTokenLabelScenarios = map[string]Scenario{
	"bad value `invalid][query`": {
		Params: map[string]string{
			"name": "invalid][query",
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `invalid label name: "invalid][query"`,
		},
	},
	"__name__": {
		Params: map[string]string{
			"name": "__name__",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []string{
				"test_metric1",
			},
		},
	},
	"namespace": {
		Params: map[string]string{
			"name": "namespace",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []string{
				"ns-a",
				"ns-b",
			},
		},
	},
	"foo": {
		Params: map[string]string{
			"name": "foo",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []string{
				"bar",
				"boo",
			},
		},
	},
	"does_not_match_anything": {
		Params: map[string]string{
			"name": "does_not_match_anything",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []string{},
		},
	},
	"test_metric_without_labels": {
		Params: map[string]string{
			"name": "test_metric_without_labels",
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []string{},
		},
	},
}
View Source
var SomeNamespacesTokenQueryScenarios = map[string]Scenario{
	"query - none expression with time 1": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"2"},
			"time":  []string{"123.4"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeScalar,
				Result: promql.Scalar{
					V: 2,
					T: timestamp.FromTime(start.Add(123*time.Second + 400*time.Millisecond)),
				},
			},
		},
	},
	"query - none expression with time 2": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"0.333"},
			"time":  []string{"1970-01-01T00:02:03Z"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeScalar,
				Result: promql.Scalar{
					V: 0.333,
					T: timestamp.FromTime(start.Add(123 * time.Second)),
				},
			},
		},
	},
	"query - bad query `invalid][query`": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"invalid][query"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `1:8: parse error: unexpected right bracket ']'`,
		},
	},
	"query - test_metric1": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"test_metric1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result: promql.Vector{
					promql.Sample{
						Metric: []labels.Label{
							{
								Name:  "__name__",
								Value: "test_metric1",
							},
							{
								Name:  "foo",
								Value: "bar",
							},
							{
								Name:  "namespace",
								Value: "ns-a",
							},
						},
						F: 0,
						T: timestamp.FromTime(start),
					},
				},
			},
		},
	},
	"query - test_metric1{namespace='ns-c'}": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"test_metric1{namespace='ns-c'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result:     promql.Vector{},
			},
		},
	},
	"query - test_metric2{foo='boo'}": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"test_metric2{foo='boo'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result:     promql.Vector{},
			},
		},
	},
	"query - test_metric1[5m]": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"test_metric1[5m]"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result: promql.Matrix{
					promql.Series{
						Floats: []promql.FPoint{
							{
								T: timestamp.FromTime(start),
								F: 0,
							},
						},
						Metric: []labels.Label{
							{
								Name:  "__name__",
								Value: "test_metric1",
							},
							{
								Name:  "foo",
								Value: "bar",
							},
							{
								Name:  "namespace",
								Value: "ns-a",
							},
						},
					},
				},
			},
		},
	},
	"query - test_metric_without_labels": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"test_metric_without_labels"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result:     promql.Vector{},
			},
		},
	},
	"query - does_not_match_anything": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"does_not_match_anything"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result:     promql.Vector{},
			},
		},
	},
	"query_range - query=time()&start=0&end=2&step=1": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result: promql.Matrix{
					promql.Series{
						Floats: []promql.FPoint{
							{F: 0, T: timestamp.FromTime(start)},
							{F: 1, T: timestamp.FromTime(start.Add(1 * time.Second))},
							{F: 2, T: timestamp.FromTime(start.Add(2 * time.Second))},
						},
						Metric: nil,
					},
				},
			},
		},
	},
	"query_range - query=time()&end=2&step=1": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `cannot parse "" to a valid timestamp`,
		},
	},
	"query_range - bad query `invalid][query`": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"invalid][query"},
			"start": []string{"0"},
			"end":   []string{"100"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `1:8: parse error: unexpected right bracket ']'`,
		},
	},
	"query_range - invalid step": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"start": []string{"1"},
			"end":   []string{"2"},
			"step":  []string{"0"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `zero or negative query resolution step widths are not accepted. Try a positive integer`,
		},
	},
	"query_range - start after end": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"start": []string{"2"},
			"end":   []string{"1"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `end timestamp must not be before start time`,
		},
	},
	"query_range - start overflows int64 internally": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"time()"},
			"start": []string{"148966367200.372"},
			"end":   []string{"1489667272.372"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     "end timestamp must not be before start time",
		},
	},
	"query_range - test_metric1": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"test_metric1"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result: promql.Matrix{
					promql.Series{
						Metric: []labels.Label{
							{
								Name:  "__name__",
								Value: "test_metric1",
							},
							{
								Name:  "foo",
								Value: "bar",
							},
							{
								Name:  "namespace",
								Value: "ns-a",
							},
						},
						Floats: []promql.FPoint{
							{F: 0, T: timestamp.FromTime(start)},
							{F: 0, T: timestamp.FromTime(start.Add(1 * time.Second))},
							{F: 0, T: timestamp.FromTime(start.Add(2 * time.Second))},
						},
					},
				},
			},
		},
	},
	"query_range - test_metric1{namespace='ns-c'}": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"test_metric1{namespace='ns-c'}"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result:     promql.Matrix{},
			},
		},
	},
	"query_range - test_metric2{foo='boo'}": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"test_metric2{foo='boo'}"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result:     promql.Matrix{},
			},
		},
	},
	"query_range - test_metric_without_labels": {
		Endpoint: "/query_range",
		Queries: url.Values{
			"query": []string{"test_metric_without_labels"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeMatrix,
				Result:     promql.Matrix{},
			},
		},
	},
	"query_range - does_not_match_anything": {
		Endpoint: "/query",
		Queries: url.Values{
			"query": []string{"does_not_match_anything"},
			"start": []string{"0"},
			"end":   []string{"2"},
			"step":  []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: &queryData{
				ResultType: parser.ValueTypeVector,
				Result:     promql.Vector{},
			},
		},
	},
}
View Source
var SomeNamespacesTokenSeriesScenarios = map[string]Scenario{
	"missing match[] query Params in series requests": {
		Queries:  url.Values{},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     "no match[] parameter provided",
		},
	},
	"bad match[] `invalid][query`": {
		Queries: url.Values{
			"match[]": []string{"invalid][query"},
		},
		RespCode: http.StatusBadRequest,
		RespBody: &jsonResponseData{
			Status:    "error",
			ErrorType: "bad_data",
			Error:     `1:8: parse error: unexpected right bracket ']'`,
		},
	},
	"test_metric1": {
		Queries: url.Values{
			"match[]": []string{"test_metric1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []labels.Labels{
				labels.FromStrings("__name__", "test_metric1", "foo", "bar", "namespace", "ns-a"),
			},
		},
	},
	"test_metric1{namespace='ns-c'}": {
		Queries: url.Values{
			"match[]": []string{"test_metric1{namespace='ns-c'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"test_metric2{foo='boo'}": {
		Queries: url.Values{
			"match[]": []string{"test_metric2{foo='boo'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"{foo='boo'}": {
		Queries: url.Values{
			"match[]": []string{"{foo='boo'}"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"two matches": {
		Queries: url.Values{
			"match[]": []string{`test_metric1{foo=~".+o$"}`, `test_metric1{foo=~".+o"}`},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"two matches, but one is `none`": {
		Queries: url.Values{
			"match[]": []string{`test_metric2{foo=~".+o"}`, `none`},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"test_metric_without_labels": {
		Queries: url.Values{
			"match[]": []string{"test_metric_without_labels"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"does_not_match_anything": {
		Queries: url.Values{
			"match[]": []string{"does_not_match_anything"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"start and end before series starts": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"-2"},
			"end":     []string{"-1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"start and end after series ends": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"100000"},
			"end":     []string{"100001"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data:   []labels.Labels{},
		},
	},
	"start and end within series": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"1"},
			"end":     []string{"100"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []labels.Labels{
				labels.FromStrings("__name__", "test_metric1", "foo", "bar", "namespace", "ns-a"),
			},
		},
	},
	"start within series, end after": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"1"},
			"end":     []string{"100000"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []labels.Labels{
				labels.FromStrings("__name__", "test_metric1", "foo", "bar", "namespace", "ns-a"),
			},
		},
	},
	"start before series, end within series": {
		Queries: url.Values{
			"match[]": []string{`test_metric1`},
			"start":   []string{"-1"},
			"end":     []string{"1"},
		},
		RespCode: http.StatusOK,
		RespBody: &jsonResponseData{
			Status: "success",
			Data: []labels.Labels{
				labels.FromStrings("__name__", "test_metric1", "foo", "bar", "namespace", "ns-a"),
			},
		},
	},
}

Functions

func MyTokenReadScenarios

func MyTokenReadScenarios(t *testing.T) map[string]Scenario

func NoneNamespacesTokenReadScenarios

func NoneNamespacesTokenReadScenarios(t *testing.T) map[string]Scenario

func SomeNamespacesTokenReadScenarios

func SomeNamespacesTokenReadScenarios(t *testing.T) map[string]Scenario

Types

type Scenario

type Scenario struct {
	Endpoint      string
	Queries       url.Values
	Params        map[string]string
	PrompbQueries []*prompb.Query

	RespCode int
	RespBody interface{}
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL