forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema_test.go
158 lines (141 loc) · 5.86 KB
/
schema_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package ast
import (
"testing"
"github.com/open-policy-agent/opa/types"
"github.com/open-policy-agent/opa/util"
)
func testParseSchema(t *testing.T, schema string, expectedType types.Type) {
var sch interface{}
err := util.Unmarshal([]byte(schema), &sch)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
newtype, err := loadSchema(sch)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if newtype == nil {
t.Fatalf("parseSchema returned nil type")
}
if types.Compare(newtype, expectedType) != 0 {
t.Fatalf("parseSchema returned an incorrect type: %s, expected: %s", newtype.String(), expectedType.String())
}
}
func TestParseSchemaObject(t *testing.T) {
//Expected type is: object<b: array<object<a: number, b: array<number>, c: any>>, foo: string>
innerObjectStaticProps := []*types.StaticProperty{}
innerObjectStaticProps = append(innerObjectStaticProps, &types.StaticProperty{Key: "a", Value: types.N})
innerObjectStaticProps = append(innerObjectStaticProps, &types.StaticProperty{Key: "b", Value: types.NewArray([]types.Type{types.N}, nil)})
innerObjectStaticProps = append(innerObjectStaticProps, &types.StaticProperty{Key: "c", Value: types.A})
innerObjectType := types.NewObject(innerObjectStaticProps, nil)
staticProps := []*types.StaticProperty{}
staticProps = append(staticProps, &types.StaticProperty{Key: "b", Value: types.NewArray([]types.Type{innerObjectType}, nil)})
staticProps = append(staticProps, &types.StaticProperty{Key: "foo", Value: types.S})
expectedType := types.NewObject(staticProps, nil)
testParseSchema(t, objectSchema, expectedType)
}
func TestSetTypesWithSchemaRef(t *testing.T) {
var sch interface{}
err := util.Unmarshal([]byte(refSchema), &sch)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
newtype, err := loadSchema(sch)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if newtype == nil {
t.Fatalf("parseSchema returned nil type")
}
if newtype.String() != "object<apiVersion: string, kind: string, metadata: object<annotations: object[any: any], clusterName: string, creationTimestamp: string, deletionGracePeriodSeconds: number, deletionTimestamp: string, finalizers: array<string>, generateName: string, generation: number, initializers: object<pending: array<object<name: string>>, result: object<apiVersion: string, code: number, details: object<causes: array<object<field: string, message: string, reason: string>>, group: string, kind: string, name: string, retryAfterSeconds: number, uid: string>, kind: string, message: string, metadata: object<continue: string, resourceVersion: string, selfLink: string>, reason: string, status: string>>, labels: object[any: any], managedFields: array<object<apiVersion: string, fields: object[any: any], manager: string, operation: string, time: string>>, name: string, namespace: string, ownerReferences: array<object<apiVersion: string, blockOwnerDeletion: boolean, controller: boolean, kind: string, name: string, uid: string>>, resourceVersion: string, selfLink: string, uid: string>>" {
t.Fatalf("parseSchema returned an incorrect type: %s", newtype.String())
}
}
func TestSetTypesWithPodSchema(t *testing.T) {
var sch interface{}
err := util.Unmarshal([]byte(podSchema), &sch)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
newtype, err := loadSchema(sch)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if newtype == nil {
t.Fatalf("parseSchema returned nil type")
}
if newtype.String() == "object<apiVersion: string, kind: string, metadata: any, spec: any, status: any>" {
t.Fatalf("parseSchema returned an incorrect type: %s", newtype.String())
}
}
func TestParseSchemaUntypedField(t *testing.T) {
//Expected type is: object<foo: any>
staticProps := []*types.StaticProperty{}
staticProps = append(staticProps, &types.StaticProperty{Key: "foo", Value: types.A})
expectedType := types.NewObject(staticProps, nil)
testParseSchema(t, untypedFieldObjectSchema, expectedType)
}
func TestParseSchemaNoChildren(t *testing.T) {
//Expected type is: object[any: any]
expectedType := types.NewObject(nil, &types.DynamicProperty{Key: types.A, Value: types.A})
testParseSchema(t, noChildrenObjectSchema, expectedType)
}
func TestParseSchemaArrayNoItems(t *testing.T) {
//Expected type is: object<b: array[any]>
staticProps := []*types.StaticProperty{}
staticProps = append(staticProps, &types.StaticProperty{Key: "b", Value: types.NewArray(nil, types.A)})
expectedType := types.NewObject(staticProps, nil)
testParseSchema(t, arrayNoItemsSchema, expectedType)
}
func TestParseSchemaBooleanField(t *testing.T) {
//Expected type is: object<a: boolean>
staticProps := []*types.StaticProperty{}
staticProps = append(staticProps, &types.StaticProperty{Key: "a", Value: types.B})
expectedType := types.NewObject(staticProps, nil)
testParseSchema(t, booleanSchema, expectedType)
}
func TestParseSchemaBasics(t *testing.T) {
tests := []struct {
note string
schema string
exp types.Type
}{
{
note: "number",
schema: `{"type": "number"}`,
exp: types.N,
},
}
for _, tc := range tests {
t.Run(tc.note, func(t *testing.T) {
testParseSchema(t, tc.schema, tc.exp)
})
}
}
func TestCompileSchemaEmptySchema(t *testing.T) {
schema := ""
var sch interface{}
err := util.Unmarshal([]byte(schema), &sch)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
jsonSchema, _ := compileSchema(sch)
if jsonSchema != nil {
t.Fatalf("Incorrect return from parseSchema with an empty schema")
}
}
func TestParseSchemaWithSchemaBadSchema(t *testing.T) {
var sch interface{}
err := util.Unmarshal([]byte(objectSchema), &sch)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
jsonSchema, err := compileSchema(sch)
if err != nil {
t.Fatalf("Unable to compile schema")
}
newtype, err := parseSchema(jsonSchema) // Did not pass the subschema
if newtype != nil {
t.Fatalf("Incorrect return from parseSchema with a bad schema")
}
}