Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
callemand committed Jul 11, 2023
1 parent 610e423 commit d1513c3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 14 deletions.
4 changes: 2 additions & 2 deletions server/services/node-red/lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ async function init() {
return;
}

const dockerBased = (await this.gladys.system.isDocker()) || true;
const dockerBased = await this.gladys.system.isDocker();
if (!dockerBased) {
this.dockerBased = false;
throw new PlatformNotCompatible('SYSTEM_NOT_RUNNING_DOCKER');
}

const networkMode = (await this.gladys.system.getNetworkMode()) || 'host';
const networkMode = await this.gladys.system.getNetworkMode();
if (networkMode !== 'host') {
this.networkModeValid = false;
throw new PlatformNotCompatible('DOCKER_BAD_NETWORK');
Expand Down
4 changes: 0 additions & 4 deletions server/services/node-red/lib/installContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ async function installContainer(config) {
const containerLog = await this.gladys.system.createContainer(containerDescriptorToMutate);
logger.trace(containerLog);

const plop = this.gladys.system.getContainers();
logger.info('COUCOUCOUCOU');
logger.info(plop);

logger.info('NodeRed: successfully installed and configured as Docker container');
this.nodeRedExist = true;
} catch (e) {
Expand Down
14 changes: 14 additions & 0 deletions server/test/services/node-red/lib/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ describe('NodeRed init', () => {
getNetworkMode: fake.resolves('host'),
restartContainer: fake.resolves(true),
},
variable: {
getValue: fake.resolves('1'),
},
};

nodeRedManager = new NodeRedManager(gladys, {}, serviceId);
Expand Down Expand Up @@ -117,4 +120,15 @@ describe('NodeRed init', () => {
assert.calledOnce(nodeRedManager.installContainer);
assert.calledWithMatch(nodeRedManager.installContainer, sinon.match(expectedNewConfig));
});

it('should not init if the service is not enabled', async () => {
gladys.variable.getValue = fake.resolves('0');

await nodeRedManager.init();

assert.notCalled(nodeRedManager.getConfiguration);
assert.notCalled(nodeRedManager.saveConfiguration);
assert.notCalled(nodeRedManager.checkForContainerUpdates);
assert.notCalled(nodeRedManager.installContainer);
});
});
16 changes: 16 additions & 0 deletions server/test/services/node-red/lib/installContainer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ describe('NodeRed installContainer', () => {
basePathOnContainer: TEMP_GLADYS_FOLDER,
}),
},
variable: {
getValue: fake.resolves('1'),
},
};

nodeRedManager = new NodeRedManager(gladys, serviceId);
Expand Down Expand Up @@ -196,4 +199,17 @@ describe('NodeRed installContainer', () => {
expect(nodeRedManager.nodeRedRunning).to.equal(false);
expect(nodeRedManager.nodeRedExist).to.equal(false);
});

it('should not create container if the service is not enabled', async () => {
gladys.variable.getValue = fake.resolves('0');

await nodeRedManager.installContainer(config);

assert.notCalled(gladys.system.pull);
assert.notCalled(gladys.system.createContainer);
assert.notCalled(gladys.system.restartContainer);

expect(nodeRedManager.nodeRedRunning).to.equal(false);
expect(nodeRedManager.nodeRedExist).to.equal(false);
});
});
24 changes: 17 additions & 7 deletions server/test/services/node-red/lib/isEnabled.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const { expect } = require('chai');
const { fake } = require('sinon');

const NodeRedManager = require('../../../../services/node-red/lib');

const gladys = {};
const gladys = {
variable: {
getValue: fake.resolves(null),
},
};
const serviceId = '625a8a9a-aa9d-474f-8cec-0718dd4ade04';

describe('NodeRed isEnabled', () => {
Expand All @@ -11,17 +16,22 @@ describe('NodeRed isEnabled', () => {
nodeRedService = new NodeRedManager(gladys, serviceId);
});

it('should return false', () => {
nodeRedService.nodeRedRunning = false;
it('should return false when value not exist', async () => {
const result = await nodeRedService.isEnabled();
expect(result).to.equal(false);
});

it('should return false', async () => {
gladys.variable.getValue = fake.resolves('0');

const result = nodeRedService.isEnabled();
const result = await nodeRedService.isEnabled();
expect(result).to.equal(false);
});

it('should return true', () => {
nodeRedService.nodeRedRunning = true;
it('should return true', async () => {
gladys.variable.getValue = fake.resolves('1');

const result = nodeRedService.isEnabled();
const result = await nodeRedService.isEnabled();
expect(result).to.equal(true);
});
});
7 changes: 6 additions & 1 deletion server/test/services/node-red/lib/status.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const { expect } = require('chai');
const { fake } = require('sinon');

const NodeRedManager = require('../../../../services/node-red/lib');

const gladys = {};
const gladys = {
variable: {
getValue: fake.resolves('1'),
},
};
const serviceId = 'f87b7af2-ca8e-44fc-b754-444354b42fee';

describe('NodeRed status', () => {
Expand Down

0 comments on commit d1513c3

Please sign in to comment.