This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
/
goformation_schema_test.go
187 lines (154 loc) · 7.66 KB
/
goformation_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
package goformation_test
import (
// Note that this is a fork of the main repo: github.com/xeipuuv/gojsonschema
// CloudFormation uses nested schema def references, which is currently broken
// in the main repo: https://github.com/xeipuuv/gojsonschema/pull/146
"os"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/xeipuuv/gojsonschema"
)
var _ = Describe("Goformation-generated JSON schemas", func() {
Context("with a valid CloudFormation template", func() {
pwd, _ := os.Getwd()
schemaLoader := NewReferenceLoader("file://" + pwd + "/schema/cloudformation.schema.json")
It("should successfully validate the CloudFormation template", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/valid-template.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Errors()).Should(BeEmpty())
Expect(result.Valid()).Should(BeTrue())
})
It("should successfully validate a template with resource attributes", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/valid-template-resource-attributes.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Errors()).Should(BeEmpty())
Expect(result.Valid()).Should(BeTrue())
})
})
Context("with a valid SAM template", func() {
pwd, _ := os.Getwd()
schemaLoader := NewReferenceLoader("file://" + pwd + "/schema/sam.schema.json")
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/valid-sam-template.json")
result, err := Validate(schemaLoader, documentLoader)
It("should successfully validate the SAM template", func() {
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Errors()).Should(BeEmpty())
Expect(result.Valid()).Should(BeTrue())
})
})
Context("with a valid template, but which contains attributes not yet supported by the generated schema", func() {
pwd, _ := os.Getwd()
schemaLoader := NewReferenceLoader("file://" + pwd + "/schema/cloudformation.schema.json")
It("should report that a template with intrinsic functions is invalid", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/valid-template-with-fns.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Valid()).Should(BeFalse())
})
})
Context("with an invalid CloudFormation template", func() {
pwd, _ := os.Getwd()
schemaLoader := NewReferenceLoader("file://" + pwd + "/schema/cloudformation.schema.json")
It("should report that a template missing required root properties is invalid", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/invalid-template-missing-resources.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Valid()).Should(BeFalse())
})
It("should report that a template missing required resource properties is invalid", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/invalid-template-missing-resource-properties.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Valid()).Should(BeFalse())
})
It("should report that a template with missing single required resource property is invalid", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/invalid-template-empty-resource-properties.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Valid()).Should(BeFalse())
})
It("should report that a template with a non-alphanumeric resource key is invalid", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/invalid-template-invalid-resource-name.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Valid()).Should(BeFalse())
})
It("should report that a template with unknown root properties is invalid", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/invalid-template-additional-property.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Valid()).Should(BeFalse())
})
It("should report that a template with unknown resource type is invalid", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/invalid-template-unknown-resource-type.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Valid()).Should(BeFalse())
})
It("should report that a template with unknown resource type is invalid", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/invalid-template-missing-resource-type.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Valid()).Should(BeFalse())
})
It("should report that a template with unknown resource properties is invalid", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/invalid-template-additional-resource-property.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Valid()).Should(BeFalse())
})
It("should report that a template with an unknown subproperty property is invalid", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/invalid-template-unknown-subproperty-property.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Valid()).Should(BeFalse())
})
It("should report that a template with a wrong subproperty type is invalid", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/invalid-template-invalid-resource-subproperty.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Valid()).Should(BeFalse())
})
It("should report that a template with a subproperty that is missing a required property is invalid", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/invalid-template-subproperty-missing-property.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Valid()).Should(BeFalse())
})
})
Context("with an invalid SAM template", func() {
pwd, _ := os.Getwd()
schemaLoader := NewReferenceLoader("file://" + pwd + "/schema/sam.schema.json")
It("should report that a template missing the Transform property is invalid", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/invalid-sam-template-no-transform.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Valid()).Should(BeFalse())
})
It("should report that a template with a non-SAM Transform value is invalid", func() {
documentLoader := NewReferenceLoader("file://" + pwd + "/test/json/invalid-sam-template-wrong-transform.json")
result, err := Validate(schemaLoader, documentLoader)
Expect(err).To(BeNil())
Expect(result).ShouldNot(BeNil())
Expect(result.Valid()).Should(BeFalse())
})
})
})