Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run isRunning and isEnabled as sudo #672

Merged
merged 2 commits into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 20 additions & 23 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 All @@ -84,18 +83,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
108 changes: 51 additions & 57 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 Expand Up @@ -251,45 +248,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