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

73 lines
2.6 KiB
JavaScript
Raw Normal View History

/* jslint node:true */
/* global it:false */
/* global describe:false */
'use strict';
2024-02-20 23:09:49 +01:00
const BoxError = require('../boxerror.js'),
expect = require('expect.js'),
path = require('path'),
2021-06-03 19:32:29 -07:00
safe = require('safetydance'),
shell = require('../shell.js');
describe('shell', function () {
2024-02-21 13:09:59 +01:00
describe('execFile', function () {
it('can run valid program', async function () {
await shell.promises.execFile('test', 'ls', [ '-l' ], {});
});
2024-02-21 13:09:59 +01:00
it('fails on invalid program', async function () {
const [error] = await safe(shell.promises.execFile('test', 'randomprogram', [ ], {}));
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
2024-02-21 13:09:59 +01:00
it('fails on failing program', async function () {
const [error] = await safe(shell.promises.execFile('test', '/usr/bin/false', [ ], {}));
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
});
2024-02-21 13:09:59 +01:00
describe('sudo', function () {
it('cannot sudo invalid program', function (done) {
shell.sudo('test', [ 'randomprogram' ], {}, function (error) {
expect(error).to.be.ok();
done();
});
});
2024-02-21 13:09:59 +01:00
it('can sudo valid program', function (done) {
let RELOAD_NGINX_CMD = path.join(__dirname, '../src/scripts/restartservice.sh');
shell.sudo('test', [ RELOAD_NGINX_CMD, 'nginx' ], {}, function (error) {
expect(error).to.be.ok();
done();
});
});
2021-06-03 19:32:29 -07:00
2024-02-21 13:09:59 +01:00
it('can run valid program (promises)', async function () {
let RELOAD_NGINX_CMD = path.join(__dirname, '../src/scripts/restartservice.sh');
await safe(shell.promises.sudo('test', [ RELOAD_NGINX_CMD, 'nginx' ], {}));
2018-11-23 10:57:54 -08:00
});
2016-05-24 10:19:00 -07:00
});
2024-02-21 13:09:59 +01:00
describe('exec', function () {
it('exec a valid shell program', async function () {
await shell.promises.exec('test', 'ls -l | wc -c', {});
});
2021-06-03 19:32:29 -07:00
2024-02-21 13:09:59 +01:00
it('exec throws for invalid program', async function () {
const [error] = await safe(shell.promises.exec('test', 'cannotexist', {}));
expect(error.reason).to.be(BoxError.SHELL_ERROR);
2018-11-23 10:57:54 -08:00
});
2016-05-24 10:19:00 -07:00
2024-02-21 13:09:59 +01:00
it('exec throws for failed program', async function () {
const [error] = await safe(shell.promises.exec('test', 'false', {}));
expect(error.reason).to.be(BoxError.SHELL_ERROR);
2018-11-23 10:57:54 -08:00
});
2024-02-21 13:09:59 +01:00
it('exec times out properly', async function () {
const [error] = await safe(shell.promises.exec('sleeping', 'sleep 20', { timeout: 1000 }));
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
});
});