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

78 lines
2.7 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'),
2024-10-14 19:10:31 +02:00
shell = require('../shell.js')('test');
describe('shell', function () {
2024-02-21 18:37:10 +01:00
describe('execArgs', function () {
2024-02-21 13:09:59 +01:00
it('can run valid program', async function () {
2024-10-14 19:10:31 +02:00
await shell.execArgs('ls', [ '-l' ], {});
});
2024-02-21 13:09:59 +01:00
it('fails on invalid program', async function () {
2024-10-14 19:10:31 +02:00
const [error] = await safe(shell.execArgs('randomprogram', [ ], {}));
2024-02-21 13:09:59 +01:00
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
2024-02-21 13:09:59 +01:00
it('fails on failing program', async function () {
2024-10-14 19:10:31 +02:00
const [error] = await safe(shell.execArgs('/usr/bin/false', [ ], {}));
2024-02-21 13:09:59 +01:00
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) {
2024-10-14 19:10:31 +02:00
shell.sudo([ 'randomprogram' ], {}, function (error) {
2024-02-21 13:09:59 +01:00
expect(error).to.be.ok();
done();
});
});
2024-02-21 13:09:59 +01:00
it('can sudo valid program', function (done) {
2024-10-14 19:10:31 +02:00
const RELOAD_NGINX_CMD = path.join(__dirname, '../src/scripts/restartservice.sh');
shell.sudo([ RELOAD_NGINX_CMD, 'nginx' ], {}, function (error) {
2024-02-21 13:09:59 +01:00
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 () {
2024-10-14 19:10:31 +02:00
const RELOAD_NGINX_CMD = path.join(__dirname, '../src/scripts/restartservice.sh');
await safe(shell.promises.sudo([ 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 throws for invalid program', async function () {
2024-10-14 19:10:31 +02:00
const [error] = await safe(shell.exec('cannotexist', {}));
2024-02-21 13:09:59 +01:00
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 () {
2024-10-14 19:10:31 +02:00
const [error] = await safe(shell.exec('false', {}));
2024-02-21 13:09:59 +01:00
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 () {
2024-10-14 19:10:31 +02:00
const [error] = await safe(shell.exec('sleep 20', { timeout: 1000 }));
2024-02-21 13:09:59 +01:00
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
2024-02-21 18:37:10 +01:00
it('cannot exec a shell program by default', async function () {
2024-10-14 19:10:31 +02:00
const [error] = await safe(shell.exec('ls -l | wc -c', {}));
2024-02-21 18:37:10 +01:00
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
it('cannot exec a shell program b', async function () {
2024-10-14 19:10:31 +02:00
await shell.exec('ls -l | wc -c', { shell: '/bin/bash' });
2024-02-21 18:37:10 +01:00
});
});
});