2021-04-26 19:54:04 +00:00
|
|
|
import { expect } from 'chai';
|
2022-10-21 13:59:51 +00:00
|
|
|
import { testfs } from 'mocha-pod';
|
2021-05-07 01:47:10 +00:00
|
|
|
|
2022-08-17 23:35:08 +00:00
|
|
|
import * as configUtils from '~/src/config/utils';
|
|
|
|
import { ExtraUEnv } from '~/src/config/backends/extra-uEnv';
|
|
|
|
import { Extlinux } from '~/src/config/backends/extlinux';
|
|
|
|
import { ConfigTxt } from '~/src/config/backends/config-txt';
|
|
|
|
import { ConfigFs } from '~/src/config/backends/config-fs';
|
|
|
|
import { SplashImage } from '~/src/config/backends/splash-image';
|
|
|
|
import { ConfigBackend } from '~/src/config/backends/backend';
|
2020-03-31 15:46:04 +00:00
|
|
|
|
2022-10-21 13:59:51 +00:00
|
|
|
import * as hostUtils from '~/lib/host-utils';
|
|
|
|
|
|
|
|
const keys = <T extends object>(obj: T) => Object.keys(obj) as Array<keyof T>;
|
|
|
|
|
|
|
|
describe('config/utils', () => {
|
2020-08-07 23:21:46 +00:00
|
|
|
it('gets list of supported backends', async () => {
|
2022-10-21 13:59:51 +00:00
|
|
|
const tFs = await testfs({
|
|
|
|
// This is only needed so config.get doesn't fail
|
2022-11-11 16:28:45 +00:00
|
|
|
[hostUtils.pathOnBoot('device-type.json')]: JSON.stringify({
|
|
|
|
slug: 'raspberrypi4-64',
|
|
|
|
arch: 'aarch64',
|
2022-10-21 13:59:51 +00:00
|
|
|
}),
|
2022-11-11 16:28:45 +00:00
|
|
|
[hostUtils.pathOnBoot('splash')]: {
|
|
|
|
'balena-logo.png': Buffer.from(
|
|
|
|
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEX/TQBcNTh/AAAAAXRSTlPM0jRW/QAAAApJREFUeJxjYgAAAAYAAzY3fKgAAAAASUVORK5CYII=',
|
|
|
|
'base64',
|
|
|
|
),
|
|
|
|
},
|
2022-10-21 13:59:51 +00:00
|
|
|
}).enable();
|
|
|
|
|
2020-08-07 23:21:46 +00:00
|
|
|
// Get list of backends
|
|
|
|
const devices = await configUtils.getSupportedBackends();
|
2021-01-05 21:37:53 +00:00
|
|
|
expect(devices.length).to.equal(2);
|
2020-08-07 23:21:46 +00:00
|
|
|
expect(devices[0].constructor.name).to.equal('ConfigTxt');
|
2021-01-05 21:37:53 +00:00
|
|
|
expect(devices[1].constructor.name).to.equal('SplashImage');
|
2022-10-21 13:59:51 +00:00
|
|
|
|
|
|
|
await tFs.restore();
|
2020-08-07 23:21:46 +00:00
|
|
|
// TO-DO: When we have a device that will match for multiple backends
|
|
|
|
// add a test that we get more then 1 backend for that device
|
|
|
|
});
|
|
|
|
|
|
|
|
it('transforms environment variables to boot configs', () => {
|
2022-10-21 13:59:51 +00:00
|
|
|
keys(CONFIGS).forEach((key) => {
|
2020-08-07 23:21:46 +00:00
|
|
|
expect(
|
2022-10-21 13:59:51 +00:00
|
|
|
configUtils.envToBootConfig(BACKENDS[key], CONFIGS[key].envVars),
|
|
|
|
).to.deep.equal(CONFIGS[key].bootConfig);
|
2020-08-07 23:21:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('transforms boot configs to environment variables', () => {
|
2022-10-21 13:59:51 +00:00
|
|
|
keys(CONFIGS).forEach((key) => {
|
2020-08-07 23:21:46 +00:00
|
|
|
expect(
|
2022-10-21 13:59:51 +00:00
|
|
|
configUtils.bootConfigToEnv(BACKENDS[key], CONFIGS[key].bootConfig),
|
|
|
|
).to.deep.equal(CONFIGS[key].envVars);
|
2020-08-07 23:21:46 +00:00
|
|
|
});
|
|
|
|
});
|
2020-06-10 18:50:26 +00:00
|
|
|
});
|
2020-08-07 23:21:46 +00:00
|
|
|
|
|
|
|
const BACKENDS: Record<string, ConfigBackend> = {
|
|
|
|
extraUEnv: new ExtraUEnv(),
|
|
|
|
extlinux: new Extlinux(),
|
|
|
|
configtxt: new ConfigTxt(),
|
|
|
|
configfs: new ConfigFs(),
|
2021-01-05 21:37:53 +00:00
|
|
|
splashImage: new SplashImage(),
|
2020-08-07 23:21:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const CONFIGS = {
|
|
|
|
extraUEnv: {
|
|
|
|
envVars: {
|
|
|
|
HOST_EXTLINUX_fdt: '/boot/mycustomdtb.dtb',
|
|
|
|
HOST_EXTLINUX_isolcpus: '1,2,3',
|
|
|
|
HOST_EXTLINUX_rootwait: '',
|
|
|
|
},
|
|
|
|
bootConfig: {
|
|
|
|
fdt: '/boot/mycustomdtb.dtb',
|
|
|
|
isolcpus: '1,2,3',
|
|
|
|
rootwait: '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
extlinux: {
|
|
|
|
envVars: {
|
|
|
|
HOST_EXTLINUX_fdt: '/boot/mycustomdtb.dtb',
|
|
|
|
HOST_EXTLINUX_isolcpus: '1,2,3',
|
|
|
|
HOST_EXTLINUX_rootwait: '',
|
|
|
|
},
|
|
|
|
bootConfig: {
|
|
|
|
fdt: '/boot/mycustomdtb.dtb',
|
|
|
|
isolcpus: '1,2,3',
|
|
|
|
rootwait: '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
configtxt: {
|
|
|
|
envVars: {
|
|
|
|
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',
|
|
|
|
},
|
|
|
|
bootConfig: {
|
|
|
|
initramfs: 'initramf.gz 0x00800000',
|
|
|
|
dtparam: ['i2c=on', 'audio=on'],
|
|
|
|
dtoverlay: ['ads7846', 'lirc-rpi,gpio_out_pin=17,gpio_in_pin=13'],
|
|
|
|
foobar: 'baz',
|
|
|
|
},
|
|
|
|
},
|
2021-01-05 21:37:53 +00:00
|
|
|
splashImage: {
|
|
|
|
envVars: {
|
|
|
|
HOST_SPLASH_image: 'data:image/png;base64,aaa',
|
|
|
|
},
|
|
|
|
bootConfig: {
|
|
|
|
image: 'data:image/png;base64,aaa',
|
|
|
|
},
|
|
|
|
},
|
2020-08-07 23:21:46 +00:00
|
|
|
// TO-DO: Config-FS is commented out because it behaves differently and doesn't
|
|
|
|
// add value to the Config Utilities if we make it work but would like to add it
|
|
|
|
// configfs: {
|
|
|
|
// envVars: {
|
|
|
|
// ssdt: 'spidev1,1'
|
|
|
|
// },
|
|
|
|
// bootConfig: {
|
|
|
|
// ssdt: ['spidev1,1']
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
};
|