Skip to content

Commit 247005e

Browse files
committed
✅ add unit test for deploy-prod-dc script
1 parent 99ff309 commit 247005e

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
@@ -21,27 +21,33 @@ function getAllMinorDcs(): string[] {
2121
return Object.keys(siteByDatacenter).filter((dc) => !MAJOR_DCS.includes(dc))
2222
}
2323

24-
const {
25-
values: { 'check-monitors': checkMonitors },
26-
positionals,
27-
} = parseArgs({
28-
allowPositionals: true,
29-
allowNegative: true,
30-
options: {
31-
'check-monitors': {
32-
type: 'boolean',
24+
if (!process.env.NODE_TEST_CONTEXT) {
25+
runMain(() => main(...process.argv.slice(2)))
26+
}
27+
28+
export async function main(...args: string[]): Promise<void> {
29+
const {
30+
values: { 'check-monitors': checkMonitors },
31+
positionals,
32+
} = parseArgs({
33+
args,
34+
allowPositionals: true,
35+
allowNegative: true,
36+
options: {
37+
'check-monitors': {
38+
type: 'boolean',
39+
default: false,
40+
},
3341
},
34-
},
35-
})
42+
})
3643

37-
const version = positionals[0]
38-
const uploadPath = positionals[1] === 'minor-dcs' ? getAllMinorDcs().join(',') : positionals[1]
44+
const version = positionals[0]
45+
const uploadPath = positionals[1] === 'minor-dcs' ? getAllMinorDcs().join(',') : positionals[1]
3946

40-
if (!uploadPath) {
41-
throw new Error('UPLOAD_PATH argument is required')
42-
}
47+
if (!uploadPath) {
48+
throw new Error('UPLOAD_PATH argument is required')
49+
}
4350

44-
runMain(async () => {
4551
if (checkMonitors) {
4652
command`node ./scripts/deploy/check-monitors.ts ${uploadPath}`.withLogs().run()
4753
}
@@ -52,7 +58,7 @@ runMain(async () => {
5258
if (checkMonitors && uploadPath !== 'root') {
5359
await gateMonitors(uploadPath)
5460
}
55-
})
61+
}
5662

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

0 commit comments

Comments
 (0)