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

Add more realistic os configure tests #2903

Merged
merged 2 commits into from
Dec 30, 2024
Merged
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
199 changes: 113 additions & 86 deletions npm-shrinkwrap.json
91 changes: 81 additions & 10 deletions tests/commands/os/configure.spec.ts
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ import * as process from 'process';
import { runCommand } from '../../helpers';
import { promisify } from 'util';
import * as tmp from 'tmp';
import type * as $imagefs from 'balena-image-fs';

tmp.setGracefulCleanup();
const tmpNameAsync = promisify(tmp.tmpName);
@@ -29,28 +30,97 @@ import { BalenaAPIMock } from '../../nock/balena-api-mock';

if (process.platform !== 'win32') {
describe('balena os configure', function () {
let imagefs: typeof $imagefs;
let api: BalenaAPIMock;
let tmpPath: string;
let tmpDummyPath: string;
let tmpMatchingDtJsonPartitionPath: string;

beforeEach(async () => {
before(async function () {
// We conditionally import balena-image-fs, since when imported on top level then unrelated tests on win32 failed with:
// EPERM: operation not permitted, rename 'C:\Users\RUNNER~1\AppData\Local\Temp\tmp-<...>.inprogress' -> 'C:\Users\RUNNER~1\AppData\Local\Temp\tmp-<...>'
// at async Object.rename (node:internal/fs/promises:782:10) {
imagefs = await import('balena-image-fs');
tmpDummyPath = (await tmpNameAsync()) as string;
await fs.copyFile('./tests/test-data/dummy.img', tmpDummyPath);
tmpMatchingDtJsonPartitionPath = (await tmpNameAsync()) as string;
await fs.copyFile(
'./tests/test-data/mock-jetson-nano-6.0.13.with-boot-partition-12.img',
tmpMatchingDtJsonPartitionPath,
);
});

beforeEach(() => {
api = new BalenaAPIMock();
api.expectGetWhoAmI({ optional: true, persist: true });
tmpPath = (await tmpNameAsync()) as string;
await fs.copyFile('./tests/test-data/dummy.img', tmpPath);
});

afterEach(async () => {
afterEach(() => {
api.done();
await fs.unlink(tmpPath);
});

it('should inject a valid config.json file', async () => {
after(async () => {
await fs.unlink(tmpDummyPath);
await fs.unlink(tmpMatchingDtJsonPartitionPath);
});

it('should inject a valid config.json file to an image with partition 12 as boot & matching device-type.json ', async () => {
api.expectGetApplication();
api.expectGetDeviceTypes();
// TODO: this shouldn't be necessary & the CLI should be able to find
// everything required from the device-type.json in the image.
api.expectGetConfigDeviceTypes();
api.expectDownloadConfig();

const command: string[] = [
`os configure ${tmpMatchingDtJsonPartitionPath}`,
'--device-type jetson-nano',
'--version 6.0.13',
'--fleet testApp',
'--config-app-update-poll-interval 10',
'--config-network ethernet',
'--initial-device-name testDeviceName',
'--provisioning-key-name testKey',
'--provisioning-key-expiry-date 2050-12-12',
];

const { err } = await runCommand(command.join(' '));
expect(err.join('')).to.equal('');

// confirm the image contains a config.json...
const config = await imagefs.interact(
tmpMatchingDtJsonPartitionPath,
12,
async (_fs) => {
const readFileAsync = promisify(_fs.readFile);
const dtJson = JSON.parse(
await readFileAsync('/device-type.json', { encoding: 'utf8' }),
);
// confirm that the device-type.json mentions the expected partition
expect(dtJson).to.have.nested.property(
'configuration.config.partition',
12,
);
return await readFileAsync('/config.json');
},
);
expect(config).to.not.be.empty;

// confirm the image has the correct config.json values...
const configObj = JSON.parse(config.toString('utf8'));
expect(configObj).to.have.property('deviceType', 'jetson-nano');
expect(configObj).to.have.property('initialDeviceName', 'testDeviceName');
});

// TODO: In the next major consider just failing when we can't find a device-types.json in the image.
it('should inject a valid config.json file to a dummy image', async () => {
api.expectGetApplication();
// Since the dummy image doesn't include a device-type.json
// we have to reach to the API to fetch the manifest of the device type.
api.expectGetConfigDeviceTypes();
api.expectDownloadConfig();

const command: string[] = [
`os configure ${tmpPath}`,
`os configure ${tmpDummyPath}`,
'--device-type raspberrypi3',
'--version 2.47.0+rev1',
'--fleet testApp',
@@ -62,11 +132,12 @@ if (process.platform !== 'win32') {
];

const { err } = await runCommand(command.join(' '));
// Once we replace the dummy.img with one that includes a os-release & device-type.json
// then we should be able to change this to expect no errors.
expect(err.join('')).to.equal('');

// confirm the image contains a config.json...
const imagefs = await import('balena-image-fs');
const config = await imagefs.interact(tmpPath, 1, async (_fs) => {
const config = await imagefs.interact(tmpDummyPath, 1, async (_fs) => {
return await promisify(_fs.readFile)('/config.json');
});
expect(config).to.not.be.empty;
35 changes: 19 additions & 16 deletions tests/nock/balena-api-mock.ts
Original file line number Diff line number Diff line change
@@ -61,22 +61,25 @@ export class BalenaAPIMock extends NockMock {
}

public expectDownloadConfig(opts: ScopeOpts = {}) {
this.optPost('/download-config', opts).reply(
200,
JSON.parse(`{
"applicationId":1301645,
"deviceType":"raspberrypi3",
"userId":43699,
"appUpdatePollInterval":600000,
"listenPort":48484,
"vpnPort":443,
"apiEndpoint":"https://api.balena-cloud.com",
"vpnEndpoint":"vpn.balena-cloud.com",
"registryEndpoint":"registry2.balena-cloud.com",
"deltaEndpoint":"https://delta.balena-cloud.com",
"apiKey":"nothingtoseehere"
}`),
);
this.optPost('/download-config', opts).reply(200, (_uri, body) => {
let deviceType = 'raspberrypi3';
if (typeof body === 'object' && 'deviceType' in body) {
deviceType = body.deviceType;
}
return JSON.parse(`{
"applicationId":1301645,
"deviceType":"${deviceType}",
"userId":43699,
"appUpdatePollInterval":600000,
"listenPort":48484,
"vpnPort":443,
"apiEndpoint":"https://api.balena-cloud.com",
"vpnEndpoint":"vpn.balena-cloud.com",
"registryEndpoint":"registry2.balena-cloud.com",
"deltaEndpoint":"https://delta.balena-cloud.com",
"apiKey":"nothingtoseehere"
}`);
});
}

public expectApplicationProvisioning(opts: ScopeOpts = {}) {
Binary file not shown.

Unchanged files with check annotations Beta

'-sha1',
process.env.SM_CODE_SIGNING_CERT_SHA1_HASH,
'-tr',
process.env.TIMESTAMP_SERVER || 'http://timestamp.comodoca.com',

Check warning on line 457 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (self-hosted, X64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 457 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (macos-latest-xlarge)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 457 in automation/build-bin.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 457 in automation/build-bin.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 457 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (macos-13)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 457 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (self-hosted, ARM64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 457 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (windows-2019)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
'-td',
'SHA256',
'-fd',
* Wait for Apple Installer Notarization to continue
*/
async function notarizeMacInstaller(): Promise<void> {
const teamId = process.env.XCODE_APP_LOADER_TEAM_ID || '66H43P8FRG';

Check warning on line 479 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (self-hosted, X64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 479 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (macos-latest-xlarge)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 479 in automation/build-bin.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 479 in automation/build-bin.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 479 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (macos-13)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 479 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (self-hosted, ARM64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 479 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (windows-2019)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
const appleId =
process.env.XCODE_APP_LOADER_EMAIL || 'accounts+apple@balena.io';

Check warning on line 481 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (self-hosted, X64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 481 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (macos-latest-xlarge)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 481 in automation/build-bin.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 481 in automation/build-bin.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 481 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (macos-13)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 481 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (self-hosted, ARM64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 481 in automation/build-bin.ts

GitHub Actions / Flowzone / Test custom (windows-2019)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
const appleIdPassword = process.env.XCODE_APP_LOADER_PASSWORD;
if (appleIdPassword && teamId) {
}
result.push('### Description');
const description = (command.description || '')

Check warning on line 42 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (self-hosted, X64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 42 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (macos-latest-xlarge)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 42 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 42 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 42 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (macos-13)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 42 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (self-hosted, ARM64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 42 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (windows-2019)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
.split('\n')
.slice(1) // remove the first line, which oclif uses as help header
.join('\n')
if (!_.isEmpty(command.args)) {
result.push('### Arguments');
for (const [name, arg] of Object.entries(command.args!)) {
result.push(`#### ${name.toUpperCase()}`, arg.description || '');

Check warning on line 56 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (self-hosted, X64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 56 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (macos-latest-xlarge)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 56 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 56 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 56 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (macos-13)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 56 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (self-hosted, ARM64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 56 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (windows-2019)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
}
}
.join()
.trim();
result.push(`#### ${flagUsage}`);
result.push(flag.description || '');

Check warning on line 72 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (self-hosted, X64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 72 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (macos-latest-xlarge)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 72 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 72 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 72 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (macos-13)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 72 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (self-hosted, ARM64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 72 in automation/capitanodoc/markdown.ts

GitHub Actions / Flowzone / Test custom (windows-2019)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
}
}
return result;
* @param args Arguments to parse (default is process.argv.slice(2))
*/
async function parse(args?: string[]) {
args = args || process.argv.slice(2);

Check warning on line 44 in automation/run.ts

GitHub Actions / Flowzone / Test custom (self-hosted, X64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 44 in automation/run.ts

GitHub Actions / Flowzone / Test custom (macos-latest-xlarge)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 44 in automation/run.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 44 in automation/run.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 44 in automation/run.ts

GitHub Actions / Flowzone / Test custom (macos-13)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 44 in automation/run.ts

GitHub Actions / Flowzone / Test custom (self-hosted, ARM64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 44 in automation/run.ts

GitHub Actions / Flowzone / Test custom (windows-2019)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
console.error(`[debug] automation/run.ts process.argv=[${process.argv}]`);
console.error(`[debug] automation/run.ts args=[${args}]`);
if (_.isEmpty(args)) {
throw new Error(getUsage(upstreams, upstreamName));
}
const packageName = upstream.module || upstream.repo;

Check warning on line 96 in automation/update-module.ts

GitHub Actions / Flowzone / Test custom (self-hosted, X64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 96 in automation/update-module.ts

GitHub Actions / Flowzone / Test custom (macos-latest-xlarge)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 96 in automation/update-module.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 96 in automation/update-module.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 96 in automation/update-module.ts

GitHub Actions / Flowzone / Test custom (macos-13)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 96 in automation/update-module.ts

GitHub Actions / Flowzone / Test custom (self-hosted, ARM64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 96 in automation/update-module.ts

GitHub Actions / Flowzone / Test custom (windows-2019)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
const oldVersion = await getVersion(packageName);
await run(`npm install ${packageName}@${process.argv[3]}`);
* @returns whether the login was successful or not
*/
export async function loginIfTokenValid(token?: string): Promise<boolean> {
token = (token || '').trim();

Check warning on line 53 in src/auth/utils.ts

GitHub Actions / Flowzone / Test custom (self-hosted, X64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 53 in src/auth/utils.ts

GitHub Actions / Flowzone / Test custom (macos-latest-xlarge)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 53 in src/auth/utils.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 53 in src/auth/utils.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 53 in src/auth/utils.ts

GitHub Actions / Flowzone / Test custom (macos-13)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 53 in src/auth/utils.ts

GitHub Actions / Flowzone / Test custom (self-hosted, ARM64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 53 in src/auth/utils.ts

GitHub Actions / Flowzone / Test custom (windows-2019)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
if (!token) {
return false;
}
{
dockerfilePath: opts.dockerfile,
noParentCheck: opts['noparent-check'] || false,
projectPath: opts.source || '.',

Check warning on line 173 in src/commands/build/index.ts

GitHub Actions / Flowzone / Test custom (self-hosted, X64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 173 in src/commands/build/index.ts

GitHub Actions / Flowzone / Test custom (macos-latest-xlarge)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 173 in src/commands/build/index.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 173 in src/commands/build/index.ts

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 173 in src/commands/build/index.ts

GitHub Actions / Flowzone / Test custom (macos-13)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 173 in src/commands/build/index.ts

GitHub Actions / Flowzone / Test custom (self-hosted, ARM64)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 173 in src/commands/build/index.ts

GitHub Actions / Flowzone / Test custom (windows-2019)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
registrySecretsPath: opts['registry-secrets'],
},
);