Merge pull request #1421 from balena-io/patch-extra-uenv-match

Patch extra uenv match
This commit is contained in:
bulldozer-balena[bot] 2020-07-31 07:18:49 +00:00 committed by GitHub
commit 0c2d9338ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 56 additions and 48 deletions

View File

@ -23,7 +23,10 @@ export async function remountAndWriteAtomic(
export abstract class DeviceConfigBackend { export abstract class DeviceConfigBackend {
// Does this config backend support the given device type? // Does this config backend support the given device type?
public abstract matches(deviceType: string, metaRelease?: string): boolean; public abstract async matches(
deviceType: string,
metaRelease?: string,
): Promise<boolean>;
// A function which reads and parses the configuration options from // A function which reads and parses the configuration options from
// specific boot config // specific boot config

View File

@ -157,7 +157,7 @@ export class ConfigfsConfigBackend extends DeviceConfigBackend {
return this; return this;
} }
public matches(deviceType: string): boolean { public async matches(deviceType: string): Promise<boolean> {
return ConfigfsConfigBackend.SupportedDeviceTypes.includes(deviceType); return ConfigfsConfigBackend.SupportedDeviceTypes.includes(deviceType);
} }

View File

@ -18,6 +18,9 @@ import * as constants from '../../lib/constants';
import log from '../../lib/supervisor-console'; import log from '../../lib/supervisor-console';
import { ExtLinuxEnvError, ExtLinuxParseError } from '../../lib/errors'; import { ExtLinuxEnvError, ExtLinuxParseError } from '../../lib/errors';
// The OS version when extlinux moved to READ ONLY partition
const EXTLINUX_READONLY = '2.47.0';
/** /**
* A backend to handle extlinux host configuration * A backend to handle extlinux host configuration
* *
@ -44,12 +47,15 @@ export class ExtlinuxConfigBackend extends DeviceConfigBackend {
'(?:' + _.escapeRegExp(ExtlinuxConfigBackend.bootConfigVarPrefix) + ')(.+)', '(?:' + _.escapeRegExp(ExtlinuxConfigBackend.bootConfigVarPrefix) + ')(.+)',
); );
public matches(deviceType: string, metaRelease: string | undefined): boolean { public async matches(
deviceType: string,
metaRelease: string | undefined,
): Promise<boolean> {
return ( return (
// Only test metaRelease with Jetson devices // Only test metaRelease with Jetson devices
deviceType.startsWith('jetson-') && deviceType.startsWith('jetson-') &&
typeof metaRelease === 'string' && typeof metaRelease === 'string' &&
semver.lt(metaRelease, constants.extLinuxReadOnly) semver.lt(metaRelease, EXTLINUX_READONLY)
); );
} }

View File

@ -1,6 +1,5 @@
import * as _ from 'lodash'; import * as _ from 'lodash';
import { fs } from 'mz'; import { fs } from 'mz';
import * as semver from 'semver';
import { import {
ConfigOptions, ConfigOptions,
@ -61,14 +60,10 @@ export class ExtraUEnvConfigBackend extends DeviceConfigBackend {
')(.+)', ')(.+)',
); );
public matches(deviceType: string, metaRelease: string | undefined): boolean { public async matches(deviceType: string): Promise<boolean> {
return ( return (
deviceType === 'intel-nuc' || (deviceType === 'intel-nuc' || deviceType.startsWith('jetson')) &&
// Test metaRelease for Jetson devices (await fs.exists(ExtraUEnvConfigBackend.bootConfigPath))
(deviceType.startsWith('jetson') &&
// Assume metaRelease is greater than or equal to EXTRA_SUPPORT if undefined
(typeof metaRelease === 'undefined' ||
semver.gte(metaRelease, constants.extLinuxReadOnly)))
); );
} }

View File

@ -50,7 +50,7 @@ export class RPiConfigBackend extends DeviceConfigBackend {
'avoid_safe_mode', 'avoid_safe_mode',
]; ];
public matches(deviceType: string): boolean { public async matches(deviceType: string): Promise<boolean> {
return deviceType.startsWith('raspberry') || deviceType === 'fincm3'; return deviceType.startsWith('raspberry') || deviceType === 'fincm3';
} }

View File

@ -29,9 +29,13 @@ async function getConfigBackend(
): Promise<DeviceConfigBackend | undefined> { ): Promise<DeviceConfigBackend | undefined> {
// Some backends are only supported by certain release versions so pass in metaRelease // Some backends are only supported by certain release versions so pass in metaRelease
const metaRelease = await getMetaOSRelease(constants.hostOSVersionPath); const metaRelease = await getMetaOSRelease(constants.hostOSVersionPath);
return _.find(configBackends, (backend) => let matched;
backend.matches(deviceType, metaRelease), for (const backend of configBackends) {
); if (await backend.matches(deviceType, metaRelease)) {
matched = backend;
}
}
return matched;
} }
export function envToBootConfig( export function envToBootConfig(

View File

@ -65,8 +65,6 @@ const constants = {
// (this number is used as an upper bound when generating // (this number is used as an upper bound when generating
// a random jitter) // a random jitter)
maxApiJitterDelay: 60 * 1000, maxApiJitterDelay: 60 * 1000,
// The OS version when extlinux moved to READ ONLY partition
extLinuxReadOnly: '2.47.0',
}; };
if (process.env.DOCKER_HOST == null) { if (process.env.DOCKER_HOST == null) {

View File

@ -122,10 +122,12 @@ describe('Extlinux Configuration', () => {
.that.equals('3,4,5'); .that.equals('3,4,5');
}); });
it('only matches supported devices', () => { it('only matches supported devices', async () => {
MATCH_TESTS.forEach(({ deviceType, metaRelease, supported }) => for (const { deviceType, metaRelease, supported } of MATCH_TESTS) {
expect(backend.matches(deviceType, metaRelease)).to.equal(supported), await expect(
); backend.matches(deviceType, metaRelease),
).to.eventually.equal(supported);
}
}); });
it('errors when cannot find extlinux.conf', async () => { it('errors when cannot find extlinux.conf', async () => {

View File

@ -66,10 +66,13 @@ describe('extra_uEnv Configuration', () => {
await expect(backend.getBootConfig()).to.eventually.deep.equal({}); await expect(backend.getBootConfig()).to.eventually.deep.equal({});
}); });
it('only matches supported devices', () => { it('only matches supported devices', async () => {
MATCH_TESTS.forEach(({ deviceType, metaRelease, supported }) => const existsStub = stub(fs, 'exists');
expect(backend.matches(deviceType, metaRelease)).to.equal(supported), for (const { hasConfigFile, deviceType, supported } of MATCH_TESTS) {
); existsStub.resolves(hasConfigFile);
await expect(backend.matches(deviceType)).to.eventually.equal(supported);
}
existsStub.restore();
}); });
it('errors when cannot find extra_uEnv.txt', async () => { it('errors when cannot find extra_uEnv.txt', async () => {
@ -235,83 +238,80 @@ const MALFORMED_CONFIGS = [
}, },
]; ];
const SUPPORTED_VERSION = '2.47.0'; // or greater
const UNSUPPORTED_VERSION = '2.45.0'; // or less
const MATCH_TESTS = [ const MATCH_TESTS = [
{ {
deviceType: 'jetson-tx1', deviceType: 'jetson-tx1',
metaRelease: SUPPORTED_VERSION, hasConfigFile: true,
supported: true, supported: true,
}, },
{ {
deviceType: 'jetson-tx2', deviceType: 'jetson-tx2',
metaRelease: SUPPORTED_VERSION, hasConfigFile: true,
supported: true, supported: true,
}, },
{ {
deviceType: 'jetson-tx2', deviceType: 'jetson-tx2',
metaRelease: UNSUPPORTED_VERSION, hasConfigFile: false,
supported: false, supported: false,
}, },
{ {
deviceType: 'jetson-nano', deviceType: 'jetson-nano',
metaRelease: SUPPORTED_VERSION, hasConfigFile: true,
supported: true, supported: true,
}, },
{ {
deviceType: 'jetson-nano', deviceType: 'jetson-nano',
metaRelease: UNSUPPORTED_VERSION, hasConfigFile: false,
supported: false, supported: false,
}, },
{ {
deviceType: 'jetson-xavier', deviceType: 'jetson-xavier',
metaRelease: SUPPORTED_VERSION, hasConfigFile: true,
supported: true, supported: true,
}, },
{ {
deviceType: 'jetson-xavier', deviceType: 'jetson-xavier',
metaRelease: UNSUPPORTED_VERSION, hasConfigFile: false,
supported: false, supported: false,
}, },
{ {
deviceType: 'intel-nuc', deviceType: 'intel-nuc',
metaRelease: SUPPORTED_VERSION, hasConfigFile: true,
supported: true, supported: true,
}, },
{ {
deviceType: 'intel-nuc', deviceType: 'intel-nuc',
metaRelease: UNSUPPORTED_VERSION, hasConfigFile: false,
supported: true,
},
{
deviceType: 'raspberry',
metaRelease: SUPPORTED_VERSION,
supported: false, supported: false,
}, },
{ {
deviceType: 'raspberry', deviceType: 'raspberry',
metaRelease: UNSUPPORTED_VERSION, hasConfigFile: true,
supported: false,
},
{
deviceType: 'raspberry',
hasConfigFile: false,
supported: false, supported: false,
}, },
{ {
deviceType: 'fincm3', deviceType: 'fincm3',
metaRelease: SUPPORTED_VERSION, hasConfigFile: true,
supported: false, supported: false,
}, },
{ {
deviceType: 'fincm3', deviceType: 'fincm3',
metaRelease: UNSUPPORTED_VERSION, hasConfigFile: false,
supported: false, supported: false,
}, },
{ {
deviceType: 'up-board', deviceType: 'up-board',
metaRelease: SUPPORTED_VERSION, hasConfigFile: true,
supported: false, supported: false,
}, },
{ {
deviceType: 'up-board', deviceType: 'up-board',
metaRelease: UNSUPPORTED_VERSION, hasConfigFile: false,
supported: false, supported: false,
}, },
]; ];