wishforge-ide/apps/provisioner/test/workspaces.test.mjs
eust-w e89f11b396 feat: run the cloud IDE on the local k3s cluster
The local cluster deploys the IDE gateway, and the provisioner creates a
Theia workspace with an OCT sidecar per project in the tenant namespace.
`build` adds the ide, ide-gateway and oct-workspace images; `web`
forwards the workbench (3100) and the gateway (3101).

- On a local development cluster (WF_ALLOW_INSECURE_HTTP), the
  provisioner accepts the in-cluster HTTP platform Git and the localhost
  gateway for workspaces.
- Builds use a proxy from the host environment, at the address the VM
  sees the host at.
- `web` refuses ports already taken on the host, where kubectl would
  otherwise listen on ::1 only. Its defaults stay clear of the local
  development stack (3000, 4400).
- Pods reading the settings or secrets carry a digest of them, so a
  redeploy with changed settings rolls them.
- The IDE build context leaves out every local node_modules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 23:24:04 +08:00

103 lines
5.1 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { renderWorkspaceResources, provisionWorkspace } from '../workspaces.mjs';
const config = {
tenantId: 'alpha',
namespace: 'wf-alpha',
controlNamespace: 'wf-control',
gitServicePort: 4504,
runtimeClass: 'wishforge-kata',
storageClass: 'test-storage',
storageSize: '10Gi',
};
const input = { tenantId: 'alpha', projectId: 'project-1', workspaceId: 'workspace-1' };
const image = `registry.invalid/theia@sha256:${'a'.repeat(64)}`;
test('workspace provisioning uses the actual Theia start contract and a private isolated endpoint', () => {
const rendered = renderWorkspaceResources(config, input, image);
const pod = rendered.resources.find(r => r.kind === 'Deployment').spec.template.spec;
assert.equal(pod.runtimeClassName, 'wishforge-kata');
assert.equal(pod.automountServiceAccountToken, false);
const env = Object.fromEntries(pod.containers[0].env.map(e => [e.name, e.value]));
assert.equal(env.WISHFORGE_IDE_PORT, '3000');
assert.equal(env.WISHFORGE_GATEWAY_ENFORCED, 'true');
assert.equal(env.WISHFORGE_WORKSPACE_PATH, '/home/theia/data/workspace');
assert.equal(env.WISHFORGE_COLLABORATION_ENABLED, 'false');
assert.equal(rendered.resources.find(r => r.kind === 'Service').spec.type, 'ClusterIP');
});
test('creating manifests does not mark an unready IDE available', async () => {
let applied = false;
const result = await provisionWorkspace(config, input, {
image,
apply: async () => {
applied = true;
},
observe: async () => false,
});
assert.ok(applied);
assert.equal(result.status, 'pending');
assert.throws(() => renderWorkspaceResources(config, { ...input, tenantId: 'beta' }, image));
});
test('a workspace of a platform repository may reach the Git service and keeps its token in memory', () => {
const repositoryUrl = 'https://git.example/git/alpha/project-1.git';
const rendered = renderWorkspaceResources(config, { ...input, repositoryUrl }, image);
const pod = rendered.resources.find(r => r.kind === 'Deployment').spec.template.spec;
const env = Object.fromEntries(pod.containers[0].env.map(e => [e.name, e.value]));
assert.equal(env.WISHFORGE_GIT_REMOTE, repositoryUrl);
assert.equal(env.WISHFORGE_GIT_CREDENTIAL_DIR, '/run/wishforge/git');
assert.deepEqual(pod.volumes.find(v => v.name === 'git-credentials').emptyDir, {
medium: 'Memory',
sizeLimit: '1Mi',
});
const egress = rendered.resources.find(r => r.kind === 'NetworkPolicy').spec.egress;
assert.deepEqual(egress.at(-1), {
to: [
{
namespaceSelector: { matchLabels: { 'kubernetes.io/metadata.name': 'wf-control' } },
podSelector: { matchLabels: { 'wishforge.io/component': 'git-storage' } },
},
],
ports: [{ protocol: 'TCP', port: 4504 }],
});
// Without a platform repository nothing beyond DNS is reachable.
const plain = renderWorkspaceResources(config, input, image);
assert.equal(plain.resources.find(r => r.kind === 'NetworkPolicy').spec.egress.length, 1);
for (const other of [
'http://git.example/git/alpha/project-1.git',
'https://token@git.example/git/alpha/project-1.git',
'https://git.example/git/beta/project-1.git',
'https://git.example/git/alpha/project-2.git',
])
assert.throws(() => renderWorkspaceResources(config, { ...input, repositoryUrl: other }, image), /REPOSITORY/);
});
test('only a local development cluster serves the repository and the gateway over plain HTTP', () => {
const repositoryUrl = 'http://git-storage.wf-control.svc.cluster.local:4504/git/alpha/project-1.git';
const collaboration = { image, gatewayBase: 'http://localhost:4400/' };
const local = { ...config, runtimeClass: undefined, insecureHttp: true };
const rendered = renderWorkspaceResources(local, { ...input, repositoryUrl }, image, collaboration);
const env = Object.fromEntries(
rendered.resources
.find(r => r.kind === 'Deployment')
.spec.template.spec.containers[0].env.map(e => [e.name, e.value]),
);
assert.equal(env.WISHFORGE_GIT_REMOTE, repositoryUrl);
assert.equal(env.WF_COLLABORATION_GATEWAY_URL, 'http://localhost:4400/workspaces/workspace-1/collaboration/');
assert.throws(() => renderWorkspaceResources(config, { ...input, repositoryUrl }, image), /REPOSITORY/);
assert.throws(() => renderWorkspaceResources(config, input, image, collaboration), /GATEWAY/);
});
test('scoped collaboration runs as a sidecar without any platform credential in the workspace', () => {
const rendered = renderWorkspaceResources(config, input, image, {
image,
gatewayBase: 'https://ide.example/',
});
const pod = rendered.resources.find(r => r.kind === 'Deployment').spec.template.spec;
{
assert.equal(pod.containers.length, 2);
assert.ok(!JSON.stringify(pod).includes('PRIVATE_KEY'));
assert.ok(!JSON.stringify(pod).includes('TOKEN'));
const octEnv = Object.fromEntries(pod.containers[1].env.map(e => [e.name, e.value]));
assert.equal(octEnv.WF_WORKSPACE_ID, input.workspaceId);
assert.equal(octEnv.WISHFORGE_GATEWAY_ENFORCED, 'true');
assert.ok(rendered.collaborationUpstreamUrl.endsWith(':8100'));
}
});