From 426d8cd10ad22fcf79b019dc49e2c2f6c27cd06d Mon Sep 17 00:00:00 2001 From: Cameron Diver Date: Wed, 8 Jan 2020 14:42:08 +0000 Subject: [PATCH] Ensure that we always have the pi4 overlay assigned Change-type: patch Closes: #1171 Signed-off-by: Cameron Diver --- src/device-config.ts | 60 ++++++++++++++++++------------- test/13-device-config.spec.coffee | 38 +++++++++++++++++--- 2 files changed, 70 insertions(+), 28 deletions(-) diff --git a/src/device-config.ts b/src/device-config.ts index ffe93ff4..90832c94 100644 --- a/src/device-config.ts +++ b/src/device-config.ts @@ -337,18 +337,14 @@ export class DeviceConfig { target: Dictionary, deviceType: string, ): boolean { - let targetBootConfig = configUtils.envToBootConfig(configBackend, target); + const targetBootConfig = configUtils.envToBootConfig(configBackend, target); const currentBootConfig = configUtils.envToBootConfig( configBackend, current, ); - if (deviceType === 'fincm3') { - // current will always have the balena-fin dtoverlay, but the target does - // not have to as this is one that we enforce. If there is no balena-fin in the - // target state, add it - targetBootConfig = DeviceConfig.ensureFinOverlay(targetBootConfig); - } + // Some devices require specific overlays, here we apply them + DeviceConfig.ensureRequiredOverlay(deviceType, targetBootConfig); if (!_.isEqual(currentBootConfig, targetBootConfig)) { _.each(targetBootConfig, (value, key) => { @@ -539,17 +535,18 @@ export class DeviceConfig { return false; } - let conf = configUtils.envToBootConfig(backend, target); + const conf = configUtils.envToBootConfig(backend, target); this.logger.logSystemMessage( `Applying boot config: ${JSON.stringify(conf)}`, {}, 'Apply boot config in progress', ); - // Ensure that the fin always has it's dtoverlay - if ((await this.config.get('deviceType')) === 'fincm3') { - conf = DeviceConfig.ensureFinOverlay(conf); - } + // Ensure devices already have required overlays + DeviceConfig.ensureRequiredOverlay( + await this.config.get('deviceType'), + conf, + ); try { await backend.setBootConfig(conf); @@ -613,19 +610,34 @@ export class DeviceConfig { } // Modifies conf - private static ensureFinOverlay(conf: ConfigOptions) { - if (conf.dtoverlay != null) { - if (_.isArray(conf.dtoverlay)) { - if (!_.includes(conf.dtoverlay, 'balena-fin')) { - conf.dtoverlay.push('balena-fin'); - } - } else if (conf.dtoverlay !== 'balena-fin') { - conf.dtoverlay = [conf.dtoverlay, 'balena-fin']; - } - } else { - conf.dtoverlay = ['balena-fin']; + private static ensureRequiredOverlay( + deviceType: string, + conf: ConfigOptions, + ) { + switch (deviceType) { + case 'fincm3': + this.ensureDtoverlay(conf, 'balena-fin'); + break; + case 'raspberrypi4-64': + this.ensureDtoverlay(conf, 'vc4-fkms-v3d'); + break; } - conf.dtoverlay = (conf.dtoverlay as string[]).filter(s => !_.isEmpty(s)); + + return conf; + } + + // Modifies conf + private static ensureDtoverlay(conf: ConfigOptions, field: string) { + if (conf.dtoverlay == null) { + conf.dtoverlay = []; + } else if (_.isString(conf.dtoverlay)) { + conf.dtoverlay = [conf.dtoverlay]; + } + if (!_.includes(conf.dtoverlay, field)) { + conf.dtoverlay.push(field); + } + conf.dtoverlay = conf.dtoverlay.filter(s => !_.isEmpty(s)); + return conf; } } diff --git a/test/13-device-config.spec.coffee b/test/13-device-config.spec.coffee index 19ff0b6c..6ef758e3 100644 --- a/test/13-device-config.spec.coffee +++ b/test/13-device-config.spec.coffee @@ -218,11 +218,11 @@ describe 'DeviceConfig', -> describe 'Balena fin', -> it 'should always add the balena-fin dtoverlay', -> - expect(DeviceConfig.ensureFinOverlay({})).to.deep.equal({ dtoverlay: ['balena-fin'] }) - expect(DeviceConfig.ensureFinOverlay({ test: '123', test2: ['123'], test3: ['123', '234'] })).to + expect(DeviceConfig.ensureRequiredOverlay('fincm3', {})).to.deep.equal({ dtoverlay: ['balena-fin'] }) + expect(DeviceConfig.ensureRequiredOverlay('fincm3', { test: '123', test2: ['123'], test3: ['123', '234'] })).to .deep.equal({ test: '123', test2: ['123'], test3: ['123', '234'], dtoverlay: ['balena-fin'] }) - expect(DeviceConfig.ensureFinOverlay({ dtoverlay: 'test' })).to.deep.equal({ dtoverlay: ['test', 'balena-fin'] }) - expect(DeviceConfig.ensureFinOverlay({ dtoverlay: ['test'] })).to.deep.equal({ dtoverlay: ['test', 'balena-fin'] }) + expect(DeviceConfig.ensureRequiredOverlay('fincm3', { dtoverlay: 'test' })).to.deep.equal({ dtoverlay: ['test', 'balena-fin'] }) + expect(DeviceConfig.ensureRequiredOverlay('fincm3', { dtoverlay: ['test'] })).to.deep.equal({ dtoverlay: ['test', 'balena-fin'] }) it 'should not cause a config change when the cloud does not specify the balena-fin overlay', -> expect(@deviceConfig.bootConfigChangeRequired( @@ -246,5 +246,35 @@ describe 'DeviceConfig', -> 'fincm3' )).to.equal(false) + describe 'Raspberry pi4', -> + it 'should always add the vc4-fkms-v3d dtoverlay', -> + expect(DeviceConfig.ensureRequiredOverlay('raspberrypi4-64', {})).to.deep.equal({ dtoverlay: ['vc4-fkms-v3d'] }) + expect(DeviceConfig.ensureRequiredOverlay('raspberrypi4-64', { test: '123', test2: ['123'], test3: ['123', '234'] })).to + .deep.equal({ test: '123', test2: ['123'], test3: ['123', '234'], dtoverlay: ['vc4-fkms-v3d'] }) + expect(DeviceConfig.ensureRequiredOverlay('raspberrypi4-64', { dtoverlay: 'test' })).to.deep.equal({ dtoverlay: ['test', 'vc4-fkms-v3d'] }) + expect(DeviceConfig.ensureRequiredOverlay('raspberrypi4-64', { dtoverlay: ['test'] })).to.deep.equal({ dtoverlay: ['test', 'vc4-fkms-v3d'] }) + + it 'should not cause a config change when the cloud does not specify the pi4 overlay', -> + expect(@deviceConfig.bootConfigChangeRequired( + rpiConfigBackend, + { HOST_CONFIG_dtoverlay: '"test","vc4-fkms-v3d"' }, + { HOST_CONFIG_dtoverlay: '"test"' }, + 'raspberrypi4-64' + )).to.equal(false) + + expect(@deviceConfig.bootConfigChangeRequired( + rpiConfigBackend, + { HOST_CONFIG_dtoverlay: '"test","vc4-fkms-v3d"' }, + { HOST_CONFIG_dtoverlay: 'test' }, + 'raspberrypi4-64' + )).to.equal(false) + + expect(@deviceConfig.bootConfigChangeRequired( + rpiConfigBackend, + { HOST_CONFIG_dtoverlay: '"test","test2","vc4-fkms-v3d"' }, + { HOST_CONFIG_dtoverlay: '"test","test2"' }, + 'raspberrypi4-64' + )).to.equal(false) + # This will require stubbing device.reboot, gosuper.post, config.get/set it 'applies the target state'