2017-11-01 06:47:48 +00:00
|
|
|
Promise = require 'bluebird'
|
2018-06-06 11:33:37 +00:00
|
|
|
{ fs } = require 'mz'
|
2017-11-01 06:47:48 +00:00
|
|
|
|
|
|
|
m = require 'mochainon'
|
|
|
|
{ expect } = m.chai
|
|
|
|
{ stub, spy } = m.sinon
|
|
|
|
|
2018-06-06 11:33:37 +00:00
|
|
|
prepare = require './lib/prepare'
|
2017-11-01 06:47:48 +00:00
|
|
|
fsUtils = require '../src/lib/fs-utils'
|
|
|
|
|
2018-12-20 17:12:04 +00:00
|
|
|
{ DeviceConfig } = require '../src/device-config'
|
2018-07-16 16:48:03 +00:00
|
|
|
{ ExtlinuxConfigBackend, RPiConfigBackend } = require '../src/config/backend'
|
2018-06-06 11:33:37 +00:00
|
|
|
|
|
|
|
extlinuxBackend = new ExtlinuxConfigBackend()
|
|
|
|
rpiConfigBackend = new RPiConfigBackend()
|
2017-11-01 06:47:48 +00:00
|
|
|
|
|
|
|
childProcess = require 'child_process'
|
|
|
|
|
|
|
|
describe 'DeviceConfig', ->
|
|
|
|
before ->
|
|
|
|
prepare()
|
|
|
|
@fakeDB = {}
|
2018-10-17 08:47:11 +00:00
|
|
|
@fakeConfig = {
|
|
|
|
get: (key) ->
|
|
|
|
Promise.try ->
|
|
|
|
if key == 'deviceType'
|
|
|
|
return 'raspberrypi3'
|
|
|
|
else
|
|
|
|
throw new Error('Unknown fake config key')
|
|
|
|
}
|
2017-11-01 06:47:48 +00:00
|
|
|
@fakeLogger = {
|
|
|
|
logSystemMessage: spy()
|
|
|
|
}
|
|
|
|
@deviceConfig = new DeviceConfig({ logger: @fakeLogger, db: @fakeDB, config: @fakeConfig })
|
|
|
|
|
|
|
|
|
|
|
|
# Test that the format for special values like initramfs and array variables is parsed correctly
|
|
|
|
it 'allows getting boot config with getBootConfig', ->
|
2018-06-06 11:33:37 +00:00
|
|
|
|
|
|
|
stub(fs, 'readFile').resolves('\
|
2017-11-01 06:47:48 +00:00
|
|
|
initramfs initramf.gz 0x00800000\n\
|
|
|
|
dtparam=i2c=on\n\
|
|
|
|
dtparam=audio=on\n\
|
|
|
|
dtoverlay=ads7846\n\
|
|
|
|
dtoverlay=lirc-rpi,gpio_out_pin=17,gpio_in_pin=13\n\
|
|
|
|
foobar=baz\n\
|
|
|
|
')
|
2018-06-06 11:33:37 +00:00
|
|
|
@deviceConfig.getBootConfig(rpiConfigBackend)
|
|
|
|
.then (conf) ->
|
|
|
|
fs.readFile.restore()
|
2017-11-01 06:47:48 +00:00
|
|
|
expect(conf).to.deep.equal({
|
2018-10-17 08:47:11 +00:00
|
|
|
HOST_CONFIG_initramfs: 'initramf.gz 0x00800000'
|
|
|
|
HOST_CONFIG_dtparam: '"i2c=on","audio=on"'
|
|
|
|
HOST_CONFIG_dtoverlay: '"ads7846","lirc-rpi,gpio_out_pin=17,gpio_in_pin=13"'
|
|
|
|
HOST_CONFIG_foobar: 'baz'
|
2017-11-01 06:47:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it 'properly reads a real config.txt file', ->
|
2018-06-06 11:33:37 +00:00
|
|
|
@deviceConfig.getBootConfig(rpiConfigBackend)
|
2017-11-01 06:47:48 +00:00
|
|
|
.then (conf) ->
|
|
|
|
expect(conf).to.deep.equal({
|
2018-10-17 08:47:11 +00:00
|
|
|
HOST_CONFIG_dtparam: '"i2c_arm=on","spi=on","audio=on"'
|
|
|
|
HOST_CONFIG_enable_uart: '1'
|
|
|
|
HOST_CONFIG_disable_splash: '1'
|
|
|
|
HOST_CONFIG_avoid_warnings: '1'
|
|
|
|
HOST_CONFIG_gpu_mem: '16'
|
2017-11-01 06:47:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
# Test that the format for special values like initramfs and array variables is preserved
|
|
|
|
it 'does not allow setting forbidden keys', ->
|
|
|
|
current = {
|
2018-10-17 08:47:11 +00:00
|
|
|
HOST_CONFIG_initramfs: 'initramf.gz 0x00800000'
|
|
|
|
HOST_CONFIG_dtparam: '"i2c=on","audio=on"'
|
|
|
|
HOST_CONFIG_dtoverlay: '"ads7846","lirc-rpi,gpio_out_pin=17,gpio_in_pin=13"'
|
|
|
|
HOST_CONFIG_foobar: 'baz'
|
2017-11-01 06:47:48 +00:00
|
|
|
}
|
|
|
|
target = {
|
2018-10-17 08:47:11 +00:00
|
|
|
HOST_CONFIG_initramfs: 'initramf.gz 0x00810000'
|
|
|
|
HOST_CONFIG_dtparam: '"i2c=on","audio=on"'
|
|
|
|
HOST_CONFIG_dtoverlay: '"ads7846","lirc-rpi,gpio_out_pin=17,gpio_in_pin=13"'
|
|
|
|
HOST_CONFIG_foobar: 'baz'
|
2017-11-01 06:47:48 +00:00
|
|
|
}
|
|
|
|
promise = Promise.try =>
|
2018-06-06 11:33:37 +00:00
|
|
|
@deviceConfig.bootConfigChangeRequired(rpiConfigBackend, current, target)
|
2017-11-01 06:47:48 +00:00
|
|
|
expect(promise).to.be.rejected
|
|
|
|
promise.catch (err) =>
|
|
|
|
expect(@fakeLogger.logSystemMessage).to.be.calledOnce
|
|
|
|
expect(@fakeLogger.logSystemMessage).to.be.calledWith('Attempt to change blacklisted config value initramfs', {
|
|
|
|
error: 'Attempt to change blacklisted config value initramfs'
|
|
|
|
}, 'Apply boot config error')
|
|
|
|
@fakeLogger.logSystemMessage.reset()
|
|
|
|
|
|
|
|
it 'does not try to change config.txt if it should not change', ->
|
|
|
|
current = {
|
2018-10-17 08:47:11 +00:00
|
|
|
HOST_CONFIG_initramfs: 'initramf.gz 0x00800000'
|
|
|
|
HOST_CONFIG_dtparam: '"i2c=on","audio=on"'
|
|
|
|
HOST_CONFIG_dtoverlay: '"ads7846","lirc-rpi,gpio_out_pin=17,gpio_in_pin=13"'
|
|
|
|
HOST_CONFIG_foobar: 'baz'
|
2017-11-01 06:47:48 +00:00
|
|
|
}
|
|
|
|
target = {
|
2018-10-17 08:47:11 +00:00
|
|
|
HOST_CONFIG_initramfs: 'initramf.gz 0x00800000'
|
|
|
|
HOST_CONFIG_dtparam: '"i2c=on","audio=on"'
|
|
|
|
HOST_CONFIG_dtoverlay: '"ads7846","lirc-rpi,gpio_out_pin=17,gpio_in_pin=13"'
|
|
|
|
HOST_CONFIG_foobar: 'baz'
|
2017-11-01 06:47:48 +00:00
|
|
|
}
|
|
|
|
promise = Promise.try =>
|
2018-06-06 11:33:37 +00:00
|
|
|
@deviceConfig.bootConfigChangeRequired(rpiConfigBackend, current, target)
|
2017-11-01 06:47:48 +00:00
|
|
|
expect(promise).to.eventually.equal(false)
|
|
|
|
promise.then =>
|
|
|
|
expect(@fakeLogger.logSystemMessage).to.not.be.called
|
|
|
|
@fakeLogger.logSystemMessage.reset()
|
|
|
|
|
|
|
|
it 'writes the target config.txt', ->
|
|
|
|
stub(fsUtils, 'writeFileAtomic').resolves()
|
|
|
|
stub(childProcess, 'execAsync').resolves()
|
|
|
|
current = {
|
2018-10-17 08:47:11 +00:00
|
|
|
HOST_CONFIG_initramfs: 'initramf.gz 0x00800000'
|
|
|
|
HOST_CONFIG_dtparam: '"i2c=on","audio=on"'
|
|
|
|
HOST_CONFIG_dtoverlay: '"ads7846","lirc-rpi,gpio_out_pin=17,gpio_in_pin=13"'
|
|
|
|
HOST_CONFIG_foobar: 'baz'
|
2017-11-01 06:47:48 +00:00
|
|
|
}
|
|
|
|
target = {
|
2018-10-17 08:47:11 +00:00
|
|
|
HOST_CONFIG_initramfs: 'initramf.gz 0x00800000'
|
|
|
|
HOST_CONFIG_dtparam: '"i2c=on","audio=off"'
|
|
|
|
HOST_CONFIG_dtoverlay: '"lirc-rpi,gpio_out_pin=17,gpio_in_pin=13"'
|
|
|
|
HOST_CONFIG_foobar: 'bat'
|
|
|
|
HOST_CONFIG_foobaz: 'bar'
|
2017-11-01 06:47:48 +00:00
|
|
|
}
|
|
|
|
promise = Promise.try =>
|
2018-06-06 11:33:37 +00:00
|
|
|
@deviceConfig.bootConfigChangeRequired(rpiConfigBackend, current, target)
|
2017-11-01 06:47:48 +00:00
|
|
|
expect(promise).to.eventually.equal(true)
|
|
|
|
promise.then =>
|
2018-06-06 11:33:37 +00:00
|
|
|
@deviceConfig.setBootConfig(rpiConfigBackend, target)
|
2017-11-01 06:47:48 +00:00
|
|
|
.then =>
|
|
|
|
expect(childProcess.execAsync).to.be.calledOnce
|
|
|
|
expect(@fakeLogger.logSystemMessage).to.be.calledTwice
|
|
|
|
expect(@fakeLogger.logSystemMessage.getCall(1).args[2]).to.equal('Apply boot config success')
|
|
|
|
expect(fsUtils.writeFileAtomic).to.be.calledWith('./test/data/mnt/boot/config.txt', '\
|
|
|
|
initramfs initramf.gz 0x00800000\n\
|
|
|
|
dtparam=i2c=on\n\
|
|
|
|
dtparam=audio=off\n\
|
|
|
|
dtoverlay=lirc-rpi,gpio_out_pin=17,gpio_in_pin=13\n\
|
|
|
|
foobar=bat\n\
|
|
|
|
foobaz=bar\n\
|
|
|
|
')
|
|
|
|
fsUtils.writeFileAtomic.restore()
|
|
|
|
childProcess.execAsync.restore()
|
|
|
|
@fakeLogger.logSystemMessage.reset()
|
|
|
|
|
2018-10-17 08:47:11 +00:00
|
|
|
it 'accepts RESIN_ and BALENA_ variables', ->
|
2018-10-20 02:26:14 +00:00
|
|
|
@deviceConfig.formatConfigKeys({
|
2018-11-02 14:15:01 +00:00
|
|
|
FOO: 'bar',
|
|
|
|
BAR: 'baz',
|
|
|
|
RESIN_HOST_CONFIG_foo: 'foobaz',
|
|
|
|
BALENA_HOST_CONFIG_foo: 'foobar',
|
|
|
|
RESIN_HOST_CONFIG_other: 'val',
|
|
|
|
BALENA_HOST_CONFIG_baz: 'bad',
|
|
|
|
BALENA_SUPERVISOR_POLL_INTERVAL: '100',
|
2018-10-17 08:47:11 +00:00
|
|
|
}).then (filteredConf) ->
|
|
|
|
expect(filteredConf).to.deep.equal({
|
|
|
|
HOST_CONFIG_foo: 'foobar',
|
|
|
|
HOST_CONFIG_other: 'val',
|
|
|
|
HOST_CONFIG_baz: 'bad',
|
|
|
|
SUPERVISOR_POLL_INTERVAL: '100',
|
2018-11-02 14:15:01 +00:00
|
|
|
})
|
2018-10-17 08:47:11 +00:00
|
|
|
|
2018-06-05 13:38:41 +00:00
|
|
|
describe 'Extlinux files', ->
|
|
|
|
|
|
|
|
it 'should correctly write to extlinux.conf files', ->
|
|
|
|
stub(fsUtils, 'writeFileAtomic').resolves()
|
|
|
|
stub(childProcess, 'execAsync').resolves()
|
|
|
|
|
|
|
|
current = {
|
|
|
|
}
|
|
|
|
target = {
|
2018-10-17 08:47:11 +00:00
|
|
|
HOST_EXTLINUX_isolcpus: '2'
|
2018-06-05 13:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
promise = Promise.try =>
|
2018-06-06 11:33:37 +00:00
|
|
|
@deviceConfig.bootConfigChangeRequired(extlinuxBackend, current, target)
|
2018-06-05 13:38:41 +00:00
|
|
|
expect(promise).to.eventually.equal(true)
|
|
|
|
promise.then =>
|
2018-06-06 11:33:37 +00:00
|
|
|
@deviceConfig.setBootConfig(extlinuxBackend, target)
|
2018-06-05 13:38:41 +00:00
|
|
|
.then =>
|
|
|
|
expect(childProcess.execAsync).to.be.calledOnce
|
|
|
|
expect(@fakeLogger.logSystemMessage).to.be.calledTwice
|
|
|
|
expect(@fakeLogger.logSystemMessage.getCall(1).args[2]).to.equal('Apply boot config success')
|
|
|
|
expect(fsUtils.writeFileAtomic).to.be.calledWith('./test/data/mnt/boot/extlinux/extlinux.conf', '\
|
|
|
|
DEFAULT primary\n\
|
|
|
|
TIMEOUT 30\n\
|
|
|
|
MENU TITLE Boot Options\n\
|
|
|
|
LABEL primary\n\
|
|
|
|
MENU LABEL primary Image\n\
|
|
|
|
LINUX /Image\n\
|
|
|
|
APPEND ${cbootargs} ${resin_kernel_root} ro rootwait isolcpus=2\n\
|
|
|
|
')
|
|
|
|
fsUtils.writeFileAtomic.restore()
|
|
|
|
childProcess.execAsync.restore()
|
|
|
|
@fakeLogger.logSystemMessage.reset()
|
|
|
|
|
2017-11-01 06:47:48 +00:00
|
|
|
# This will require stubbing device.reboot, gosuper.post, config.get/set
|
|
|
|
it 'applies the target state'
|