Files
baiyue-firstleg/scripts/blue-green-deployment-contract.test.mjs
T

101 lines
5.5 KiB
JavaScript
Raw Normal View History

2026-09-20 21:16:15 +08:00
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_container "\$candidate"/)
assert.match(script, /BAIYUE_RUNTIME_SCHEDULING_ENABLED=false/)
assert.match(script, /NACOS_REGISTER_ENABLED=false/)
assert.match(script, /start_worker_candidate/)
2026-09-20 22:00:10 +08:00
assert.match(script, /worker_java_options="-Xms512m -Xmx2g/)
assert.match(script, /worker_java_options="-Xms256m -Xmx1g/)
assert.match(script, /connect_routing_aliases "\$candidate" "\$ROUTING_ALIAS" "\$NACOS_SERVICE"/)
assert.match(script, /file,file-service:file/)
2026-09-20 21:16:15 +08:00
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.lastIndexOf('compose run -d')
const healthy = script.indexOf('if ! wait_for_container "$candidate"')
const route = script.indexOf('connect_routing_aliases "$candidate"')
2026-09-20 21:16:15 +08:00
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, /尚未完成首次蓝绿迁移,拒绝切换 Gateway/)
assert.match(script, /frontend-\$frontend_service\.active/)
assert.match(script, /尚未使用稳定上游 \$required_alias,拒绝切换 Gateway/)
assert.match(script, /reload_frontend_proxies/)
2026-09-20 21:16:15 +08:00
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('every scheduled worker service can disable Nacos registration', async () => {
for (const application of [
'iam/src/main/resources/application.yml',
'firstleg/src/main/resources/application.yml',
'finance/src/main/resources/application.yml',
'order/src/main/resources/application.yml',
'file/src/main/resources/application.yml',
'event-bridge-service/src/main/resources/application.yml',
'audit-service/src/main/resources/application.yml',
'lastleg-service/src/main/resources/application.yml'
]) {
assert.match(await read(application), /register-enabled: \$\{NACOS_REGISTER_ENABLED:true\}/, application)
}
})
2026-09-20 21:16:15 +08:00
test('production single-service pipeline dispatches the blue-green executor', async () => {
const deploy = await read('deploy/scripts/deploy-built-service.sh')
const batch = await read('deploy/scripts/deploy-services.sh')
2026-09-20 21:16:15 +08:00
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"/)
assert.match(deploy, /TARGET_IS_LOCAL[\s\S]*?deploy-blue-green-service\.sh/)
2026-09-20 22:21:02 +08:00
assert.match(batch, /if \[ "\$DEPLOY_ENV" = "test" \] \|\| \[ "\$DEPLOY_ENV" = "production" \][\s\S]*?deploy-blue-green-service\.sh/)
2026-09-20 21:16:15 +08:00
})
2026-09-20 22:18:59 +08:00
test('test business services use blue-green while test gateways retain their fixed entry ports', async () => {
const deploy = await read('deploy/scripts/deploy-built-service.sh')
const batch = await read('deploy/scripts/deploy-services.sh')
assert.match(deploy, /\[ "\$DEPLOY_ENV" = "test" \][\s\S]*?\[ "\$SERVICE" != "gateway" \]/)
assert.match(batch, /\[ "\$DEPLOY_ENV" = "test" \] \|\| \[ "\$DEPLOY_ENV" = "production" \]/)
assert.match(batch, /\[ "\$service" = gateway \] \|\| \[ "\$service" = gateway-overseas \]/)
})