Skip to content

Commit 4c05b69

Browse files
committed
✅ add unit test for deploy-prod-dc script
1 parent 7d5496c commit 4c05b69

File tree

2 files changed

+93
-18
lines changed

2 files changed

+93
-18
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import assert from 'node:assert/strict'
2+
import path from 'node:path'
3+
import { beforeEach, before, describe, it, mock } from 'node:test'
4+
import type { CommandDetail } from './lib/testHelpers.ts'
5+
import { mockModule, mockCommandImplementation } from './lib/testHelpers.ts'
6+
7+
// eslint-disable-next-line
8+
describe.only('deploy-prod-dc', () => {
9+
const commandMock = mock.fn()
10+
11+
let commands: CommandDetail[]
12+
13+
before(async () => {
14+
await mockModule(path.resolve(import.meta.dirname, '../lib/command.ts'), { command: commandMock })
15+
await mockModule(path.resolve(import.meta.dirname, '../lib/executionUtils.ts'), {
16+
timeout: () => Promise.resolve(),
17+
})
18+
})
19+
20+
beforeEach(() => {
21+
commands = mockCommandImplementation(commandMock)
22+
})
23+
24+
it('should deploy a given datacenter', async () => {
25+
await runScript('./deploy-prod-dc.ts', 'v6', 'us1')
26+
27+
assert.deepEqual(commands, [
28+
{ command: 'node ./scripts/deploy/deploy.ts prod v6 us1' },
29+
{ command: 'node ./scripts/deploy/upload-source-maps.ts v6 us1' },
30+
])
31+
})
32+
33+
it('should deploy a given datacenter with check monitors', async () => {
34+
await runScript('./deploy-prod-dc.ts', 'v6', 'us1', '--check-monitors')
35+
36+
assert.deepEqual(commands, [
37+
{ command: 'node ./scripts/deploy/check-monitors.ts us1' },
38+
{ command: 'node ./scripts/deploy/deploy.ts prod v6 us1' },
39+
{ command: 'node ./scripts/deploy/upload-source-maps.ts v6 us1' },
40+
// 1 monitor check per minute for 30 minutes
41+
...Array.from({ length: 30 }, () => ({ command: 'node ./scripts/deploy/check-monitors.ts us1' })),
42+
])
43+
})
44+
45+
it('should only check monitors before deploying if the upload path is root', async () => {
46+
await runScript('./deploy-prod-dc.ts', 'v6', 'root', '--check-monitors')
47+
48+
assert.deepEqual(commands, [
49+
{ command: 'node ./scripts/deploy/check-monitors.ts root' },
50+
{ command: 'node ./scripts/deploy/deploy.ts prod v6 root' },
51+
{ command: 'node ./scripts/deploy/upload-source-maps.ts v6 root' },
52+
])
53+
})
54+
55+
it('should deploy all minor datacenters', async () => {
56+
await runScript('./deploy-prod-dc.ts', 'v6', 'minor-dcs', '--no-check-monitors')
57+
58+
assert.deepEqual(commands, [
59+
{ command: 'node ./scripts/deploy/deploy.ts prod v6 us3,us5,ap1,ap2,prtest00' },
60+
{ command: 'node ./scripts/deploy/upload-source-maps.ts v6 us3,us5,ap1,ap2,prtest00' },
61+
])
62+
})
63+
})
64+
65+
async function runScript(scriptPath: string, ...args: string[]): Promise<void> {
66+
const { main } = (await import(scriptPath)) as { main: (...args: string[]) => Promise<void> }
67+
68+
return main(...args)
69+
}

scripts/deploy/deploy-prod-dc.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,33 @@ const ONE_MINUTE_IN_SECOND = 60
1212
const GATE_DURATION = 30 * ONE_MINUTE_IN_SECOND
1313
const GATE_INTERVAL = ONE_MINUTE_IN_SECOND
1414

15-
const {
16-
values: { 'check-monitors': checkMonitors },
17-
positionals,
18-
} = parseArgs({
19-
allowPositionals: true,
20-
allowNegative: true,
21-
options: {
22-
'check-monitors': {
23-
type: 'boolean',
15+
if (!process.env.NODE_TEST_CONTEXT) {
16+
runMain(() => main(...process.argv.slice(2)))
17+
}
18+
19+
export async function main(...args: string[]): Promise<void> {
20+
const {
21+
values: { 'check-monitors': checkMonitors },
22+
positionals,
23+
} = parseArgs({
24+
args,
25+
allowPositionals: true,
26+
allowNegative: true,
27+
options: {
28+
'check-monitors': {
29+
type: 'boolean',
30+
default: false,
31+
},
2432
},
25-
},
26-
})
33+
})
2734

28-
const version = positionals[0]
29-
const uploadPath = positionals[1] === 'minor-dcs' ? getAllMinorDcs().join(',') : positionals[1]
35+
const version = positionals[0]
36+
const uploadPath = positionals[1] === 'minor-dcs' ? getAllMinorDcs().join(',') : positionals[1]
3037

31-
if (!uploadPath) {
32-
throw new Error('UPLOAD_PATH argument is required')
33-
}
38+
if (!uploadPath) {
39+
throw new Error('UPLOAD_PATH argument is required')
40+
}
3441

35-
runMain(async () => {
3642
if (checkMonitors) {
3743
command`node ./scripts/deploy/check-monitors.ts ${uploadPath}`.withLogs().run()
3844
}
@@ -43,7 +49,7 @@ runMain(async () => {
4349
if (checkMonitors && uploadPath !== 'root') {
4450
await gateMonitors(uploadPath)
4551
}
46-
})
52+
}
4753

4854
async function gateMonitors(uploadPath: string): Promise<void> {
4955
printLog(`Check monitors for ${uploadPath} during ${GATE_DURATION / ONE_MINUTE_IN_SECOND} minutes`)

0 commit comments

Comments
 (0)