diff --git a/server/services/mqtt/lib/getConfiguration.js b/server/services/mqtt/lib/getConfiguration.js index 9627e9d43e..4787ec7a5a 100644 --- a/server/services/mqtt/lib/getConfiguration.js +++ b/server/services/mqtt/lib/getConfiguration.js @@ -1,4 +1,5 @@ const { CONFIGURATION } = require('./constants'); +const logger = require('../../../utils/logger'); const containerDescriptor = require('../docker/eclipse-mosquitto-container.json'); /** @@ -21,7 +22,20 @@ async function getConfiguration() { // Look for broker docker image if (dockerBased) { - networkModeValid = await this.checkDockerNetwork(); + try { + // In case the docker socket is not available + // It's ok, we don't crash + networkModeValid = await this.checkDockerNetwork(); + const dockerImages = await this.gladys.system.getContainers({ + all: true, + filters: { + name: [containerDescriptor.name], + }, + }); + brokerContainerAvailable = dockerImages.length > 0; + } catch (e) { + logger.debug(e); + } const useEmbeddedBrokerVariable = await this.gladys.variable.getValue( CONFIGURATION.MQTT_EMBEDDED_BROKER_KEY, @@ -29,15 +43,6 @@ async function getConfiguration() { ); // Boolean stored as integer, we need to check it useEmbeddedBroker = networkModeValid && useEmbeddedBrokerVariable !== '0'; - - const dockerImages = await this.gladys.system.getContainers({ - all: true, - filters: { - name: [containerDescriptor.name], - }, - }); - brokerContainerAvailable = dockerImages.length > 0; - mosquittoVersion = await this.gladys.variable.getValue(CONFIGURATION.MQTT_MOSQUITTO_VERSION, this.serviceId); } diff --git a/server/test/services/mqtt/lib/getConfiguration.test.js b/server/test/services/mqtt/lib/getConfiguration.test.js index b617c66490..b846118a1d 100644 --- a/server/test/services/mqtt/lib/getConfiguration.test.js +++ b/server/test/services/mqtt/lib/getConfiguration.test.js @@ -265,4 +265,35 @@ describe('mqttHandler.getConfiguration', () => { assert.calledOnce(gladys.system.getContainers); assert.calledOnce(gladys.system.getNetworkMode); }); + it('should not crash, even if docker based but not with docker socket mounted', async () => { + const gladys = { + variable: { + getValue: fake.resolves(null), + }, + system: { + isDocker: fake.resolves(true), + getContainers: fake.rejects(new Error('SYSTEM_NOT_RUNNING_DOCKER')), + getNetworkMode: fake.rejects(new Error('SYSTEM_NOT_RUNNING_DOCKER')), + }, + }; + + const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId); + const config = await mqttHandler.getConfiguration(); + + const expectedConfig = { + mqttUrl: null, + mqttUsername: null, + mqttPassword: null, + useEmbeddedBroker: false, + dockerBased: true, + brokerContainerAvailable: false, + networkModeValid: false, + mosquittoVersion: null, + }; + expect(config).to.deep.eq(expectedConfig); + + assert.callCount(gladys.variable.getValue, 5); + assert.calledOnce(gladys.system.isDocker); + assert.calledOnce(gladys.system.getNetworkMode); + }); });