2022-04-04 19:22:14 -05:00
|
|
|
package converter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2022-05-24 15:17:11 -05:00
|
|
|
"strings"
|
2022-04-04 19:22:14 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/experimental"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadPromFrames(t *testing.T) {
|
|
|
|
files := []string{
|
|
|
|
"prom-labels",
|
|
|
|
"prom-matrix",
|
|
|
|
"prom-matrix-with-nans",
|
2022-05-15 19:47:18 -05:00
|
|
|
"prom-matrix-histogram-no-labels",
|
|
|
|
"prom-matrix-histogram-partitioned",
|
|
|
|
"prom-vector-histogram-no-labels",
|
2022-04-04 19:22:14 -05:00
|
|
|
"prom-vector",
|
2022-05-04 19:26:32 -05:00
|
|
|
"prom-string",
|
2022-05-04 15:03:48 -05:00
|
|
|
"prom-scalar",
|
2022-04-04 19:22:14 -05:00
|
|
|
"prom-series",
|
2022-05-04 14:01:18 -05:00
|
|
|
"prom-warnings",
|
|
|
|
"prom-error",
|
2022-04-04 19:22:14 -05:00
|
|
|
"prom-exemplars",
|
|
|
|
"loki-streams-a",
|
|
|
|
"loki-streams-b",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range files {
|
2022-05-24 15:17:11 -05:00
|
|
|
t.Run(name, runScenario(name, Options{}))
|
|
|
|
t.Run(name, runScenario(name, Options{MatrixWideSeries: true, VectorWideSeries: true}))
|
|
|
|
}
|
|
|
|
}
|
2022-04-04 19:22:14 -05:00
|
|
|
|
2022-05-24 15:17:11 -05:00
|
|
|
func runScenario(name string, opts Options) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
// nolint:gosec
|
|
|
|
// We can ignore the gosec G304 because this is a test with static defined paths
|
|
|
|
f, err := os.Open(path.Join("testdata", name+".json"))
|
|
|
|
require.NoError(t, err)
|
2022-04-04 19:22:14 -05:00
|
|
|
|
2022-05-24 15:17:11 -05:00
|
|
|
if opts.MatrixWideSeries || opts.VectorWideSeries {
|
|
|
|
name = name + "-wide"
|
|
|
|
}
|
|
|
|
|
|
|
|
iter := jsoniter.Parse(jsoniter.ConfigDefault, f, 1024)
|
|
|
|
rsp := ReadPrometheusStyleResult(iter, opts)
|
2022-04-04 19:22:14 -05:00
|
|
|
|
2022-05-24 15:17:11 -05:00
|
|
|
out, err := jsoniter.MarshalIndent(rsp, "", " ")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
save := true
|
|
|
|
fpath := path.Join("testdata", name+"-frame.json")
|
|
|
|
|
|
|
|
// nolint:gosec
|
|
|
|
// We can ignore the gosec G304 because this is a test with static defined paths
|
|
|
|
current, err := ioutil.ReadFile(fpath)
|
|
|
|
if err == nil {
|
|
|
|
same := assert.JSONEq(t, string(out), string(current))
|
|
|
|
if !same {
|
2022-04-04 19:22:14 -05:00
|
|
|
save = true
|
|
|
|
}
|
2022-05-24 15:17:11 -05:00
|
|
|
} else {
|
|
|
|
assert.Fail(t, "missing file: "+fpath)
|
|
|
|
save = true
|
|
|
|
}
|
2022-04-04 19:22:14 -05:00
|
|
|
|
2022-05-24 15:17:11 -05:00
|
|
|
if save {
|
|
|
|
err = os.WriteFile(fpath, out, 0600)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2022-04-04 19:22:14 -05:00
|
|
|
|
2022-05-24 15:17:11 -05:00
|
|
|
// skip checking golden file for error response. it's not currently supported
|
|
|
|
if strings.Contains(name, "prom-error") {
|
|
|
|
return
|
|
|
|
}
|
2022-05-04 14:01:18 -05:00
|
|
|
|
2022-05-24 15:17:11 -05:00
|
|
|
fpath = path.Join("testdata", name+"-golden.txt")
|
|
|
|
err = experimental.CheckGoldenDataResponse(fpath, rsp, true)
|
|
|
|
assert.NoError(t, err)
|
2022-04-04 19:22:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTimeConversions(t *testing.T) {
|
|
|
|
// include millisecond precision
|
|
|
|
assert.Equal(t,
|
|
|
|
time.Date(2020, time.September, 14, 15, 22, 25, 479000000, time.UTC),
|
|
|
|
timeFromFloat(1600096945.479))
|
|
|
|
|
|
|
|
// Loki date parsing
|
|
|
|
assert.Equal(t,
|
|
|
|
time.Date(2022, time.February, 16, 16, 50, 46, 277587968, time.UTC),
|
|
|
|
timeFromLokiString("1645030246277587968"))
|
|
|
|
|
|
|
|
assert.Equal(t,
|
|
|
|
time.Date(2033, time.May, 18, 3, 33, 20, 0, time.UTC),
|
|
|
|
timeFromLokiString("2000000000000000000"))
|
|
|
|
}
|