2021-05-05 20:32:31 +00:00
|
|
|
import { expect } from 'chai';
|
2024-02-29 22:00:39 +00:00
|
|
|
import type { Image } from '~/src/compose/images';
|
2022-08-17 23:35:08 +00:00
|
|
|
import Network from '~/src/compose/network';
|
|
|
|
import Volume from '~/src/compose/volume';
|
2024-02-08 05:16:45 +00:00
|
|
|
import { LocksTakenMap } from '~/lib/update-lock';
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2023-03-07 22:32:10 +00:00
|
|
|
import {
|
|
|
|
createService,
|
|
|
|
createImage,
|
|
|
|
createApp,
|
|
|
|
DEFAULT_NETWORK,
|
|
|
|
expectSteps,
|
|
|
|
expectNoStep,
|
|
|
|
} from '~/test-lib/state-helper';
|
|
|
|
|
2021-05-05 20:32:31 +00:00
|
|
|
const defaultContext = {
|
2023-04-06 17:50:10 +00:00
|
|
|
keepVolumes: false,
|
2021-07-23 16:48:27 +00:00
|
|
|
availableImages: [] as Image[],
|
2021-05-05 20:32:31 +00:00
|
|
|
containerIds: {},
|
2021-07-23 16:48:27 +00:00
|
|
|
downloading: [] as string[],
|
2024-02-08 05:16:45 +00:00
|
|
|
locksTaken: new LocksTakenMap(),
|
2021-05-05 20:32:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
describe('compose/app', () => {
|
|
|
|
describe('volume state behavior', () => {
|
2023-06-15 02:50:26 +00:00
|
|
|
it('should correctly infer a volume create step', () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
// Setup current and target apps
|
|
|
|
const current = createApp();
|
|
|
|
const target = createApp({
|
2021-08-25 21:50:34 +00:00
|
|
|
volumes: [Volume.fromComposeObject('test-volume', 1, 'deadbeef')],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Calculate the steps
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
// Check that a createVolume step has been created
|
|
|
|
const [createVolumeStep] = expectSteps('createVolume', steps);
|
|
|
|
expect(createVolumeStep)
|
|
|
|
.to.have.property('target')
|
|
|
|
.that.deep.includes({ name: 'test-volume' });
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
it('should correctly infer more than one volume create step', () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
const current = createApp();
|
|
|
|
const target = createApp({
|
|
|
|
volumes: [
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('test-volume', 1, 'deadbeef'),
|
|
|
|
Volume.fromComposeObject('test-volume-2', 1, 'deadbeef'),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
// Check that 2 createVolume steps are found
|
|
|
|
const createVolumeSteps = expectSteps('createVolume', steps, 2);
|
|
|
|
|
|
|
|
// Check that the steps contain the volumes without any order
|
|
|
|
// expectation
|
|
|
|
expect(
|
|
|
|
createVolumeSteps.filter(
|
|
|
|
(step: any) => step.target && step.target.name === 'test-volume',
|
|
|
|
),
|
|
|
|
).to.have.lengthOf(1);
|
|
|
|
|
|
|
|
expect(
|
|
|
|
createVolumeSteps.filter(
|
|
|
|
(step: any) => step.target && step.target.name === 'test-volume-2',
|
|
|
|
),
|
|
|
|
).to.have.lengthOf(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
// We don't remove volumes until the end
|
2023-06-15 02:50:26 +00:00
|
|
|
it('should not infer a volume remove step when the app is still referenced', () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
const current = createApp({
|
|
|
|
volumes: [
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('test-volume', 1, 'deadbeef'),
|
|
|
|
Volume.fromComposeObject('test-volume-2', 1, 'deadbeef'),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
2021-08-25 21:50:34 +00:00
|
|
|
volumes: [Volume.fromComposeObject('test-volume-2', 1, 'deadbeef')],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
expectNoStep('removeVolume', steps);
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
it('should correctly infer volume recreation steps', () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
const current = createApp({
|
2021-08-25 21:50:34 +00:00
|
|
|
volumes: [Volume.fromComposeObject('test-volume', 1, 'deadbeef')],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
volumes: [
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('test-volume', 1, 'deadbeef', {
|
2021-05-05 20:32:31 +00:00
|
|
|
labels: { test: 'test' },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// First step should create a volume removal step
|
2023-06-15 02:50:26 +00:00
|
|
|
const stepsForRemoval = current.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
defaultContext,
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
|
|
|
|
const [removalStep] = expectSteps('removeVolume', stepsForRemoval);
|
|
|
|
expect(removalStep)
|
|
|
|
.to.have.property('current')
|
2021-08-25 21:50:34 +00:00
|
|
|
.that.has.property('name')
|
|
|
|
.that.equals('test-volume');
|
|
|
|
expect(removalStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.that.has.property('appId')
|
|
|
|
.that.equals(1);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
// we are assuming that after the execution steps the current state of the
|
|
|
|
// app will look like this
|
|
|
|
const intermediate = createApp({
|
|
|
|
volumes: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
// This test is extra since we have already tested that the volume gets created
|
2023-06-15 02:50:26 +00:00
|
|
|
const stepsForCreation = intermediate.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
defaultContext,
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
const [creationStep] = expectSteps('createVolume', stepsForCreation);
|
|
|
|
|
|
|
|
expect(creationStep)
|
|
|
|
.to.have.property('target')
|
|
|
|
.that.has.property('config')
|
|
|
|
.that.deep.includes({
|
2021-08-25 21:50:34 +00:00
|
|
|
labels: {
|
|
|
|
'io.balena.supervised': 'true',
|
|
|
|
'io.balena.app-uuid': 'deadbeef',
|
|
|
|
test: 'test',
|
|
|
|
},
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should kill dependencies of a volume before changing config', async () => {
|
|
|
|
const current = createApp({
|
2021-08-27 19:22:04 +00:00
|
|
|
services: [
|
|
|
|
await createService({
|
2023-03-07 22:32:10 +00:00
|
|
|
serviceName: 'test',
|
2021-08-03 23:12:47 +00:00
|
|
|
composition: { volumes: ['test-volume:/data'] },
|
2021-08-27 19:22:04 +00:00
|
|
|
}),
|
|
|
|
],
|
2021-08-25 21:50:34 +00:00
|
|
|
volumes: [Volume.fromComposeObject('test-volume', 1, 'deadbeef')],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
2021-08-27 19:22:04 +00:00
|
|
|
services: [
|
|
|
|
await createService({
|
2023-03-07 22:32:10 +00:00
|
|
|
serviceName: 'test',
|
2021-08-03 23:12:47 +00:00
|
|
|
composition: { volumes: ['test-volume:/data'] },
|
2021-08-27 19:22:04 +00:00
|
|
|
}),
|
|
|
|
],
|
2021-05-05 20:32:31 +00:00
|
|
|
volumes: [
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('test-volume', 1, 'deadbeef', {
|
2021-05-05 20:32:31 +00:00
|
|
|
labels: { test: 'test' },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Calculate steps
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
const [killStep] = expectSteps('kill', steps);
|
|
|
|
expect(killStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.that.deep.includes({ serviceName: 'test' });
|
|
|
|
});
|
|
|
|
|
2023-06-15 01:06:24 +00:00
|
|
|
it('should correctly infer to remove an app volumes when the app is being removed', () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
const current = createApp({
|
2021-08-25 21:50:34 +00:00
|
|
|
volumes: [Volume.fromComposeObject('test-volume', 1, 'deadbeef')],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
|
2023-06-15 01:06:24 +00:00
|
|
|
const steps = current.stepsToRemoveApp(defaultContext);
|
2021-05-05 20:32:31 +00:00
|
|
|
const [removeVolumeStep] = expectSteps('removeVolume', steps);
|
|
|
|
|
|
|
|
expect(removeVolumeStep).to.have.property('current').that.deep.includes({
|
|
|
|
name: 'test-volume',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not output a kill step for a service which is already stopping when changing a volume', async () => {
|
2021-08-27 19:22:04 +00:00
|
|
|
const service = await createService({
|
2023-03-07 22:32:10 +00:00
|
|
|
serviceName: 'test',
|
2021-08-03 23:12:47 +00:00
|
|
|
composition: { volumes: ['test-volume:/data'] },
|
2021-08-27 19:22:04 +00:00
|
|
|
});
|
2021-05-05 20:32:31 +00:00
|
|
|
service.status = 'Stopping';
|
|
|
|
const current = createApp({
|
|
|
|
services: [service],
|
2021-08-25 21:50:34 +00:00
|
|
|
volumes: [Volume.fromComposeObject('test-volume', 1, 'deadbeef')],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [service],
|
|
|
|
volumes: [
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('test-volume', 1, 'deadbeef', {
|
2021-05-05 20:32:31 +00:00
|
|
|
labels: { test: 'test' },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
expectNoStep('kill', steps);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate the correct step sequence for a volume purge request', async () => {
|
|
|
|
const service = await createService({
|
2021-08-25 23:25:47 +00:00
|
|
|
appId: 1,
|
|
|
|
appUuid: 'deadbeef',
|
2021-05-05 20:32:31 +00:00
|
|
|
image: 'test-image',
|
2021-08-03 23:12:47 +00:00
|
|
|
composition: { volumes: ['db-volume:/data'] },
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
2021-08-25 23:25:47 +00:00
|
|
|
const volume = Volume.fromComposeObject('db-volume', 1, 'deadbeef');
|
2021-05-05 20:32:31 +00:00
|
|
|
const contextWithImages = {
|
|
|
|
...defaultContext,
|
|
|
|
...{
|
|
|
|
availableImages: [
|
2021-07-23 16:48:27 +00:00
|
|
|
createImage({
|
2021-05-05 20:32:31 +00:00
|
|
|
appId: service.appId,
|
|
|
|
name: 'test-image',
|
2021-07-23 16:48:27 +00:00
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// Temporarily set target services & volumes to empty, as in doPurge
|
|
|
|
const intermediateTarget = createApp({
|
|
|
|
services: [],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Generate initial state with one service & one volume
|
|
|
|
const current = createApp({
|
|
|
|
services: [service],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
volumes: [volume],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Step 1: kill
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
contextWithImages,
|
|
|
|
intermediateTarget,
|
|
|
|
);
|
|
|
|
expectSteps('kill', steps);
|
|
|
|
|
|
|
|
// Step 2: noop (service is stopping)
|
|
|
|
service.status = 'Stopping';
|
2023-06-15 02:50:26 +00:00
|
|
|
const secondStageSteps = current.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
contextWithImages,
|
|
|
|
intermediateTarget,
|
|
|
|
);
|
|
|
|
expectSteps('noop', secondStageSteps);
|
|
|
|
expect(secondStageSteps).to.have.length(1);
|
|
|
|
|
|
|
|
// No steps, simulate container removal & explicit volume removal as in doPurge
|
|
|
|
const currentWithServiceRemoved = createApp({
|
|
|
|
services: [],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
volumes: [volume],
|
|
|
|
});
|
|
|
|
expect(
|
2023-06-15 02:50:26 +00:00
|
|
|
currentWithServiceRemoved.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
contextWithImages,
|
|
|
|
intermediateTarget,
|
|
|
|
),
|
|
|
|
).to.have.length(0);
|
|
|
|
|
|
|
|
// Simulate volume removal
|
|
|
|
const currentWithVolumesRemoved = createApp({
|
|
|
|
services: [],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
volumes: [],
|
|
|
|
});
|
|
|
|
|
2021-08-27 19:22:04 +00:00
|
|
|
// Step 3: createVolume
|
2021-05-05 20:32:31 +00:00
|
|
|
service.status = 'Running';
|
|
|
|
const target = createApp({
|
|
|
|
services: [service],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
volumes: [volume],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
2022-09-19 15:33:52 +00:00
|
|
|
const recreateVolumeSteps =
|
2023-06-15 02:50:26 +00:00
|
|
|
currentWithVolumesRemoved.nextStepsForAppUpdate(
|
2022-09-19 15:33:52 +00:00
|
|
|
contextWithImages,
|
|
|
|
target,
|
|
|
|
);
|
2021-08-27 19:22:04 +00:00
|
|
|
|
|
|
|
expect(recreateVolumeSteps).to.have.length(1);
|
|
|
|
expectSteps('createVolume', recreateVolumeSteps);
|
|
|
|
|
|
|
|
// Final step: start service
|
|
|
|
const currentWithVolumeRecreated = createApp({
|
|
|
|
services: [],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-08-27 19:22:04 +00:00
|
|
|
volumes: [volume],
|
|
|
|
});
|
|
|
|
|
2022-09-19 15:33:52 +00:00
|
|
|
const createServiceSteps =
|
2023-06-15 02:50:26 +00:00
|
|
|
currentWithVolumeRecreated.nextStepsForAppUpdate(
|
2022-09-19 15:33:52 +00:00
|
|
|
contextWithImages,
|
|
|
|
target,
|
|
|
|
);
|
2021-08-27 19:22:04 +00:00
|
|
|
expectSteps('start', createServiceSteps);
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('network state behavior', () => {
|
2023-06-15 02:50:26 +00:00
|
|
|
it('should correctly infer a network create step', () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
const current = createApp({ networks: [] });
|
|
|
|
const target = createApp({
|
2021-08-25 23:25:47 +00:00
|
|
|
networks: [Network.fromComposeObject('default', 1, 'deadbeef', {})],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
const [createNetworkStep] = expectSteps('createNetwork', steps);
|
|
|
|
expect(createNetworkStep).to.have.property('target').that.deep.includes({
|
|
|
|
name: 'default',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
it('should correctly infer a network remove step', () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
const current = createApp({
|
2021-08-25 23:25:47 +00:00
|
|
|
networks: [
|
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {}),
|
|
|
|
],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({ networks: [], isTarget: true });
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
const [removeNetworkStep] = expectSteps('removeNetwork', steps);
|
|
|
|
|
|
|
|
expect(removeNetworkStep).to.have.property('current').that.deep.includes({
|
|
|
|
name: 'test-network',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
it('should correctly remove default duplicate networks', () => {
|
2023-02-10 19:13:38 +00:00
|
|
|
const current = createApp({
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK, DEFAULT_NETWORK],
|
2023-02-10 19:13:38 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
networks: [],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2023-02-10 19:13:38 +00:00
|
|
|
|
|
|
|
const [removeNetworkStep] = expectSteps('removeNetwork', steps);
|
|
|
|
|
|
|
|
expect(removeNetworkStep).to.have.property('current').that.deep.includes({
|
|
|
|
name: 'default',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
it('should correctly remove duplicate networks', () => {
|
2023-02-10 19:13:38 +00:00
|
|
|
const current = createApp({
|
|
|
|
networks: [
|
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {}),
|
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {}),
|
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
networks: [
|
|
|
|
// The target is a single network
|
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {}),
|
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2023-02-10 19:13:38 +00:00
|
|
|
|
|
|
|
const [removeNetworkStep] = expectSteps('removeNetwork', steps);
|
|
|
|
|
|
|
|
expect(removeNetworkStep).to.have.property('current').that.deep.includes({
|
|
|
|
name: 'test-network',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
it('should ignore the duplicates if there are changes already', () => {
|
2023-02-10 19:13:38 +00:00
|
|
|
const current = createApp({
|
|
|
|
networks: [
|
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {}),
|
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
networks: [
|
|
|
|
// The target is a single network
|
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {
|
|
|
|
config_only: true,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2023-02-10 19:13:38 +00:00
|
|
|
const [removeNetworkStep] = expectSteps('removeNetwork', steps);
|
|
|
|
|
|
|
|
expect(removeNetworkStep).to.have.property('current').that.deep.includes({
|
|
|
|
name: 'test-network',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// This should never happen because there can never be a service that is refencing
|
|
|
|
// a network that has a duplicate
|
|
|
|
it('should generate service kill steps if there are duplicate networks', async () => {
|
|
|
|
const current = createApp({
|
|
|
|
appUuid: 'deadbeef',
|
|
|
|
networks: [
|
2023-03-07 22:32:10 +00:00
|
|
|
DEFAULT_NETWORK,
|
2023-02-10 19:13:38 +00:00
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {}),
|
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {}),
|
|
|
|
],
|
|
|
|
services: [
|
|
|
|
await createService({
|
|
|
|
appId: 1,
|
|
|
|
appUuid: 'deadbeef',
|
2023-03-07 22:32:10 +00:00
|
|
|
serviceName: 'test',
|
2023-02-10 19:13:38 +00:00
|
|
|
composition: { networks: ['test-network'] },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
networks: [
|
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {}),
|
|
|
|
],
|
|
|
|
services: [
|
|
|
|
await createService({
|
|
|
|
appId: 1,
|
|
|
|
appUuid: 'deadbeef',
|
2023-03-07 22:32:10 +00:00
|
|
|
serviceName: 'test',
|
2023-02-10 19:13:38 +00:00
|
|
|
composition: { networks: ['test-network'] },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2023-02-10 19:13:38 +00:00
|
|
|
|
|
|
|
const [removeNetworkStep] = expectSteps('kill', steps);
|
|
|
|
|
|
|
|
expect(removeNetworkStep).to.have.property('current').that.deep.includes({
|
|
|
|
serviceName: 'test',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
it('should correctly infer more than one network removal step', () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
const current = createApp({
|
|
|
|
networks: [
|
2021-08-25 23:25:47 +00:00
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {}),
|
|
|
|
Network.fromComposeObject('test-network-2', 1, 'deadbeef', {}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
const target = createApp({ networks: [], isTarget: true });
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
const [first, second] = expectSteps('removeNetwork', steps, 2);
|
|
|
|
|
|
|
|
expect(first).to.have.property('current').that.deep.includes({
|
|
|
|
name: 'test-network',
|
|
|
|
});
|
|
|
|
expect(second).to.have.property('current').that.deep.includes({
|
|
|
|
name: 'test-network-2',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
it('should correctly infer a network recreation step', () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
const current = createApp({
|
2021-08-25 23:25:47 +00:00
|
|
|
networks: [
|
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {}),
|
|
|
|
],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
networks: [
|
2021-08-25 23:25:47 +00:00
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {
|
2021-05-05 20:32:31 +00:00
|
|
|
labels: { TEST: 'TEST' },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const stepsForRemoval = current.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
defaultContext,
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
|
|
|
|
const [removeStep] = expectSteps('removeNetwork', stepsForRemoval);
|
|
|
|
expect(removeStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.that.deep.includes({ name: 'test-network' });
|
|
|
|
|
|
|
|
// We assume that the intermediate state looks like this
|
|
|
|
const intermediate = createApp({
|
|
|
|
networks: [],
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const stepsForCreation = intermediate.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
defaultContext,
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
|
|
|
|
const [createNetworkStep] = expectSteps(
|
|
|
|
'createNetwork',
|
|
|
|
stepsForCreation,
|
|
|
|
1,
|
|
|
|
2, // The update will also generate a step for the default network but we don't care about that
|
|
|
|
);
|
|
|
|
expect(createNetworkStep)
|
|
|
|
.to.have.property('target')
|
|
|
|
.that.deep.includes({ name: 'test-network' });
|
|
|
|
|
|
|
|
expect(createNetworkStep)
|
|
|
|
.to.have.property('target')
|
|
|
|
.that.has.property('config')
|
2021-08-25 23:25:47 +00:00
|
|
|
.that.deep.includes({
|
|
|
|
labels: { TEST: 'TEST', 'io.balena.app-id': '1' },
|
|
|
|
});
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should kill dependencies of networks before removing', async () => {
|
|
|
|
const current = createApp({
|
2021-08-25 23:25:47 +00:00
|
|
|
appUuid: 'deadbeef',
|
2021-08-03 23:12:47 +00:00
|
|
|
services: [
|
|
|
|
await createService({
|
2021-08-25 23:25:47 +00:00
|
|
|
appId: 1,
|
|
|
|
appUuid: 'deadbeef',
|
2023-03-07 22:32:10 +00:00
|
|
|
serviceName: 'test',
|
2021-08-25 23:25:47 +00:00
|
|
|
composition: { networks: ['test-network'] },
|
2021-08-03 23:12:47 +00:00
|
|
|
}),
|
|
|
|
],
|
2021-08-25 23:25:47 +00:00
|
|
|
networks: [
|
|
|
|
Network.fromComposeObject('test-network', 1, 'deadbeef', {}),
|
|
|
|
],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
2021-08-25 23:25:47 +00:00
|
|
|
appUuid: 'deadbeef',
|
2023-03-07 22:32:10 +00:00
|
|
|
services: [
|
|
|
|
await createService({ appUuid: 'deadbeef', serviceName: 'test' }),
|
|
|
|
],
|
2021-05-05 20:32:31 +00:00
|
|
|
networks: [],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-04-25 18:43:15 +00:00
|
|
|
const availableImages = [createImage({ appUuid: 'deadbeef' })];
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(
|
2023-04-25 18:43:15 +00:00
|
|
|
{ ...defaultContext, availableImages },
|
|
|
|
target,
|
|
|
|
);
|
2021-05-05 20:32:31 +00:00
|
|
|
const [killStep] = expectSteps('kill', steps);
|
|
|
|
expect(killStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.that.deep.includes({ serviceName: 'test' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should kill dependencies of networks before changing config', async () => {
|
|
|
|
const current = createApp({
|
2021-08-03 23:12:47 +00:00
|
|
|
services: [
|
|
|
|
await createService({
|
2023-03-07 22:32:10 +00:00
|
|
|
serviceName: 'test',
|
2021-08-25 23:25:47 +00:00
|
|
|
composition: { networks: ['test-network'] },
|
2021-08-03 23:12:47 +00:00
|
|
|
}),
|
|
|
|
],
|
2021-08-25 23:25:47 +00:00
|
|
|
networks: [Network.fromComposeObject('test-network', 1, 'appuuid', {})],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
2021-08-03 23:12:47 +00:00
|
|
|
services: [
|
|
|
|
await createService({
|
2023-03-07 22:32:10 +00:00
|
|
|
serviceName: 'test',
|
2021-08-03 23:12:47 +00:00
|
|
|
composition: { networks: { 'test-network': {} } },
|
|
|
|
}),
|
|
|
|
],
|
2021-05-05 20:32:31 +00:00
|
|
|
networks: [
|
2021-08-25 23:25:47 +00:00
|
|
|
Network.fromComposeObject('test-network', 1, 'appuuid', {
|
2021-05-05 20:32:31 +00:00
|
|
|
labels: { test: 'test' },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
const [killStep] = expectSteps('kill', steps);
|
|
|
|
|
|
|
|
expect(killStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.that.deep.includes({ serviceName: 'test' });
|
|
|
|
|
|
|
|
// We shouldn't try to remove the network until we have gotten rid of the dependencies
|
|
|
|
expectNoStep('removeNetwork', steps);
|
|
|
|
});
|
|
|
|
|
2023-04-25 18:43:15 +00:00
|
|
|
it('should always kill dependencies of networks before removing', async () => {
|
|
|
|
const current = createApp({
|
|
|
|
services: [
|
|
|
|
// The device for some reason is already running some services
|
|
|
|
// of the new release, but we need to kill it anyways
|
|
|
|
await createService({
|
|
|
|
image: 'alpine',
|
|
|
|
serviceName: 'one',
|
|
|
|
commit: 'deadca1f',
|
|
|
|
composition: { command: 'sleep infinity', networks: ['default'] },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
networks: [Network.fromComposeObject('default', 1, 'appuuid', {})],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
|
|
|
await createService({
|
|
|
|
image: 'alpine',
|
|
|
|
serviceName: 'one',
|
|
|
|
commit: 'deadca1f',
|
|
|
|
composition: { command: 'sleep infinity', networks: ['default'] },
|
|
|
|
}),
|
|
|
|
await createService({
|
|
|
|
image: 'alpine',
|
|
|
|
serviceName: 'two',
|
|
|
|
commit: 'deadca1f',
|
|
|
|
composition: {
|
|
|
|
command: 'sh -c "echo two && sleep infinity"',
|
|
|
|
networks: ['default'],
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
networks: [
|
|
|
|
Network.fromComposeObject('default', 1, 'appuuid', {
|
|
|
|
labels: { test: 'test' },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const availableImages = [
|
|
|
|
createImage({ appId: 1, serviceName: 'one', name: 'alpine' }),
|
|
|
|
createImage({ appId: 1, serviceName: 'two', name: 'alpine' }),
|
|
|
|
];
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(
|
2023-04-25 18:43:15 +00:00
|
|
|
{ ...defaultContext, availableImages },
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
const [killStep] = expectSteps('kill', steps);
|
|
|
|
|
|
|
|
expect(killStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.that.deep.includes({ serviceName: 'one' });
|
|
|
|
|
|
|
|
// We shouldn't try to remove the network until we have gotten rid of the dependencies
|
|
|
|
expectNoStep('removeNetwork', steps);
|
|
|
|
});
|
|
|
|
|
2023-04-21 22:19:34 +00:00
|
|
|
it('should kill dependencies of networks before updating between releases', async () => {
|
|
|
|
const current = createApp({
|
|
|
|
services: [
|
|
|
|
await createService({
|
|
|
|
image: 'alpine',
|
|
|
|
serviceName: 'one',
|
|
|
|
commit: 'deadbeef',
|
|
|
|
composition: { command: 'sleep infinity', networks: ['default'] },
|
|
|
|
}),
|
|
|
|
await createService({
|
|
|
|
image: 'alpine',
|
|
|
|
serviceName: 'two',
|
|
|
|
commit: 'deadbeef',
|
|
|
|
composition: { command: 'sleep infinity', networks: ['default'] },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
networks: [Network.fromComposeObject('default', 1, 'appuuid', {})],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
|
|
|
await createService({
|
|
|
|
image: 'alpine',
|
|
|
|
serviceName: 'one',
|
|
|
|
commit: 'deadca1f',
|
|
|
|
composition: { command: 'sleep infinity', networks: ['default'] },
|
|
|
|
}),
|
|
|
|
await createService({
|
|
|
|
image: 'alpine',
|
|
|
|
serviceName: 'two',
|
|
|
|
commit: 'deadca1f',
|
|
|
|
composition: {
|
|
|
|
command: 'sh -c "echo two && sleep infinity"',
|
|
|
|
networks: ['default'],
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
networks: [
|
|
|
|
Network.fromComposeObject('default', 1, 'appuuid', {
|
|
|
|
labels: { test: 'test' },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const availableImages = [
|
|
|
|
createImage({ appId: 1, serviceName: 'one', name: 'alpine' }),
|
|
|
|
createImage({ appId: 1, serviceName: 'two', name: 'alpine' }),
|
|
|
|
];
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(
|
2023-04-21 22:19:34 +00:00
|
|
|
{ ...defaultContext, availableImages },
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
expectSteps('kill', steps, 2);
|
|
|
|
|
|
|
|
expect(steps.map((s) => (s as any).current.serviceName)).to.have.members([
|
|
|
|
'one',
|
|
|
|
'two',
|
|
|
|
]);
|
|
|
|
|
|
|
|
// We shouldn't try to remove the network until we have gotten rid of the dependencies
|
|
|
|
expectNoStep('removeNetwork', steps);
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
it('should create the default network if it does not exist', () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
const current = createApp({ networks: [] });
|
|
|
|
const target = createApp({ networks: [], isTarget: true });
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
// A default network should always be created
|
|
|
|
const [createNetworkStep] = expectSteps('createNetwork', steps);
|
|
|
|
expect(createNetworkStep)
|
|
|
|
.to.have.property('target')
|
|
|
|
.that.deep.includes({ name: 'default' });
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
it('should not create the default network if it already exists', () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
const current = createApp({
|
2021-08-25 23:25:47 +00:00
|
|
|
networks: [Network.fromComposeObject('default', 1, 'deadbeef', {})],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({ networks: [], isTarget: true });
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
// The network should not be created again
|
|
|
|
expectNoStep('createNetwork', steps);
|
|
|
|
});
|
2022-11-15 04:22:45 +00:00
|
|
|
|
|
|
|
it('should create a config-only network if network_mode is host for all services', async () => {
|
|
|
|
const svcOne = await createService({
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'one',
|
|
|
|
composition: { network_mode: 'host' },
|
|
|
|
});
|
|
|
|
const svcTwo = await createService({
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'two',
|
|
|
|
composition: { network_mode: 'host' },
|
|
|
|
});
|
|
|
|
const current = createApp({
|
|
|
|
services: [svcOne, svcTwo],
|
|
|
|
networks: [],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [svcOne, svcTwo],
|
|
|
|
networks: [],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2022-11-15 04:22:45 +00:00
|
|
|
|
|
|
|
const [createNetworkStep] = expectSteps('createNetwork', steps);
|
|
|
|
expect(createNetworkStep)
|
|
|
|
.to.have.property('target')
|
|
|
|
.that.has.property('config')
|
|
|
|
.that.deep.includes({ configOnly: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not create a config-only network if network_mode: host is not specified for any service', async () => {
|
|
|
|
const svcOne = await createService({
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'one',
|
|
|
|
composition: { network_mode: 'host' },
|
|
|
|
});
|
|
|
|
const svcTwo = await createService({
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'two',
|
|
|
|
});
|
|
|
|
const current = createApp({
|
|
|
|
services: [svcOne, svcTwo],
|
|
|
|
networks: [],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [svcOne, svcTwo],
|
|
|
|
networks: [],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2022-11-15 04:22:45 +00:00
|
|
|
|
|
|
|
const [createNetworkStep] = expectSteps('createNetwork', steps);
|
|
|
|
expect(createNetworkStep)
|
|
|
|
.to.have.property('target')
|
|
|
|
.that.has.property('config')
|
|
|
|
.that.deep.includes({ configOnly: false });
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
it('should create a config-only network if there are no services in the app', () => {
|
2022-11-15 04:22:45 +00:00
|
|
|
const current = createApp({});
|
|
|
|
const target = createApp({ isTarget: true });
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2022-11-15 04:22:45 +00:00
|
|
|
|
|
|
|
const [createNetworkStep] = expectSteps('createNetwork', steps);
|
|
|
|
expect(createNetworkStep)
|
|
|
|
.to.have.property('target')
|
|
|
|
.that.has.property('config')
|
|
|
|
.that.deep.includes({ configOnly: true });
|
|
|
|
});
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('service state behavior', () => {
|
2021-08-25 23:25:47 +00:00
|
|
|
it('should create a kill step for a service which is no longer referenced', async () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
const current = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({ appId: 1, serviceName: 'main' }),
|
|
|
|
await createService({ appId: 1, serviceName: 'aux' }),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
2021-07-23 16:48:27 +00:00
|
|
|
services: [await createService({ appId: 1, serviceName: 'main' })],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-03-07 22:32:10 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(
|
|
|
|
{
|
|
|
|
...defaultContext,
|
|
|
|
// With default download-then-kill, a kill should be inferred
|
|
|
|
// once all images in target state are available
|
|
|
|
availableImages: [createImage({ serviceName: 'main' })],
|
|
|
|
},
|
|
|
|
target,
|
|
|
|
);
|
2021-05-05 20:32:31 +00:00
|
|
|
const [killStep] = expectSteps('kill', steps);
|
|
|
|
expect(killStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.to.deep.include({ serviceName: 'aux' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should emit a noop when a service which is no longer referenced is already stopping', async () => {
|
|
|
|
const current = createApp({
|
|
|
|
services: [
|
|
|
|
await createService(
|
2021-07-23 16:48:27 +00:00
|
|
|
{ serviceName: 'main' },
|
|
|
|
{ state: { status: 'Stopping' } },
|
2021-05-05 20:32:31 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const target = createApp({ services: [], isTarget: true });
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
expectSteps('noop', steps);
|
|
|
|
|
|
|
|
// Kill was already emitted for this service
|
|
|
|
expectNoStep('kill', steps);
|
|
|
|
});
|
|
|
|
|
2022-08-02 21:34:25 +00:00
|
|
|
it('should emit a noop while waiting on a stopping service', async () => {
|
|
|
|
const current = createApp({
|
|
|
|
services: [
|
|
|
|
await createService(
|
|
|
|
{ serviceName: 'main', running: true },
|
|
|
|
{ state: { status: 'Stopping' } },
|
|
|
|
),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [await createService({ serviceName: 'main', running: true })],
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2022-08-02 21:34:25 +00:00
|
|
|
expectSteps('noop', steps);
|
|
|
|
});
|
|
|
|
|
2021-05-05 20:32:31 +00:00
|
|
|
it('should remove a dead container that is still referenced in the target state', async () => {
|
|
|
|
const current = createApp({
|
|
|
|
services: [
|
|
|
|
await createService(
|
2021-07-23 16:48:27 +00:00
|
|
|
{ serviceName: 'main' },
|
|
|
|
{ state: { status: 'Dead' } },
|
2021-05-05 20:32:31 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
2021-07-23 16:48:27 +00:00
|
|
|
services: [await createService({ serviceName: 'main' })],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
const [removeStep] = expectSteps('remove', steps);
|
|
|
|
|
|
|
|
expect(removeStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.to.deep.include({ serviceName: 'main' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove a dead container that is not referenced in the target state', async () => {
|
|
|
|
const current = createApp({
|
|
|
|
services: [
|
|
|
|
await createService(
|
2021-07-23 16:48:27 +00:00
|
|
|
{ serviceName: 'main' },
|
|
|
|
{ state: { status: 'Dead' } },
|
2021-05-05 20:32:31 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const target = createApp({ services: [], isTarget: true });
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
const [removeStep] = expectSteps('remove', steps);
|
|
|
|
|
|
|
|
expect(removeStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.to.deep.include({ serviceName: 'main' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should emit a noop when a service has an image downloading', async () => {
|
|
|
|
const current = createApp({ services: [] });
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({ image: 'main-image', serviceName: 'main' }),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(
|
2021-07-23 16:48:27 +00:00
|
|
|
{ ...defaultContext, ...{ downloading: ['main-image'] } },
|
2021-05-05 20:32:31 +00:00
|
|
|
target,
|
|
|
|
);
|
|
|
|
expectSteps('noop', steps);
|
|
|
|
expectNoStep('fetch', steps);
|
|
|
|
});
|
|
|
|
|
2024-02-27 00:45:56 +00:00
|
|
|
it('should emit a takeLock followed by an updateMetadata step when a service has not changed but the release has', async () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
const current = createApp({
|
|
|
|
services: [
|
2024-02-27 00:45:56 +00:00
|
|
|
await createService({
|
|
|
|
serviceName: 'main',
|
|
|
|
appId: 1,
|
|
|
|
commit: 'old-release',
|
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
2024-02-27 00:45:56 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
2024-02-27 00:45:56 +00:00
|
|
|
await createService({
|
|
|
|
serviceName: 'main',
|
|
|
|
appId: 1,
|
|
|
|
commit: 'new-release',
|
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
2024-02-27 00:45:56 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2024-02-27 00:45:56 +00:00
|
|
|
// Take lock before updating metadata
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2024-02-27 00:45:56 +00:00
|
|
|
const [takeLockStep] = expectSteps('takeLock', steps);
|
|
|
|
expect(takeLockStep).to.have.property('appId').that.equals(1);
|
|
|
|
expect(takeLockStep)
|
|
|
|
.to.have.property('services')
|
|
|
|
.that.deep.equals(['main']);
|
|
|
|
|
|
|
|
// Infer updateMetadata after locks are taken
|
|
|
|
const steps2 = current.nextStepsForAppUpdate(
|
|
|
|
{
|
|
|
|
...defaultContext,
|
|
|
|
locksTaken: new LocksTakenMap([{ appId: 1, services: ['main'] }]),
|
|
|
|
},
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
const [updateMetadataStep] = expectSteps('updateMetadata', steps2);
|
2021-05-05 20:32:31 +00:00
|
|
|
expect(updateMetadataStep)
|
|
|
|
.to.have.property('current')
|
2021-07-23 16:48:27 +00:00
|
|
|
.to.deep.include({ serviceName: 'main', commit: 'old-release' });
|
2021-05-05 20:32:31 +00:00
|
|
|
expect(updateMetadataStep)
|
|
|
|
.to.have.property('target')
|
2021-07-23 16:48:27 +00:00
|
|
|
.to.deep.include({ serviceName: 'main', commit: 'new-release' });
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should stop a container which has `running: false` as its target', async () => {
|
|
|
|
const current = createApp({
|
2021-07-23 16:48:27 +00:00
|
|
|
services: [await createService({ serviceName: 'main' })],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({ running: false, serviceName: 'main' }),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
const [stopStep] = expectSteps('stop', steps);
|
|
|
|
expect(stopStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.to.deep.include({ serviceName: 'main' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not try to start a container which has exited and has restart policy of no', async () => {
|
|
|
|
// Container is a "run once" type of service so it has exitted.
|
|
|
|
const current = createApp({
|
|
|
|
services: [
|
|
|
|
await createService(
|
2021-08-03 23:12:47 +00:00
|
|
|
{ composition: { restart: 'no' }, running: false },
|
2021-05-05 20:32:31 +00:00
|
|
|
{ state: { containerId: 'run_once' } },
|
|
|
|
),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Now test that another start step is not added on this service
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
|
|
|
await createService(
|
2021-08-03 23:12:47 +00:00
|
|
|
{ composition: { restart: 'no' }, running: false },
|
2021-05-05 20:32:31 +00:00
|
|
|
{ state: { containerId: 'run_once' } },
|
|
|
|
),
|
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
expectNoStep('start', steps);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should recreate a container if the target configuration changes', async () => {
|
|
|
|
const contextWithImages = {
|
|
|
|
...defaultContext,
|
|
|
|
...{
|
|
|
|
availableImages: [
|
2021-07-23 16:48:27 +00:00
|
|
|
createImage({ appId: 1, serviceName: 'main', name: 'main-image' }),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const current = createApp({
|
2021-07-23 16:48:27 +00:00
|
|
|
services: [await createService({ appId: 1, serviceName: 'main' })],
|
2021-05-05 20:32:31 +00:00
|
|
|
// Default network was already created
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'main',
|
2021-08-03 23:12:47 +00:00
|
|
|
composition: { privileged: true },
|
2021-07-23 16:48:27 +00:00
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// should see a 'stop'
|
2023-06-15 02:50:26 +00:00
|
|
|
const stepsToIntermediate = current.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
contextWithImages,
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
const [killStep] = expectSteps('kill', stepsToIntermediate);
|
|
|
|
expect(killStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.that.deep.includes({ serviceName: 'main' });
|
|
|
|
|
|
|
|
// assume the intermediate step has already removed the app
|
|
|
|
const intermediate = createApp({
|
|
|
|
services: [],
|
|
|
|
// Default network was already created
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// now should see a 'start'
|
2023-06-15 02:50:26 +00:00
|
|
|
const stepsToTarget = intermediate.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
contextWithImages,
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
|
|
|
|
const [startStep] = expectSteps('start', stepsToTarget);
|
|
|
|
expect(startStep)
|
|
|
|
.to.have.property('target')
|
|
|
|
.that.deep.includes({ serviceName: 'main' });
|
|
|
|
expect(startStep)
|
|
|
|
.to.have.property('target')
|
|
|
|
.that.has.property('config')
|
|
|
|
.that.deep.includes({ privileged: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not start a container when it depends on a service which is being installed', async () => {
|
|
|
|
const availableImages = [
|
2021-07-23 16:48:27 +00:00
|
|
|
createImage({ appId: 1, serviceName: 'main', name: 'main-image' }),
|
|
|
|
createImage({ appId: 1, serviceName: 'dep', name: 'dep-image' }),
|
2021-05-05 20:32:31 +00:00
|
|
|
];
|
|
|
|
const contextWithImages = { ...defaultContext, ...{ availableImages } };
|
|
|
|
|
|
|
|
const current = createApp({
|
|
|
|
services: [
|
|
|
|
await createService(
|
|
|
|
{
|
2021-07-23 16:48:27 +00:00
|
|
|
running: false,
|
2021-05-05 20:32:31 +00:00
|
|
|
appId: 1,
|
|
|
|
serviceName: 'dep',
|
2021-07-23 16:48:27 +00:00
|
|
|
},
|
|
|
|
{
|
2021-05-05 20:32:31 +00:00
|
|
|
state: {
|
|
|
|
status: 'Installing',
|
|
|
|
containerId: 'dep-id',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'main',
|
2021-08-03 23:12:47 +00:00
|
|
|
composition: {
|
|
|
|
depends_on: ['dep'],
|
|
|
|
},
|
2021-07-23 16:48:27 +00:00
|
|
|
}),
|
|
|
|
await createService({
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'dep',
|
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const stepsToIntermediate = current.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
contextWithImages,
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Only one start step and it should be that of the 'dep' service
|
|
|
|
const [startStep] = expectSteps('start', stepsToIntermediate);
|
|
|
|
expect(startStep)
|
|
|
|
.to.have.property('target')
|
|
|
|
.that.deep.includes({ serviceName: 'dep' });
|
|
|
|
|
|
|
|
// we now make our current state have the 'dep' service as started...
|
|
|
|
const intermediate = createApp({
|
|
|
|
services: [
|
|
|
|
await createService(
|
2021-07-23 16:48:27 +00:00
|
|
|
{ appId: 1, serviceName: 'dep' },
|
|
|
|
{ state: { containerId: 'dep-id' } },
|
2021-05-05 20:32:31 +00:00
|
|
|
),
|
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// we should now see a start for the 'main' service...
|
2023-06-15 02:50:26 +00:00
|
|
|
const stepsToTarget = intermediate.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
{ ...contextWithImages, ...{ containerIds: { dep: 'dep-id' } } },
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
|
|
|
|
const [startMainStep] = expectSteps('start', stepsToTarget);
|
|
|
|
expect(startMainStep)
|
|
|
|
.to.have.property('target')
|
|
|
|
.that.deep.includes({ serviceName: 'main' });
|
|
|
|
});
|
|
|
|
|
2022-04-12 06:05:56 +00:00
|
|
|
it('should not create a start step when all that changes is a running state', async () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
const contextWithImages = {
|
|
|
|
...defaultContext,
|
|
|
|
...{
|
|
|
|
availableImages: [
|
2021-07-23 16:48:27 +00:00
|
|
|
createImage({ appId: 1, name: 'main-image', serviceName: 'main' }),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const current = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({ running: false, serviceName: 'main' }),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
2021-07-23 16:48:27 +00:00
|
|
|
services: [await createService({ serviceName: 'main' })],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(contextWithImages, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2022-04-12 06:05:56 +00:00
|
|
|
// There should be no steps since the engine manages restart policy for stopped containers
|
|
|
|
expect(steps.length).to.equal(0);
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should create a kill step when a service release has to be updated but the strategy is kill-then-download', async () => {
|
|
|
|
const contextWithImages = {
|
|
|
|
...defaultContext,
|
|
|
|
...{
|
|
|
|
availableImages: [
|
2021-07-23 16:48:27 +00:00
|
|
|
createImage({ appId: 1, name: 'main-image', serviceName: 'main' }),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const labels = {
|
|
|
|
'io.balena.update.strategy': 'kill-then-download',
|
|
|
|
};
|
|
|
|
|
|
|
|
const current = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({
|
|
|
|
labels,
|
|
|
|
image: 'main-image',
|
|
|
|
serviceName: 'main',
|
|
|
|
commit: 'old-release',
|
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({
|
|
|
|
labels,
|
|
|
|
image: 'main-image-2',
|
|
|
|
serviceName: 'main',
|
|
|
|
commit: 'new-release',
|
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const stepsToIntermediate = current.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
contextWithImages,
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
|
|
|
|
const [killStep] = expectSteps('kill', stepsToIntermediate);
|
|
|
|
expect(killStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.that.deep.includes({ serviceName: 'main' });
|
|
|
|
|
|
|
|
// assume steps were applied
|
|
|
|
const intermediate = createApp({
|
|
|
|
services: [],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const stepsToTarget = intermediate.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
contextWithImages,
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
|
|
|
|
const [fetchStep] = expectSteps('fetch', stepsToTarget);
|
|
|
|
expect(fetchStep)
|
|
|
|
.to.have.property('image')
|
|
|
|
.that.deep.includes({ name: 'main-image-2' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not infer a kill step with the default strategy if a dependency is not downloaded', async () => {
|
|
|
|
const contextWithImages = {
|
|
|
|
...defaultContext,
|
|
|
|
...{
|
2021-07-23 16:48:27 +00:00
|
|
|
downloading: ['dep-image-2'], // The depended service image is being downloaded
|
2021-05-05 20:32:31 +00:00
|
|
|
availableImages: [
|
2021-07-23 16:48:27 +00:00
|
|
|
createImage({ appId: 1, name: 'main-image', serviceName: 'main' }),
|
|
|
|
createImage({ appId: 1, name: 'dep-image', serviceName: 'dep' }),
|
|
|
|
createImage({
|
2021-05-05 20:32:31 +00:00
|
|
|
appId: 1,
|
|
|
|
name: 'main-image-2',
|
|
|
|
serviceName: 'main',
|
2021-07-23 16:48:27 +00:00
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const current = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({
|
|
|
|
image: 'main-image',
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'main',
|
|
|
|
commit: 'old-release',
|
2021-08-03 23:12:47 +00:00
|
|
|
composition: {
|
|
|
|
depends_on: ['dep'],
|
|
|
|
},
|
2021-07-23 16:48:27 +00:00
|
|
|
}),
|
|
|
|
await createService({
|
|
|
|
image: 'dep-image',
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'dep',
|
|
|
|
commit: 'old-release',
|
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({
|
|
|
|
image: 'main-image-2',
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'main',
|
|
|
|
commit: 'new-release',
|
2021-08-03 23:12:47 +00:00
|
|
|
composition: {
|
|
|
|
depends_on: ['dep'],
|
|
|
|
},
|
2021-07-23 16:48:27 +00:00
|
|
|
}),
|
|
|
|
await createService({
|
|
|
|
image: 'dep-image-2',
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'dep',
|
|
|
|
commit: 'new-release',
|
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// No kill steps should be generated
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(contextWithImages, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
expectNoStep('kill', steps);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create several kill steps as long as there are unmet dependencies', async () => {
|
|
|
|
const contextWithImages = {
|
|
|
|
...defaultContext,
|
|
|
|
...{
|
|
|
|
availableImages: [
|
2021-07-23 16:48:27 +00:00
|
|
|
createImage({ appId: 1, name: 'main-image', serviceName: 'main' }),
|
|
|
|
createImage({
|
2021-05-05 20:32:31 +00:00
|
|
|
appId: 1,
|
|
|
|
name: 'main-image-2',
|
|
|
|
serviceName: 'main',
|
2021-07-23 16:48:27 +00:00
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const current = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({
|
|
|
|
image: 'main-image',
|
|
|
|
serviceName: 'main',
|
|
|
|
commit: 'old-release',
|
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({
|
|
|
|
image: 'main-image-2',
|
2021-05-05 20:32:31 +00:00
|
|
|
// new release as target
|
2021-07-23 16:48:27 +00:00
|
|
|
serviceName: 'main',
|
|
|
|
commit: 'new-release',
|
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const stepsFirstTry = current.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
contextWithImages,
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
|
|
|
|
const [killStep] = expectSteps('kill', stepsFirstTry);
|
|
|
|
expect(killStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.that.deep.includes({ serviceName: 'main' });
|
|
|
|
|
|
|
|
// if at first you don't succeed
|
2023-06-15 02:50:26 +00:00
|
|
|
const stepsSecondTry = current.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
contextWithImages,
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Since current state has not changed, another kill step needs to be generated
|
|
|
|
const [newKillStep] = expectSteps('kill', stepsSecondTry);
|
|
|
|
expect(newKillStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.that.deep.includes({ serviceName: 'main' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create a kill step when a service config has to be updated but the strategy is kill-then-download', async () => {
|
|
|
|
const labels = {
|
|
|
|
'io.balena.update.strategy': 'kill-then-download',
|
|
|
|
};
|
|
|
|
|
|
|
|
const current = createApp({
|
2023-03-07 22:32:10 +00:00
|
|
|
services: [await createService({ serviceName: 'test', labels })],
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
|
|
|
await createService({
|
2023-03-07 22:32:10 +00:00
|
|
|
serviceName: 'test',
|
2021-05-05 20:32:31 +00:00
|
|
|
labels,
|
2021-08-03 23:12:47 +00:00
|
|
|
composition: {
|
|
|
|
privileged: true,
|
|
|
|
},
|
2021-05-05 20:32:31 +00:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
const [killStep] = expectSteps('kill', steps);
|
|
|
|
expect(killStep)
|
|
|
|
.to.have.property('current')
|
|
|
|
.that.deep.includes({ serviceName: 'test' });
|
|
|
|
});
|
|
|
|
|
2021-11-10 20:22:40 +00:00
|
|
|
it('should not start a service when a network it depends on is not ready', async () => {
|
2023-03-07 22:32:10 +00:00
|
|
|
const current = createApp({ networks: [DEFAULT_NETWORK] });
|
2021-05-05 20:32:31 +00:00
|
|
|
const target = createApp({
|
2021-08-03 23:12:47 +00:00
|
|
|
services: [
|
|
|
|
await createService({
|
|
|
|
composition: { networks: ['test'] },
|
|
|
|
appId: 1,
|
|
|
|
}),
|
|
|
|
],
|
2021-08-25 23:25:47 +00:00
|
|
|
networks: [
|
2023-03-07 22:32:10 +00:00
|
|
|
DEFAULT_NETWORK,
|
2021-08-25 23:25:47 +00:00
|
|
|
Network.fromComposeObject('test', 1, 'appuuid', {}),
|
|
|
|
],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
const [createNetworkStep] = expectSteps('createNetwork', steps);
|
|
|
|
expect(createNetworkStep)
|
|
|
|
.to.have.property('target')
|
|
|
|
.that.deep.includes({ name: 'test' });
|
|
|
|
|
|
|
|
// service should not be created yet
|
|
|
|
expectNoStep('start', steps);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create several kill steps as long as there are no unmet dependencies', async () => {
|
|
|
|
const current = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'one',
|
|
|
|
commit: 'old-release',
|
|
|
|
}),
|
|
|
|
await createService({
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'two',
|
|
|
|
commit: 'old-release',
|
|
|
|
}),
|
|
|
|
await createService({
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'three',
|
|
|
|
commit: 'old-release',
|
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'three',
|
|
|
|
commit: 'new-release',
|
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-03-07 22:32:10 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(
|
|
|
|
{
|
|
|
|
...defaultContext,
|
|
|
|
// With default download-then-kill strategy, target images
|
|
|
|
// should all be available before a kill step is inferred
|
|
|
|
availableImages: [createImage({ serviceName: 'three' })],
|
|
|
|
},
|
|
|
|
target,
|
|
|
|
);
|
2021-05-05 20:32:31 +00:00
|
|
|
expectSteps('kill', steps, 2);
|
|
|
|
});
|
2021-11-10 20:22:40 +00:00
|
|
|
|
|
|
|
it('should not infer a kill step with the default strategy before all target images have been downloaded', async () => {
|
|
|
|
const contextWithImages = {
|
|
|
|
...defaultContext,
|
|
|
|
...{
|
|
|
|
downloading: ['other-image-2'], // One of the images is being downloaded
|
|
|
|
availableImages: [
|
|
|
|
createImage({ appId: 1, name: 'main-image', serviceName: 'main' }),
|
|
|
|
createImage({
|
|
|
|
appId: 1,
|
|
|
|
name: 'other-image',
|
|
|
|
serviceName: 'other',
|
|
|
|
}),
|
|
|
|
createImage({
|
|
|
|
appId: 1,
|
|
|
|
name: 'main-image-2',
|
|
|
|
serviceName: 'main',
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const current = createApp({
|
|
|
|
services: [
|
|
|
|
await createService({
|
|
|
|
image: 'main-image',
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'main',
|
|
|
|
commit: 'old-release',
|
|
|
|
}),
|
|
|
|
await createService({
|
|
|
|
image: 'other-image',
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'other',
|
|
|
|
commit: 'old-release',
|
|
|
|
}),
|
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-11-10 20:22:40 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
|
|
|
await createService({
|
|
|
|
image: 'main-image-2',
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'main',
|
|
|
|
commit: 'new-release',
|
|
|
|
}),
|
|
|
|
await createService({
|
|
|
|
image: 'other-image-2',
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'other',
|
|
|
|
commit: 'new-release',
|
|
|
|
}),
|
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-11-10 20:22:40 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// No kill steps should be generated
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(contextWithImages, target);
|
2021-11-10 20:22:40 +00:00
|
|
|
expectNoStep('kill', steps);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not infer a start step before all target images have been downloaded', async () => {
|
|
|
|
const contextWithImages = {
|
|
|
|
...defaultContext,
|
|
|
|
...{
|
|
|
|
downloading: ['other-image'], // One of the images is being downloaded
|
|
|
|
availableImages: [
|
|
|
|
createImage({ appId: 1, name: 'main-image', serviceName: 'main' }),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const current = createApp({
|
|
|
|
services: [],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-11-10 20:22:40 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
|
|
|
await createService({
|
|
|
|
image: 'main-image',
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'main',
|
|
|
|
commit: 'new-release',
|
|
|
|
}),
|
|
|
|
await createService({
|
|
|
|
image: 'other-image',
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'other',
|
|
|
|
commit: 'new-release',
|
|
|
|
}),
|
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-11-10 20:22:40 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// No kill steps should be generated
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(contextWithImages, target);
|
2021-11-10 20:22:40 +00:00
|
|
|
expectNoStep('start', steps);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should infer a start step only when target images have been downloaded', async () => {
|
|
|
|
const contextWithImages = {
|
|
|
|
...defaultContext,
|
|
|
|
...{
|
|
|
|
downloading: [], // One of the images is being downloaded
|
|
|
|
availableImages: [
|
|
|
|
createImage({ appId: 1, name: 'main-image', serviceName: 'main' }),
|
|
|
|
createImage({
|
|
|
|
appId: 1,
|
|
|
|
name: 'other-image',
|
|
|
|
serviceName: 'other',
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const current = createApp({
|
|
|
|
services: [],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-11-10 20:22:40 +00:00
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
|
|
|
await createService({
|
|
|
|
image: 'main-image',
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'main',
|
|
|
|
commit: 'new-release',
|
|
|
|
}),
|
|
|
|
await createService({
|
|
|
|
image: 'other-image',
|
|
|
|
appId: 1,
|
|
|
|
serviceName: 'other',
|
|
|
|
commit: 'new-release',
|
|
|
|
}),
|
|
|
|
],
|
2023-03-07 22:32:10 +00:00
|
|
|
networks: [DEFAULT_NETWORK],
|
2021-11-10 20:22:40 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// No kill steps should be generated
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(contextWithImages, target);
|
2021-11-10 20:22:40 +00:00
|
|
|
expectSteps('start', steps, 2);
|
|
|
|
});
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('image state behavior', () => {
|
|
|
|
it('should emit a fetch step when an image has not been downloaded for a service', async () => {
|
|
|
|
const current = createApp({ services: [] });
|
|
|
|
const target = createApp({
|
2021-07-23 16:48:27 +00:00
|
|
|
services: [await createService({ serviceName: 'main' })],
|
2021-05-05 20:32:31 +00:00
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
const [fetchStep] = expectSteps('fetch', steps);
|
|
|
|
expect(fetchStep)
|
|
|
|
.to.have.property('image')
|
|
|
|
.that.deep.includes({ serviceName: 'main' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not infer a fetch step when the download is already in progress', async () => {
|
|
|
|
const contextWithDownloading = {
|
|
|
|
...defaultContext,
|
|
|
|
...{
|
2021-07-23 16:48:27 +00:00
|
|
|
downloading: ['image2'],
|
2021-05-05 20:32:31 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
const current = createApp({ services: [] });
|
|
|
|
const target = createApp({
|
|
|
|
services: [
|
2021-07-23 16:48:27 +00:00
|
|
|
await createService({ image: 'image2', serviceName: 'main' }),
|
2021-05-05 20:32:31 +00:00
|
|
|
],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(
|
2021-05-05 20:32:31 +00:00
|
|
|
contextWithDownloading,
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
expectNoStep('fetch', steps);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not infer a kill step with the default strategy if a dependency is not downloaded', async () => {
|
|
|
|
const current = createApp({
|
|
|
|
services: [await createService({ image: 'image1' })],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services: [await createService({ image: 'image2' })],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
2023-06-15 02:50:26 +00:00
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
const [fetchStep] = expectSteps('fetch', steps);
|
|
|
|
expect(fetchStep)
|
|
|
|
.to.have.property('image')
|
|
|
|
.that.deep.includes({ name: 'image2' });
|
|
|
|
|
|
|
|
expectNoStep('kill', steps);
|
|
|
|
});
|
|
|
|
});
|
2024-02-08 05:16:45 +00:00
|
|
|
|
|
|
|
describe('update lock state behavior', () => {
|
|
|
|
it('should infer a releaseLock step if there are locks to be released before settling target state', async () => {
|
|
|
|
const services = [
|
|
|
|
await createService({ serviceName: 'server' }),
|
|
|
|
await createService({ serviceName: 'client' }),
|
|
|
|
];
|
|
|
|
const current = createApp({
|
|
|
|
services,
|
|
|
|
networks: [DEFAULT_NETWORK],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services,
|
|
|
|
networks: [DEFAULT_NETWORK],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const steps = current.nextStepsForAppUpdate(
|
|
|
|
{
|
|
|
|
...defaultContext,
|
|
|
|
locksTaken: new LocksTakenMap([
|
|
|
|
{ appId: 1, services: ['server', 'client'] },
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
const [releaseLockStep] = expectSteps('releaseLock', steps, 1);
|
|
|
|
expect(releaseLockStep).to.have.property('appId').that.equals(1);
|
|
|
|
|
|
|
|
// Even if not all the locks are taken, releaseLock should be inferred
|
|
|
|
const steps2 = current.nextStepsForAppUpdate(
|
|
|
|
{
|
|
|
|
...defaultContext,
|
|
|
|
locksTaken: new LocksTakenMap([{ appId: 1, services: ['server'] }]),
|
|
|
|
},
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
const [releaseLockStep2] = expectSteps('releaseLock', steps2, 1);
|
|
|
|
expect(releaseLockStep2).to.have.property('appId').that.equals(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not infer a releaseLock step if there are no locks to be released', async () => {
|
|
|
|
const services = [
|
|
|
|
await createService({ serviceName: 'server' }),
|
|
|
|
await createService({ serviceName: 'client' }),
|
|
|
|
];
|
|
|
|
const current = createApp({
|
|
|
|
services,
|
|
|
|
networks: [DEFAULT_NETWORK],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services,
|
|
|
|
networks: [DEFAULT_NETWORK],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const steps = current.nextStepsForAppUpdate(defaultContext, target);
|
|
|
|
expect(steps).to.have.length(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should infer a releaseLock step for the current appId only', async () => {
|
|
|
|
const services = [
|
|
|
|
await createService({ serviceName: 'server' }),
|
|
|
|
await createService({ serviceName: 'client' }),
|
|
|
|
];
|
|
|
|
const current = createApp({
|
|
|
|
services,
|
|
|
|
networks: [DEFAULT_NETWORK],
|
|
|
|
});
|
|
|
|
const target = createApp({
|
|
|
|
services,
|
|
|
|
networks: [DEFAULT_NETWORK],
|
|
|
|
isTarget: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const steps = current.nextStepsForAppUpdate(
|
|
|
|
{
|
|
|
|
...defaultContext,
|
|
|
|
locksTaken: new LocksTakenMap([
|
|
|
|
{ appId: 1, services: ['server', 'client'] },
|
|
|
|
{ appId: 2, services: ['main'] },
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
target,
|
|
|
|
);
|
|
|
|
const [releaseLockStep] = expectSteps('releaseLock', steps, 1);
|
|
|
|
expect(releaseLockStep).to.have.property('appId').that.equals(1);
|
|
|
|
});
|
|
|
|
});
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|