Prometheus: Add golden JSON tests (#48941)
This commit is contained in:
@@ -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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user