-
Notifications
You must be signed in to change notification settings - Fork 1
/
queries_test.go
55 lines (46 loc) · 1.15 KB
/
queries_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package seeds_test
import (
"fmt"
"io/fs"
"os"
"os/exec"
"path"
"strings"
"testing"
"github.com/google/go-jsonnet"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/appuio/appuio-cloud-reporting/pkg/testsuite"
)
func TestQueries(t *testing.T) {
wd := os.DirFS(".")
testFiles, err := fs.Glob(wd, "promtest/*.jsonnet")
require.NoError(t, err)
for _, tFile := range testFiles {
t.Run(tFile, func(t *testing.T) {
tmp := renderJsonnet(t, tFile)
runPromtool(t, tmp)
})
}
}
func runPromtool(t *testing.T, tmp string) {
t.Helper()
cmd := exec.Command(testsuite.PromtoolBin, "test", "rules", tmp)
var stderr, stdout strings.Builder
cmd.Stderr = &stderr
cmd.Stdout = &stdout
assert.NoError(t, cmd.Run())
// Not using t.Log to keep formatting sane
fmt.Println("STDOUT")
fmt.Println(stdout.String())
fmt.Println("STDERR")
fmt.Println(stderr.String())
}
func renderJsonnet(t *testing.T, tFile string) string {
t.Helper()
ev, err := jsonnet.MakeVM().EvaluateFile(tFile)
require.NoError(t, err)
tmp := path.Join(t.TempDir(), "test.json")
require.NoError(t, os.WriteFile(tmp, []byte(ev), 0644))
return tmp
}