-
Notifications
You must be signed in to change notification settings - Fork 20
/
e2e.js
335 lines (287 loc) · 14 KB
/
e2e.js
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
Copyright 2023 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const sdk = require('../src/index')
const path = require('path')
const deepClone = require('lodash.clonedeep')
const { utils } = require('../src')
const fs = jest.requireActual('fs-extra')
const { createHttpsProxy } = require('@adobe/aio-lib-test-proxy')
jest.unmock('openwhisk')
jest.unmock('archiver')
jest.setTimeout(40000)
// load .env values in the e2e folder, if any
require('dotenv').config({ path: path.join(__dirname, '.env') })
let proxyServer
let sdkClient = {}
let config = {}
const apiKey = process.env['RuntimeAPI_API_KEY']
const apihost = process.env['RuntimeAPI_APIHOST'] || 'https://adobeioruntime.net'
const namespace = process.env['RuntimeAPI_NAMESPACE']
const E2E_USE_PROXY = process.env.E2E_USE_PROXY
const HTTPS_PROXY = process.env.HTTPS_PROXY
beforeAll(async () => {
if (E2E_USE_PROXY) {
proxyServer = await createHttpsProxy()
console.log(`Using test proxy at ${proxyServer.url}`)
}
sdkClient = await sdk.init({ api_key: apiKey, apihost })
})
afterAll(() => {
if (proxyServer) {
proxyServer.stop()
}
})
beforeEach(() => {
config = deepClone(global.sampleAppConfig)
config.ow.namespace = namespace
config.ow.auth = apiKey
config.root = path.resolve('./test/__fixtures__/sample-app')
config.actions.src = path.resolve(config.root + '/' + config.actions.src)
config.actions.dist = path.resolve(config.root + '/' + config.actions.dist)
config.manifest.src = path.resolve(config.root + '/' + 'manifest.yml')
})
// eslint-disable-next-line jest/expect-expect
test('HTTPS_PROXY must be set if E2E_USE_PROXY is set', () => {
// jest wraps process.env, so libraries will not pick up an env change via code change, so it has to be set on the shell level
if (E2E_USE_PROXY) {
if (!HTTPS_PROXY) {
throw new Error(`If you set E2E_USE_PROXY, you must set the HTTPS_PROXY environment variable. Please set it to HTTPS_PROXY=${proxyServer.url}.`)
}
}
})
describe('build-actions', () => {
test('full config', async () => {
/* skip checking previously built actions */
utils.dumpActionsBuiltInfo = jest.fn(() => false)
utils.actionBuiltBefore = jest.fn(() => false)
expect(await sdk.buildActions(config)).toEqual(expect.arrayContaining([
expect.stringContaining('action.zip'),
expect.stringContaining('action-zip.zip')
]))
expect(fs.readdirSync(path.resolve(config.actions.dist, 'sample-app-1.0.0'))).toEqual(expect.arrayContaining(['action-temp', 'action-zip-temp', 'action-zip.zip', 'action.zip']))
fs.emptydirSync(config.actions.dist)
fs.rmdirSync(config.actions.dist)
})
})
describe('build, deploy, invoke and undeploy of actions', () => {
test('basic manifest', async () => {
/* skip checking previously built actions */
utils.dumpActionsBuiltInfo = jest.fn(() => false)
utils.actionBuiltBefore = jest.fn(() => false)
// Build
expect(await sdk.buildActions(config)).toEqual(expect.arrayContaining([
expect.stringContaining('action.zip'),
expect.stringContaining('action-zip.zip')
]))
// Deploy
config.root = path.resolve('./')
const deployedEntities = await sdk.deployActions(config)
expect(deployedEntities.actions[0].url.endsWith('.adobeio-static.net/api/v1/web/sample-app-1.0.0/action')).toEqual(true)
expect(deployedEntities.actions[1].url.endsWith('.adobeio-static.net/api/v1/web/sample-app-1.0.0/action-zip')).toEqual(true)
expect(deployedEntities.actions[2].url.endsWith('.adobeio-static.net/api/v1/web/sample-app-1.0.0/action-sequence')).toEqual(true)
// Cleanup build files
fs.emptydirSync(config.actions.dist)
fs.rmdirSync(config.actions.dist)
// Verify actions created in openwhisk
let actions = await sdkClient.actions.list({ limit: 3 })
expect(actions).toEqual(expect.arrayContaining([expect.objectContaining({ name: 'action-sequence', namespace: expect.stringContaining('/sample-app-1.0.0') }),
expect.objectContaining({ name: 'action-zip', namespace: expect.stringContaining('/sample-app-1.0.0') }),
expect.objectContaining({ name: 'action', namespace: expect.stringContaining('/sample-app-1.0.0') })]))
await sdkClient.actions.invoke('sample-app-1.0.0/action')
// Undeploy
await sdk.undeployActions(config)
actions = await sdkClient.actions.list({ limit: 1 })
if (actions.length > 0) {
// eslint-disable-next-line jest/no-conditional-expect
expect(actions[0].name).not.toEqual('action-sequence')
}
})
test('manifest with includes', async () => {
config = deepClone(global.sampleAppIncludesConfig)
config.ow.namespace = namespace
config.ow.auth = apiKey
config.root = path.resolve('./test/__fixtures__/sample-app-includes')
config.actions.src = path.resolve(config.root + '/' + config.actions.src)
config.actions.dist = path.resolve(config.root + '/' + config.actions.dist)
config.manifest.src = path.resolve(config.root + '/' + 'manifest.yml')
// Build
expect(await sdk.buildActions(config)).toEqual(expect.arrayContaining([
expect.stringContaining('action.zip')
]))
// Deploy
config.root = path.resolve('./')
const deployedEntities = await sdk.deployActions(config)
expect(deployedEntities.actions[0].url.endsWith('.adobeio-static.net/api/v1/web/sample-app-include-1.0.0/action')).toEqual(true)
// Cleanup build files
fs.emptydirSync(config.actions.dist)
fs.rmdirSync(config.actions.dist)
// Verify actions created in openwhisk
let actions = await sdkClient.actions.list({ limit: 3 })
expect(actions).toEqual(expect.arrayContaining([expect.objectContaining({ name: 'action', namespace: expect.stringContaining('/sample-app-include-1.0.0') })]))
await sdkClient.actions.invoke('sample-app-include-1.0.0/action')
// Undeploy
await sdk.undeployActions(config)
actions = await sdkClient.actions.list({ limit: 1 })
if (actions.length > 0) {
// eslint-disable-next-line jest/no-conditional-expect
expect(actions[0].name).not.toEqual('action')
}
})
test('manifest with default package', async () => {
config = deepClone(global.sampleAppReducedDefaultPackageConfig)
config.ow.namespace = namespace
config.ow.auth = apiKey
config.root = path.resolve('./test/__fixtures__/sample-app-reduced-default-package')
config.actions.src = path.resolve(config.root + '/' + config.actions.src)
config.actions.dist = path.resolve(config.root + '/' + config.actions.dist)
config.manifest.src = path.resolve(config.root + '/' + 'manifest.yml')
// Build
expect(await sdk.buildActions(config)).toEqual(expect.arrayContaining([
expect.stringContaining('action.zip')
]))
// Deploy
config.root = path.resolve('./')
const deployedEntities = await sdk.deployActions(config)
// action is not behind CDN since we don't have static assets
expect(deployedEntities.actions[0].url.endsWith('.adobeioruntime.net/api/v1/web/default/action')).toEqual(true)
// Cleanup build files
fs.emptydirSync(config.actions.dist)
fs.rmdirSync(config.actions.dist)
// Verify actions created in openwhisk
let actions = await sdkClient.actions.list({ limit: 3 })
expect(actions).toEqual(expect.arrayContaining([expect.objectContaining({
name: 'action',
namespace: expect.not.stringContaining('/default') // if it's the default package, it won't show in the namespace property
})]))
await sdkClient.actions.invoke('action')
// Undeploy
await sdk.undeployActions(config)
actions = await sdkClient.actions.list({ limit: 1 })
if (actions.length > 0) {
// eslint-disable-next-line jest/no-conditional-expect
expect(actions[0].name).not.toEqual('action')
}
})
test('basic manifest with filter', async () => {
// Build
expect(await sdk.buildActions(config, ['action'])).toEqual([expect.stringContaining('action.zip')])
// Deploy
config.root = path.resolve('./')
const deployedEntities = await sdk.deployActions(config, { filterEntities: { actions: ['action'] } })
expect(deployedEntities.actions.length).toBe(1)
expect(deployedEntities.actions[0].url.endsWith('.adobeio-static.net/api/v1/web/sample-app-1.0.0/action')).toEqual(true)
// Cleanup build files
fs.emptydirSync(config.actions.dist)
fs.rmdirSync(config.actions.dist)
// Verify actions created in openwhisk
let actions = await sdkClient.actions.list({ limit: 3 })
expect(actions).toEqual(expect.arrayContaining([
expect.objectContaining({ name: 'action', namespace: expect.stringContaining('/sample-app-1.0.0') })]))
await sdkClient.actions.invoke('sample-app-1.0.0/action')
// NOTE: undeploy does not have a corresponding filterEntities, we do a quick manual filter
delete config.manifest.full.packages.__APP_PACKAGE__.rules.rule1
delete config.manifest.package.rules.rule1
// Undeploy
await sdk.undeployActions(config)
actions = await sdkClient.actions.list({ limit: 1 })
if (actions.length > 0) {
// eslint-disable-next-line jest/no-conditional-expect
expect(actions[0].name).not.toEqual('action')
}
})
})
describe('print logs', () => {
test('basic', async () => {
const logs = []
const storeLogs = (str) => { logs.push(str) }
// Runtime waits for about 60 secs to return a 503 when it cannot serve the request.
jest.setTimeout(100000)
try {
const retResult = await sdk.printActionLogs(config, storeLogs, 1, [], false, false)
expect(typeof retResult).toEqual('object')
} catch (err) {
// If the request was not successful, it has to be a 503 from Runtime.
expect(typeof err).toEqual('object') // eslint-disable-line jest/no-conditional-expect
expect(err.message).toEqual(expect.stringContaining('503')) // eslint-disable-line jest/no-conditional-expect
expect(err.message).toEqual(expect.stringContaining('Service Unavailable')) // eslint-disable-line jest/no-conditional-expect
}
jest.setTimeout(30000)
})
})
test('delete non-existing trigger', async () => {
const now = new Date()
const triggerName = `e2eTrigger${now.getTime()}`
// Delete non existing trigger
const call = sdkClient.triggers.delete({ name: triggerName })
await expect(call).rejects.toThrow('The requested resource does not exist')
})
test('basic trigger', async () => {
const now = new Date()
const triggerName = `e2eTrigger${now.getTime()}`
// Create
expect(await sdkClient.triggers.create({ name: triggerName })).toEqual(expect.objectContaining({ name: triggerName, version: '0.0.1' }))
// Get
expect(await sdkClient.triggers.get({ name: triggerName })).toEqual(expect.objectContaining({ name: triggerName, version: '0.0.1' }))
// Delete
expect(await sdkClient.triggers.delete({ name: triggerName })).toEqual(expect.objectContaining({ name: triggerName, version: '0.0.1' }))
})
test('trigger with feed /whisk.system/alarms/alarm', async () => {
const now = new Date()
const triggerName = `e2eTrigger${now.getTime()}`
// Create
expect(await sdkClient.triggers.create({ name: triggerName, trigger: { feed: '/whisk.system/alarms/alarm', parameters: [{ key: 'cron', value: '* * * * *' }] } })).toEqual(expect.objectContaining({ name: triggerName, version: '0.0.1', annotations: expect.arrayContaining([expect.objectContaining({ key: 'feed' })]) }))
// Get
expect(await sdkClient.triggers.get({ name: triggerName })).toEqual(expect.objectContaining({ name: triggerName, version: '0.0.1' }))
// Delete
expect(await sdkClient.triggers.delete({ name: triggerName })).toEqual(expect.objectContaining({ name: triggerName, version: '0.0.1' }))
})
test('trigger with feed /whisk.system/alarms/once', async () => {
const now = new Date()
const triggerName = `e2eTrigger${now.getTime()}`
// add 1 day. "now" is 1 day later. need this for the future alarm
now.setDate(now.getDate() + 1)
// Create
expect(await sdkClient.triggers.create({ name: triggerName, trigger: { feed: '/whisk.system/alarms/once', parameters: [{ key: 'date', value: now.toISOString() }] } })).toEqual(expect.objectContaining({ name: triggerName, version: '0.0.1', annotations: expect.arrayContaining([expect.objectContaining({ key: 'feed' })]) }))
// Get
expect(await sdkClient.triggers.get({ name: triggerName })).toEqual(expect.objectContaining({ name: triggerName, version: '0.0.1' }))
// Delete
expect(await sdkClient.triggers.delete({ name: triggerName })).toEqual(expect.objectContaining({ name: triggerName, version: '0.0.1' }))
})
describe('filter manifest based on built actions', () => {
test('it should build & deploy just one of two.', async () => {
// Prepare
const fileData = JSON.stringify({ 'action-zip': 1632317755882 })
fs.readFile = jest.fn(() => (fileData))
const deployConfig = {
filterEntities: {
byBuiltActions: true
}
}
// Build
expect(await sdk.buildActions(config)).toEqual(expect.arrayContaining([
expect.stringContaining('action.zip')
]))
// Deploy
config.root = path.resolve('./')
const deployedEntities = await sdk.deployActions(config, deployConfig)
expect(deployedEntities.actions).toEqual(expect.arrayContaining([
expect.objectContaining({ name: 'sample-app-1.0.0/action' }),
expect.objectContaining({ name: 'sample-app-1.0.0/action-sequence' })
]))
// Undeploy
await sdk.undeployActions(config)
const actions = await sdkClient.actions.list({ limit: 1 })
if (actions.length > 0) {
// eslint-disable-next-line jest/no-conditional-expect
expect(actions[0].name).not.toEqual('action-sequence')
}
})
})