The run page mixed English stage events and agent summaries into the Chinese console. Messages are now written in Simplified Chinese at their source (control plane, dispatcher, worker, POC build), keeping commands, SHAs, branches and error codes verbatim; raw runtime errors get a Chinese prefix. The agent prompt asks for the plan, summary and questions in Chinese. File-change parsing accepts old and new messages, and the event label table covers every event kind written. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
278 lines
11 KiB
TypeScript
278 lines
11 KiB
TypeScript
import { it, expect } from 'vitest';
|
||
import { createHash } from 'node:crypto';
|
||
import { mkdtemp, rm } from 'node:fs/promises';
|
||
import os from 'node:os';
|
||
import path from 'node:path';
|
||
import {
|
||
ComposeDeploymentAdapter,
|
||
KubernetesDeploymentAdapter,
|
||
type Command,
|
||
type ReleaseManifest,
|
||
} from '../src/index.js';
|
||
|
||
const release: ReleaseManifest = {
|
||
schemaVersion: 1,
|
||
id: 'new-release',
|
||
tenantId: 'tenant',
|
||
environmentId: 'env',
|
||
sourceCommit: 'a'.repeat(40),
|
||
image: `registry/web@sha256:${'b'.repeat(64)}`,
|
||
containerPort: 3000,
|
||
healthPath: '/health',
|
||
smokeChecks: [{ path: '/', expectedStatus: 200, bodyIncludes: 'ready' }],
|
||
expectedPreviousReleaseId: 'old-release',
|
||
};
|
||
it('validates a separate Kubernetes candidate before an atomic stable-service switch, preserving the old version', async () => {
|
||
const commands: Command[] = [];
|
||
let healthChecks = 0;
|
||
let switched = false;
|
||
const rev = createHash('sha256').update(release.id).digest('hex').slice(0, 12);
|
||
const deployment = {
|
||
metadata: {
|
||
generation: 1,
|
||
annotations: JSON.parse(new KubernetesDeploymentAdapter().render(release)).items[0].metadata.annotations,
|
||
},
|
||
spec: { template: { spec: { containers: [{ image: release.image }] } } },
|
||
status: { observedGeneration: 1, updatedReplicas: 1, availableReplicas: 1, replicas: 1 },
|
||
};
|
||
const execute = async (command: Command) => {
|
||
commands.push(command);
|
||
if (command.args.includes('patch')) {
|
||
expect(healthChecks).toBe(2);
|
||
const patch = JSON.parse(command.args.at(-1)!);
|
||
expect(patch[0]).toEqual({ op: 'test', path: '/metadata/resourceVersion', value: '42' });
|
||
switched = true;
|
||
}
|
||
const payload = command.args.includes('get')
|
||
? command.args.includes('service')
|
||
? {
|
||
metadata: {
|
||
resourceVersion: '42',
|
||
annotations: { 'wishforge.io/release': switched ? release.id : 'old-release' },
|
||
},
|
||
spec: {
|
||
selector: {
|
||
'app.kubernetes.io/name': JSON.parse(new KubernetesDeploymentAdapter().render(release)).items[1].spec
|
||
.selector['app.kubernetes.io/name'],
|
||
'wishforge.io/revision': switched ? rev : 'old',
|
||
},
|
||
ports: [],
|
||
},
|
||
}
|
||
: deployment
|
||
: {};
|
||
return { exitCode: 0, stdout: JSON.stringify(payload), stderr: '' };
|
||
};
|
||
const adapter = new KubernetesDeploymentAdapter(
|
||
{
|
||
context: 'test',
|
||
namespace: 'test',
|
||
healthUrl: 'https://stable.test',
|
||
candidateHealthUrlTemplate: 'http://{service}.{namespace}.svc',
|
||
},
|
||
execute,
|
||
async () => {
|
||
healthChecks++;
|
||
return new Response('ready');
|
||
},
|
||
);
|
||
const result = await adapter.deploy(release, { execute: true });
|
||
expect(result.status).toBe('verified');
|
||
expect(result.evidence.trafficSwitched).toBe(true);
|
||
expect(commands.some(command => command.args.includes('delete'))).toBe(false);
|
||
const rendered = JSON.parse(commands.find(command => command.args.includes('apply'))!.input!);
|
||
expect(rendered.items[0].metadata.name).toContain(rev);
|
||
});
|
||
|
||
/** A Kubernetes target whose candidate is ready and whose stable service still selects the previous release. */
|
||
function kubernetesTarget(fetcher: typeof fetch, notices: string[] = []) {
|
||
const commands: Command[] = [];
|
||
const adapter = new KubernetesDeploymentAdapter(
|
||
{
|
||
context: 'test',
|
||
namespace: 'test',
|
||
healthUrl: 'https://stable.test',
|
||
candidateHealthUrlTemplate: 'http://{service}.test',
|
||
},
|
||
async command => {
|
||
commands.push(command);
|
||
return {
|
||
exitCode: 0,
|
||
stdout: JSON.stringify(
|
||
command.args.includes('service')
|
||
? { metadata: { resourceVersion: '1', annotations: { 'wishforge.io/release': 'old-release' } } }
|
||
: {
|
||
metadata: {
|
||
generation: 1,
|
||
annotations: JSON.parse(new KubernetesDeploymentAdapter().render(release)).items[0].metadata
|
||
.annotations,
|
||
},
|
||
spec: { template: { spec: { containers: [{ image: release.image }] } } },
|
||
status: { observedGeneration: 1, updatedReplicas: 1, availableReplicas: 1, replicas: 1 },
|
||
},
|
||
),
|
||
stderr: '',
|
||
};
|
||
},
|
||
fetcher,
|
||
{ attempts: 3, delayMs: 1, onRetry: notice => notices.push(notice) },
|
||
);
|
||
return { adapter, commands };
|
||
}
|
||
|
||
it('does not switch traffic when candidate smoke checks fail', async () => {
|
||
let checks = 0;
|
||
const { adapter, commands } = kubernetesTarget(async () => {
|
||
checks++;
|
||
return new Response('failed', { status: 500 });
|
||
});
|
||
expect((await adapter.deploy(release, { execute: true })).status).toBe('applied');
|
||
expect(commands.some(command => command.args.includes('patch'))).toBe(false);
|
||
// The candidate was asked as often as the patience allows before it was given up.
|
||
expect(checks).toBe(3);
|
||
});
|
||
|
||
it('leaves traffic on the previous release when a candidate never answers', async () => {
|
||
const notices: string[] = [];
|
||
const { adapter, commands } = kubernetesTarget(async () => {
|
||
throw new TypeError('fetch failed');
|
||
}, notices);
|
||
const result = await adapter.deploy(release, { execute: true });
|
||
expect(result.status).toBe('applied');
|
||
expect(result.evidence).toMatchObject({
|
||
trafficSwitched: false,
|
||
reason: 'Candidate did not answer: fetch failed',
|
||
});
|
||
expect(commands.some(command => command.args.includes('patch'))).toBe(false);
|
||
expect(notices).toEqual([
|
||
'fetch failed;1 秒后重试(第 2 次,共 3 次)',
|
||
'fetch failed;1 秒后重试(第 3 次,共 3 次)',
|
||
]);
|
||
});
|
||
|
||
/**
|
||
* A Compose target behind Caddy: `answer` serves the candidate and, once the proxy selects it,
|
||
* the stable entry; it gets the URL and whether traffic was switched yet.
|
||
*/
|
||
async function composeTarget(answer: (url: string, switched: boolean) => Promise<Response>, notices: string[] = []) {
|
||
const directory = await mkdtemp(path.join(os.tmpdir(), 'wishforge-bluegreen-'));
|
||
let selected = '127.0.0.1:41000';
|
||
const commands: Command[] = [];
|
||
const patches: string[] = [];
|
||
const current = (isNew: boolean) => [
|
||
{
|
||
Config: {
|
||
Image: release.image,
|
||
Labels: {
|
||
'wishforge.io/release': isNew ? release.id : 'old-release',
|
||
'wishforge.io/tenant': 'tenant',
|
||
'wishforge.io/environment': 'env',
|
||
...(isNew ? JSON.parse(new ComposeDeploymentAdapter().render(release)).services.web.labels : {}),
|
||
},
|
||
},
|
||
State: { Running: true },
|
||
NetworkSettings: { Ports: { '3000/tcp': [{ HostPort: isNew ? '41001' : '41000' }] } },
|
||
},
|
||
];
|
||
const execute = async (command: Command) => {
|
||
commands.push(command);
|
||
if (command.args.includes('ps'))
|
||
return {
|
||
exitCode: 0,
|
||
stdout: command.args.join(' ').includes(createHash('sha256').update(release.id).digest('hex').slice(0, 12))
|
||
? 'bbbb'
|
||
: 'aaaa',
|
||
stderr: '',
|
||
};
|
||
if (command.args.includes('inspect'))
|
||
return { exitCode: 0, stdout: JSON.stringify(current(command.args.includes('bbbb'))), stderr: '' };
|
||
return { exitCode: 0, stdout: '', stderr: '' };
|
||
};
|
||
const fetcher: typeof fetch = async (url, options) => {
|
||
if (String(url).includes('/config/')) {
|
||
if (options?.method === 'PATCH') {
|
||
expect(new Headers(options.headers).get('if-match')).toBe('"etag-1"');
|
||
const body = JSON.parse(String(options.body));
|
||
selected = body.upstreams[0].dial;
|
||
patches.push(selected);
|
||
return new Response('', { status: 200 });
|
||
}
|
||
return new Response(JSON.stringify({ handler: 'reverse_proxy', upstreams: [{ dial: selected }] }), {
|
||
headers: { etag: '"etag-1"' },
|
||
});
|
||
}
|
||
return answer(String(url), selected === '127.0.0.1:41001');
|
||
};
|
||
const adapter = new ComposeDeploymentAdapter(
|
||
{
|
||
directory,
|
||
project: 'project',
|
||
healthUrl: 'http://stable.test',
|
||
candidateHealthUrlTemplate: 'http://127.0.0.1:{port}',
|
||
proxyAdminUrl: 'http://127.0.0.1:2019',
|
||
proxyConfigPath: '/config/apps/http/servers/app/routes/0/handle/0',
|
||
proxyDialHost: '127.0.0.1',
|
||
},
|
||
execute,
|
||
fetcher,
|
||
{ attempts: 4, delayMs: 1, onRetry: notice => notices.push(notice) },
|
||
);
|
||
return { adapter, commands, patches, cleanup: () => rm(directory, { recursive: true, force: true }) };
|
||
}
|
||
|
||
it('uses a separate Compose project and ETag-guarded Caddy switch after candidate checks', async () => {
|
||
const seen: string[] = [];
|
||
const target = await composeTarget(async (url, switched) => {
|
||
seen.push(`${switched ? 'switched' : 'candidate'} ${url}`);
|
||
return new Response('ready');
|
||
});
|
||
try {
|
||
expect((await target.adapter.deploy(release, { execute: true })).status).toBe('verified');
|
||
// Health and the smoke check pass on the candidate before the proxy is touched, then on the stable entry.
|
||
expect(seen).toEqual([
|
||
'candidate http://127.0.0.1:41001/health',
|
||
'candidate http://127.0.0.1:41001/',
|
||
'switched http://stable.test/health',
|
||
'switched http://stable.test/',
|
||
]);
|
||
expect(target.patches).toEqual(['127.0.0.1:41001']);
|
||
expect(target.commands.some(command => command.args.includes('down') || command.args.includes('rm'))).toBe(false);
|
||
expect(target.commands.find(command => command.args.includes('compose'))!.args).toContain(
|
||
`project-${createHash('sha256').update(release.id).digest('hex').slice(0, 12)}`,
|
||
);
|
||
} finally {
|
||
await target.cleanup();
|
||
}
|
||
});
|
||
|
||
it('waits for a candidate that is still starting, and for the entry to settle after the switch', async () => {
|
||
const notices: string[] = [];
|
||
let refused = 0;
|
||
let settling = 0;
|
||
const target = await composeTarget(async (url, switched) => {
|
||
// The container runs before its server listens.
|
||
if (url.startsWith('http://127.0.0.1:41001') && refused < 2) {
|
||
refused++;
|
||
throw new TypeError('fetch failed');
|
||
}
|
||
// Right after the switch the entry answers once from a connection that was still closing.
|
||
if (switched && url.startsWith('http://stable.test') && settling < 1) {
|
||
settling++;
|
||
return new Response('', { status: 502 });
|
||
}
|
||
return new Response('ready');
|
||
}, notices);
|
||
try {
|
||
expect((await target.adapter.deploy(release, { execute: true })).status).toBe('verified');
|
||
// Switched once, and not switched back.
|
||
expect(target.patches).toEqual(['127.0.0.1:41001']);
|
||
expect(notices).toEqual([
|
||
'fetch failed;1 秒后重试(第 2 次,共 4 次)',
|
||
'fetch failed;1 秒后重试(第 3 次,共 4 次)',
|
||
'Health returned 502;1 秒后重试(第 2 次,共 4 次)',
|
||
]);
|
||
} finally {
|
||
await target.cleanup();
|
||
}
|
||
});
|