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

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 () {
describe('spawn', function () {
2024-02-21 13:09:59 +01:00
it('can run valid program', async function () {
await shell.spawn('ls', [ '-l' ], {});
});
2024-02-21 13:09:59 +01:00
it('fails on invalid program', async function () {
const [error] = await safe(shell.spawn('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 () {
const [error] = await safe(shell.spawn('/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' ], {}));
});
2016-05-24 10:19:00 -07:00
});
describe('spawn', function () {
it('spawn throws for invalid program', async function () {
const [error] = await safe(shell.spawn('cannotexist', [], {}));
2024-02-21 13:09:59 +01:00
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
2016-05-24 10:19:00 -07:00
it('spawn throws for failed program', async function () {
const [error] = await safe(shell.spawn('false', [], {}));
2024-02-21 13:09:59 +01:00
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
it('spawn times out properly', async function () {
const [error] = await safe(shell.spawn('sleep', ['20'], { timeout: 1000 }));
2024-02-21 13:09:59 +01:00
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
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', {}));
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' });
});
});
});