-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathopa_test.go
73 lines (62 loc) · 1.42 KB
/
opa_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package authz
import (
"context"
"fmt"
"io/fs"
"os"
"strings"
"testing"
"embed"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/tester"
)
var (
//go:embed conf
regoFS embed.FS
)
func parseModules() (map[string]*ast.Module, error) {
result := make(map[string]*ast.Module)
if err := fs.WalkDir(regoFS, ".", func(path string, entry fs.DirEntry, err error) error {
if !strings.HasSuffix(path, ".rego") {
return nil
}
if err != nil {
return fmt.Errorf("walk: %w", err)
}
if entry.IsDir() {
return nil
}
b, err := regoFS.ReadFile(path)
if err != nil {
return fmt.Errorf("walk: %w", err)
}
m, err := ast.ParseModule(path, string(b))
if err != nil {
return fmt.Errorf("walk: %w", err)
}
result[path] = m
return nil
}); err != nil {
return nil, fmt.Errorf("parse module: %w", err)
}
return result, nil
}
// TestRego runs the OPA tests found in the rego files under this package.
func TestRego(t *testing.T) {
ctx := context.Background()
mods, err := parseModules()
if err != nil {
t.Fatalf("Failed to parse test modules: %v", err)
}
rch, err := tester.NewRunner().
SetModules(mods).
CapturePrintOutput(true).
EnableTracing(true).
RunTests(ctx, nil)
if err != nil {
t.Fatalf("Error running tests: %v", err)
}
if err := (tester.PrettyReporter{Output: os.Stderr, Verbose: true}).Report(rch); err != nil {
t.Fatalf("Error in test: %v", err)
}
}