Files
cloudron-box/src/test/tasks-test.js
T

132 lines
4.4 KiB
JavaScript
Raw Normal View History

2026-02-19 13:24:14 +01:00
import { describe, it, before, after } from 'mocha';
import BoxError from '../boxerror.js';
2026-02-14 15:43:24 +01:00
import common from './common.js';
2026-02-18 22:21:54 +01:00
import assert from 'node:assert/strict';
import fs from 'node:fs';
import paths from '../paths.js';
import safe from 'safetydance';
import tasks from '../tasks.js';
2026-02-14 15:43:24 +01:00
import _ from '../underscore.js';
2018-12-10 21:05:46 -08:00
describe('task', function () {
2021-07-12 23:35:30 -07:00
const { setup, cleanup } = common;
2018-12-10 21:05:46 -08:00
2021-07-12 23:35:30 -07:00
before(setup);
after(cleanup);
2019-08-27 22:39:59 -07:00
2021-07-12 23:35:30 -07:00
let taskId;
2025-02-13 14:03:25 +01:00
const TASK = {
2021-07-12 23:35:30 -07:00
type: 'tasktype',
args: [ 1 ],
percent: 0,
message: 'Queued'
};
it('add succeeds', async function () {
const id = await tasks.add(TASK.type, TASK.args);
2026-02-18 22:21:54 +01:00
assert.ok(id);
2021-07-12 23:35:30 -07:00
taskId = id;
});
it('get succeeds', async function () {
const task = await tasks.get(taskId);
2026-02-18 22:21:54 +01:00
assert.deepEqual(_.pick(task, Object.keys(TASK)), TASK);
2021-07-12 23:35:30 -07:00
});
it('get random task fails', async function () {
const task = await tasks.get('random');
2026-02-18 22:21:54 +01:00
assert.equal(task, null);
2021-07-12 23:35:30 -07:00
});
it('update succeeds', async function () {
TASK.percent = 34;
TASK.message = 'almost ther';
await tasks.update(taskId, { percent: TASK.percent, message: TASK.message });
const task = await tasks.get(taskId);
2026-02-18 22:21:54 +01:00
assert.deepEqual(_.pick(task, Object.keys(TASK)), TASK);
2021-07-12 23:35:30 -07:00
});
it('list succeeds - does not exist', async function () {
const result = await tasks.list(1, 1, { type: 'randomtask' });
2026-02-18 22:21:54 +01:00
assert.equal(result.length, 0);
2021-07-12 23:35:30 -07:00
});
it('list succeeds - by type', async function () {
const result = await tasks.list(1, 1, { type: TASK.type });
2026-02-18 22:21:54 +01:00
assert.equal(result.length, 1);
assert.deepEqual(_.pick(result[0], Object.keys(TASK)), TASK);
2021-07-12 23:35:30 -07:00
});
it('list succeeds - all', async function () {
const result = await tasks.list(1, 1, { type: null });
2026-02-18 22:21:54 +01:00
assert.equal(result.length, 1);
assert.deepEqual(_.pick(result[0], Object.keys(TASK)), TASK);
2021-07-12 23:35:30 -07:00
});
it('del succeeds', async function () {
await tasks._del(taskId);
const task = await tasks.get(taskId);
2026-02-18 22:21:54 +01:00
assert.equal(task, null);
2021-07-12 23:35:30 -07:00
});
it('del missing task fails', async function () {
2023-07-11 16:32:28 +05:30
const [error] = await safe(tasks._del('1235'));
2026-02-18 22:21:54 +01:00
assert.equal(error.reason, BoxError.NOT_FOUND);
2021-07-12 23:35:30 -07:00
});
it('can run valid task - success', async function () {
2026-02-18 08:18:37 +01:00
const successTaskId = await tasks.add(tasks._TASK_IDENTITY, [ 'ping' ]);
2021-07-12 23:35:30 -07:00
2026-02-18 08:18:37 +01:00
const [error, result] = await safe(tasks.startTask(successTaskId, {}));
2025-06-17 18:54:12 +02:00
if (error) throw error;
2026-02-18 22:21:54 +01:00
assert.equal(result, 'ping');
2018-12-10 21:05:46 -08:00
});
2021-07-12 23:35:30 -07:00
it('can run valid task - error', async function () {
2026-02-18 08:18:37 +01:00
const errorTaskId = await tasks.add(tasks._TASK_ERROR, [ 'ping' ]);
2019-08-27 22:39:59 -07:00
2026-02-18 08:18:37 +01:00
const [error, result] = await safe(tasks.startTask(errorTaskId, {}));
2025-06-17 18:54:12 +02:00
if (!error) throw new Error('expecting task to fail');
2026-02-18 22:21:54 +01:00
assert.equal(error.message, 'Task crashed. Failed for arg: ping');
assert.ok(!(result));
2018-12-10 21:05:46 -08:00
});
2021-07-12 23:35:30 -07:00
it('can get logs of crash', async function () {
2026-02-18 08:18:37 +01:00
const crashTaskId = await tasks.add(tasks._TASK_CRASH, [ 'ping' ]);
2019-08-27 22:39:59 -07:00
2026-02-18 08:18:37 +01:00
const [error, result] = await safe(tasks.startTask(crashTaskId, {}));
2025-06-17 18:54:12 +02:00
if (!error) throw new Error('expecting task to crash');
2026-02-18 22:21:54 +01:00
assert.ok(error.message.includes(`Task ${crashTaskId} crashed`));
assert.ok(!(result));
2025-06-17 18:54:12 +02:00
2026-02-18 08:18:37 +01:00
const logs = fs.readFileSync(`${paths.TASKS_LOG_DIR}/${crashTaskId}.log`, 'utf8');
2026-02-18 22:21:54 +01:00
assert.ok(logs.includes('Crashing for arg: ping'));
2018-12-10 21:05:46 -08:00
});
2021-07-12 23:35:30 -07:00
it('can stop task', async function () {
2026-02-18 08:18:37 +01:00
const sleepTaskId = await tasks.add(tasks._TASK_SLEEP, [ 10000 ]);
2019-08-27 22:39:59 -07:00
2025-06-17 18:54:12 +02:00
setTimeout(async function () {
2026-02-18 08:18:37 +01:00
await tasks.stopTask(sleepTaskId);
2025-06-17 18:54:12 +02:00
}, 2000);
2026-02-18 08:18:37 +01:00
const [error, result] = await safe(tasks.startTask(sleepTaskId, {}));
2025-06-17 18:54:12 +02:00
if (!error) throw new Error('expecting task to stop');
2026-02-18 22:21:54 +01:00
assert.ok(error.message.includes('stopped'));
assert.equal(error.code, tasks.ESTOPPED);
assert.ok(!(result));
});
it('task timesout', async function () {
2026-02-18 08:18:37 +01:00
const timeoutTaskId = await tasks.add(tasks._TASK_SLEEP, [ 10000 ]);
2026-02-18 08:18:37 +01:00
const [error, result] = await safe(tasks.startTask(timeoutTaskId, { timeout: 2000 }));
if (!error) throw new Error('expecting task to timeout');
2026-02-18 22:21:54 +01:00
assert.equal(error.code, tasks.ETIMEOUT);
assert.ok(error.message.includes('timed out'));
assert.ok(!(result));
2018-12-10 21:42:03 -08:00
});
2018-12-10 21:05:46 -08:00
});