This commit is contained in:
2026-09-20 21:16:15 +08:00
parent f87fcf65de
commit e3415a8778
21 changed files with 363 additions and 51 deletions
@@ -0,0 +1,64 @@
import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import test from 'node:test'
const read = async (path) => (await readFile(new URL(`../${path}`, import.meta.url), 'utf8'))
.replaceAll('\r\n', '\n')
test('all backend business services have blue-green runtime contracts', async () => {
const script = await read('deploy/scripts/deploy-blue-green-service.sh')
for (const service of [
'gateway', 'auth', 'iam', 'firstleg', 'finance', 'order', 'warehouse', 'file', 'print',
'integration-service', 'audit-service', 'gateway-overseas', 'lastleg-service',
'warehouse-overseas-service', 'event-bridge-service'
]) assert.match(script, new RegExp(`${service.replaceAll('-', '\\-')}:`))
assert.match(script, /target_slot="green"/)
assert.match(script, /target_port=\$\(\(BASE_PORT \+ 10000\)\)/)
assert.match(script, /compose run -d --no-deps --name "\$candidate"/)
assert.match(script, /wait_for_candidate/)
assert.match(script, /connect_routing_alias "\$ROUTING_ALIAS" "\$candidate"/)
assert.match(script, /docker stop --time "\$STOP_TIMEOUT" "\$active_container"/)
assert.match(script, /mv -f "\$STATE_FILE\.tmp" "\$STATE_FILE"/)
})
test('candidate failure keeps the active container and successful cutover stops it last', async () => {
const script = await read('deploy/scripts/deploy-blue-green-service.sh')
const start = script.indexOf('compose run -d')
const healthy = script.indexOf('if ! wait_for_candidate')
const route = script.indexOf('connect_routing_alias "$ROUTING_ALIAS" "$candidate"')
const gateway = script.indexOf('if ! switch_gateway_proxy')
const stop = script.indexOf('docker stop --time "$STOP_TIMEOUT" "$active_container"')
const state = script.indexOf('mv -f "$STATE_FILE.tmp" "$STATE_FILE"')
assert.ok(start >= 0 && start < healthy)
assert.ok(healthy < route && route < gateway && gateway < stop && stop < state)
assert.match(script.slice(healthy, route), /cleanup_candidate/)
})
test('gateway cutover is atomic and restores nginx config on validation failure', async () => {
const script = await read('deploy/scripts/deploy-blue-green-service.sh')
assert.match(script, /api-cn\.buggoglobal\.com\.conf/)
assert.match(script, /api-global\.buggoglobal\.com\.conf/)
assert.match(script, /cp -f "\$config" "\$backup"/)
assert.match(script, /nginx -t \|\| ! nginx -s reload/)
assert.match(script, /cp -f "\$backup" "\$config"/)
})
test('compose templates preserve rolling defaults and expose slot metadata', async () => {
const domestic = await read('deploy/docker-compose.yml')
const overseas = await read('deploy/docker-compose.overseas.yml')
for (const compose of [domestic, overseas]) {
assert.match(compose, /BAIYUE_DEPLOY_SLOT: \$\{DEPLOY_SLOT:-rolling\}/)
assert.match(compose, /BAIYUE_RELEASE_ID: \$\{RELEASE_ID:-local\}/)
assert.match(compose, /SERVER_SHUTDOWN: graceful/)
assert.match(compose, /stop_grace_period: \$\{STOP_GRACE_PERIOD:-45s\}/)
assert.match(compose, /\$\{DEPLOY_SLOT_SUFFIX:-\}/)
}
})
test('production single-service pipeline dispatches the blue-green executor', async () => {
const deploy = await read('deploy/scripts/deploy-built-service.sh')
assert.match(deploy, /run_scp "deploy\/scripts\/deploy-blue-green-service\.sh"/)
assert.match(deploy, /if \[ "\$DEPLOY_ENV" = "production" \]/)
assert.match(deploy, /run_ssh sh "\$REMOTE_DIR\/\$REMOTE_BLUE_GREEN_NAME"/)
})
@@ -0,0 +1,140 @@
import assert from 'node:assert/strict'
import { chmod, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { existsSync } from 'node:fs'
import { delimiter, join, resolve } from 'node:path'
import { tmpdir } from 'node:os'
import { spawnSync } from 'node:child_process'
import test from 'node:test'
const root = resolve(import.meta.dirname, '..')
const script = resolve(root, 'deploy/scripts/deploy-blue-green-service.sh')
const shell = process.platform === 'win32' ? 'C:/Program Files/Git/bin/bash.exe' : '/bin/sh'
const dockerMock = `#!/usr/bin/env sh
set -eu
printf '%s\\n' "$*" >> "$MOCK_LOG"
command="$1"
shift
case "$command" in
network)
[ "$1" = inspect ] && { echo '{}'; exit 0; }
exit 0
;;
compose)
for argument in "$@"; do
if [ "$argument" = run ]; then
: > "$MOCK_CANDIDATE_EXISTS"
exit 0
fi
done
exit 0
;;
inspect)
target=''
for argument in "$@"; do target="$argument"; done
case "$target" in
firstleg-production-file)
[ ! -f "$MOCK_LEGACY_REMOVED" ]
;;
firstleg-production-file-green)
[ -f "$MOCK_CANDIDATE_EXISTS" ] || exit 1
case "$*" in
*State.Health*) printf '%s\\n' "\${MOCK_CANDIDATE_HEALTH:-healthy}" ;;
*Config.Image*) printf '%s\\n' 'registry.local/group/file:newtag' ;;
esac
;;
*) exit 1 ;;
esac
;;
stop)
target=''
for argument in "$@"; do target="$argument"; done
[ "$target" = firstleg-production-file ] && : > "$MOCK_LEGACY_STOPPED"
;;
rm)
target=''
for argument in "$@"; do target="$argument"; done
case "$target" in
firstleg-production-file) : > "$MOCK_LEGACY_REMOVED" ;;
firstleg-production-file-green) rm -f "$MOCK_CANDIDATE_EXISTS" ;;
esac
;;
logs) echo 'candidate logs' ;;
*) echo "unexpected docker command: $command $*" >&2; exit 2 ;;
esac
`
async function runBlueGreen(health) {
const temp = await mkdtemp(join(tmpdir(), 'blue-green-runtime-'))
const bin = join(temp, 'bin')
const docker = join(bin, 'docker')
const sleep = join(bin, 'sleep')
const envFile = join(temp, 'deployment.env')
const compose = join(temp, 'compose.yml')
const stateRoot = join(temp, 'state')
const log = join(temp, 'docker.log')
await mkdir(bin)
await mkdir(stateRoot)
await writeFile(docker, dockerMock, 'utf8')
await writeFile(sleep, '#!/usr/bin/env sh\nexit 0\n', 'utf8')
await chmod(docker, 0o755)
await chmod(sleep, 0o755)
await writeFile(envFile, 'PLACEHOLDER=true\n', 'utf8')
await writeFile(compose, 'services: {}\n', 'utf8')
await writeFile(log, '', 'utf8')
const paths = {
candidate: join(temp, 'candidate'), stopped: join(temp, 'legacy-stopped'),
removed: join(temp, 'legacy-removed')
}
const result = spawnSync(shell, [
script.replaceAll('\\', '/'), 'production', 'registry.local/group', 'newtag', 'file',
envFile.replaceAll('\\', '/'), compose.replaceAll('\\', '/'), 'firstleg-production'
], {
cwd: root, encoding: 'utf8', env: {
...process.env,
PATH: `${bin}${delimiter}${process.env.PATH ?? ''}`,
BLUE_GREEN_STATE_ROOT: stateRoot,
BLUE_GREEN_STABILIZATION_SECONDS: '0',
DEPLOY_HEALTH_TIMEOUT_SECONDS: '2',
DEPLOY_HEALTH_POLL_SECONDS: '1',
MOCK_LOG: log,
MOCK_CANDIDATE_EXISTS: paths.candidate,
MOCK_LEGACY_STOPPED: paths.stopped,
MOCK_LEGACY_REMOVED: paths.removed,
MOCK_CANDIDATE_HEALTH: health
}
})
const output = `${result.stdout}\n${result.stderr}`
const dockerLog = await readFile(log, 'utf8')
const stateFile = join(stateRoot, 'firstleg-production-file.active')
const state = existsSync(stateFile) ? await readFile(stateFile, 'utf8') : ''
const evidence = {
...result, output, dockerLog, state,
stopped: existsSync(paths.stopped), removed: existsSync(paths.removed)
}
await rm(temp, { recursive: true, force: true })
return evidence
}
test('healthy candidate becomes active only after health and old instance drains', async (context) => {
if (!existsSync(shell)) context.skip(`shell unavailable: ${shell}`)
const result = await runBlueGreen('healthy')
assert.equal(result.status, 0, result.output)
assert.equal(result.stopped, true)
assert.equal(result.removed, true)
assert.match(result.state, /^green\|firstleg-production-file-green\|19206\|registry\.local\/group\/file:newtag/)
const run = result.dockerLog.indexOf('compose --project-name firstleg-production')
const stop = result.dockerLog.indexOf('stop --time 45 firstleg-production-file')
assert.ok(run >= 0 && run < stop)
})
test('unhealthy candidate is removed while old instance and state remain untouched', async (context) => {
if (!existsSync(shell)) context.skip(`shell unavailable: ${shell}`)
const result = await runBlueGreen('unhealthy')
assert.equal(result.status, 1, result.output)
assert.equal(result.stopped, false)
assert.equal(result.removed, false)
assert.equal(result.state, '')
assert.doesNotMatch(result.dockerLog, /stop --time .*firstleg-production-file/)
})
@@ -76,7 +76,7 @@ test('overseas compose is isolated from CN defaults and disables implicit schema
assert.match(compose, /^ lastleg-service:\s*$/m)
assert.match(compose, /^ warehouse-overseas-service:\s*$/m)
assert.match(compose, /^ event-bridge-service:\s*$/m)
assert.match(compose, /EVENT_BRIDGE_OVERSEAS_PORT:-9313}:9312/)
assert.match(compose, /EVENT_BRIDGE_OVERSEAS_PUBLISHED_PORT:-\$\{EVENT_BRIDGE_OVERSEAS_PORT:-9313\}}:9312/)
assert.match(compose, /INTERNAL_SERVICE_TOKEN: \$\{INTERNAL_SERVICE_TOKEN\}/)
assert.match(compose, /LASTLEG_DB_URL: \$\{LASTLEG_DB_URL\}/)
assert.match(compose, /EVENT_BRIDGE_DB_URL: \$\{EVENT_BRIDGE_OVERSEAS_DB_URL\}/)
@@ -180,10 +180,12 @@ test('parallel remote deployments keep env and compose assets isolated by projec
assert.match(directDeployScript, /REMOTE_COMPOSE_NAME="docker-compose\.\$PROJECT_NAME\.yml"/)
assert.match(directDeployScript, /REMOTE_ENV_TMP="\$REMOTE_DIR\/\$REMOTE_ENV_NAME\.\$SUFFIX\.tmp"/)
assert.match(directDeployScript, /REMOTE_COMPOSE_TMP="\$REMOTE_DIR\/\$REMOTE_COMPOSE_NAME\.\$SUFFIX\.tmp"/)
assert.match(directDeployScript, /env_target="\$3"/)
assert.match(directDeployScript, /compose_target="\$4"/)
assert.match(directDeployScript, /env_target="\$4"/)
assert.match(directDeployScript, /compose_target="\$5"/)
assert.match(directDeployScript, /blue_green_target="\$6"/)
assert.match(directDeployScript, /mv -f "\$env_tmp" "\$env_target"/)
assert.match(directDeployScript, /mv -f "\$compose_tmp" "\$compose_target"/)
assert.match(directDeployScript, /mv -f "\$blue_green_tmp" "\$blue_green_target"/)
assert.match(directDeployScript, /env_file="\$7"/)
assert.match(directDeployScript, /compose_file="\$8"/)
assert.match(directDeployScript, /--env-file "\$env_file"/)