Skip to content

Commit

Permalink
fix(systemd): run isEnabled() as sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
aileen authored and acburdine committed Mar 20, 2018
1 parent bf1a20e commit 5552a0e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 40 deletions.
21 changes: 10 additions & 11 deletions extensions/systemd/systemd.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,17 @@ class SystemdProcessManager extends cli.ProcessManager {
}

isEnabled() {
try {
execa.shellSync(`systemctl is-enabled ${this.systemdName}`);
return true;
} catch (e) {
// Systemd prints out "disabled" if service isn't enabled
// or "failed to get unit file state" if something else goes wrong
if (!e.message.match(/disabled|Failed to get unit file state/)) {
throw e;
}
return this.ui.sudo(`systemctl is-enabled ${this.systemdName}`)
.then(() => Promise.resolve(true))
.catch((error) => {
// Systemd prints out "disabled" if service isn't enabled
// or "failed to get unit file state" if something else goes wrong
if (!error.message.match(/disabled|Failed to get unit file state/)) {
return Promise.reject(error);
}

return false;
}
return Promise.resolve(false);
});
}

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

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

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

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

expect(ext.isEnabled()).to.be.true;
expect(execaStub.calledOnce).to.be.true;
expect(execaStub.args[0][0]).to.equal(expectedCmd);
ext.isEnabled().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('ponies'));
const ext = makeSystemd({execa: {shellSync: execaStub}});
const ui = {sudo: sinon.stub().rejects(new Error('unknown'))};
const ext = makeSystemd(null, ui);
const expectedCmd = 'systemctl is-enabled ghost_ghost_org';

try {
ext.isEnabled();
ext.isEnabled().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('ponies');
}
}).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 disabled errors through', function () {
let execaStub = sinon.stub().throws(new Error('disabled'));
const proxyOpts = {execa: {shellSync: execaStub}};
let ext = makeSystemd(proxyOpts);

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

execaStub = sinon.stub().throws(new Error('Failed to get unit file state'));
ext = makeSystemd(proxyOpts);
it('Doesn\'t pass stopped errors through', function () {
const ui = {sudo: sinon.stub().rejects(new Error('disabled'))};
const ext = makeSystemd(null, ui);
const expectedCmd = 'systemctl is-enabled ghost_ghost_org';

expect(ext.isEnabled()).to.be.false;
ext.isEnabled().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 5552a0e

Please sign in to comment.