diff --git a/server/services/node-red/lib/init.js b/server/services/node-red/lib/init.js index 86462a7d66..ea3b4aadd5 100644 --- a/server/services/node-red/lib/init.js +++ b/server/services/node-red/lib/init.js @@ -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'); diff --git a/server/services/node-red/lib/installContainer.js b/server/services/node-red/lib/installContainer.js index b45aa080b6..0326957dd8 100644 --- a/server/services/node-red/lib/installContainer.js +++ b/server/services/node-red/lib/installContainer.js @@ -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) { diff --git a/server/test/services/node-red/lib/init.test.js b/server/test/services/node-red/lib/init.test.js index 896374e7ef..1a31473689 100644 --- a/server/test/services/node-red/lib/init.test.js +++ b/server/test/services/node-red/lib/init.test.js @@ -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); @@ -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); + }); }); diff --git a/server/test/services/node-red/lib/installContainer.test.js b/server/test/services/node-red/lib/installContainer.test.js index c9ddecaa1a..c5125a4a11 100644 --- a/server/test/services/node-red/lib/installContainer.test.js +++ b/server/test/services/node-red/lib/installContainer.test.js @@ -45,6 +45,9 @@ describe('NodeRed installContainer', () => { basePathOnContainer: TEMP_GLADYS_FOLDER, }), }, + variable: { + getValue: fake.resolves('1'), + }, }; nodeRedManager = new NodeRedManager(gladys, serviceId); @@ -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); + }); }); diff --git a/server/test/services/node-red/lib/isEnabled.test.js b/server/test/services/node-red/lib/isEnabled.test.js index d1bd9bd4fc..4709d96396 100644 --- a/server/test/services/node-red/lib/isEnabled.test.js +++ b/server/test/services/node-red/lib/isEnabled.test.js @@ -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', () => { @@ -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); }); }); diff --git a/server/test/services/node-red/lib/status.test.js b/server/test/services/node-red/lib/status.test.js index 4a1d541b09..fd42fc8837 100644 --- a/server/test/services/node-red/lib/status.test.js +++ b/server/test/services/node-red/lib/status.test.js @@ -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', () => {