-
Notifications
You must be signed in to change notification settings - Fork 2
/
property_subschema_test.go
142 lines (124 loc) · 4.26 KB
/
property_subschema_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cfschema_test
import (
"testing"
cfschema "github.com/hashicorp/aws-cloudformation-resource-schema-sdk-go"
)
func TestPropertySubschema_Resource(t *testing.T) {
testCases := []struct {
TestDescription string
MetaSchemaPath string
ResourceSchemaPath string
ExpectError bool
ExpectedAllOf int
ExpectedAnyOf int
ExpectedOneOf int
}{
{
TestDescription: "resource anyOf",
MetaSchemaPath: "provider.definition.schema.v1.json",
ResourceSchemaPath: "AWS_CloudWatch_MetricStream.json",
ExpectedAnyOf: 2,
},
{
TestDescription: "resource allOf",
MetaSchemaPath: "provider.definition.schema.v1.json",
ResourceSchemaPath: "AWS_GameLift_Fleet.json",
ExpectedAllOf: 3,
},
{
TestDescription: "resource no subschema",
MetaSchemaPath: "provider.definition.schema.v1.json",
ResourceSchemaPath: "AWS_ECS_Cluster.json",
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.TestDescription, func(t *testing.T) {
resource := loadAndValidateResourceSchema(t, testCase.MetaSchemaPath, testCase.ResourceSchemaPath)
if actual, expected := len(resource.AllOf), testCase.ExpectedAllOf; actual != expected {
t.Errorf("expected %d allOf elements, got: %d", expected, actual)
}
if actual, expected := len(resource.AnyOf), testCase.ExpectedAnyOf; actual != expected {
t.Errorf("expected %d anyOf elements, got: %d", expected, actual)
}
if actual, expected := len(resource.OneOf), testCase.ExpectedOneOf; actual != expected {
t.Errorf("expected %d oneOf elements, got: %d", expected, actual)
}
})
}
}
func TestPropertySubschema_Property(t *testing.T) {
testCases := []struct {
TestDescription string
MetaSchemaPath string
ResourceSchemaPath string
ExpectError bool
PropertyPath []string
ExpectedAllOf int
ExpectedAnyOf int
ExpectedOneOf int
}{
{
TestDescription: "property anyOf",
MetaSchemaPath: "provider.definition.schema.v1.json",
ResourceSchemaPath: "AWS_S3Outposts_Bucket.json",
PropertyPath: []string{"LifecycleConfiguration", "Rules"},
ExpectedAnyOf: 3,
},
{
TestDescription: "property oneOf",
MetaSchemaPath: "provider.definition.schema.v1.json",
ResourceSchemaPath: "AWS_S3Outposts_Bucket.json",
PropertyPath: []string{"LifecycleConfiguration", "Rules", "Filter"},
ExpectedOneOf: 3,
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.TestDescription, func(t *testing.T) {
resource := loadAndValidateResourceSchema(t, testCase.MetaSchemaPath, testCase.ResourceSchemaPath)
err := resource.Expand()
if err != nil && !testCase.ExpectError {
t.Fatalf("unexpected error: %s", err)
}
if err == nil && testCase.ExpectError {
t.Fatal("expected error, got none")
}
properties := resource.Properties
for i, propertyName := range testCase.PropertyPath {
property, ok := properties[propertyName]
if !ok {
t.Fatalf("expected resource property (%s), got none", propertyName)
}
if property.Type == nil {
t.Fatalf("unexpected resource property (%s) type, got none", propertyName)
}
if i == len(testCase.PropertyPath)-1 {
if typ := (*property.Type).String(); typ == cfschema.PropertyTypeArray {
property = property.Items
}
if actual, expected := len(property.AllOf), testCase.ExpectedAllOf; actual != expected {
t.Errorf("expected %d allOf elements, got: %d", expected, actual)
}
if actual, expected := len(property.AnyOf), testCase.ExpectedAnyOf; actual != expected {
t.Errorf("expected %d anyOf elements, got: %d", expected, actual)
}
if actual, expected := len(property.OneOf), testCase.ExpectedOneOf; actual != expected {
t.Errorf("expected %d oneOf elements, got: %d", expected, actual)
}
} else {
switch typ := (*property.Type).String(); typ {
case cfschema.PropertyTypeArray:
properties = property.Items.Properties
case cfschema.PropertyTypeObject:
properties = property.Properties
default:
t.Fatalf("resource property (%s) type (%s)", propertyName, typ)
}
}
}
})
}
}