Skip to content

Commit

Permalink
Merge pull request #1738 from balena-io/v6-number-image-image-size-
Browse files Browse the repository at this point in the history
Fix v6 image.image_size no longer being a number
  • Loading branch information
flowzone-app[bot] authored Sep 30, 2024
2 parents 7828517 + ecf79f4 commit 8a9e289
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@balena/env-parsing": "^1.2.0",
"@balena/es-version": "^1.0.3",
"@balena/node-metrics-gatherer": "^6.0.3",
"@balena/pinejs": "^19.0.0",
"@balena/pinejs": "^19.0.2",
"@balena/pinejs-webresource-cloudfront": "^0.2.1",
"@sentry/node": "^8.30.0",
"@types/basic-auth": "^1.1.8",
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ export const envVarsConfig = {
DEVICE_TYPE_SPECIFIC_CONFIG_VAR_PROPERTIES,
};

// Needed so that the augmented `@balena/sbvr-types` typings
// automatically become available to consumer projects.
import './translations/v6/numeric-big-integer-hack.js';

export const translations = {
v7: {
getTranslations: getV7Translations,
Expand Down
31 changes: 31 additions & 0 deletions src/translations/v6/numeric-big-integer-hack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import SbvrTypes from '@balena/sbvr-types';
import type * as TypeUtils from '@balena/sbvr-types/out/type-utils.js';

// We use the TsTypes as the type name here so that it doesn't conflict later
// when we augment the @balena/sbvr-types Types type.
type TsTypes = TypeUtils.TsTypes<number, number>;
export { TsTypes as Types };
export type DbWriteType = number;

// Augmenting is necessary so that the TS v6-model.ts compiles properly
declare module '@balena/sbvr-types' {
export interface Types {
'Numeric Big Integer': TsTypes;
}
}

// @ts-expect-error we are augmenting SbvrTypes w/ Numeric Big Integer
SbvrTypes.default['Numeric Big Integer'] = {
...SbvrTypes.default['Big Integer'],

fetchProcessing(data) {
const processedData =
SbvrTypes.default['Big Integer'].fetchProcessing(data);
if (processedData == null) {
return processedData;
}
return parseInt(processedData, 10);
},

validate: SbvrTypes.default.Integer.validate,
} satisfies TypeUtils.SbvrType<TsTypes['Read'], TsTypes['Write'], DbWriteType>;
9 changes: 9 additions & 0 deletions src/translations/v6/v6.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './numeric-big-integer-hack.js';

import type { ConfigLoader } from '@balena/pinejs';
import {
aliasFields,
Expand Down Expand Up @@ -29,6 +31,13 @@ for (const resource of ['device', 'application']) {
renameResourceField(v6AbstractSqlModel, resource, 'env var name', 'name');
}

overrideFieldType(
v6AbstractSqlModel,
'image',
'image size',
'Numeric Big Integer',
);

export const getV6Translations = (abstractSqlModel = v6AbstractSqlModel) => {
const deviceFieldSet = new Set(
abstractSqlModel.tables['device'].fields.map((f) => f.fieldName),
Expand Down
76 changes: 75 additions & 1 deletion test/07_versioned-releases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { UserObjectParam } from './test-lib/supertest.js';
import { supertest } from './test-lib/supertest.js';
import * as versions from './test-lib/versions.js';
import { setTimeout } from 'timers/promises';
import { assertExists } from './test-lib/common.js';
import { assertExists, itExpectsError } from './test-lib/common.js';

export default () => {
versions.test((version, pineTest) => {
Expand Down Expand Up @@ -38,6 +38,80 @@ export default () => {
await fixtures.clean(fx);
});

const imageSizeType = versions.lte(version, 'v6') ? 'number' : 'string';
it(`should return an image.image_size of type ${imageSizeType}`, async function () {
const { body: image } = await pineUser
.get({
resource: 'image',
id: fx.images.release1_image1.id,
options: {
$select: 'image_size',
},
})
.expect(200);
const imageSize = versions.lte(version, 'v6')
? 75123123123
: '75123123123';
expect(image).to.have.property('image_size', imageSize);
});

it(`should be able to PATCH a ${imageSizeType} value < MAX_SAFE_INTEGER as an image.image_size`, async function () {
const newSize = Number.MAX_SAFE_INTEGER - 1;
const newSizeBodyValue = versions.lte(version, 'v6')
? newSize
: `${newSize}`;
await pineUser
.patch({
resource: 'image',
id: fx.images.release1_image1.id,
body: {
image_size: newSizeBodyValue.toString(),
},
})
.expect(200);
const { body: image } = await pineUser
.get({
resource: 'image',
id: fx.images.release1_image1.id,
options: {
$select: 'image_size',
},
})
.expect(200);
expect(image).to.have.property('image_size', newSizeBodyValue);
});

itExpectsError(
`should be able to PATCH a value > MAX_SAFE_INTEGER as an image.image_size but always retrieve it as a string`,
async function () {
const newBigIntSize = (
BigInt(Number.MAX_SAFE_INTEGER) + BigInt(10)
).toString();
await pineUser
.patch({
resource: 'image',
id: fx.images.release1_image1.id,
body: {
image_size: newBigIntSize,
},
})
.expect(200);
const { body: image } = await pineUser
.get({
resource: 'image',
id: fx.images.release1_image1.id,
options: {
$select: 'image_size',
},
})
.expect(200);
expect(image).to.have.property('image_size', newBigIntSize);
},
versions.lte(version, 'v6')
? `expected { image_size: 9007199254741000 } to have property 'image_size' of '9007199254741001', but got 9007199254741000`
: `expected { image_size: '9007199254741000' } to have property 'image_size' of '9007199254741001', but got '9007199254741000'`,
);

it('should be able to create a new failed release for a given commit', async () => {
await supertest(user)
.post(`/${version}/release`)
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/07-releases/images.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"release1_image1": {
"user": "admin",
"service": "app1_service1",
"releases": [ "release1" ],
"image_size": "75123123123",
"project_type": "Dockerfile.template",
"build_log": "This is also the build log",
"status": "success"
}
}
7 changes: 7 additions & 0 deletions test/fixtures/07-releases/services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"app1_service1": {
"user": "admin",
"application": "app1",
"service_name": "app1_service1"
}
}

0 comments on commit 8a9e289

Please sign in to comment.