Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1474b0aa3f | ||
|
|
67a9e1c009 | ||
|
|
394665e88a | ||
|
|
38a6d1158d | ||
|
|
15f8ec4c9b | ||
|
|
1f75a4a63a |
No files matched your search
+40
-5
@@ -1,6 +1,6 @@
|
||||
name: CI
|
||||
|
||||
# One pipeline: lint, unit tests, audit, integration and UI tests must all pass before main deploys (#45).
|
||||
# One pipeline: lint, unit tests, audit, scans, integration and UI tests must all pass before main deploys (#45).
|
||||
# Actions are pinned to full commit SHAs (version in the trailing comment).
|
||||
|
||||
on:
|
||||
@@ -62,6 +62,15 @@ jobs:
|
||||
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
|
||||
- name: Audit production and dev dependencies
|
||||
run: npm audit --audit-level=high
|
||||
- name: Software bill of materials (production dependencies)
|
||||
run: npm sbom --sbom-format cyclonedx --omit dev --package-lock-only > sbom.cdx.json
|
||||
- name: Keep the SBOM for each main build
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||
uses: actions/upload-artifact@c6a3b2bd78b3985e4b2f15397fec357f0fd808de # v3.2.2-node20 (v4 is unsupported on Gitea)
|
||||
with:
|
||||
name: sbom-cyclonedx
|
||||
path: sbom.cdx.json
|
||||
retention-days: 90
|
||||
|
||||
integration:
|
||||
name: Integration (api)
|
||||
@@ -87,6 +96,32 @@ jobs:
|
||||
if: always()
|
||||
run: tail -n 200 .ui-stack/*.log || true
|
||||
|
||||
scan:
|
||||
name: Scan (secrets, workflows, shell)
|
||||
runs-on: ubuntu-latest
|
||||
container: node:20-bookworm
|
||||
steps:
|
||||
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
|
||||
with:
|
||||
fetch-depth: 0 # gitleaks scans the whole history, not just this commit
|
||||
- name: Install scanners (pinned versions, verified checksums)
|
||||
run: |
|
||||
cd /tmp
|
||||
curl -fsSLo gitleaks.tgz https://github.com/gitleaks/gitleaks/releases/download/v8.30.1/gitleaks_8.30.1_linux_x64.tar.gz
|
||||
echo "551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb gitleaks.tgz" | sha256sum -c
|
||||
curl -fsSLo actionlint.tgz https://github.com/rhysd/actionlint/releases/download/v1.7.12/actionlint_1.7.12_linux_amd64.tar.gz
|
||||
echo "8aca8db96f1b94770f1b0d72b6dddcb1ebb8123cb3712530b08cc387b349a3d8 actionlint.tgz" | sha256sum -c
|
||||
tar -xzf gitleaks.tgz -C /usr/local/bin gitleaks
|
||||
tar -xzf actionlint.tgz -C /usr/local/bin actionlint
|
||||
apt-get update -qq
|
||||
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends shellcheck >/dev/null
|
||||
- name: Secrets in code and history (gitleaks)
|
||||
run: gitleaks git --no-banner --redact --exit-code 1 .
|
||||
- name: Workflow files (actionlint, with shellcheck on run scripts)
|
||||
run: actionlint .gitea/workflows/*.yml
|
||||
- name: Shell scripts (shellcheck)
|
||||
run: git ls-files -z '*.sh' | xargs -0 shellcheck
|
||||
|
||||
ui:
|
||||
name: UI (playwright)
|
||||
runs-on: ubuntu-latest
|
||||
@@ -125,7 +160,7 @@ jobs:
|
||||
deploy:
|
||||
name: Deploy
|
||||
runs-on: ubuntu-latest
|
||||
needs: [lint, test, audit, integration, ui]
|
||||
needs: [lint, test, audit, scan, integration, ui]
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||
# Never run two deploys at once; queue instead of cancelling a deploy mid-way.
|
||||
concurrency:
|
||||
@@ -159,7 +194,7 @@ jobs:
|
||||
# this exact sha, runs npm ci and restarts, and rolls back if /healthz
|
||||
# never reports it. Succeed only when production reports this sha with
|
||||
# a working database (up to 4 minutes, covering npm ci).
|
||||
for i in $(seq 1 48); do
|
||||
for _ in $(seq 1 48); do
|
||||
body=$(curl -fsS --connect-timeout 5 --max-time 5 http://10.10.10.65:8080/healthz || true)
|
||||
if echo "$body" | grep -q "\"sha\":\"${GIT_SHA}\"" && echo "$body" | grep -q '"db":true'; then
|
||||
echo "Production is healthy on ${GIT_SHA}: ${body}"
|
||||
@@ -184,7 +219,7 @@ jobs:
|
||||
notify:
|
||||
name: Notify
|
||||
runs-on: ubuntu-latest
|
||||
needs: [lint, test, audit, integration, ui, deploy]
|
||||
needs: [lint, test, audit, scan, integration, ui, deploy]
|
||||
# Alert on any failure on main or in the weekly audit, and on every deploy outcome.
|
||||
if: >-
|
||||
always() && (
|
||||
@@ -198,7 +233,7 @@ jobs:
|
||||
REPO: ${{ github.repository }}
|
||||
BRANCH: ${{ github.ref_name }}
|
||||
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
RESULTS: "lint=${{ needs.lint.result }} test=${{ needs.test.result }} audit=${{ needs.audit.result }} integration=${{ needs.integration.result }} ui=${{ needs.ui.result }} deploy=${{ needs.deploy.result }}"
|
||||
RESULTS: "lint=${{ needs.lint.result }} test=${{ needs.test.result }} audit=${{ needs.audit.result }} scan=${{ needs.scan.result }} integration=${{ needs.integration.result }} ui=${{ needs.ui.result }} deploy=${{ needs.deploy.result }}"
|
||||
DEPLOYED: ${{ needs.deploy.result == 'success' }}
|
||||
run: |
|
||||
if [ -z "$MATRIX_WEBHOOK_URL" ] || [ "$MATRIX_WEBHOOK_URL" = "CONFIGURE_ME" ]; then exit 0; fi
|
||||
|
||||
@@ -38,3 +38,4 @@ __pycache__/
|
||||
.ui-stack/
|
||||
test-results/
|
||||
playwright-report/
|
||||
sbom.cdx.json
|
||||
@@ -587,15 +587,17 @@ Everything runs from `.gitea/workflows/ci.yml`:
|
||||
| Job | What it does | When |
|
||||
|---|---|---|
|
||||
| `Lint (eslint)` | `npm run lint`, plus a check that fails on em or en dashes in Pulse-owned files | Every push and PR |
|
||||
| `Test (jest)` | `npm test`: unit tests for `lib/` and the deploy-script sandbox tests | Every push and PR |
|
||||
| `Security (npm audit)` | `npm audit --audit-level=high` | Every push and PR, and weekly (Monday 06:00) |
|
||||
| `Test (jest)` | `npm test`: unit tests for `lib/` and the worker, and the deploy-script sandbox tests. Fails if coverage drops below the floors in `package.json` (`lib/` 90% lines, `worker/` 40%) | Every push and PR |
|
||||
| `Security (npm audit)` | `npm audit --audit-level=high`, plus a CycloneDX SBOM of production dependencies (kept 90 days as the `sbom-cyclonedx` artifact of each `main` build) | Every push and PR, and weekly (Monday 06:00) |
|
||||
| `Scan (secrets, workflows, shell)` | gitleaks over the whole git history, actionlint on the workflow files (with shellcheck on their scripts), and shellcheck on every `*.sh` | Every push and PR |
|
||||
| `Integration (api)` | Starts the real server and three workers on Node 20 against MariaDB, then runs `tests/integration/` over HTTP and WebSocket: the Gandalf API contract, HTTP and WebSocket auth, and execution reliability (worker crash, capacity, abort) | Every push and PR |
|
||||
| `UI (playwright)` | Starts the real server and three workers against a MariaDB service container, then runs the Playwright tests in `tests/ui/` (desktop and phone) | Every push and PR |
|
||||
| `Deploy` | Calls the `pulse-deploy` webhook on CT 122, waits until `/healthz` reports the tested commit, then tags it `deploy-*` | Push to `main`, only after all five checks pass |
|
||||
| `Deploy` | Calls the `pulse-deploy` webhook on CT 122, waits until `/healthz` reports the tested commit, then tags it `deploy-*` | Push to `main`, only after all six checks pass |
|
||||
| `Notify` | Matrix alert on any failure on `main` or in the weekly audit, and on every deploy | As needed |
|
||||
|
||||
Branch protection on `main` requires `CI / Lint (eslint) (push)`, `CI / Test (jest) (push)`,
|
||||
`CI / Security (npm audit) (push)`, `CI / Integration (api) (push)` and `CI / UI (playwright) (push)`. Actions are pinned to commit
|
||||
`CI / Security (npm audit) (push)`, `CI / Scan (secrets, workflows, shell) (push)`,
|
||||
`CI / Integration (api) (push)` and `CI / UI (playwright) (push)`. Actions are pinned to commit
|
||||
SHAs and builds use `npm ci`. The plan for the rest of the pipeline is tracked in #45.
|
||||
|
||||
### Integration tests
|
||||
|
||||
@@ -31,5 +31,26 @@
|
||||
"@playwright/test": "1.64.1",
|
||||
"eslint": "^8.57.1",
|
||||
"jest": "^30.5.2"
|
||||
},
|
||||
"jest": {
|
||||
"testPathIgnorePatterns": [
|
||||
"/node_modules/",
|
||||
"/tests/ui/",
|
||||
"/tests/integration/"
|
||||
],
|
||||
"coverageThreshold": {
|
||||
"./lib/": {
|
||||
"statements": 90,
|
||||
"branches": 80,
|
||||
"functions": 80,
|
||||
"lines": 90
|
||||
},
|
||||
"./worker/": {
|
||||
"statements": 40,
|
||||
"branches": 70,
|
||||
"functions": 25,
|
||||
"lines": 40
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,10 +74,18 @@ async function startWorker(name, env = {}) {
|
||||
let log = '';
|
||||
child.stdout.on('data', d => { log += d; });
|
||||
child.stderr.on('data', d => { log += d; });
|
||||
const fail = e => { child.kill('SIGKILL'); throw new Error(`${e.message}\n${log}`); };
|
||||
// The first heartbeat marks the worker 'online' before its WebSocket is connected,
|
||||
// so wait until the server actually accepts a command for it (a harmless `true`).
|
||||
const worker = await waitFor(async () => {
|
||||
const w = await workerByName(name);
|
||||
return w && w.status === 'online' ? w : null;
|
||||
}, { what: `worker ${name} to come online` }).catch(e => { child.kill('SIGKILL'); throw new Error(`${e.message}\n${log}`); });
|
||||
}, { what: `worker ${name} to register` }).catch(fail);
|
||||
const probe = await waitFor(async () => {
|
||||
const r = await gandalf('POST', '/api/internal/command', { worker_id: worker.id, command: 'true' });
|
||||
return r.status === 200 ? r.json.execution_id : null;
|
||||
}, { what: `worker ${name} to connect its WebSocket` }).catch(fail);
|
||||
await waitForExecution(probe).catch(fail);
|
||||
return {
|
||||
id: worker.id,
|
||||
child,
|
||||
@@ -88,7 +96,10 @@ async function startWorker(name, env = {}) {
|
||||
await new Promise(r => child.once('exit', r));
|
||||
}
|
||||
// Remove it so other suites (and the UI tests' worker counts) don't see it.
|
||||
await waitFor(async () => (await workerByName(name))?.status === 'offline', { timeout: 5_000, what: `${name} to show offline` });
|
||||
// Never throws: cleanup must not hide the test's own failure. The offline
|
||||
// status itself is asserted by its own test in reliability.int.js.
|
||||
await waitFor(async () => (await workerByName(name))?.status === 'offline', { timeout: 5_000, what: `${name} offline` })
|
||||
.catch(e => console.warn(`cleanup: ${e.message}`));
|
||||
await sso('DELETE', `/api/workers/${worker.id}`);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
'use strict';
|
||||
|
||||
// Unit tests for the worker's command execution (worker/worker.js), with no
|
||||
// server: results are captured from sendResult instead of a WebSocket.
|
||||
const { PulseWorker } = require('../worker/worker');
|
||||
|
||||
function makeWorker(opts = {}) {
|
||||
const w = new PulseWorker();
|
||||
w.maxConcurrentTasks = opts.max || 5;
|
||||
w.results = [];
|
||||
w.sendResult = r => w.results.push(r);
|
||||
return w;
|
||||
}
|
||||
const run = (w, command, extra = {}) =>
|
||||
w.executeCommand({ command, execution_id: 'ex-1', command_id: 'cmd-1', ...extra });
|
||||
|
||||
beforeAll(() => {
|
||||
jest.spyOn(console, 'log').mockImplementation(() => {});
|
||||
jest.spyOn(console, 'error').mockImplementation(() => {});
|
||||
});
|
||||
afterAll(() => jest.restoreAllMocks());
|
||||
|
||||
test('a successful command reports stdout, stderr and duration', async () => {
|
||||
const w = makeWorker();
|
||||
await run(w, 'echo out; echo err >&2');
|
||||
expect(w.results).toHaveLength(1);
|
||||
const r = w.results[0];
|
||||
expect(r).toMatchObject({
|
||||
type: 'command_result', execution_id: 'ex-1', command_id: 'cmd-1',
|
||||
worker_id: w.workerId, success: true, stdout: 'out\n', stderr: 'err\n',
|
||||
});
|
||||
expect(typeof r.duration).toBe('number');
|
||||
expect(w.activeTasks).toBe(0);
|
||||
});
|
||||
|
||||
test('a failing command reports its exit code and output (#14)', async () => {
|
||||
const w = makeWorker();
|
||||
await run(w, 'echo partial; echo broke >&2; exit 7');
|
||||
const r = w.results[0];
|
||||
expect(r).toMatchObject({ success: false, exit_code: 7, stdout: 'partial\n', stderr: 'broke\n' });
|
||||
expect(r.timed_out).toBeUndefined();
|
||||
expect(r.error).toMatch(/Command failed/);
|
||||
expect(w.activeTasks).toBe(0);
|
||||
});
|
||||
|
||||
test('a command that runs past its timeout is killed and reported as timed out', async () => {
|
||||
const w = makeWorker();
|
||||
const started = Date.now();
|
||||
await run(w, 'sleep 5', { timeout: 300 });
|
||||
expect(Date.now() - started).toBeLessThan(3000);
|
||||
expect(w.results[0]).toMatchObject({ success: false, timed_out: true, signal: 'SIGTERM' });
|
||||
expect(w.activeTasks).toBe(0);
|
||||
});
|
||||
|
||||
test('at capacity, extra commands get an immediate failure instead of silence (#7)', async () => {
|
||||
const w = makeWorker({ max: 1 });
|
||||
const first = run(w, 'sleep 0.5; echo first', { execution_id: 'ex-a', command_id: 'a' });
|
||||
await run(w, 'echo second', { execution_id: 'ex-b', command_id: 'b' });
|
||||
|
||||
const rejected = w.results.find(r => r.command_id === 'b');
|
||||
expect(rejected).toMatchObject({ success: false, execution_id: 'ex-b' });
|
||||
expect(rejected.error).toMatch(/capacity/);
|
||||
|
||||
await first;
|
||||
expect(w.results.find(r => r.command_id === 'a')).toMatchObject({ success: true, stdout: 'first\n' });
|
||||
expect(w.activeTasks).toBe(0);
|
||||
});
|
||||
|
||||
test('a slot frees up again after a command fails', async () => {
|
||||
const w = makeWorker({ max: 1 });
|
||||
await run(w, 'exit 1');
|
||||
await run(w, 'echo ok', { command_id: 'next' });
|
||||
expect(w.results.find(r => r.command_id === 'next')).toMatchObject({ success: true });
|
||||
});
|
||||
|
||||
test('ping is answered with pong over the socket', async () => {
|
||||
const w = makeWorker();
|
||||
const sent = [];
|
||||
w.ws = { readyState: 1, send: m => sent.push(JSON.parse(m)) }; // 1 = WebSocket.OPEN
|
||||
await w.handleMessage({ type: 'ping' });
|
||||
expect(sent).toEqual([{ type: 'pong', worker_id: w.workerId }]);
|
||||
});
|
||||
|
||||
test('results are not sent while disconnected', () => {
|
||||
const w = new PulseWorker();
|
||||
const send = jest.fn();
|
||||
w.ws = { readyState: 3, send }; // 3 = CLOSED
|
||||
w.sendResult({ type: 'command_result' });
|
||||
expect(send).not.toHaveBeenCalled();
|
||||
});
|
||||
+19
-16
@@ -256,22 +256,25 @@ class PulseWorker {
|
||||
}
|
||||
}
|
||||
|
||||
// Start worker
|
||||
const worker = new PulseWorker();
|
||||
module.exports = { PulseWorker };
|
||||
|
||||
// Handle graceful shutdown
|
||||
process.on('SIGTERM', async () => {
|
||||
await worker.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
// Start only when run directly (node worker.js), so tests can load the class.
|
||||
if (require.main === module) {
|
||||
const worker = new PulseWorker();
|
||||
|
||||
process.on('SIGINT', async () => {
|
||||
await worker.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
// Handle graceful shutdown
|
||||
process.on('SIGTERM', async () => {
|
||||
await worker.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
// Start the worker
|
||||
worker.start().catch((error) => {
|
||||
console.error('[PULSE Worker] Fatal error:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
process.on('SIGINT', async () => {
|
||||
await worker.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
worker.start().catch((error) => {
|
||||
console.error('[PULSE Worker] Fatal error:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user