-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgoserverless_test.go
262 lines (214 loc) · 8.62 KB
/
goserverless_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package goserverless_test
import (
"io/ioutil"
"os"
"github.com/awslabs/goformation/v4/cloudformation/dynamodb"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/thepauleh/goserverless"
)
var _ = Describe("GoServerless", func() {
Context("Transform and hydrate a basic serverless config back to our structs", func() {
importedTemplate, err := goserverless.Open("test/yaml/basic-service.yaml")
It("should read basic params", func() {
Expect(err).To(BeNil())
Expect(importedTemplate).To(Not(BeNil()))
Expect(importedTemplate.Service.Name).To(Equal("basicService"))
Expect(importedTemplate.FrameworkVersion).To(Equal(">=1.0.0 <2.1.9"))
})
It("should roundtrip without error", func() {
bytes, err := importedTemplate.YAML()
Expect(err).To(BeNil())
file, err := ioutil.TempFile("", "goformation")
defer os.Remove(file.Name())
Expect(err).To(BeNil())
_, err = file.Write(bytes)
Expect(err).To(BeNil())
err = file.Close()
Expect(err).To(BeNil())
roundtripTemplate, err := goserverless.Open(file.Name())
Expect(err).To(BeNil())
Expect(roundtripTemplate).To(Not(BeNil()))
})
})
Context("Transform and hydrate a serverless config with a string service value back to our structs", func() {
importedTemplate, err := goserverless.Open("test/yaml/service-just-name.yaml")
It("should read basic params", func() {
Expect(err).To(BeNil())
Expect(importedTemplate).To(Not(BeNil()))
Expect(importedTemplate.Service.Name).To(Equal("basicService"))
Expect(importedTemplate.FrameworkVersion).To(Equal(">=1.0.0 <2.1.9"))
})
It("should roundtrip without error", func() {
bytes, err := importedTemplate.YAML()
Expect(err).To(BeNil())
file, err := ioutil.TempFile("", "goformation")
defer os.Remove(file.Name())
Expect(err).To(BeNil())
_, err = file.Write(bytes)
Expect(err).To(BeNil())
err = file.Close()
Expect(err).To(BeNil())
roundtripTemplate, err := goserverless.Open(file.Name())
Expect(err).To(BeNil())
Expect(roundtripTemplate).To(Not(BeNil()))
})
})
Context("Transform and hydrate a full serverless config back to our structs", func() {
importedTemplate, err := goserverless.Open("test/yaml/simple-serverless.yaml")
It("should get the imported template", func() {
Expect(err).To(BeNil())
Expect(importedTemplate).To(Not(BeNil()))
})
// Start Testing the function events!
usersCreateFunction := importedTemplate.Functions["usersCreate"]
It("should read basic params", func() {
Expect(err).To(BeNil())
Expect(importedTemplate).To(Not(BeNil()))
Expect(importedTemplate.Service.Name).To(Equal("myService"))
Expect(importedTemplate.FrameworkVersion).To(Equal(">=1.0.0 <2.0.0"))
})
It("should read provider params", func() {
// Test the provider
Expect(importedTemplate.Provider.Name).To(Equal("aws"))
Expect(importedTemplate.Provider.Runtime).To(Equal("nodejs6.10"))
Expect(importedTemplate.Provider.MemorySize).To(Equal(512))
const serviceEnvVar float64 = 123456789
Expect(importedTemplate.Provider.Environment["serviceEnvVar"]).To(Equal(serviceEnvVar))
})
It("should read the provider vpc settings", func() {
Expect(importedTemplate.Provider.VPC.SecurityGroupIds[0]).To(Equal("securityGroupId1"))
})
It("should read the stack tags", func() {
Expect(importedTemplate.Provider.StackTags["key"]).To(Equal("value"))
})
It("should read the apiKeys", func() {
Expect(importedTemplate.Provider.APIKeys[0]).To(Equal("myFirstKey"))
})
It("should read the iamRole", func() {
Expect(importedTemplate.Provider.IAMRoleStatements[0]["Effect"]).To(Equal("Allow"))
})
It("should read the stack policy", func() {
Expect(importedTemplate.Provider.StackPolicy[0]["Effect"]).To(Equal("Allow"))
})
It("should read the notificationArns", func() {
Expect(importedTemplate.Provider.NotificationARNs[0]).To(Equal("arn:aws:sns:us-east-1:XXXXXX:mytopic"))
})
It("should read package params", func() {
// Test the package
Expect(importedTemplate.Package.Include[0]).To(Equal("src/**"))
Expect(importedTemplate.Package.Include[1]).To(Equal("handler.js"))
Expect(importedTemplate.Package.ExcludeDevDependencies).To(Equal(false))
})
It("should read functions params", func() {
// Test the function
Expect(usersCreateFunction.Handler).To(Equal("users.create"))
Expect(usersCreateFunction.Name).To(Equal("${self:provider.stage}-lambdaName"))
Expect(usersCreateFunction.MemorySize).To(Equal(512))
})
It("should read the functons vpc settings", func() {
Expect(usersCreateFunction.VPC.SecurityGroupIds[0]).To(Equal("securityGroupId1"))
})
It("should read the http request", func() {
// HTTP Request
event := usersCreateFunction.Events[0].HTTPEvent
Expect(event.Path).To(Equal("users/create"))
Expect(event.Method).To(Equal("get"))
Expect(event.Authorizer["name"]).To(Equal("authorizerFunc"))
Expect(event.Authorizer["identityValidationExpression"]).To(Equal("someRegex"))
})
It("should read the s3 event", func() {
// S3 Object
event := usersCreateFunction.Events[1].S3Event
Expect(event.Bucket).To(Equal("photos"))
Expect(event.Event).To(Equal("s3:ObjectCreated:*"))
// TODO: Rules
})
It("should read the schedule event", func() {
// Schedule/Cron Event
event := usersCreateFunction.Events[2].ScheduleEvent
Expect(event.Rate).To(Equal("rate(10 minutes)"))
Expect(event.Enabled).To(Equal(false))
Expect(event.Input["key1"]).To(Equal("value1"))
Expect(event.Input["key2"]).To(Equal("value2"))
testStageParams := map[string]interface{}{
"stage": "dev",
}
Expect(event.Input["stageParams"]).To(Equal(testStageParams))
})
It("should read the sns event", func() {
event := usersCreateFunction.Events[3].SNSEvent
Expect(event.TopicName).To(Equal("aggregate"))
Expect(event.DisplayName).To(Equal("Data aggregation pipeline"))
})
It("should read the stream event", func() {
event := usersCreateFunction.Events[4].StreamEvent
Expect(event.ARN).To(Equal("arn:aws:kinesis:region:XXXXXX:stream/foo"))
Expect(event.BatchSize).To(Equal(100))
Expect(event.StartingPosition).To(Equal("LATEST"))
Expect(event.Enabled).To(Equal(false))
})
// AlexaSkill
It("should read the alex skill event", func() {
event := usersCreateFunction.Events[5].AlexaSkillEvent
Expect(event.AppID).To(Equal("amzn1.ask.skill.xx-xx-xx-xx"))
Expect(event.Enabled).To(Equal(true))
})
// AlexaSmartHome
It("should read the alex smart home event", func() {
event := usersCreateFunction.Events[6].AlexaSmartHomeEvent
Expect(event.AppID).To(Equal("amzn1.ask.skill.xx-xx-xx-xx"))
Expect(event.Enabled).To(Equal(true))
})
// IOT
It("should read the iot event", func() {
event := usersCreateFunction.Events[7].IOTEvent
Expect(event.Name).To(Equal("myIoTEvent"))
Expect(event.Description).To(Equal("An IoT event"))
Expect(event.Enabled).To(Equal(true))
Expect(event.SQL).To(Equal("SELECT * FROM 'some_topic'"))
Expect(event.SQLVersion).To(Equal("beta"))
})
// Cloudwatch Event
It("should read the cloudwatch event", func() {
event := usersCreateFunction.Events[8].CloudwatchEvent
Expect(event.Input["key1"]).To(Equal("value1"))
// TODO: Input StageParams cannot be extracted under current interface
Expect(event.InputPath).To(Equal("$.stageVariables"))
})
// Cloudwatch Log
It("should read the cloudwatch log event", func() {
event := usersCreateFunction.Events[9].CloudwatchLogEvent
Expect(event.LogGroup).To(Equal("/aws/lambda/hello"))
Expect(event.Filter).To(Equal("{$.userIdentity.type = Root}"))
})
// Cognito User Pool
It("should read the cognito user pool event", func() {
event := usersCreateFunction.Events[10].CognitoUserPoolEvent
Expect(event.Pool).To(Equal("MyUserPool"))
Expect(event.Trigger).To(Equal("PreSignUp"))
})
It("should read resources params", func() {
// Test the provider
Expect(importedTemplate.Resources.Resources).ShouldNot(BeNil())
Expect(importedTemplate.Resources.Resources["usersTable"]).Should(BeAssignableToTypeOf(new(dynamodb.Table)))
})
It("Should pull out the resource policy", func() {
Expect(importedTemplate.Provider.ResourcePolicy).ShouldNot(BeNil())
})
It("should roundtrip without error", func() {
bytes, err := importedTemplate.YAML()
Expect(err).To(BeNil())
file, err := ioutil.TempFile("", "goformation")
defer os.Remove(file.Name())
Expect(err).To(BeNil())
_, err = file.Write(bytes)
Expect(err).To(BeNil())
err = file.Close()
Expect(err).To(BeNil())
roundtripTemplate, err := goserverless.Open(file.Name())
Expect(err).To(BeNil())
Expect(roundtripTemplate).To(Not(BeNil()))
})
})
})