From 947a0b47a1190f71eae06d6160790d8bed7862da Mon Sep 17 00:00:00 2001 From: Simon Pasquier Date: Tue, 23 Apr 2024 20:21:36 +0200 Subject: [PATCH] chore: use golden file for tests (#211) Modifying existing tests for alerts and rules endpoints is very tedious as you need to do lots of manual edits. This change relies on golden files which can be automatically updated as the reponse payload evolves. Signed-off-by: Simon Pasquier --- go.mod | 2 + go.sum | 2 + injectproxy/rules_test.go | 634 +----------------- ...alerts_incomplete_upstream_response.golden | 0 .../alerts_invalid_upstream_response.golden | 0 .../alerts_match_namespace_ns1.golden | 39 ++ .../alerts_match_namespace_ns2.golden | 17 + ...alerts_match_namespaces_ns1_and_ns2.golden | 49 ++ injectproxy/testdata/alerts_no_match.golden | 6 + .../testdata/alerts_no_namespace_error.golden | 1 + .../testdata/alerts_upstream_error.golden | 1 + .../rules_incomplete_upstream_response.golden | 0 .../rules_invalid_upstream_response.golden | 0 .../testdata/rules_match_namespace_ns1.golden | 111 +++ .../testdata/rules_match_namespace_ns2.golden | 116 ++++ .../rules_match_namespaces_ns1_and_ns2.golden | 220 ++++++ injectproxy/testdata/rules_no_match.golden | 6 + ...es_no_match_with_gzip_not_requested.golden | 6 + .../rules_no_match_with_gzip_requested.golden | 6 + .../testdata/rules_no_namespace_error.golden | 1 + .../testdata/rules_upstream_error.golden | 1 + 21 files changed, 610 insertions(+), 608 deletions(-) create mode 100644 injectproxy/testdata/alerts_incomplete_upstream_response.golden create mode 100644 injectproxy/testdata/alerts_invalid_upstream_response.golden create mode 100644 injectproxy/testdata/alerts_match_namespace_ns1.golden create mode 100644 injectproxy/testdata/alerts_match_namespace_ns2.golden create mode 100644 injectproxy/testdata/alerts_match_namespaces_ns1_and_ns2.golden create mode 100644 injectproxy/testdata/alerts_no_match.golden create mode 100644 injectproxy/testdata/alerts_no_namespace_error.golden create mode 100644 injectproxy/testdata/alerts_upstream_error.golden create mode 100644 injectproxy/testdata/rules_incomplete_upstream_response.golden create mode 100644 injectproxy/testdata/rules_invalid_upstream_response.golden create mode 100644 injectproxy/testdata/rules_match_namespace_ns1.golden create mode 100644 injectproxy/testdata/rules_match_namespace_ns2.golden create mode 100644 injectproxy/testdata/rules_match_namespaces_ns1_and_ns2.golden create mode 100644 injectproxy/testdata/rules_no_match.golden create mode 100644 injectproxy/testdata/rules_no_match_with_gzip_not_requested.golden create mode 100644 injectproxy/testdata/rules_no_match_with_gzip_requested.golden create mode 100644 injectproxy/testdata/rules_no_namespace_error.golden create mode 100644 injectproxy/testdata/rules_upstream_error.golden diff --git a/go.mod b/go.mod index 0e444ba8..d56822b3 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/prometheus/client_golang v1.19.0 github.com/prometheus/prometheus v0.50.1 golang.org/x/exp v0.0.0-20240119083558-1b970713d09a + gotest.tools/v3 v3.5.1 ) require ( @@ -33,6 +34,7 @@ require ( github.com/go-openapi/spec v0.21.0 // indirect github.com/go-openapi/swag v0.23.0 // indirect github.com/go-openapi/validate v0.24.0 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd // indirect github.com/josharian/intern v1.0.0 // indirect diff --git a/go.sum b/go.sum index 438a5443..1ce7d72c 100644 --- a/go.sum +++ b/go.sum @@ -235,6 +235,8 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU= +gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU= k8s.io/apimachinery v0.28.6 h1:RsTeR4z6S07srPg6XYrwXpTJVMXsjPXn0ODakMytSW0= k8s.io/apimachinery v0.28.6/go.mod h1:QFNX/kCl/EMT2WTSz8k4WLCv2XnkOLMaL8GAVRMdpsA= k8s.io/client-go v0.28.6 h1:Gge6ziyIdafRchfoBKcpaARuz7jfrK1R1azuwORIsQI= diff --git a/injectproxy/rules_test.go b/injectproxy/rules_test.go index dd81e215..894b916b 100644 --- a/injectproxy/rules_test.go +++ b/injectproxy/rules_test.go @@ -22,6 +22,8 @@ import ( "net/http/httptest" "net/url" "testing" + + "gotest.tools/v3/golden" ) type gzipResponseWriter struct { @@ -332,12 +334,12 @@ func TestRules(t *testing.T) { reqHeaders http.Header expCode int - expBody []byte + golden string }{ { // No "namespace" parameter returns an error. expCode: http.StatusBadRequest, - expBody: []byte(`{"error":"The \"namespace\" query parameter must be provided.","errorType":"prom-label-proxy","status":"error"}` + "\n"), + golden: "rules_no_namespace_error.golden", }, { // non 200 status code from upstream is passed as-is. @@ -348,7 +350,7 @@ func TestRules(t *testing.T) { }), expCode: http.StatusBadRequest, - expBody: []byte("error"), + golden: "rules_upstream_error.golden", }, { // incomplete API response triggers a 502 error. @@ -358,7 +360,7 @@ func TestRules(t *testing.T) { }), expCode: http.StatusBadGateway, - expBody: []byte(""), + golden: "rules_incomplete_upstream_response.golden", }, { // invalid API response triggers a 502 error. @@ -368,7 +370,7 @@ func TestRules(t *testing.T) { }), expCode: http.StatusBadGateway, - expBody: []byte(""), + golden: "rules_invalid_upstream_response.golden", }, { // "namespace" parameter matching no rule. @@ -376,12 +378,7 @@ func TestRules(t *testing.T) { upstream: validRules(), expCode: http.StatusOK, - expBody: []byte(`{ - "status": "success", - "data": { - "groups": [] - } -}`), + golden: "rules_no_match.golden", }, { // Gzipped response should be handled when explictly asked by the original client. @@ -392,12 +389,7 @@ func TestRules(t *testing.T) { }, expCode: http.StatusOK, - expBody: []byte(`{ - "status": "success", - "data": { - "groups": [] - } -}`), + golden: "rules_no_match_with_gzip_requested.golden", }, { // When the client doesn't ask explicitly for gzip encoding, the Go @@ -407,477 +399,28 @@ func TestRules(t *testing.T) { upstream: gzipHandler(validRules()), expCode: http.StatusOK, - expBody: []byte(`{ - "status": "success", - "data": { - "groups": [] - } -}`), + golden: "rules_no_match_with_gzip_not_requested.golden", }, { labelv: []string{"ns1"}, upstream: validRules(), expCode: http.StatusOK, - expBody: []byte(`{ - "status": "success", - "data": { - "groups": [ - { - "name": "group1", - "file": "testdata/rules1.yml", - "rules": [ - { - "name": "metric1", - "query": "0", - "labels": { - "namespace": "ns1" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "metric2", - "query": "1", - "labels": { - "namespace": "ns1", - "operation": "create" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "metric2", - "query": "0", - "labels": { - "namespace": "ns1", - "operation": "update" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "metric2", - "query": "0", - "labels": { - "namespace": "ns1", - "operation": "delete" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "Alert1", - "query": "metric1{namespace=\"ns1\"} == 0", - "duration": 0, - "labels": { - "namespace": "ns1" - }, - "annotations": {}, - "alerts": [ - { - "labels": { - "alertname": "Alert1", - "namespace": "ns1" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:44.543981127+01:00", - "value": "0e+00" - } - ], - "health": "ok", - "type": "alerting" - }, - { - "name": "Alert2", - "query": "metric2{namespace=\"ns1\"} == 0", - "duration": 0, - "labels": { - "namespace": "ns1" - }, - "annotations": {}, - "alerts": [ - { - "labels": { - "alertname": "Alert2", - "namespace": "ns1", - "operation": "update" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:44.543981127+01:00", - "value": "0e+00" - }, - { - "labels": { - "alertname": "Alert2", - "namespace": "ns1", - "operation": "delete" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:44.543981127+01:00", - "value": "0e+00" - } - ], - "health": "ok", - "type": "alerting" - } - ], - "interval": 10 - } - ] - } -}`), + golden: "rules_match_namespace_ns1.golden", }, { labelv: []string{"ns2"}, upstream: validRules(), expCode: http.StatusOK, - expBody: []byte(`{ - "status": "success", - "data": { - "groups": [ - { - "name": "group1", - "file": "testdata/rules2.yml", - "rules": [ - { - "name": "metric1", - "query": "1", - "labels": { - "namespace": "ns2" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "Alert1", - "query": "metric1{namespace=\"ns2\"} == 0", - "duration": 0, - "labels": { - "namespace": "ns2" - }, - "annotations": {}, - "alerts": [], - "health": "ok", - "type": "alerting" - } - ], - "interval": 10 - }, - { - "name": "group2", - "file": "testdata/rules2.yml", - "rules": [ - { - "name": "metric2", - "query": "1", - "labels": { - "namespace": "ns2", - "operation": "create" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "metric2", - "query": "2", - "labels": { - "namespace": "ns2", - "operation": "update" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "metric2", - "query": "3", - "labels": { - "namespace": "ns2", - "operation": "delete" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "metric3", - "query": "0", - "labels": { - "namespace": "ns2" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "Alert2", - "query": "metric2{namespace=\"ns2\"} == 0", - "duration": 0, - "labels": { - "namespace": "ns2" - }, - "annotations": {}, - "alerts": [], - "health": "ok", - "type": "alerting" - }, - { - "name": "Alert3", - "query": "metric3{namespace=\"ns2\"} == 0", - "duration": 0, - "labels": { - "namespace": "ns2" - }, - "annotations": {}, - "alerts": [ - { - "labels": { - "alertname": "Alert3", - "namespace": "ns2" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:39.972915521+01:00", - "value": "0e+00" - } - ], - "health": "ok", - "type": "alerting" - } - ], - "interval": 10 - } - ] - } -}`), + golden: "rules_match_namespace_ns2.golden", }, { labelv: []string{"ns1", "ns2"}, upstream: validRules(), expCode: http.StatusOK, - expBody: []byte(`{ - "status": "success", - "data": { - "groups": [ - { - "name": "group1", - "file": "testdata/rules1.yml", - "rules": [ - { - "name": "metric1", - "query": "0", - "labels": { - "namespace": "ns1" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "metric2", - "query": "1", - "labels": { - "namespace": "ns1", - "operation": "create" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "metric2", - "query": "0", - "labels": { - "namespace": "ns1", - "operation": "update" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "metric2", - "query": "0", - "labels": { - "namespace": "ns1", - "operation": "delete" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "Alert1", - "query": "metric1{namespace=\"ns1\"} == 0", - "duration": 0, - "labels": { - "namespace": "ns1" - }, - "annotations": {}, - "alerts": [ - { - "labels": { - "alertname": "Alert1", - "namespace": "ns1" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:44.543981127+01:00", - "value": "0e+00" - } - ], - "health": "ok", - "type": "alerting" - }, - { - "name": "Alert2", - "query": "metric2{namespace=\"ns1\"} == 0", - "duration": 0, - "labels": { - "namespace": "ns1" - }, - "annotations": {}, - "alerts": [ - { - "labels": { - "alertname": "Alert2", - "namespace": "ns1", - "operation": "update" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:44.543981127+01:00", - "value": "0e+00" - }, - { - "labels": { - "alertname": "Alert2", - "namespace": "ns1", - "operation": "delete" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:44.543981127+01:00", - "value": "0e+00" - } - ], - "health": "ok", - "type": "alerting" - } - ], - "interval": 10 - }, - { - "name": "group1", - "file": "testdata/rules2.yml", - "rules": [ - { - "name": "metric1", - "query": "1", - "labels": { - "namespace": "ns2" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "Alert1", - "query": "metric1{namespace=\"ns2\"} == 0", - "duration": 0, - "labels": { - "namespace": "ns2" - }, - "annotations": {}, - "alerts": [], - "health": "ok", - "type": "alerting" - } - ], - "interval": 10 - }, - { - "name": "group2", - "file": "testdata/rules2.yml", - "rules": [ - { - "name": "metric2", - "query": "1", - "labels": { - "namespace": "ns2", - "operation": "create" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "metric2", - "query": "2", - "labels": { - "namespace": "ns2", - "operation": "update" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "metric2", - "query": "3", - "labels": { - "namespace": "ns2", - "operation": "delete" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "metric3", - "query": "0", - "labels": { - "namespace": "ns2" - }, - "health": "ok", - "type": "recording" - }, - { - "name": "Alert2", - "query": "metric2{namespace=\"ns2\"} == 0", - "duration": 0, - "labels": { - "namespace": "ns2" - }, - "annotations": {}, - "alerts": [], - "health": "ok", - "type": "alerting" - }, - { - "name": "Alert3", - "query": "metric3{namespace=\"ns2\"} == 0", - "duration": 0, - "labels": { - "namespace": "ns2" - }, - "annotations": {}, - "alerts": [ - { - "labels": { - "alertname": "Alert3", - "namespace": "ns2" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:39.972915521+01:00", - "value": "0e+00" - } - ], - "health": "ok", - "type": "alerting" - } - ], - "interval": 10 - } - ] - } -}`), + golden: "rules_match_namespaces_ns1_and_ns2.golden", }, } { t.Run(fmt.Sprintf("%s=%s", proxyLabel, tc.labelv), func(t *testing.T) { @@ -917,22 +460,13 @@ func TestRules(t *testing.T) { body, _ := io.ReadAll(resp.Body) if resp.StatusCode != http.StatusOK { - if string(body) != string(tc.expBody) { - t.Fatalf("expected: %q, got: %q", string(tc.expBody), string(body)) - } + golden.Assert(t, string(body), tc.golden) return } // We need to unmarshal/marshal the result to run deterministic comparisons. got := normalizeAPIResponse(t, body) - expected := normalizeAPIResponse(t, tc.expBody) - if got != expected { - t.Logf("expected:") - t.Logf(expected) - t.Logf("got:") - t.Logf(got) - t.FailNow() - } + golden.Assert(t, got, tc.golden) }) } } @@ -943,12 +477,12 @@ func TestAlerts(t *testing.T) { upstream http.Handler expCode int - expBody []byte + golden string }{ { // No "namespace" parameter returns an error. expCode: http.StatusBadRequest, - expBody: []byte(`{"error":"The \"namespace\" query parameter must be provided.","errorType":"prom-label-proxy","status":"error"}` + "\n"), + golden: "alerts_no_namespace_error.golden", }, { // non 200 status code from upstream is passed as-is. @@ -959,7 +493,7 @@ func TestAlerts(t *testing.T) { }), expCode: http.StatusBadRequest, - expBody: []byte("error"), + golden: "alerts_upstream_error.golden", }, { // incomplete API response triggers a 502 error. @@ -969,7 +503,7 @@ func TestAlerts(t *testing.T) { }), expCode: http.StatusBadGateway, - expBody: []byte(""), + golden: "alerts_incomplete_upstream_response.golden", }, { // invalid API response triggers a 502 error. @@ -979,7 +513,7 @@ func TestAlerts(t *testing.T) { }), expCode: http.StatusBadGateway, - expBody: []byte(""), + golden: "alerts_invalid_upstream_response.golden", }, { // "namespace" parameter matching no rule. @@ -987,135 +521,28 @@ func TestAlerts(t *testing.T) { upstream: validAlerts(), expCode: http.StatusOK, - expBody: []byte(`{ - "status": "success", - "data": { - "alerts": [] - } -}`), + golden: "alerts_no_match.golden", }, { labelv: []string{"ns1"}, upstream: validAlerts(), expCode: http.StatusOK, - expBody: []byte(`{ - "status": "success", - "data": { - "alerts": [ - { - "labels": { - "alertname": "Alert1", - "namespace": "ns1" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:44.543981127+01:00", - "value": "0e+00" - }, - { - "labels": { - "alertname": "Alert2", - "namespace": "ns1", - "operation": "update" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:44.543981127+01:00", - "value": "0e+00" - }, - { - "labels": { - "alertname": "Alert2", - "namespace": "ns1", - "operation": "delete" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:44.543981127+01:00", - "value": "0e+00" - } - ] - } -}`), + golden: "alerts_match_namespace_ns1.golden", }, { labelv: []string{"ns2"}, upstream: validAlerts(), expCode: http.StatusOK, - expBody: []byte(`{ - "status": "success", - "data": { - "alerts": [ - { - "labels": { - "alertname": "Alert3", - "namespace": "ns2" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:39.972915521+01:00", - "value": "0e+00" - } - ] - } -}`), + golden: "alerts_match_namespace_ns2.golden", }, { labelv: []string{"ns1", "ns2"}, upstream: validAlerts(), expCode: http.StatusOK, - expBody: []byte(`{ - "status": "success", - "data": { - "alerts": [ - { - "labels": { - "alertname": "Alert1", - "namespace": "ns1" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:44.543981127+01:00", - "value": "0e+00" - }, - { - "labels": { - "alertname": "Alert2", - "namespace": "ns1", - "operation": "update" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:44.543981127+01:00", - "value": "0e+00" - }, - { - "labels": { - "alertname": "Alert2", - "namespace": "ns1", - "operation": "delete" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:44.543981127+01:00", - "value": "0e+00" - }, - { - "labels": { - "alertname": "Alert3", - "namespace": "ns2" - }, - "annotations": {}, - "state": "firing", - "activeAt": "2019-12-18T13:14:39.972915521+01:00", - "value": "0e+00" - } - ] - } -}`), + golden: "alerts_match_namespaces_ns1_and_ns2.golden", }, } { t.Run(fmt.Sprintf("%s=%s", proxyLabel, tc.labelv), func(t *testing.T) { @@ -1148,22 +575,13 @@ func TestAlerts(t *testing.T) { body, _ := io.ReadAll(resp.Body) if resp.StatusCode != http.StatusOK { - if string(body) != string(tc.expBody) { - t.Fatalf("expected: %q, got: %q", string(tc.expBody), string(body)) - } + golden.Assert(t, string(body), tc.golden) return } // We need to unmarshal/marshal the result to run deterministic comparisons. got := normalizeAPIResponse(t, body) - expected := normalizeAPIResponse(t, tc.expBody) - if got != expected { - t.Logf("expected:") - t.Logf(expected) - t.Logf("got:") - t.Logf(got) - t.FailNow() - } + golden.Assert(t, got, tc.golden) }) } } diff --git a/injectproxy/testdata/alerts_incomplete_upstream_response.golden b/injectproxy/testdata/alerts_incomplete_upstream_response.golden new file mode 100644 index 00000000..e69de29b diff --git a/injectproxy/testdata/alerts_invalid_upstream_response.golden b/injectproxy/testdata/alerts_invalid_upstream_response.golden new file mode 100644 index 00000000..e69de29b diff --git a/injectproxy/testdata/alerts_match_namespace_ns1.golden b/injectproxy/testdata/alerts_match_namespace_ns1.golden new file mode 100644 index 00000000..5ffdeb5a --- /dev/null +++ b/injectproxy/testdata/alerts_match_namespace_ns1.golden @@ -0,0 +1,39 @@ +{ + "status": "success", + "data": { + "alerts": [ + { + "labels": { + "alertname": "Alert1", + "namespace": "ns1" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + }, + { + "labels": { + "alertname": "Alert2", + "namespace": "ns1", + "operation": "update" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + }, + { + "labels": { + "alertname": "Alert2", + "namespace": "ns1", + "operation": "delete" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + } + ] + } +} \ No newline at end of file diff --git a/injectproxy/testdata/alerts_match_namespace_ns2.golden b/injectproxy/testdata/alerts_match_namespace_ns2.golden new file mode 100644 index 00000000..245cdcdf --- /dev/null +++ b/injectproxy/testdata/alerts_match_namespace_ns2.golden @@ -0,0 +1,17 @@ +{ + "status": "success", + "data": { + "alerts": [ + { + "labels": { + "alertname": "Alert3", + "namespace": "ns2" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:39.972915521+01:00", + "value": "0e+00" + } + ] + } +} \ No newline at end of file diff --git a/injectproxy/testdata/alerts_match_namespaces_ns1_and_ns2.golden b/injectproxy/testdata/alerts_match_namespaces_ns1_and_ns2.golden new file mode 100644 index 00000000..c3f4c465 --- /dev/null +++ b/injectproxy/testdata/alerts_match_namespaces_ns1_and_ns2.golden @@ -0,0 +1,49 @@ +{ + "status": "success", + "data": { + "alerts": [ + { + "labels": { + "alertname": "Alert1", + "namespace": "ns1" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + }, + { + "labels": { + "alertname": "Alert2", + "namespace": "ns1", + "operation": "update" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + }, + { + "labels": { + "alertname": "Alert2", + "namespace": "ns1", + "operation": "delete" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + }, + { + "labels": { + "alertname": "Alert3", + "namespace": "ns2" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:39.972915521+01:00", + "value": "0e+00" + } + ] + } +} \ No newline at end of file diff --git a/injectproxy/testdata/alerts_no_match.golden b/injectproxy/testdata/alerts_no_match.golden new file mode 100644 index 00000000..9fb25f8b --- /dev/null +++ b/injectproxy/testdata/alerts_no_match.golden @@ -0,0 +1,6 @@ +{ + "status": "success", + "data": { + "alerts": [] + } +} \ No newline at end of file diff --git a/injectproxy/testdata/alerts_no_namespace_error.golden b/injectproxy/testdata/alerts_no_namespace_error.golden new file mode 100644 index 00000000..a14ac5d2 --- /dev/null +++ b/injectproxy/testdata/alerts_no_namespace_error.golden @@ -0,0 +1 @@ +{"error":"The \"namespace\" query parameter must be provided.","errorType":"prom-label-proxy","status":"error"} diff --git a/injectproxy/testdata/alerts_upstream_error.golden b/injectproxy/testdata/alerts_upstream_error.golden new file mode 100644 index 00000000..760589cb --- /dev/null +++ b/injectproxy/testdata/alerts_upstream_error.golden @@ -0,0 +1 @@ +error \ No newline at end of file diff --git a/injectproxy/testdata/rules_incomplete_upstream_response.golden b/injectproxy/testdata/rules_incomplete_upstream_response.golden new file mode 100644 index 00000000..e69de29b diff --git a/injectproxy/testdata/rules_invalid_upstream_response.golden b/injectproxy/testdata/rules_invalid_upstream_response.golden new file mode 100644 index 00000000..e69de29b diff --git a/injectproxy/testdata/rules_match_namespace_ns1.golden b/injectproxy/testdata/rules_match_namespace_ns1.golden new file mode 100644 index 00000000..413476de --- /dev/null +++ b/injectproxy/testdata/rules_match_namespace_ns1.golden @@ -0,0 +1,111 @@ +{ + "status": "success", + "data": { + "groups": [ + { + "name": "group1", + "file": "testdata/rules1.yml", + "rules": [ + { + "name": "metric1", + "query": "0", + "labels": { + "namespace": "ns1" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "metric2", + "query": "1", + "labels": { + "namespace": "ns1", + "operation": "create" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "metric2", + "query": "0", + "labels": { + "namespace": "ns1", + "operation": "update" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "metric2", + "query": "0", + "labels": { + "namespace": "ns1", + "operation": "delete" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "Alert1", + "query": "metric1{namespace=\"ns1\"} == 0", + "duration": 0, + "labels": { + "namespace": "ns1" + }, + "annotations": {}, + "alerts": [ + { + "labels": { + "alertname": "Alert1", + "namespace": "ns1" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + } + ], + "health": "ok", + "type": "alerting" + }, + { + "name": "Alert2", + "query": "metric2{namespace=\"ns1\"} == 0", + "duration": 0, + "labels": { + "namespace": "ns1" + }, + "annotations": {}, + "alerts": [ + { + "labels": { + "alertname": "Alert2", + "namespace": "ns1", + "operation": "update" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + }, + { + "labels": { + "alertname": "Alert2", + "namespace": "ns1", + "operation": "delete" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + } + ], + "health": "ok", + "type": "alerting" + } + ], + "interval": 10 + } + ] + } +} \ No newline at end of file diff --git a/injectproxy/testdata/rules_match_namespace_ns2.golden b/injectproxy/testdata/rules_match_namespace_ns2.golden new file mode 100644 index 00000000..23f1bb75 --- /dev/null +++ b/injectproxy/testdata/rules_match_namespace_ns2.golden @@ -0,0 +1,116 @@ +{ + "status": "success", + "data": { + "groups": [ + { + "name": "group1", + "file": "testdata/rules2.yml", + "rules": [ + { + "name": "metric1", + "query": "1", + "labels": { + "namespace": "ns2" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "Alert1", + "query": "metric1{namespace=\"ns2\"} == 0", + "duration": 0, + "labels": { + "namespace": "ns2" + }, + "annotations": {}, + "alerts": [], + "health": "ok", + "type": "alerting" + } + ], + "interval": 10 + }, + { + "name": "group2", + "file": "testdata/rules2.yml", + "rules": [ + { + "name": "metric2", + "query": "1", + "labels": { + "namespace": "ns2", + "operation": "create" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "metric2", + "query": "2", + "labels": { + "namespace": "ns2", + "operation": "update" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "metric2", + "query": "3", + "labels": { + "namespace": "ns2", + "operation": "delete" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "metric3", + "query": "0", + "labels": { + "namespace": "ns2" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "Alert2", + "query": "metric2{namespace=\"ns2\"} == 0", + "duration": 0, + "labels": { + "namespace": "ns2" + }, + "annotations": {}, + "alerts": [], + "health": "ok", + "type": "alerting" + }, + { + "name": "Alert3", + "query": "metric3{namespace=\"ns2\"} == 0", + "duration": 0, + "labels": { + "namespace": "ns2" + }, + "annotations": {}, + "alerts": [ + { + "labels": { + "alertname": "Alert3", + "namespace": "ns2" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:39.972915521+01:00", + "value": "0e+00" + } + ], + "health": "ok", + "type": "alerting" + } + ], + "interval": 10 + } + ] + } +} \ No newline at end of file diff --git a/injectproxy/testdata/rules_match_namespaces_ns1_and_ns2.golden b/injectproxy/testdata/rules_match_namespaces_ns1_and_ns2.golden new file mode 100644 index 00000000..a4661f37 --- /dev/null +++ b/injectproxy/testdata/rules_match_namespaces_ns1_and_ns2.golden @@ -0,0 +1,220 @@ +{ + "status": "success", + "data": { + "groups": [ + { + "name": "group1", + "file": "testdata/rules1.yml", + "rules": [ + { + "name": "metric1", + "query": "0", + "labels": { + "namespace": "ns1" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "metric2", + "query": "1", + "labels": { + "namespace": "ns1", + "operation": "create" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "metric2", + "query": "0", + "labels": { + "namespace": "ns1", + "operation": "update" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "metric2", + "query": "0", + "labels": { + "namespace": "ns1", + "operation": "delete" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "Alert1", + "query": "metric1{namespace=\"ns1\"} == 0", + "duration": 0, + "labels": { + "namespace": "ns1" + }, + "annotations": {}, + "alerts": [ + { + "labels": { + "alertname": "Alert1", + "namespace": "ns1" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + } + ], + "health": "ok", + "type": "alerting" + }, + { + "name": "Alert2", + "query": "metric2{namespace=\"ns1\"} == 0", + "duration": 0, + "labels": { + "namespace": "ns1" + }, + "annotations": {}, + "alerts": [ + { + "labels": { + "alertname": "Alert2", + "namespace": "ns1", + "operation": "update" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + }, + { + "labels": { + "alertname": "Alert2", + "namespace": "ns1", + "operation": "delete" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:44.543981127+01:00", + "value": "0e+00" + } + ], + "health": "ok", + "type": "alerting" + } + ], + "interval": 10 + }, + { + "name": "group1", + "file": "testdata/rules2.yml", + "rules": [ + { + "name": "metric1", + "query": "1", + "labels": { + "namespace": "ns2" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "Alert1", + "query": "metric1{namespace=\"ns2\"} == 0", + "duration": 0, + "labels": { + "namespace": "ns2" + }, + "annotations": {}, + "alerts": [], + "health": "ok", + "type": "alerting" + } + ], + "interval": 10 + }, + { + "name": "group2", + "file": "testdata/rules2.yml", + "rules": [ + { + "name": "metric2", + "query": "1", + "labels": { + "namespace": "ns2", + "operation": "create" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "metric2", + "query": "2", + "labels": { + "namespace": "ns2", + "operation": "update" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "metric2", + "query": "3", + "labels": { + "namespace": "ns2", + "operation": "delete" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "metric3", + "query": "0", + "labels": { + "namespace": "ns2" + }, + "health": "ok", + "type": "recording" + }, + { + "name": "Alert2", + "query": "metric2{namespace=\"ns2\"} == 0", + "duration": 0, + "labels": { + "namespace": "ns2" + }, + "annotations": {}, + "alerts": [], + "health": "ok", + "type": "alerting" + }, + { + "name": "Alert3", + "query": "metric3{namespace=\"ns2\"} == 0", + "duration": 0, + "labels": { + "namespace": "ns2" + }, + "annotations": {}, + "alerts": [ + { + "labels": { + "alertname": "Alert3", + "namespace": "ns2" + }, + "annotations": {}, + "state": "firing", + "activeAt": "2019-12-18T13:14:39.972915521+01:00", + "value": "0e+00" + } + ], + "health": "ok", + "type": "alerting" + } + ], + "interval": 10 + } + ] + } +} \ No newline at end of file diff --git a/injectproxy/testdata/rules_no_match.golden b/injectproxy/testdata/rules_no_match.golden new file mode 100644 index 00000000..e764d07b --- /dev/null +++ b/injectproxy/testdata/rules_no_match.golden @@ -0,0 +1,6 @@ +{ + "status": "success", + "data": { + "groups": [] + } +} \ No newline at end of file diff --git a/injectproxy/testdata/rules_no_match_with_gzip_not_requested.golden b/injectproxy/testdata/rules_no_match_with_gzip_not_requested.golden new file mode 100644 index 00000000..e764d07b --- /dev/null +++ b/injectproxy/testdata/rules_no_match_with_gzip_not_requested.golden @@ -0,0 +1,6 @@ +{ + "status": "success", + "data": { + "groups": [] + } +} \ No newline at end of file diff --git a/injectproxy/testdata/rules_no_match_with_gzip_requested.golden b/injectproxy/testdata/rules_no_match_with_gzip_requested.golden new file mode 100644 index 00000000..e764d07b --- /dev/null +++ b/injectproxy/testdata/rules_no_match_with_gzip_requested.golden @@ -0,0 +1,6 @@ +{ + "status": "success", + "data": { + "groups": [] + } +} \ No newline at end of file diff --git a/injectproxy/testdata/rules_no_namespace_error.golden b/injectproxy/testdata/rules_no_namespace_error.golden new file mode 100644 index 00000000..a14ac5d2 --- /dev/null +++ b/injectproxy/testdata/rules_no_namespace_error.golden @@ -0,0 +1 @@ +{"error":"The \"namespace\" query parameter must be provided.","errorType":"prom-label-proxy","status":"error"} diff --git a/injectproxy/testdata/rules_upstream_error.golden b/injectproxy/testdata/rules_upstream_error.golden new file mode 100644 index 00000000..760589cb --- /dev/null +++ b/injectproxy/testdata/rules_upstream_error.golden @@ -0,0 +1 @@ +error \ No newline at end of file