2021-05-05 20:32:31 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
|
2022-08-17 23:35:08 +00:00
|
|
|
import * as imageManager from '~/src/compose/images';
|
|
|
|
import { createImage, withMockerode } from '~/test-lib/mockerode';
|
2022-09-08 02:25:10 +00:00
|
|
|
import { createDockerImage } from '~/test-lib/docker-helper';
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2024-03-05 18:15:30 +00:00
|
|
|
import Docker from 'dockerode';
|
2022-09-08 02:25:10 +00:00
|
|
|
import * as db from '~/src/db';
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
// TODO: this code is duplicated in multiple tests
|
|
|
|
// create a test module with all helper functions like this
|
|
|
|
function createDBImage(
|
|
|
|
{
|
|
|
|
appId = 1,
|
|
|
|
name = 'test-image',
|
|
|
|
serviceName = 'test',
|
|
|
|
...extra
|
|
|
|
} = {} as Partial<imageManager.Image>,
|
|
|
|
) {
|
|
|
|
return {
|
|
|
|
appId,
|
|
|
|
name,
|
|
|
|
serviceName,
|
|
|
|
...extra,
|
|
|
|
} as imageManager.Image;
|
|
|
|
}
|
|
|
|
|
2021-05-05 20:32:31 +00:00
|
|
|
describe('compose/images', () => {
|
2022-09-08 02:25:10 +00:00
|
|
|
const docker = new Docker();
|
2021-05-05 20:32:31 +00:00
|
|
|
before(async () => {
|
2022-09-08 02:25:10 +00:00
|
|
|
await db.initialized();
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
|
2022-09-08 02:25:10 +00:00
|
|
|
afterEach(async () => {
|
|
|
|
await db.models('image').del();
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
|
2022-09-08 02:25:10 +00:00
|
|
|
after(async () => {
|
|
|
|
await docker.pruneImages({ filters: { dangling: { false: true } } });
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
|
2021-07-07 22:01:48 +00:00
|
|
|
it('finds image by matching digest on the database', async () => {
|
2021-08-03 23:12:47 +00:00
|
|
|
const dbImage = createDBImage({
|
2022-09-19 15:33:52 +00:00
|
|
|
name: 'registry2.balena-cloud.com/v2/aaaaa@sha256:2c969a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
2021-05-05 20:32:31 +00:00
|
|
|
dockerImageId:
|
|
|
|
'sha256:f1154d76c731f04711e5856b6e6858730e3023d9113124900ac65c2ccc90e8e7',
|
2021-08-03 23:12:47 +00:00
|
|
|
});
|
2022-09-08 02:25:10 +00:00
|
|
|
await db.models('image').insert([dbImage]);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
const images = [
|
|
|
|
createImage(
|
|
|
|
{
|
2022-09-19 15:33:52 +00:00
|
|
|
Id: 'sha256:f1154d76c731f04711e5856b6e6858730e3023d9113124900ac65c2ccc90e8e7',
|
2021-05-05 20:32:31 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
References: [
|
2021-07-07 22:01:48 +00:00
|
|
|
// Different image repo
|
|
|
|
'registry2.balena-cloud.com/v2/bbbb@sha256:2c969a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
2022-09-08 02:25:10 +00:00
|
|
|
// INFO: We use mockerode here because cannot create images with a specific digest on the engine
|
|
|
|
// but we need to be able to test looking images by digest
|
2021-05-05 20:32:31 +00:00
|
|
|
await withMockerode(
|
|
|
|
async (mockerode) => {
|
|
|
|
// Looking by name should fail, if not, this is a mockerode issue
|
|
|
|
await expect(mockerode.getImage(dbImage.name).inspect()).to.be.rejected;
|
|
|
|
|
|
|
|
// Looking up the image by id should succeed
|
2021-08-03 23:12:47 +00:00
|
|
|
await expect(mockerode.getImage(dbImage.dockerImageId!).inspect()).to
|
|
|
|
.not.be.rejected;
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2021-07-07 22:01:48 +00:00
|
|
|
// The image is found
|
|
|
|
expect(await imageManager.inspectByName(dbImage.name))
|
|
|
|
.to.have.property('Id')
|
|
|
|
.that.equals(
|
|
|
|
'sha256:f1154d76c731f04711e5856b6e6858730e3023d9113124900ac65c2ccc90e8e7',
|
|
|
|
);
|
|
|
|
|
|
|
|
// It couldn't find the image by name so it finds it by matching digest
|
|
|
|
expect(mockerode.getImage).to.have.been.calledWith(
|
|
|
|
'sha256:f1154d76c731f04711e5856b6e6858730e3023d9113124900ac65c2ccc90e8e7',
|
|
|
|
);
|
|
|
|
},
|
|
|
|
{ images },
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('finds image by tag on the engine', async () => {
|
2022-09-08 02:25:10 +00:00
|
|
|
const dockerImageId = await createDockerImage(
|
|
|
|
'some-image:some-tag',
|
|
|
|
['io.balena.testing=1'],
|
|
|
|
docker,
|
2021-07-07 22:01:48 +00:00
|
|
|
);
|
|
|
|
|
2022-09-08 02:25:10 +00:00
|
|
|
expect(await imageManager.inspectByName('some-image:some-tag'))
|
|
|
|
.to.have.property('Id')
|
|
|
|
.that.equals(dockerImageId);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2022-09-08 02:25:10 +00:00
|
|
|
await expect(
|
|
|
|
imageManager.inspectByName('non-existing-image:non-existing-tag'),
|
|
|
|
).to.be.rejected;
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2022-09-08 02:25:10 +00:00
|
|
|
await docker.getImage('some-image:some-tag').remove();
|
2021-07-07 22:01:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('finds image by reference on the engine', async () => {
|
|
|
|
const images = [
|
|
|
|
createImage(
|
|
|
|
{
|
2022-09-19 15:33:52 +00:00
|
|
|
Id: 'sha256:f1154d76c731f04711e5856b6e6858730e3023d9113124900ac65c2ccc90e8e7',
|
2021-07-07 22:01:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
References: [
|
|
|
|
// the reference we expect to look for is registry2.balena-cloud.com/v2/one
|
|
|
|
'registry2.balena-cloud.com/v2/one:delta-one@sha256:12345a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
|
|
|
'registry2.balena-cloud.com/v2/one:latest@sha256:12345a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
2022-09-08 02:25:10 +00:00
|
|
|
// INFO: we cannot create specific references to test on the engine, we need to
|
|
|
|
// use mockerode instead.
|
|
|
|
// QUESTION: Maybe the image search is overspecified and we should find a
|
|
|
|
// common identifier for all image search (e.g. label?)
|
2021-07-07 22:01:48 +00:00
|
|
|
await withMockerode(
|
|
|
|
async (mockerode) => {
|
|
|
|
// This is really testing mockerode functionality
|
|
|
|
expect(
|
|
|
|
await mockerode.listImages({
|
|
|
|
filters: {
|
|
|
|
reference: ['registry2.balena-cloud.com/v2/one'],
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.have.lengthOf(1);
|
|
|
|
|
|
|
|
expect(
|
|
|
|
await imageManager.inspectByName(
|
|
|
|
// different target digest but same imageName
|
|
|
|
'registry2.balena-cloud.com/v2/one@sha256:2c969a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.to.have.property('Id')
|
|
|
|
.that.equals(
|
|
|
|
'sha256:f1154d76c731f04711e5856b6e6858730e3023d9113124900ac65c2ccc90e8e7',
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(mockerode.getImage).to.have.been.calledWith(
|
|
|
|
'sha256:f1154d76c731f04711e5856b6e6858730e3023d9113124900ac65c2ccc90e8e7',
|
|
|
|
);
|
|
|
|
|
|
|
|
// Looking for the reference with correct name tag shoud not throw
|
|
|
|
await expect(
|
|
|
|
imageManager.inspectByName(
|
|
|
|
// different target digest but same tag
|
|
|
|
'registry2.balena-cloud.com/v2/one:delta-one@sha256:2c969a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
|
|
|
),
|
|
|
|
).to.not.be.rejected;
|
|
|
|
|
|
|
|
// Looking for a non existing reference should throw
|
|
|
|
await expect(
|
|
|
|
imageManager.inspectByName(
|
|
|
|
'registry2.balena-cloud.com/v2/two@sha256:2c969a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
|
|
|
),
|
|
|
|
).to.be.rejected;
|
|
|
|
await expect(
|
|
|
|
imageManager.inspectByName(
|
|
|
|
'registry2.balena-cloud.com/v2/one:some-tag@sha256:2c969a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
|
|
|
),
|
|
|
|
).to.be.rejected;
|
|
|
|
},
|
|
|
|
{ images },
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns all images in both the database and the engine', async () => {
|
2022-09-08 02:25:10 +00:00
|
|
|
await db.models('image').insert([
|
2021-08-03 23:12:47 +00:00
|
|
|
createDBImage({
|
2021-07-07 22:01:48 +00:00
|
|
|
name: 'first-image-name:first-image-tag',
|
|
|
|
serviceName: 'app_1',
|
|
|
|
dockerImageId: 'sha256:first-image-id',
|
2021-08-03 23:12:47 +00:00
|
|
|
}),
|
|
|
|
createDBImage({
|
2021-07-07 22:01:48 +00:00
|
|
|
name: 'second-image-name:second-image-tag',
|
|
|
|
serviceName: 'app_2',
|
|
|
|
dockerImageId: 'sha256:second-image-id',
|
2021-08-03 23:12:47 +00:00
|
|
|
}),
|
|
|
|
createDBImage({
|
2022-09-19 15:33:52 +00:00
|
|
|
name: 'registry2.balena-cloud.com/v2/three@sha256:2c969a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf558',
|
2021-07-07 22:01:48 +00:00
|
|
|
serviceName: 'app_3',
|
|
|
|
// Third image has different name but same docker id
|
|
|
|
dockerImageId: 'sha256:second-image-id',
|
2021-08-03 23:12:47 +00:00
|
|
|
}),
|
|
|
|
createDBImage({
|
2021-07-07 22:01:48 +00:00
|
|
|
name: 'fourth-image-name:fourth-image-tag',
|
|
|
|
serviceName: 'app_4',
|
|
|
|
// The fourth image exists on the engine but with the wrong id
|
|
|
|
dockerImageId: 'sha256:fourth-image-id',
|
2021-08-03 23:12:47 +00:00
|
|
|
}),
|
2021-07-07 22:01:48 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
const images = [
|
|
|
|
createImage(
|
|
|
|
{
|
|
|
|
Id: 'sha256:first-image-id',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
References: ['first-image-name:first-image-tag'],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
createImage(
|
|
|
|
{
|
|
|
|
Id: 'sha256:second-image-id',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
References: [
|
|
|
|
// The tag for the second image does not exist on the engine but it should be found
|
|
|
|
'not-second-image-name:some-image-tag',
|
|
|
|
'fourth-image-name:fourth-image-tag',
|
|
|
|
'registry2.balena-cloud.com/v2/three@sha256:2c969a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf558',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Perform the test with our specially crafted data
|
|
|
|
await withMockerode(
|
|
|
|
async (mockerode) => {
|
|
|
|
// failsafe to check for mockerode problems
|
|
|
|
expect(
|
|
|
|
await mockerode.listImages(),
|
|
|
|
'images exist on the engine before test',
|
|
|
|
).to.have.lengthOf(2);
|
|
|
|
|
|
|
|
const availableImages = await imageManager.getAvailable();
|
|
|
|
expect(availableImages).to.have.lengthOf(4);
|
2021-05-05 20:32:31 +00:00
|
|
|
},
|
|
|
|
{ images },
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes a single legacy db images without dockerImageId', async () => {
|
2022-09-08 02:25:10 +00:00
|
|
|
await createDockerImage(
|
|
|
|
'image-name:image-tag',
|
|
|
|
['io.balena.testing=1'],
|
|
|
|
docker,
|
|
|
|
);
|
|
|
|
|
2021-05-05 20:32:31 +00:00
|
|
|
// Legacy images don't have a dockerImageId so they are queried by name
|
2021-08-03 23:12:47 +00:00
|
|
|
const imageToRemove = createDBImage({
|
2021-07-07 22:01:48 +00:00
|
|
|
name: 'image-name:image-tag',
|
2021-08-03 23:12:47 +00:00
|
|
|
});
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2022-09-08 02:25:10 +00:00
|
|
|
await db.models('image').insert([imageToRemove]);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2022-09-08 02:25:10 +00:00
|
|
|
// Check that our legacy image exists
|
|
|
|
// failsafe to check for mockerode problems
|
|
|
|
await expect(
|
|
|
|
docker.getImage(imageToRemove.name).inspect(),
|
|
|
|
'image exists on the engine before test',
|
|
|
|
).to.not.be.rejected;
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2022-09-08 02:25:10 +00:00
|
|
|
// Check that the image exists on the db
|
|
|
|
expect(
|
|
|
|
await db.models('image').select().where(imageToRemove),
|
|
|
|
).to.have.lengthOf(1);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2022-09-08 02:25:10 +00:00
|
|
|
// Now remove this image...
|
|
|
|
await imageManager.remove(imageToRemove);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2022-09-08 02:25:10 +00:00
|
|
|
// Check that the image was removed from the db
|
|
|
|
expect(await db.models('image').select().where(imageToRemove)).to.be.empty;
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
|
2021-07-07 22:01:48 +00:00
|
|
|
it('removes image from DB and engine when there is a single DB image with matching name', async () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
// Newer image
|
2021-08-03 23:12:47 +00:00
|
|
|
const imageToRemove = createDBImage({
|
2022-09-19 15:33:52 +00:00
|
|
|
name: 'registry2.balena-cloud.com/v2/one@sha256:2c969a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
2021-07-07 22:01:48 +00:00
|
|
|
dockerImageId: 'sha256:image-id-one',
|
2021-08-03 23:12:47 +00:00
|
|
|
});
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
// Insert images into the db
|
2022-09-08 02:25:10 +00:00
|
|
|
await db.models('image').insert([
|
2021-05-05 20:32:31 +00:00
|
|
|
imageToRemove,
|
2021-08-03 23:12:47 +00:00
|
|
|
createDBImage({
|
2022-09-19 15:33:52 +00:00
|
|
|
name: 'registry2.balena-cloud.com/v2/two@sha256:12345a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
2021-07-07 22:01:48 +00:00
|
|
|
dockerImageId: 'sha256:image-id-two',
|
2021-08-03 23:12:47 +00:00
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Engine image state
|
|
|
|
const images = [
|
|
|
|
// The image to remove
|
|
|
|
createImage(
|
|
|
|
{
|
2021-07-07 22:01:48 +00:00
|
|
|
Id: 'sha256:image-id-one',
|
2021-05-05 20:32:31 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
References: [
|
2021-07-07 22:01:48 +00:00
|
|
|
'registry2.balena-cloud.com/v2/one:delta-one@sha256:2c969a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
// Other images to test
|
|
|
|
createImage(
|
|
|
|
{
|
|
|
|
Id: 'aaa',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
References: ['balena/aarch64-supervisor:11.11.11'],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
// The other image on the database
|
|
|
|
createImage(
|
|
|
|
{
|
2021-07-07 22:01:48 +00:00
|
|
|
Id: 'sha256:image-id-two',
|
2021-05-05 20:32:31 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
References: [
|
2021-07-07 22:01:48 +00:00
|
|
|
'registry2.balena-cloud.com/v2/two:delta-two@sha256:12345a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Perform the test with our specially crafted data
|
|
|
|
await withMockerode(
|
|
|
|
async (mockerode) => {
|
|
|
|
// Check that the image exists
|
|
|
|
// this is really checking that mockerode works ok
|
|
|
|
await expect(
|
|
|
|
mockerode.getImage(imageToRemove.name).inspect(),
|
|
|
|
'image exists on the engine before test',
|
|
|
|
).to.not.be.rejected;
|
|
|
|
|
|
|
|
// Check that only one image with this dockerImageId exists in the db
|
|
|
|
// in memory db is a bit flaky sometimes, this checks for issues
|
|
|
|
expect(
|
2022-09-08 02:25:10 +00:00
|
|
|
await db.models('image').where(imageToRemove).select(),
|
2021-05-05 20:32:31 +00:00
|
|
|
'image exists on db before the test',
|
|
|
|
).to.have.lengthOf(1);
|
|
|
|
|
|
|
|
// Now remove this image...
|
|
|
|
await imageManager.remove(imageToRemove);
|
|
|
|
|
|
|
|
// Check that the remove method was only called once
|
|
|
|
expect(mockerode.removeImage).to.have.been.calledOnceWith(
|
2021-07-07 22:01:48 +00:00
|
|
|
'registry2.balena-cloud.com/v2/one:delta-one',
|
2021-05-05 20:32:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Check that the database no longer has this image
|
2022-09-08 02:25:10 +00:00
|
|
|
expect(await db.models('image').select().where(imageToRemove)).to.be
|
2021-05-05 20:32:31 +00:00
|
|
|
.empty;
|
|
|
|
|
|
|
|
// Expect 1 entry left on the database
|
2022-09-08 02:25:10 +00:00
|
|
|
expect(await db.models('image').select()).to.have.lengthOf(1);
|
2021-05-05 20:32:31 +00:00
|
|
|
},
|
|
|
|
{ images },
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-07-07 22:01:48 +00:00
|
|
|
it('removes the requested image even when there are multiple DB images with same docker ID', async () => {
|
2022-09-08 02:25:10 +00:00
|
|
|
const dockerImageId = await createDockerImage(
|
|
|
|
'registry2.balena-cloud.com/v2/one',
|
|
|
|
['io.balena.testing=1'],
|
|
|
|
docker,
|
|
|
|
);
|
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
const imageToRemove = createDBImage({
|
2022-09-08 02:25:10 +00:00
|
|
|
name: 'registry2.balena-cloud.com/v2/one',
|
|
|
|
dockerImageId,
|
2021-08-03 23:12:47 +00:00
|
|
|
});
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
const imageWithSameDockerImageId = createDBImage({
|
2022-09-19 15:33:52 +00:00
|
|
|
name: 'registry2.balena-cloud.com/v2/two@sha256:2c969a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
2021-05-05 20:32:31 +00:00
|
|
|
// Same imageId
|
2022-09-08 02:25:10 +00:00
|
|
|
dockerImageId,
|
2021-08-03 23:12:47 +00:00
|
|
|
});
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
// Insert images into the db
|
2022-09-08 02:25:10 +00:00
|
|
|
await db.models('image').insert([
|
2021-05-05 20:32:31 +00:00
|
|
|
imageToRemove,
|
|
|
|
// Another image from the same app
|
|
|
|
imageWithSameDockerImageId,
|
|
|
|
]);
|
|
|
|
|
2022-09-08 02:25:10 +00:00
|
|
|
// Check that multiple images with the same dockerImageId are returned
|
|
|
|
expect(
|
|
|
|
await db
|
|
|
|
.models('image')
|
|
|
|
.where({ dockerImageId: imageToRemove.dockerImageId })
|
|
|
|
.select(),
|
|
|
|
).to.have.lengthOf(2);
|
|
|
|
|
|
|
|
// Now remove these images
|
|
|
|
await imageManager.remove(imageToRemove);
|
|
|
|
|
|
|
|
// Check that the database no longer has this image
|
|
|
|
expect(await db.models('image').select().where(imageToRemove)).to.be.empty;
|
|
|
|
|
|
|
|
// Check that the image with the same dockerImageId is still on the database
|
|
|
|
expect(
|
|
|
|
await db
|
|
|
|
.models('image')
|
|
|
|
.select()
|
|
|
|
.where({ dockerImageId: imageWithSameDockerImageId.dockerImageId }),
|
|
|
|
).to.have.lengthOf(1);
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
|
2021-07-07 22:01:48 +00:00
|
|
|
it('removes image from DB by tag when deltas are being used', async () => {
|
2021-08-03 23:12:47 +00:00
|
|
|
const imageToRemove = createDBImage({
|
2022-09-19 15:33:52 +00:00
|
|
|
name: 'registry2.balena-cloud.com/v2/one@sha256:2c969a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
2021-07-07 22:01:48 +00:00
|
|
|
dockerImageId: 'sha256:image-one-id',
|
2021-08-03 23:12:47 +00:00
|
|
|
});
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
const imageWithSameDockerImageId = createDBImage({
|
2022-09-19 15:33:52 +00:00
|
|
|
name: 'registry2.balena-cloud.com/v2/two@sha256:2c969a1ba1c6bc10df53481f48c6a74dbd562cfb41ba58f81beabd03facf5582',
|
2021-07-07 22:01:48 +00:00
|
|
|
// Same docker id
|
|
|
|
dockerImageId: 'sha256:image-one-id',
|
2021-08-03 23:12:47 +00:00
|
|
|
});
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
// Insert images into the db
|
2022-09-08 02:25:10 +00:00
|
|
|
await db.models('image').insert([
|
2021-05-05 20:32:31 +00:00
|
|
|
imageToRemove,
|
|
|
|
// Another image from the same app
|
|
|
|
imageWithSameDockerImageId,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Engine image state
|
|
|
|
const images = [
|
|
|
|
// The image to remove
|
|
|
|
createImage(
|
|
|
|
{
|
2021-08-03 23:12:47 +00:00
|
|
|
Id: imageToRemove.dockerImageId!,
|
2021-05-05 20:32:31 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
References: [
|
|
|
|
// The image has two deltas with different digests than those in image.name
|
2021-07-07 22:01:48 +00:00
|
|
|
'registry2.balena-cloud.com/v2/one:delta-one@sha256:6eb712fc797ff68f258d9032cf292c266cb9bd8be4cbdaaafeb5a8824bb104fd',
|
|
|
|
'registry2.balena-cloud.com/v2/two:delta-two@sha256:f1154d76c731f04711e5856b6e6858730e3023d9113124900ac65c2ccc901234',
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Perform the test with our specially crafted data
|
|
|
|
await withMockerode(
|
|
|
|
async (mockerode) => {
|
|
|
|
// Check that the image is on the engine
|
|
|
|
await expect(
|
2021-08-03 23:12:47 +00:00
|
|
|
mockerode.getImage(imageToRemove.dockerImageId!).inspect(),
|
2021-05-05 20:32:31 +00:00
|
|
|
'image can be found by id before the test',
|
|
|
|
).to.not.be.rejected;
|
|
|
|
|
|
|
|
// Check that a single image is returned when given entire object
|
|
|
|
expect(
|
2022-09-08 02:25:10 +00:00
|
|
|
await db.models('image').select().where(imageToRemove),
|
2021-05-05 20:32:31 +00:00
|
|
|
).to.have.lengthOf(1);
|
|
|
|
|
|
|
|
// Check that multiple images with the same dockerImageId are returned
|
|
|
|
expect(
|
2022-09-08 02:25:10 +00:00
|
|
|
await db
|
2021-05-05 20:32:31 +00:00
|
|
|
.models('image')
|
|
|
|
.where({ dockerImageId: imageToRemove.dockerImageId })
|
|
|
|
.select(),
|
|
|
|
).to.have.lengthOf(2);
|
|
|
|
|
|
|
|
// Now remove these images
|
|
|
|
await imageManager.remove(imageToRemove);
|
|
|
|
|
|
|
|
// This tests the behavior
|
|
|
|
expect(mockerode.removeImage).to.have.been.calledOnceWith(
|
2021-07-07 22:01:48 +00:00
|
|
|
'registry2.balena-cloud.com/v2/one:delta-one',
|
2021-05-05 20:32:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Check that the database no longer has this image
|
2022-09-08 02:25:10 +00:00
|
|
|
expect(await db.models('image').select().where(imageToRemove)).to.be
|
2021-05-05 20:32:31 +00:00
|
|
|
.empty;
|
|
|
|
|
|
|
|
// Check that the image with the same dockerImageId is still on the database
|
|
|
|
expect(
|
2022-09-08 02:25:10 +00:00
|
|
|
await db.models('image').select().where(imageWithSameDockerImageId),
|
2021-05-05 20:32:31 +00:00
|
|
|
).to.have.lengthOf(1);
|
|
|
|
},
|
|
|
|
{ images },
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|