-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdeploy.spec.ts
More file actions
446 lines (396 loc) · 14.3 KB
/
deploy.spec.ts
File metadata and controls
446 lines (396 loc) · 14.3 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* eslint-disable no-console */
import path from 'node:path'
import config from 'config'
import { v4 as uuidv4 } from 'uuid'
import axios from 'axios'
import { DateTime, Duration } from 'luxon'
import { describe, it, expect, beforeAll, beforeEach, afterAll, afterEach } from 'vitest'
import Projects from '../../src/rest/projects'
import { FixtureSandbox, RunOptions } from '../../src/testing/fixture-sandbox'
import { ExecaError } from 'execa'
async function cleanupProjects (projectLogicalId?: string) {
const baseURL: string = config.get('baseURL')
const accountId: string = config.get('accountId')
const apiKey: string = config.get('apiKey')
// Why create an axios client rather than using the one in rest/api?
// The rest/api client is configured based on the NODE_ENV and CLI config file, which isn't suitable for e2e tests.
const api = axios.create({
baseURL,
headers: {
'x-checkly-account': accountId,
'Authorization': `Bearer ${apiKey}`,
},
})
const projectsApi = new Projects(api)
if (projectLogicalId) {
await projectsApi.deleteProject(projectLogicalId)
return
}
const { data: projects } = await projectsApi.getAll()
for (const project of projects) {
// Also delete any old projects that may have been missed in previous e2e tests
const leftoverE2eProject = project.name.startsWith('e2e-test-deploy-project-')
&& DateTime.fromISO(project.created_at) < DateTime.now().minus(Duration.fromObject({ minutes: 20 }))
if (leftoverE2eProject) {
await projectsApi.deleteProject(project.logicalId)
}
}
}
async function getAllResources (type: 'checks' | 'check-groups' | 'private-locations') {
const baseURL: string = config.get('baseURL')
const accountId: string = config.get('accountId')
const apiKey: string = config.get('apiKey')
const entries: any[] = []
const api = axios.create({
baseURL,
headers: {
'x-checkly-account': accountId,
'Authorization': `Bearer ${apiKey}`,
},
})
// PL endpoint doesn't have pagination
if (type === 'private-locations') {
const { data } = await api({
method: 'get',
url: `/v1/${type}`,
})
return data
}
let pageNumber = 1
while (true) {
const { data } = await api({
method: 'get',
url: `/v1/${type}?&page=${pageNumber}&limit=100`,
})
if (data.length === 0) {
break
}
entries.push(...data)
pageNumber++
}
return entries
}
async function runDeploy (fixt: FixtureSandbox, args: string[], options?: RunOptions) {
const result = await fixt.run('npx', [
'checkly',
'deploy',
...args,
], {
timeout: 120_000,
...options,
})
if (result.exitCode !== 0) {
console.error('stderr', result.stderr)
console.error('stdout', result.stdout)
}
expect(result.exitCode).toBe(0)
return result
}
describe('deploy', { timeout: 45_000 }, () => {
// Create a unique ID suffix to support parallel test executions
let projectLogicalId: string
let privateLocationSlugname: string
// Cleanup projects that may have not been deleted in previous runs
beforeAll(async () => {
await cleanupProjects()
})
beforeEach(() => {
projectLogicalId = `e2e-test-deploy-project-${uuidv4()}`
privateLocationSlugname = `private-location-cli-${uuidv4().split('-')[0]}`
})
// Clean up by deleting the project
afterEach(() => cleanupProjects(projectLogicalId))
afterAll(() => cleanupProjects())
describe('deploy-project', () => {
let fixt: FixtureSandbox
beforeAll(async () => {
fixt = await FixtureSandbox.create({
source: path.join(__dirname, 'fixtures', 'deploy-project'),
})
}, 180_000)
afterAll(async () => {
await fixt?.destroy()
})
it('Simple project should deploy successfully (version v4.0.8)', async () => {
const { stderr, stdout } = await runDeploy(fixt, ['--force'], {
env: {
PROJECT_LOGICAL_ID: projectLogicalId,
PRIVATE_LOCATION_SLUG_NAME: privateLocationSlugname,
CHECKLY_CLI_VERSION: '4.0.8',
},
})
expect(stderr).toBe('')
// expect not to change version since the version is specified
expect(stdout).not.toContain('Notice: replacing version')
const checks = await getAllResources('checks')
const checkGroups = await getAllResources('check-groups')
const privateLocations = await getAllResources('private-locations')
expect(checks.filter(({ privateLocations }: { privateLocations?: string[] }) =>
privateLocations?.some(slugName => slugName.startsWith(privateLocationSlugname))).length).toEqual(1)
expect(checkGroups.filter(({ privateLocations }: { privateLocations: string[] }) =>
privateLocations.some(slugName => slugName.startsWith(privateLocationSlugname))).length).toEqual(2)
expect(privateLocations
.filter(({ slugName }: { slugName: string }) => slugName.startsWith(privateLocationSlugname)).length).toEqual(1)
})
it('Simple project should deploy successfully', async () => {
const { stderr, stdout } = await runDeploy(fixt, ['--force'], {
env: {
PROJECT_LOGICAL_ID: projectLogicalId,
PRIVATE_LOCATION_SLUG_NAME: privateLocationSlugname,
CHECKLY_CLI_VERSION: undefined,
},
})
expect(stderr).toBe('')
// Non-interactive runs should no longer emit the local-dev version notice.
expect(stdout).not.toContain('Notice: replacing version')
const checks = await getAllResources('checks')
const checkGroups = await getAllResources('check-groups')
const privateLocations = await getAllResources('private-locations')
expect(checks.filter(({ privateLocations }: { privateLocations?: string[] }) =>
privateLocations?.some(slugName => slugName.startsWith(privateLocationSlugname))).length).toEqual(1)
expect(checkGroups.filter(({ privateLocations }: { privateLocations: string[] }) =>
privateLocations.some(slugName => slugName.startsWith(privateLocationSlugname))).length).toEqual(2)
expect(privateLocations
.filter(({ slugName }: { slugName: string }) => slugName.startsWith(privateLocationSlugname)).length).toEqual(1)
})
it('Should deploy with different config file', async () => {
const resultOne = await runDeploy(fixt, ['--preview'], {
env: {
PROJECT_LOGICAL_ID: projectLogicalId,
PRIVATE_LOCATION_SLUG_NAME: privateLocationSlugname,
CHECKLY_CLI_VERSION: '4.8.0',
},
timeout: 10000,
})
const resultTwo = await runDeploy(fixt, ['--preview', '--config', 'checkly.staging.config.ts'], {
env: {
PROJECT_LOGICAL_ID: projectLogicalId,
PRIVATE_LOCATION_SLUG_NAME: privateLocationSlugname,
CHECKLY_CLI_VERSION: '4.8.0',
},
timeout: 10000,
})
expect(resultOne.stdout).toContain(
`Create:
ApiCheck: api-check
ApiCheck: api-check-high-freq
ApiCheck: api-check-incident-trigger
ApiCheck: api-check-retry-only-on-network-error
DnsMonitor: dns-nonexistent-all-assertion-types
DnsMonitor: dns-welcome-a
DnsMonitor: dns-welcome-aaaa
HeartbeatMonitor: heartbeat-monitor-1
BrowserCheck: homepage-browser-check
IcmpMonitor: icmp-welcome
TcpMonitor: tcp-monitor
CheckGroupV2: my-group-1
CheckGroupV1: my-group-2-v1
Dashboard: dashboard-1
MaintenanceWindow: maintenance-window-1
PrivateLocation: private-location-1
StatusPage: test-page-1
StatusPageService: bar-service
StatusPageService: foo-service
`)
expect(resultTwo.stdout).toContain(
`Create:
ApiCheck: api-check
ApiCheck: api-check-high-freq
ApiCheck: api-check-incident-trigger
ApiCheck: api-check-retry-only-on-network-error
DnsMonitor: dns-nonexistent-all-assertion-types
DnsMonitor: dns-welcome-a
DnsMonitor: dns-welcome-aaaa
HeartbeatMonitor: heartbeat-monitor-1
BrowserCheck: homepage-browser-check
IcmpMonitor: icmp-welcome
BrowserCheck: snapshot-test.test.ts
TcpMonitor: tcp-monitor
CheckGroupV2: my-group-1
CheckGroupV1: my-group-2-v1
Dashboard: dashboard-1
MaintenanceWindow: maintenance-window-1
PrivateLocation: private-location-1
`)
})
})
describe('deploy-esm-project', () => {
let fixt: FixtureSandbox
beforeAll(async () => {
fixt = await FixtureSandbox.create({
source: path.join(__dirname, 'fixtures', 'deploy-esm-project'),
})
}, 180_000)
afterAll(async () => {
await fixt?.destroy()
})
it('Simple esm project should deploy successfully', async () => {
const { stderr } = await runDeploy(fixt, ['--force'], {
env: {
PROJECT_LOGICAL_ID: projectLogicalId,
PRIVATE_LOCATION_SLUG_NAME: privateLocationSlugname,
CHECKLY_CLI_VERSION: '4.8.0',
},
})
expect(stderr).toBe('')
})
})
describe('deploy-agentic-project', () => {
let fixt: FixtureSandbox
beforeAll(async () => {
fixt = await FixtureSandbox.create({
source: path.join(__dirname, 'fixtures', 'deploy-agentic-project'),
})
}, 180_000)
afterAll(async () => {
await fixt?.destroy()
})
it('Should preview an agentic check deployment', async () => {
// Use --preview so the test doesn't depend on the e2e account being
// entitled to actually create agentic checks. The plan still goes
// through full server-side validation, so we get coverage of the
// deploy round-trip without leaving resources behind.
const { stdout } = await runDeploy(fixt, ['--preview'], {
env: {
PROJECT_LOGICAL_ID: projectLogicalId,
PRIVATE_LOCATION_SLUG_NAME: privateLocationSlugname,
CHECKLY_CLI_VERSION: undefined,
},
})
expect(stdout).toContain(
`Create:
AgenticCheck: agentic-pricing-check
AgenticCheck: agentic-runtime-check
`)
})
it('Should deploy and re-read an agentic check', async () => {
const { stderr, stdout } = await runDeploy(fixt, ['--force'], {
env: {
PROJECT_LOGICAL_ID: projectLogicalId,
PRIVATE_LOCATION_SLUG_NAME: privateLocationSlugname,
CHECKLY_CLI_VERSION: undefined,
},
})
expect(stderr).toBe('')
expect(stdout).not.toContain('Notice: replacing version')
const checks = await getAllResources('checks')
const pricingCheck = checks.find(({ name }: { name: string }) =>
name === 'Agentic Pricing Check')
const runtimeCheck = checks.find(({ name }: { name: string }) =>
name === 'Agentic Runtime Check')
expect(pricingCheck).toBeDefined()
expect(pricingCheck.checkType).toEqual('AGENTIC')
// The construct hardcodes a single location for agentic checks.
expect(pricingCheck.locations).toEqual(['us-east-1'])
expect(pricingCheck.tags).toEqual(expect.arrayContaining(['e2e', 'agentic']))
expect(runtimeCheck).toBeDefined()
expect(runtimeCheck.checkType).toEqual('AGENTIC')
expect(runtimeCheck.locations).toEqual(['us-east-1'])
})
})
describe('test-only-project', () => {
let fixt: FixtureSandbox
beforeAll(async () => {
fixt = await FixtureSandbox.create({
source: path.join(__dirname, 'fixtures', 'test-only-project'),
})
}, 180_000)
afterAll(async () => {
await fixt?.destroy()
})
it('Should mark testOnly check as skipped', async () => {
const { stdout } = await runDeploy(fixt, ['--preview'], {
env: {
PROJECT_LOGICAL_ID: projectLogicalId,
PRIVATE_LOCATION_SLUG_NAME: privateLocationSlugname,
TEST_ONLY: 'true',
CHECKLY_CLI_VERSION: '4.8.0',
},
})
expect(stdout).toContain(
`Create:
ApiCheck: not-testonly-default-check
ApiCheck: not-testonly-false-check
Skip (testOnly):
ApiCheck: testonly-true-check
`)
})
it('Should mark testOnly check as deleted if there is a deletion', async () => {
// Deploy a check (testOnly=false)
await runDeploy(fixt, ['--force'], {
env: {
PROJECT_LOGICAL_ID: projectLogicalId,
PRIVATE_LOCATION_SLUG_NAME: privateLocationSlugname,
TEST_ONLY: 'false',
CHECKLY_CLI_VERSION: '4.8.0',
},
})
// Deploy a check (testOnly=true)
const { stdout } = await runDeploy(fixt, ['--force', '--output'], {
env: {
PROJECT_LOGICAL_ID: projectLogicalId,
PRIVATE_LOCATION_SLUG_NAME: privateLocationSlugname,
TEST_ONLY: 'true',
CHECKLY_CLI_VERSION: '4.8.0',
},
})
// Moving the check to testOnly causes it to be deleted.
// The check should only be listed under "Delete" and not "Skip".
expect(stdout).toContain(
`Delete:
Check: testonly-true-check
Update and Unchanged:
ApiCheck: not-testonly-default-check
ApiCheck: not-testonly-false-check`)
})
})
describe('empty-project', () => {
let fixt: FixtureSandbox
beforeAll(async () => {
fixt = await FixtureSandbox.create({
source: path.join(__dirname, 'fixtures', 'empty-project'),
})
}, 180_000)
afterAll(async () => {
await fixt?.destroy()
})
it('Should terminate when no resources are found', async () => {
expect.assertions(1)
try {
await runDeploy(fixt, ['--force'], {
env: {
PROJECT_LOGICAL_ID: projectLogicalId,
PRIVATE_LOCATION_SLUG_NAME: privateLocationSlugname,
CHECKLY_CLI_VERSION: '4.8.0',
},
})
} catch (err: any) {
if (err instanceof ExecaError) {
expect(`${err.stdout}\n${err.stderr}`).toContain('Failed to deploy your project. Unable to find constructs to deploy.')
} else {
throw err
}
}
})
})
describe('snapshot-project', () => {
let fixt: FixtureSandbox
beforeAll(async () => {
fixt = await FixtureSandbox.create({
source: path.join(__dirname, 'fixtures', 'snapshot-project'),
})
}, 180_000)
afterAll(async () => {
await fixt?.destroy()
})
it('Should deploy a project with snapshots', async () => {
await runDeploy(fixt, ['--force'], {
env: {
PROJECT_LOGICAL_ID: projectLogicalId,
CHECKLY_CLI_VERSION: '4.8.0',
},
})
// TODO: Add assertions that the snapshots are successfully uploaded.
})
})
})