Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DO NOT MERGE UI TAKEOVER TEST DO NOT MERGE #36

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

import { ActionsConfig } from './types';
import type { ActionsConfig } from './types';

export const actionsConfig: ActionsConfig = {
actions: {
Expand Down Expand Up @@ -50,6 +50,9 @@ export const actionsConfig: ActionsConfig = {
resinhup11: {},
resinhup12: {},
},
'raspberrypi3-64': {
takeover: { minTargetVersion: '2.94.3' },
},
'beaglebone-black': {
resinhup11: {},
resinhup12: {
Expand Down Expand Up @@ -82,6 +85,13 @@ export const actionsConfig: ActionsConfig = {
minSourceVersion: '2.7.4',
},
},
'jetson-xavier-nx-devkit-emmc': {
takeover: {
// NOTE: this version is here as a placeholder for
// testing. Replace with the correct version before merging
minTargetVersion: '5.1.45+rev1',
},
},
qemux86: {
balenahup: {
minSourceVersion: '2.9.3',
Expand Down
28 changes: 27 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as bSemver from 'balena-semver';
import { TypedError } from 'typed-error';
import { actionsConfig as defaultActionsConfig } from './config';
import { ActionName, ActionsConfig } from './types';
import type { ActionName, ActionsConfig } from './types';
export { actionsConfig } from './config';
export * from './types';

Expand Down Expand Up @@ -120,8 +120,13 @@ export class HUPActionHelper {
);
}
} else {
// Takeover overrides the checks below for the device type
if (this.isTakeoverRequired(deviceType, currentVersion, targetVersion)) {
return 'takeover';
}
actionName = 'balenahup';
}

const { actionsConfig } = this;
const defaultActions = actionsConfig.deviceTypesDefaults;
const deviceActions = actionsConfig.deviceTypes[deviceType] || {};
Expand Down Expand Up @@ -177,6 +182,27 @@ export class HUPActionHelper {
return actionName;
}

public isTakeoverRequired(
deviceType: string,
currentVersion: string,
targetVersion: string,
) {
const { actionsConfig } = this;
const deviceActions = actionsConfig.deviceTypes[deviceType] || {};

if (deviceActions.takeover == null) {
return false;
}

const { minTargetVersion } = deviceActions.takeover;
if (
bSemver.lt(currentVersion, minTargetVersion) &&
bSemver.gte(targetVersion, minTargetVersion)
) {
return true;
}
}

/**
* @summary Returns whether the provided device type supports OS updates between the current and target balenaOS versions
* @name isSupportedOsUpdate
Expand Down
9 changes: 8 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ export interface ActionConfig {
maxTargetVersion?: string;
}

// The per device configuration can override the version configuration of the
// action, or define a 'takeover' version when a jump between two versions
// cannot be done with balenahup, but needs a full re-flash
type DeviceTypeConfig = {
[K in ActionName]?: Partial<ActionConfig>;
} & { takeover?: Pick<ActionConfig, 'minTargetVersion'> };

export interface ActionsConfig {
actions: { [K in ActionName]: ActionConfig };
deviceTypesDefaults: { [K in ActionName]?: Partial<ActionConfig> };
deviceTypes: Partial<{
[deviceTypeSlug: string]: { [K in ActionName]?: Partial<ActionConfig> };
[deviceTypeSlug: string]: DeviceTypeConfig;
}>;
}
30 changes: 30 additions & 0 deletions tests/01-actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,5 +729,35 @@ describe('BalenaHupActionUtils', () => {
).to.equal('balenahup');
});
});

describe('takeover', () => {
[
{
deviceType: 'jetson-xavier-nx-devkit-emmc',
before: '5.0.0',
cutoff: '5.1.45',
takeover: '5.1.45+rev1',
after: '5.2.0',
},
].forEach(({ deviceType, before, cutoff, takeover, after }) => {
it(`should return 'balenahup' if doing HUP for ${deviceType} to a version before ${takeover}`, () => {
expect(
hupActionHelper.getHUPActionType(deviceType, before, cutoff),
).to.equal('balenahup');
});

it(`should return 'takeover' if doing HUP for ${deviceType} to a version after ${takeover}`, () => {
expect(
hupActionHelper.getHUPActionType(deviceType, before, after),
).to.equal('takeover');
});

it(`should return 'balenahup' if doing HUP for ${deviceType} from a version after ${takeover}`, () => {
expect(
hupActionHelper.getHUPActionType(deviceType, takeover, after),
).to.equal('balenahup');
});
});
});
});
});