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

85 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 () {
it('can run valid program', function (done) {
2021-08-13 10:41:10 -07:00
let cp = shell.spawn('test', 'ls', [ '-l' ], { }, function (error) {
expect(cp).to.be.ok();
expect(error).to.be(null);
done();
});
});
it('fails on invalid program', function (done) {
2018-11-23 10:57:54 -08:00
shell.spawn('test', 'randomprogram', [ ], { }, function (error) {
expect(error).to.be.ok();
done();
});
});
it('fails on failing program', function (done) {
2018-11-23 10:57:54 -08:00
shell.spawn('test', '/usr/bin/false', [ ], { }, function (error) {
expect(error).to.be.ok();
done();
});
});
it('cannot sudo invalid program', function (done) {
2018-11-25 14:57:17 -08:00
shell.sudo('test', [ 'randomprogram' ], {}, function (error) {
expect(error).to.be.ok();
done();
});
});
it('can sudo valid program', function (done) {
2021-08-13 10:41:10 -07:00
let RELOAD_NGINX_CMD = path.join(__dirname, '../src/scripts/restartservice.sh');
2021-03-23 11:01:14 -07:00
shell.sudo('test', [ RELOAD_NGINX_CMD, 'nginx' ], {}, function (error) {
expect(error).to.be.ok();
done();
});
});
2016-05-24 10:19:00 -07:00
2021-06-03 19:32:29 -07: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' ], {}));
});
it('exec a valid shell program', function (done) {
2024-02-20 21:11:09 +01:00
shell.exec('test', 'ls -l | wc -c', {}, function (error) {
2018-11-23 10:57:54 -08:00
done(error);
});
2016-05-24 10:19:00 -07:00
});
it('exec a valid shell program (promises)', async function () {
2024-02-20 21:11:09 +01:00
await shell.promises.exec('test', 'ls -l | wc -c', {});
2021-06-03 19:32:29 -07:00
});
it('exec throws for invalid program', function (done) {
2024-02-20 21:11:09 +01:00
shell.exec('test', 'cannotexist', {}, function (error) {
2018-11-23 10:57:54 -08:00
expect(error).to.be.ok();
done();
});
2016-05-24 10:19:00 -07:00
});
it('exec throws for failed program', function (done) {
2024-02-20 21:11:09 +01:00
shell.exec('test', 'false', {}, function (error) {
2018-11-23 10:57:54 -08:00
expect(error).to.be.ok();
done();
});
2016-05-24 10:19:00 -07:00
});
it('exec times out properly', async function () {
const [error] = await safe(shell.promises.exec('sleeping', 'sleep 20', { timeout: 1000 }));
2024-02-20 23:09:49 +01:00
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
});