CI / UI screenshots (before/after) (push) Skipped
CI / Lint (eslint) (push) Successful in 17s
CI / Test (jest) (push) Successful in 18s
CI / Security (npm audit) (push) Successful in 9s
CI / Scan (secrets, workflows, shell) (push) Successful in 2m2s
CI / Integration (api) (push) Successful in 2m31s
CI / Lint (eslint) (pull_request) Successful in 16s
CI / Test (jest) (pull_request) Successful in 15s
CI / Security (npm audit) (pull_request) Successful in 7s
CI / UI (playwright) (push) Successful in 1m39s
CI / Deploy (push) Skipped
CI / Deploy worker (push) Skipped
CI / Notify (push) Skipped
CI / Integration (api) (pull_request) Successful in 1m51s
CI / Scan (secrets, workflows, shell) (pull_request) Successful in 1m24s
CI / UI screenshots (before/after) (pull_request) Successful in 6s
CI / UI (playwright) (pull_request) Successful in 2m21s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped
- workflows.int.js (11): all-worker execute, failure stops the workflow, parse to route, false conditions skip, prompts (invalid option refused, routes on the answer), params (required, substituted, unsafe refused), dry run, endless goto detection, abort stays failed. - scheduler.int.js (5): a due schedule runs on its worker and advances next_run; disabled schedules don't run; admin-only and input checks. - Known bugs as test.failing (each verified to fail on its assertion): abort doesn't stop the engine (#8), goto to a missing step completes (#20), an offline worker's schedule leaves no trace (#9), and an invalid cron expression returns 500 instead of 400. - PULSE_SCHEDULER_TICK_MS (default 60 s, minimum 1 s) so the test stack ticks the scheduler every second. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
98 lines
4.5 KiB
JavaScript
98 lines
4.5 KiB
JavaScript
'use strict';
|
|
|
|
// The scheduler (processScheduledCommands in server.js). The test stack ticks every
|
|
// second (PULSE_SCHEDULER_TICK_MS); a schedule is made due at once by setting its
|
|
// next_run in the database, the one place these tests reach past the API.
|
|
const mysql = require('mysql2/promise');
|
|
const { api, sso, SSO, waitFor, waitForExecution, workerByName, commandResult, startWorker, sleep } = require('./helpers');
|
|
|
|
const unique = () => `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`;
|
|
let db;
|
|
|
|
beforeAll(async () => {
|
|
db = await mysql.createConnection({
|
|
host: process.env.DB_HOST || '127.0.0.1',
|
|
port: Number(process.env.DB_PORT || 3306),
|
|
user: process.env.DB_USER || 'pulse',
|
|
password: process.env.DB_PASSWORD || 'ci-only',
|
|
database: process.env.DB_NAME || 'pulse',
|
|
});
|
|
});
|
|
afterAll(() => db && db.end());
|
|
|
|
async function createSchedule(fields) {
|
|
const r = await sso('POST', '/api/scheduled-commands', {
|
|
name: `int ${unique()}`, schedule_type: 'interval', schedule_value: '60', ...fields,
|
|
});
|
|
return r;
|
|
}
|
|
const makeDue = id => db.query('UPDATE scheduled_commands SET next_run = NOW() - INTERVAL 1 SECOND WHERE id = ?', [id]);
|
|
async function executionsOf(name) {
|
|
const [rows] = await db.query('SELECT id, status FROM executions WHERE started_by = ? ORDER BY started_at', [`scheduler:${name}`]);
|
|
return rows;
|
|
}
|
|
|
|
test('a due schedule runs its command on its worker and moves next_run forward', async () => {
|
|
const worker = await workerByName('node-02');
|
|
const name = `int ${unique()}`;
|
|
const created = await createSchedule({ name, command: 'echo scheduled-ok', worker_ids: [worker.id] });
|
|
expect(created.status).toBe(200);
|
|
await makeDue(created.json.id);
|
|
|
|
const [run] = await waitFor(async () => { const r = await executionsOf(name); return r.length ? r : null; },
|
|
{ what: 'the scheduled execution' });
|
|
const ex = await waitForExecution(run.id);
|
|
expect(ex.status).toBe('completed');
|
|
expect(commandResult(ex).stdout.trim()).toBe('scheduled-ok');
|
|
|
|
const [[row]] = await db.query('SELECT next_run, last_run FROM scheduled_commands WHERE id = ?', [created.json.id]);
|
|
expect(row.last_run).not.toBeNull();
|
|
expect(new Date(row.next_run).getTime()).toBeGreaterThan(Date.now() + 50 * 60_000); // about 60 min ahead
|
|
});
|
|
|
|
test('a disabled schedule does not run', async () => {
|
|
const worker = await workerByName('node-02');
|
|
const name = `int ${unique()}`;
|
|
const created = await createSchedule({ name, command: 'echo nope', worker_ids: [worker.id] });
|
|
const toggled = await sso('PUT', `/api/scheduled-commands/${created.json.id}/toggle`);
|
|
expect(toggled.json.enabled).toBe(false);
|
|
await makeDue(created.json.id);
|
|
await sleep(3500); // several scheduler ticks
|
|
expect(await executionsOf(name)).toEqual([]);
|
|
});
|
|
|
|
test('only admins can create schedules, and bad input is refused', async () => {
|
|
const worker = await workerByName('node-01');
|
|
const asEmployee = await api('POST', '/api/scheduled-commands', {
|
|
headers: { ...SSO, 'Remote-Groups': 'employee' },
|
|
body: { name: 'x', command: 'true', worker_ids: [worker.id], schedule_type: 'interval', schedule_value: '5' },
|
|
});
|
|
expect(asEmployee.status).toBe(403);
|
|
expect((await createSchedule({ command: 'true' })).status).toBe(400); // no workers
|
|
expect((await createSchedule({ command: 'true', worker_ids: [worker.id], schedule_value: '-5' })).status).toBe(400);
|
|
});
|
|
|
|
// Known bug: calculateNextRun throws on a bad cron expression, and the route answers 500.
|
|
test.failing('an invalid cron expression is a 400, not a server error', async () => {
|
|
const worker = await workerByName('node-01');
|
|
const r = await createSchedule({ command: 'true', worker_ids: [worker.id], schedule_type: 'cron', schedule_value: 'not a cron' });
|
|
expect(r.status).toBe(400);
|
|
});
|
|
|
|
// Known bug #9: a schedule whose worker is offline is skipped silently, leaving no trace.
|
|
test.failing('a schedule whose worker is offline records a failed run (#9)', async () => {
|
|
const workerName = `int-sched-${unique()}`;
|
|
const w = await startWorker(workerName);
|
|
const name = `int ${unique()}`;
|
|
try {
|
|
const created = await createSchedule({ name, command: 'echo unreachable', worker_ids: [w.id] });
|
|
w.child.kill('SIGKILL');
|
|
await waitFor(async () => (await workerByName(workerName))?.status === 'offline', { what: 'the worker to go offline' });
|
|
await makeDue(created.json.id);
|
|
await sleep(3500);
|
|
const runs = await executionsOf(name);
|
|
expect(runs).toHaveLength(1);
|
|
expect(runs[0].status).toBe('failed');
|
|
} finally { await w.stop(); }
|
|
});
|