forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema_test.go
193 lines (175 loc) · 6.44 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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) {
t.Helper()
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: %v", 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) {
innerObjectStaticProps := []*types.StaticProperty{}
innerObjectStaticProps = append(innerObjectStaticProps, &types.StaticProperty{Key: "a", Value: types.N})
innerObjectStaticProps = append(innerObjectStaticProps, &types.StaticProperty{Key: "b", Value: types.NewArray(nil, types.N)})
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(nil, innerObjectType)})
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,
},
{
note: "array of objects",
schema: `{
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {"type": "string"},
"value": {"type": "number"}
}
}
}`,
exp: types.NewArray(nil, types.NewObject([]*types.StaticProperty{
types.NewStaticProperty("id", types.S),
types.NewStaticProperty("value", types.N),
}, nil)),
},
{
note: "static array items",
schema: `{
"type": "array",
"items": [
{"type": "string"},
{"type": "number"}
]
}`,
exp: types.NewArray([]types.Type{
types.S,
types.N,
}, nil),
},
}
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: %v", err)
}
newtype, err := parseSchema(jsonSchema) // Did not pass the subschema
if err == nil {
t.Fatalf("Expected parseSchema() = error, got nil")
}
if newtype != nil {
t.Fatalf("Incorrect return from parseSchema with a bad schema")
}
}