Prometheus: Add golden JSON tests (#48941)

This commit is contained in:
Todd Treece
2022-05-12 11:09:58 -04:00
committed by GitHub
parent 186ba26b59
commit 6cbaa18cf6
5 changed files with 305 additions and 2 deletions

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))
})
}
}

View File

@@ -0,0 +1,63 @@
{
"frames": [
{
"schema": {
"name": "1 / 0",
"meta": {
"custom": {
"resultType": "matrix"
},
"executedQueryString": "Expr: 1 / 0\nStep: 1s"
},
"fields": [
{
"name": "Time",
"type": "time",
"typeInfo": {
"frame": "time.Time"
},
"config": {
"interval": 1000
}
},
{
"name": "Value",
"type": "number",
"typeInfo": {
"frame": "float64",
"nullable": true
},
"labels": {},
"config": {
"displayNameFromDS": "1 / 0"
}
}
]
},
"data": {
"values": [
[
1641889530000,
1641889531000,
1641889532000
],
[
null,
null,
null
]
],
"entities": [
null,
{
"Inf": [
0,
1,
2
]
}
]
}
}
]
}

View File

@@ -0,0 +1,56 @@
{
"frames": [
{
"schema": {
"name": "go_goroutines{job=\"prometheus\"}",
"meta": {
"custom": {
"resultType": "matrix"
},
"executedQueryString": "Expr: test1\nStep: 1s"
},
"fields": [
{
"name": "Time",
"type": "time",
"typeInfo": {
"frame": "time.Time"
},
"config": {
"interval": 1000
}
},
{
"name": "Value",
"type": "number",
"typeInfo": {
"frame": "float64",
"nullable": true
},
"labels": {
"__name__": "go_goroutines",
"job": "prometheus"
},
"config": {
"displayNameFromDS": "go_goroutines{job=\"prometheus\"}"
}
}
]
},
"data": {
"values": [
[
1641889533000,
1641889534000,
1641889537000
],
[
21,
32,
43
]
]
}
}
]
}

View File

@@ -0,0 +1,56 @@
{
"frames": [
{
"schema": {
"name": "{handler=\"/api/v1/query_range\", job=\"prometheus\"}",
"meta": {
"custom": {
"resultType": "matrix"
},
"executedQueryString": "Expr: \nStep: 1s"
},
"fields": [
{
"name": "Time",
"type": "time",
"typeInfo": {
"frame": "time.Time"
},
"config": {
"interval": 1000
}
},
{
"name": "Value",
"type": "number",
"typeInfo": {
"frame": "float64",
"nullable": true
},
"labels": {
"handler": "/api/v1/query_range",
"job": "prometheus"
},
"config": {
"displayNameFromDS": "{handler=\"/api/v1/query_range\", job=\"prometheus\"}"
}
}
]
},
"data": {
"values": [
[
1641889530000,
1641889531000,
1641889532000
],
[
null,
null,
null
]
]
}
}
]
}

View File

@@ -0,0 +1,112 @@
{
"frames": [
{
"schema": {
"name": "prometheus_http_requests_total{code=\"200\", handler=\"/api/v1/query_range\", job=\"prometheus\"}",
"meta": {
"custom": {
"resultType": "matrix"
},
"executedQueryString": "Expr: \nStep: 1s"
},
"fields": [
{
"name": "Time",
"type": "time",
"typeInfo": {
"frame": "time.Time"
},
"config": {
"interval": 1000
}
},
{
"name": "Value",
"type": "number",
"typeInfo": {
"frame": "float64",
"nullable": true
},
"labels": {
"__name__": "prometheus_http_requests_total",
"code": "200",
"handler": "/api/v1/query_range",
"job": "prometheus"
},
"config": {
"displayNameFromDS": "prometheus_http_requests_total{code=\"200\", handler=\"/api/v1/query_range\", job=\"prometheus\"}"
}
}
]
},
"data": {
"values": [
[
1641889530123,
1641889531123,
1641889532123
],
[
21,
32,
43
]
]
}
},
{
"schema": {
"name": "prometheus_http_requests_total{code=\"400\", handler=\"/api/v1/query_range\", job=\"prometheus\"}",
"meta": {
"custom": {
"resultType": "matrix"
},
"executedQueryString": "Expr: \nStep: 1s"
},
"fields": [
{
"name": "Time",
"type": "time",
"typeInfo": {
"frame": "time.Time"
},
"config": {
"interval": 1000
}
},
{
"name": "Value",
"type": "number",
"typeInfo": {
"frame": "float64",
"nullable": true
},
"labels": {
"__name__": "prometheus_http_requests_total",
"code": "400",
"handler": "/api/v1/query_range",
"job": "prometheus"
},
"config": {
"displayNameFromDS": "prometheus_http_requests_total{code=\"400\", handler=\"/api/v1/query_range\", job=\"prometheus\"}"
}
}
]
},
"data": {
"values": [
[
1641889530123,
1641889531123,
1641889532123
],
[
54,
65,
76
]
]
}
}
]
}