2015-07-20 00:09:47 -07:00
|
|
|
/* 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'),
|
2015-07-20 00:09:47 -07:00
|
|
|
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');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
describe('shell', function () {
|
2024-10-15 10:10:15 +02:00
|
|
|
describe('spawn', function () {
|
2024-02-21 13:09:59 +01:00
|
|
|
it('can run valid program', async function () {
|
2024-10-15 10:10:15 +02:00
|
|
|
await shell.spawn('ls', [ '-l' ], {});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
|
2024-02-21 13:09:59 +01:00
|
|
|
it('fails on invalid program', async function () {
|
2024-10-15 10:10:15 +02:00
|
|
|
const [error] = await safe(shell.spawn('randomprogram', [], {}));
|
2024-02-21 13:09:59 +01:00
|
|
|
expect(error.reason).to.be(BoxError.SHELL_ERROR);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
|
2024-02-21 13:09:59 +01:00
|
|
|
it('fails on failing program', async function () {
|
2024-10-15 10:10:15 +02:00
|
|
|
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);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
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();
|
|
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
|
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();
|
|
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
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-10-15 10:10:15 +02: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);
|
2018-11-23 10:57:54 -08:00
|
|
|
});
|
2016-05-24 10:19:00 -07:00
|
|
|
|
2024-10-15 10:10:15 +02: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);
|
2018-11-23 10:57:54 -08:00
|
|
|
});
|
2024-02-20 21:38:54 +01:00
|
|
|
|
2024-10-15 10:10:15 +02:00
|
|
|
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);
|
|
|
|
|
});
|
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
|
|
|
});
|
2024-02-20 21:38:54 +01:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|