Prometheus: Add golden JSON tests (#48941)

This commit is contained in:
Todd Treece
2022-05-12 17:09:58 +02:00
committed by GitHub
parent 186ba26b59
commit 6cbaa18cf6
5 changed files with 305 additions and 2 deletions
+18 -2
View File
@@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@@ -22,6 +23,8 @@ import (
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
)
var update = false
func TestMatrixResponses(t *testing.T) {
tt := []struct {
name string
@@ -37,7 +40,7 @@ func TestMatrixResponses(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
queryFileName := filepath.Join("../testdata", test.filepath+".query.json")
responseFileName := filepath.Join("../testdata", test.filepath+".result.json")
goldenFileName := filepath.Join("../testdata", test.filepath+".result.golden.txt")
goldenFileName := filepath.Join("../testdata", test.filepath+".result.golden")
query, err := loadStoredPrometheusQuery(queryFileName)
require.NoError(t, err)
@@ -52,7 +55,20 @@ func TestMatrixResponses(t *testing.T) {
dr, found := result.Responses["A"]
require.True(t, found)
require.NoError(t, experimental.CheckGoldenDataResponse(goldenFileName, &dr, true))
actual, err := json.MarshalIndent(&dr, "", " ")
require.NoError(t, err)
// nolint:gosec
// We can ignore the gosec G304 because this is a test with static defined paths
expected, err := ioutil.ReadFile(goldenFileName + ".json")
if err != nil || update {
err = os.WriteFile(goldenFileName+".json", actual, 0600)
require.NoError(t, err)
}
require.JSONEq(t, string(expected), string(actual))
require.NoError(t, experimental.CheckGoldenDataResponse(goldenFileName+".txt", &dr, update))
})
}
}