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

143 lines
4.5 KiB
JavaScript
Raw Normal View History

2018-12-10 21:05:46 -08:00
/* jslint node:true */
/* global it:false */
/* global before:false */
/* global after:false */
/* global describe:false */
'use strict';
2021-07-12 23:35:30 -07:00
const BoxError = require('../boxerror.js'),
common = require('./common.js'),
2018-12-10 21:05:46 -08:00
expect = require('expect.js'),
fs = require('fs'),
paths = require('../paths.js'),
2021-07-12 23:35:30 -07:00
safe = require('safetydance'),
tasks = require('../tasks.js'),
_ = require('underscore');
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;
let TASK = {
type: 'tasktype',
args: [ 1 ],
percent: 0,
message: 'Queued'
};
it('add succeeds', async function () {
const id = await tasks.add(TASK.type, TASK.args);
expect(id).to.be.ok();
taskId = id;
});
it('get succeeds', async function () {
const task = await tasks.get(taskId);
expect(_.pick(task, Object.keys(TASK))).to.eql(TASK);
});
it('get random task fails', async function () {
const task = await tasks.get('random');
expect(task).to.be(null);
});
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);
expect(_.pick(task, Object.keys(TASK))).to.eql(TASK);
});
it('list succeeds - does not exist', async function () {
const result = await tasks.listByTypePaged('randomtask', 1, 1);
expect(result.length).to.be(0);
});
it('list succeeds - by type', async function () {
const result = await tasks.listByTypePaged(TASK.type, 1, 1);
expect(result.length).to.be(1);
expect(_.pick(result[0], Object.keys(TASK))).to.eql(TASK);
});
it('list succeeds - all', async function () {
const result = await tasks.listByTypePaged(null, 1, 1);
expect(result.length).to.be(1);
expect(_.pick(result[0], Object.keys(TASK))).to.eql(TASK);
});
it('del succeeds', async function () {
await tasks._del(taskId);
const task = await tasks.get(taskId);
expect(task).to.be(null);
});
it('del missing task fails', async function () {
2023-07-11 16:32:28 +05:30
const [error] = await safe(tasks._del('1235'));
2021-07-12 23:35:30 -07:00
expect(error.reason).to.be(BoxError.NOT_FOUND);
});
it('can run valid task - success', async function () {
const taskId = await tasks.add(tasks._TASK_IDENTITY, [ 'ping' ]);
return new Promise((resolve, reject) => {
2019-08-27 22:39:59 -07:00
tasks.startTask(taskId, {}, function (error, result) {
2021-07-12 23:35:30 -07:00
if (error) return reject(error);
2019-08-27 22:39:59 -07:00
expect(result).to.equal('ping');
2021-07-12 23:35:30 -07:00
resolve();
2019-08-27 22:39:59 -07:00
});
2018-12-10 21:05:46 -08:00
});
});
2021-07-12 23:35:30 -07:00
it('can run valid task - error', async function () {
const taskId = await tasks.add(tasks._TASK_ERROR, [ 'ping' ]);
2019-08-27 22:39:59 -07:00
2021-07-12 23:35:30 -07:00
return new Promise((resolve, reject) => {
2019-08-27 22:39:59 -07:00
tasks.startTask(taskId, {}, function (error, result) {
2021-07-12 23:35:30 -07:00
if (!error) return reject(new Error('expecting task to fail'));
2019-08-27 22:39:59 -07:00
expect(error.message).to.be('Failed for arg: ping');
expect(result).to.be(null);
2021-07-12 23:35:30 -07:00
resolve();
2019-08-27 22:39:59 -07:00
});
2018-12-10 21:05:46 -08:00
});
});
2021-07-12 23:35:30 -07:00
it('can get logs of crash', async function () {
const taskId = await tasks.add(tasks._TASK_CRASH, [ 'ping' ]);
2019-08-27 22:39:59 -07:00
2021-07-12 23:35:30 -07:00
return new Promise((resolve, reject) => {
2019-08-27 22:39:59 -07:00
tasks.startTask(taskId, {}, function (error, result) {
2021-07-12 23:35:30 -07:00
if (!error) return reject(new Error('expecting task to crash'));
2019-08-27 22:39:59 -07:00
expect(error.message).to.contain(`Task ${taskId} crashed`);
expect(result).to.be(null);
2021-07-12 23:35:30 -07:00
const logs = fs.readFileSync(`${paths.TASKS_LOG_DIR}/${taskId}.log`, 'utf8');
2019-08-27 22:39:59 -07:00
expect(logs).to.contain('Crashing for arg: ping');
2021-07-12 23:35:30 -07:00
resolve();
2019-08-27 22:39:59 -07:00
});
2018-12-10 21:05:46 -08:00
});
});
2021-07-12 23:35:30 -07:00
it('can stop task', async function () {
const taskId = await tasks.add(tasks._TASK_SLEEP, [ 10000 ]);
2019-08-27 22:39:59 -07:00
2021-07-12 23:35:30 -07:00
return new Promise((resolve, reject) => {
2019-08-27 22:39:59 -07:00
tasks.startTask(taskId, {}, function (error, result) {
2021-07-12 23:35:30 -07:00
if (!error) return reject(new Error('expecting task to stop'));
2020-08-06 22:04:46 -07:00
expect(error.message).to.contain('stopped');
2019-08-27 22:39:59 -07:00
expect(result).to.be(null);
2021-07-12 23:35:30 -07:00
resolve();
2019-08-27 22:39:59 -07:00
});
2021-07-12 23:35:30 -07:00
setTimeout(async function () {
await tasks.stopTask(taskId);
}, 2000);
2018-12-10 21:42:03 -08:00
});
});
2018-12-10 21:05:46 -08:00
});