Initial
This commit is contained in:
@@ -3,6 +3,7 @@ import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
|
||||
const { listJobs, getJob } = await load('../models/jobModel.js');
|
||||
const { cancelSystemJob, listSystemJobs, runSystemJob } = await load('../services/systemJobService.js');
|
||||
|
||||
const owner = makeUser({ email: 'job-owner@test.local' });
|
||||
const stranger = makeUser({ email: 'job-stranger@test.local' });
|
||||
@@ -23,6 +24,11 @@ db.prepare(`INSERT INTO jobs (id, runplan_id, script_id, status, triggered_by, c
|
||||
const scriptTestJobId = 'job_script_test_1';
|
||||
db.prepare(`INSERT INTO jobs (id, runplan_id, script_id, status, triggered_by, created_at)
|
||||
VALUES (?, NULL, ?, 'failed', ?, ?)`).run(scriptTestJobId, scriptId, owner, ts);
|
||||
const runningJobId = 'job_cancel_test_1';
|
||||
db.prepare(`INSERT INTO jobs (id, runplan_id, script_id, status, triggered_by, started_at, created_at)
|
||||
VALUES (?, ?, ?, 'running', ?, ?, ?)`).run(runningJobId, runplanId, scriptId, owner, ts, ts);
|
||||
db.prepare(`INSERT INTO job_hosts (id, job_id, host_id, status, started_at)
|
||||
VALUES ('jhost_cancel_test_1', ?, NULL, 'queued', NULL)`).run(runningJobId);
|
||||
|
||||
test('the triggering operator can list and read their job', () => {
|
||||
assert.ok(listJobs(owner, false).some((j) => j.id === jobId));
|
||||
@@ -45,3 +51,36 @@ test('admins can see any job', () => {
|
||||
assert.ok(listJobs(admin, true).some((j) => j.id === jobId));
|
||||
assert.ok(getJob(jobId, admin, true));
|
||||
});
|
||||
|
||||
test('system jobs list execution and background jobs', () => {
|
||||
const listJobId = 'job_list_system_test_1';
|
||||
db.prepare(`INSERT INTO jobs (id, runplan_id, script_id, status, triggered_by, started_at, created_at)
|
||||
VALUES (?, ?, ?, 'running', ?, ?, ?)`).run(listJobId, runplanId, scriptId, owner, ts, ts);
|
||||
const payload = listSystemJobs();
|
||||
assert.ok(payload.jobs.some((job) => job.id === listJobId && job.canCancel));
|
||||
assert.ok(payload.jobs.some((job) => job.id === 'worker:host-group-sync' && job.canRunNow));
|
||||
});
|
||||
|
||||
test('cancelSystemJob marks running jobs canceled and audits the action', () => {
|
||||
const result = cancelSystemJob(runningJobId, admin, 'test cancellation');
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(db.prepare('SELECT status FROM jobs WHERE id = ?').get(runningJobId).status, 'canceled');
|
||||
assert.equal(db.prepare('SELECT status FROM job_hosts WHERE job_id = ?').get(runningJobId).status, 'canceled');
|
||||
const action = db.prepare('SELECT * FROM system_job_actions WHERE target_id = ? AND action = ?').get(runningJobId, 'cancel');
|
||||
assert.equal(action.status, 'success');
|
||||
});
|
||||
|
||||
test('cancelSystemJob audits idle background worker cancellation attempts', () => {
|
||||
const result = cancelSystemJob('worker:host-group-sync', admin, 'not running');
|
||||
assert.equal(result.ok, false);
|
||||
assert.match(result.error, /not running/i);
|
||||
const action = db.prepare('SELECT * FROM system_job_actions WHERE target_id = ? AND action = ? ORDER BY created_at DESC').get('worker:host-group-sync', 'cancel');
|
||||
assert.equal(action.status, 'failed');
|
||||
});
|
||||
|
||||
test('runSystemJob executes host group sync and audits run-now', async () => {
|
||||
const result = await runSystemJob('worker:host-group-sync', admin);
|
||||
assert.equal(result.ok, true);
|
||||
const action = db.prepare('SELECT * FROM system_job_actions WHERE target_id = ? AND action = ? ORDER BY created_at DESC').get('worker:host-group-sync', 'run-now');
|
||||
assert.ok(action);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user