forked from open-policy-agent/conftest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_config_test.go
75 lines (70 loc) · 1.67 KB
/
parse_config_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
74
75
package builtins
import (
"context"
"strings"
"testing"
"github.com/open-policy-agent/conftest/parser"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
)
func TestParseConfig(t *testing.T) {
testCases := []struct {
desc string
parser string
config string
wantErrMsg string
}{
{
desc: "No parser supplied",
wantErrMsg: "create config parser",
},
{
desc: "Invalid parser supplied",
parser: "no-such-parser",
wantErrMsg: "create config parser",
},
{
desc: "Invalid YAML",
parser: parser.YAML,
config: "```NOTVALID!",
wantErrMsg: "unmarshal config",
},
{
desc: "Empty YAML",
parser: parser.YAML,
},
{
desc: "Valid YAML",
parser: parser.YAML,
config: `some_field: some_value
another_field:
- arr1
- arr2`,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
pv, err := ast.InterfaceToValue(tc.parser)
if err != nil {
t.Fatalf("Could not convert parser %q to ast.Value: %v", tc.parser, err)
}
cv, err := ast.InterfaceToValue(tc.config)
if err != nil {
t.Fatalf("Could not convert config %q to ast.Value: %v", tc.config, err)
}
bctx := rego.BuiltinContext{Context: context.Background()}
_, err = parseConfig(bctx, ast.NewTerm(pv), ast.NewTerm(cv))
if err == nil && tc.wantErrMsg == "" {
return
}
if err != nil && tc.wantErrMsg == "" {
t.Errorf("Error was returned when no error was expected: %v", err)
return
}
if !strings.Contains(err.Error(), tc.wantErrMsg) {
t.Errorf("Error %q does not contain expected string %q", err.Error(), tc.wantErrMsg)
return
}
})
}
}