Skip to content

Commit

Permalink
fix(systemd): run isRunning() as sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
aileen committed Mar 16, 2018
1 parent 29b4a0b commit 81ce940
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 40 deletions.
22 changes: 10 additions & 12 deletions extensions/systemd/systemd.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,16 @@ class SystemdProcessManager extends cli.ProcessManager {
}

isRunning() {
try {
execa.shellSync(`systemctl is-active ${this.systemdName}`);
return true;
} catch (e) {
// Systemd prints out "inactive" if service isn't running
// or "activating" if service hasn't completely started yet
if (!e.message.match(/inactive|activating/)) {
throw e;
}

return false;
}
return this.ui.sudo(`systemctl is-active ${this.systemdName}`)
.then(() => Promise.resolve(true))
.catch ((error) => {
// Systemd prints out "inactive" if service isn't running
// or "activating" if service hasn't completely started yet
if (!error.message.match(/inactive|activating/)) {
return Promise.reject(error);
}
return Promise.resolve(false);
});
}

_precheck() {
Expand Down
53 changes: 25 additions & 28 deletions extensions/systemd/test/systemd-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,45 +251,42 @@ describe('Unit: Systemd > Process Manager', function () {
});

describe('isRunning', function () {
let execaStub;

beforeEach(function () {
execaStub = sinon.stub();
});

it('Calls execa', function () {
it('Returns true if process manager is running', function () {
const ui = {sudo: sinon.stub().resolves()};
const expectedCmd = 'systemctl is-active ghost_ghost_org';
const ext = makeSystemd({execa: {shellSync: execaStub}});
const ext = makeSystemd(null, ui);

expect(ext.isRunning()).to.be.true;
expect(execaStub.calledOnce).to.be.true;
expect(execaStub.args[0][0]).to.equal(expectedCmd);
ext.isRunning().then((result) => {
expect(result).to.be.true;
expect(ui.sudo.calledOnce).to.be.true;
expect(ui.sudo.args[0][0]).to.equal(expectedCmd);
});
});

it('Passes bad errors through', function () {
execaStub = sinon.stub().throws(new Error('Zebra'));
const ext = makeSystemd({execa: {shellSync: execaStub}});
const ui = {sudo: sinon.stub().rejects(new Error('unknown'))};
const ext = makeSystemd(null, ui);
const expectedCmd = 'systemctl is-active ghost_ghost_org';

try {
ext.isRunning();
ext.isRunning().then(() => {
expect(false, 'An error should have been thrown').to.be.true;
} catch (error) {
expect(execaStub.calledOnce).to.be.true;
expect(error.message).to.equal('Zebra');
}
}).catch((error) => {
expect(error.message).to.equal('unknown');
expect(ui.sudo.calledOnce).to.be.true;
expect(ui.sudo.args[0][0]).to.equal(expectedCmd);
});
});

it('Doesn\'t pass stopped errors through', function () {
let execaStub = sinon.stub().throws(new Error('inactive'));
const proxyOpts = {execa: {shellSync: execaStub}};
let ext = makeSystemd(proxyOpts);

expect(ext.isRunning()).to.be.false;

execaStub = sinon.stub().throws(new Error('activating'));
ext = makeSystemd(proxyOpts);
const ui = {sudo: sinon.stub().rejects(new Error('inactive'))};
const ext = makeSystemd(null, ui);
const expectedCmd = 'systemctl is-active ghost_ghost_org';

expect(ext.isRunning()).to.be.false;
ext.isRunning().then((result) => {
expect(result).to.be.false;
expect(ui.sudo.calledOnce).to.be.true;
expect(ui.sudo.args[0][0]).to.equal(expectedCmd);
});
});
});

Expand Down

0 comments on commit 81ce940

Please sign in to comment.