-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MQTT: Replace shell script by javascript functions (#1668)
- Loading branch information
Showing
8 changed files
with
162 additions
and
45 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const fs = require('fs/promises'); | ||
const { constants } = require('fs'); | ||
const os = require('os'); | ||
|
||
const logger = require('../../../utils/logger'); | ||
const { DEFAULT } = require('./constants'); | ||
|
||
const MOSQUITTO_DIRECTORY = '/var/lib/gladysassistant/mosquitto'; | ||
const MOSQUITTO_CONFIG_FILE_PATH = `${MOSQUITTO_DIRECTORY}/mosquitto.conf`; | ||
const MOSQUITTO_PASSWORD_FILE_PATH = `${MOSQUITTO_DIRECTORY}/mosquitto.passwd`; | ||
|
||
const MOSQUITTO_CONFIG_PORT = 'listener 1883'; | ||
const MOSQUITTO_CONFIG_CONTENT = [ | ||
'allow_anonymous false', | ||
'connection_messages false', | ||
`password_file ${DEFAULT.PASSWORD_FILE_PATH}`, | ||
MOSQUITTO_CONFIG_PORT, | ||
]; | ||
|
||
/** | ||
* @description Configure MQTT container. | ||
* @example | ||
* mqtt.configureContainer(); | ||
*/ | ||
async function configureContainer() { | ||
logger.info('MQTT broker Docker container is being configured...'); | ||
|
||
// Create configuration path (if not exists) | ||
await fs.mkdir(MOSQUITTO_DIRECTORY, { recursive: true }); | ||
|
||
// Check if config file not already exists | ||
try { | ||
// eslint-disable-next-line no-bitwise | ||
await fs.access(MOSQUITTO_CONFIG_FILE_PATH, constants.R_OK | constants.W_OK); | ||
logger.info('eclipse-mosquitto configuration file already exists.'); | ||
|
||
// Check for breaking change | ||
const configContent = await fs.readFile(MOSQUITTO_CONFIG_FILE_PATH); | ||
if (!configContent.includes(MOSQUITTO_CONFIG_PORT)) { | ||
await fs.appendFile(MOSQUITTO_CONFIG_FILE_PATH, `${os.EOL}${MOSQUITTO_CONFIG_PORT}`); | ||
} | ||
} catch (e) { | ||
logger.info('Writting default eclipse-mosquitto configuration...'); | ||
await fs.writeFile(MOSQUITTO_CONFIG_FILE_PATH, MOSQUITTO_CONFIG_CONTENT.join(os.EOL)); | ||
} | ||
|
||
// Create empty password file if not already exists | ||
const pwdFile = await fs.open(MOSQUITTO_PASSWORD_FILE_PATH, 'w'); | ||
await pwdFile.close(); | ||
} | ||
|
||
module.exports = { | ||
configureContainer, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const sinon = require('sinon'); | ||
const os = require('os'); | ||
const { constants } = require('fs'); | ||
const proxiquire = require('proxyquire').noCallThru(); | ||
|
||
const { assert, fake } = sinon; | ||
|
||
const gladys = {}; | ||
const mqttClient = {}; | ||
const serviceId = 'faea9c35-759a-44d5-bcc9-2af1de37b8b4'; | ||
|
||
describe('mqttHandler.configureContainer', () => { | ||
const fsMock = {}; | ||
const configureContainer = proxiquire('../../../../services/mqtt/lib/configureContainer', { | ||
'fs/promises': fsMock, | ||
}); | ||
const MqttHandler = proxiquire('../../../../services/mqtt/lib', { | ||
'./configureContainer': configureContainer, | ||
}); | ||
|
||
beforeEach(() => { | ||
fsMock.mkdir = fake.resolves(true); | ||
fsMock.access = fake.resolves(true); | ||
fsMock.readFile = fake.resolves('read'); | ||
fsMock.appendFile = fake.resolves(true); | ||
fsMock.writeFile = fake.resolves('write'); | ||
fsMock.open = fake.resolves({ close: () => {} }); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.reset(); | ||
}); | ||
|
||
it('should not write configuration', async () => { | ||
fsMock.readFile = fake.resolves(`1st line${os.EOL}listener 1883`); | ||
|
||
const mqttHandler = new MqttHandler(gladys, mqttClient, serviceId); | ||
await mqttHandler.configureContainer(); | ||
|
||
assert.calledOnceWithExactly(fsMock.mkdir, '/var/lib/gladysassistant/mosquitto', { recursive: true }); | ||
assert.calledOnceWithExactly( | ||
fsMock.access, | ||
'/var/lib/gladysassistant/mosquitto/mosquitto.conf', | ||
// eslint-disable-next-line no-bitwise | ||
constants.R_OK | constants.W_OK, | ||
); | ||
assert.calledOnceWithExactly(fsMock.readFile, '/var/lib/gladysassistant/mosquitto/mosquitto.conf'); | ||
assert.calledOnceWithExactly(fsMock.open, '/var/lib/gladysassistant/mosquitto/mosquitto.passwd', 'w'); | ||
assert.notCalled(fsMock.appendFile); | ||
assert.notCalled(fsMock.writeFile); | ||
}); | ||
|
||
it('should write default port', async () => { | ||
const mqttHandler = new MqttHandler(gladys, mqttClient, serviceId); | ||
await mqttHandler.configureContainer(); | ||
|
||
assert.calledOnceWithExactly(fsMock.mkdir, '/var/lib/gladysassistant/mosquitto', { recursive: true }); | ||
assert.calledOnceWithExactly( | ||
fsMock.access, | ||
'/var/lib/gladysassistant/mosquitto/mosquitto.conf', | ||
// eslint-disable-next-line no-bitwise | ||
constants.R_OK | constants.W_OK, | ||
); | ||
assert.calledOnceWithExactly(fsMock.readFile, '/var/lib/gladysassistant/mosquitto/mosquitto.conf'); | ||
assert.calledOnceWithExactly( | ||
fsMock.appendFile, | ||
'/var/lib/gladysassistant/mosquitto/mosquitto.conf', | ||
`${os.EOL}listener 1883`, | ||
); | ||
assert.calledOnceWithExactly(fsMock.open, '/var/lib/gladysassistant/mosquitto/mosquitto.passwd', 'w'); | ||
assert.notCalled(fsMock.writeFile); | ||
}); | ||
|
||
it('should create default configuration file', async () => { | ||
fsMock.access = fake.rejects(); | ||
|
||
const mqttHandler = new MqttHandler(gladys, mqttClient, serviceId); | ||
await mqttHandler.configureContainer(); | ||
|
||
assert.calledOnceWithExactly(fsMock.mkdir, '/var/lib/gladysassistant/mosquitto', { recursive: true }); | ||
assert.calledOnceWithExactly( | ||
fsMock.access, | ||
'/var/lib/gladysassistant/mosquitto/mosquitto.conf', | ||
// eslint-disable-next-line no-bitwise | ||
constants.R_OK | constants.W_OK, | ||
); | ||
assert.notCalled(fsMock.readFile); | ||
assert.notCalled(fsMock.appendFile); | ||
assert.calledOnceWithExactly( | ||
fsMock.writeFile, | ||
'/var/lib/gladysassistant/mosquitto/mosquitto.conf', | ||
`allow_anonymous false${os.EOL}connection_messages false${os.EOL}password_file /mosquitto/config/mosquitto.passwd${os.EOL}listener 1883`, | ||
); | ||
assert.calledOnceWithExactly(fsMock.open, '/var/lib/gladysassistant/mosquitto/mosquitto.passwd', 'w'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters