2019-06-19 12:56:56 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as Docker from 'dockerode';
|
|
|
|
import * as sinon from 'sinon';
|
2019-06-25 07:16:46 +00:00
|
|
|
|
2020-05-28 17:15:33 +00:00
|
|
|
import * as db from '../src/db';
|
2020-06-02 16:56:58 +00:00
|
|
|
import { docker } from '../src/lib/docker-utils';
|
2019-06-19 12:56:56 +00:00
|
|
|
import LocalModeManager, {
|
|
|
|
EngineSnapshot,
|
|
|
|
EngineSnapshotRecord,
|
|
|
|
} from '../src/local-mode';
|
2019-11-17 01:15:12 +00:00
|
|
|
import ShortStackError from './lib/errors';
|
2019-06-19 12:56:56 +00:00
|
|
|
|
2021-04-08 16:29:06 +00:00
|
|
|
describe.skip('LocalModeManager', () => {
|
2019-06-19 12:56:56 +00:00
|
|
|
let localMode: LocalModeManager;
|
2020-06-02 16:56:58 +00:00
|
|
|
let dockerStub: sinon.SinonStubbedInstance<typeof docker>;
|
2019-06-19 12:56:56 +00:00
|
|
|
|
2019-07-17 13:34:28 +00:00
|
|
|
const supervisorContainerId = 'super-container-1';
|
|
|
|
|
2019-06-25 07:16:46 +00:00
|
|
|
const recordsCount = async () =>
|
|
|
|
await db
|
|
|
|
.models('engineSnapshot')
|
|
|
|
.count('* as cnt')
|
|
|
|
.first()
|
2020-05-15 11:01:51 +00:00
|
|
|
.then((r) => r.cnt);
|
2019-06-25 07:16:46 +00:00
|
|
|
|
2019-06-19 12:56:56 +00:00
|
|
|
// Cleanup the database (to make sure nothing is left since last tests).
|
|
|
|
beforeEach(async () => {
|
|
|
|
await db.models('engineSnapshot').delete();
|
|
|
|
});
|
|
|
|
|
|
|
|
before(async () => {
|
2020-05-28 17:15:33 +00:00
|
|
|
await db.initialized;
|
2019-06-19 12:56:56 +00:00
|
|
|
|
2020-06-02 16:56:58 +00:00
|
|
|
dockerStub = sinon.stub(docker);
|
2019-06-19 12:56:56 +00:00
|
|
|
|
2020-06-08 12:57:31 +00:00
|
|
|
localMode = new LocalModeManager(supervisorContainerId);
|
2020-06-02 16:56:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
sinon.restore();
|
2019-06-19 12:56:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('EngineSnapshot', () => {
|
|
|
|
it('can calculate a diff', () => {
|
|
|
|
const original = new EngineSnapshot(
|
|
|
|
['c1', 'c2'],
|
|
|
|
['i1'],
|
|
|
|
['v1', 'v2'],
|
|
|
|
['nn'],
|
|
|
|
);
|
|
|
|
const newOne = new EngineSnapshot(
|
|
|
|
['c2', 'c3'],
|
|
|
|
['i1', 'i2', 'i3'],
|
|
|
|
['v1'],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
const diff = newOne.diff(original);
|
|
|
|
|
|
|
|
expect(diff.containers).to.deep.equal(['c3']);
|
|
|
|
expect(diff.images).to.deep.equal(['i2', 'i3']);
|
|
|
|
expect(diff.volumes).to.deep.equal([]);
|
|
|
|
expect(diff.networks).to.deep.equal([]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('engine snapshots collection', () => {
|
|
|
|
before(() => {
|
|
|
|
// Stub the engine to return images, containers, volumes, and networks.
|
|
|
|
dockerStub.listImages.returns(
|
|
|
|
Promise.resolve([
|
|
|
|
{ Id: 'image-1' } as Docker.ImageInfo,
|
|
|
|
{ Id: 'image-2' } as Docker.ImageInfo,
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
dockerStub.listContainers.returns(
|
|
|
|
Promise.resolve([
|
|
|
|
{ Id: 'container-1' } as Docker.ContainerInfo,
|
|
|
|
{ Id: 'container-2' } as Docker.ContainerInfo,
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
dockerStub.listVolumes.returns(
|
|
|
|
Promise.resolve({
|
|
|
|
Volumes: [
|
|
|
|
{ Name: 'volume-1' } as Docker.VolumeInspectInfo,
|
|
|
|
{ Name: 'volume-2' } as Docker.VolumeInspectInfo,
|
|
|
|
],
|
|
|
|
Warnings: [],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
dockerStub.listNetworks.returns(
|
|
|
|
Promise.resolve([{ Id: 'network-1' }, { Id: 'network-2' }]),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('collects all necessary engine entities', async () => {
|
|
|
|
const snapshotRecord = await localMode.collectEngineSnapshot();
|
|
|
|
|
|
|
|
expect(snapshotRecord.snapshot.containers).to.include(
|
|
|
|
'container-1',
|
|
|
|
'container-2',
|
|
|
|
);
|
|
|
|
expect(snapshotRecord.snapshot.images).to.include('image-1', 'image-2');
|
|
|
|
expect(snapshotRecord.snapshot.volumes).to.include(
|
|
|
|
'volume-1',
|
|
|
|
'volume-2',
|
|
|
|
);
|
|
|
|
expect(snapshotRecord.snapshot.networks).to.include(
|
|
|
|
'network-1',
|
|
|
|
'network-2',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('marks snapshot with a timestamp', async () => {
|
|
|
|
const startTime = new Date();
|
|
|
|
const snapshotRecord = await localMode.collectEngineSnapshot();
|
|
|
|
expect(snapshotRecord.timestamp).to.be.at.least(startTime);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('local mode switch', () => {
|
2019-07-17 13:34:28 +00:00
|
|
|
// Info returned when we inspect our own container.
|
|
|
|
const supervisorContainer = {
|
|
|
|
Id: 'super-container-1',
|
|
|
|
State: {
|
|
|
|
Status: 'running',
|
|
|
|
Running: true,
|
|
|
|
},
|
|
|
|
Image: 'super-image-1',
|
|
|
|
HostConfig: {
|
2021-04-29 16:02:52 +00:00
|
|
|
ContainerIDFile: '/resin-data/balena-supervisor/container-id',
|
2019-07-17 13:34:28 +00:00
|
|
|
},
|
|
|
|
Mounts: [
|
|
|
|
{
|
|
|
|
Type: 'volume',
|
|
|
|
Name: 'super-volume-1',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
NetworkSettings: {
|
|
|
|
Networks: {
|
|
|
|
'some-name': {
|
|
|
|
NetworkID: 'super-network-1',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-06-19 12:56:56 +00:00
|
|
|
const storeCurrentSnapshot = async (
|
|
|
|
containers: string[],
|
|
|
|
images: string[],
|
|
|
|
volumes: string[],
|
|
|
|
networks: string[],
|
|
|
|
) => {
|
|
|
|
await localMode.storeEngineSnapshot(
|
|
|
|
new EngineSnapshotRecord(
|
|
|
|
new EngineSnapshot(containers, images, volumes, networks),
|
|
|
|
new Date(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-07-17 13:34:28 +00:00
|
|
|
interface EngineStubbedObject {
|
2019-06-19 12:56:56 +00:00
|
|
|
remove(): Promise<void>;
|
2019-07-17 13:34:28 +00:00
|
|
|
inspect(): Promise<any>;
|
2019-06-19 12:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stub get<Object> methods on docker, so we can verify remove calls.
|
2019-07-17 13:34:28 +00:00
|
|
|
const stubEngineObjectMethods = (
|
2019-06-19 12:56:56 +00:00
|
|
|
removeThrows: boolean,
|
2019-07-17 13:34:28 +00:00
|
|
|
): Array<sinon.SinonStubbedInstance<EngineStubbedObject>> => {
|
2019-11-10 20:32:14 +00:00
|
|
|
const resArray: Array<sinon.SinonStubbedInstance<
|
|
|
|
EngineStubbedObject
|
|
|
|
>> = [];
|
2019-06-19 12:56:56 +00:00
|
|
|
|
|
|
|
const stub = <T>(
|
2019-07-17 13:34:28 +00:00
|
|
|
c: sinon.StubbableType<EngineStubbedObject>,
|
2019-06-19 12:56:56 +00:00
|
|
|
type: string,
|
|
|
|
) => {
|
|
|
|
const res = sinon.createStubInstance(c);
|
|
|
|
if (removeThrows) {
|
2019-11-17 01:15:12 +00:00
|
|
|
res.remove.rejects(new ShortStackError(`error removing ${type}`));
|
2019-06-19 12:56:56 +00:00
|
|
|
} else {
|
|
|
|
res.remove.resolves();
|
|
|
|
}
|
2019-07-17 13:34:28 +00:00
|
|
|
|
|
|
|
if (c === Docker.Container) {
|
|
|
|
res.inspect.resolves(supervisorContainer);
|
|
|
|
}
|
|
|
|
|
2019-06-19 12:56:56 +00:00
|
|
|
resArray.push(res);
|
|
|
|
return (res as unknown) as T;
|
|
|
|
};
|
|
|
|
dockerStub.getImage.returns(stub(Docker.Image, 'image'));
|
|
|
|
dockerStub.getContainer.returns(stub(Docker.Container, 'container'));
|
|
|
|
dockerStub.getVolume.returns(stub(Docker.Volume, 'volume'));
|
|
|
|
dockerStub.getNetwork.returns(stub(Docker.Network, 'network'));
|
|
|
|
return resArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
dockerStub.getImage.resetHistory();
|
|
|
|
dockerStub.getContainer.resetHistory();
|
|
|
|
dockerStub.getVolume.resetHistory();
|
|
|
|
dockerStub.getNetwork.resetHistory();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('stores new snapshot on local mode enter', async () => {
|
|
|
|
await localMode.handleLocalModeStateChange(true);
|
|
|
|
|
|
|
|
const snapshot = await localMode.retrieveLatestSnapshot();
|
|
|
|
expect(snapshot).to.be.not.null;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('deletes newly created objects on local mode exit', async () => {
|
2019-07-17 13:34:28 +00:00
|
|
|
const removeStubs = stubEngineObjectMethods(false);
|
2019-06-19 12:56:56 +00:00
|
|
|
// All the objects returned by list<Objects> are not included into this snapshot.
|
2019-07-17 13:34:28 +00:00
|
|
|
// Hence, removal should be called twice (stubbed methods return 2 objects per type).
|
2019-06-19 12:56:56 +00:00
|
|
|
await storeCurrentSnapshot(
|
|
|
|
['previous-container'],
|
|
|
|
['previous-image'],
|
|
|
|
['previous-volume'],
|
|
|
|
['previous-network'],
|
|
|
|
);
|
|
|
|
|
|
|
|
await localMode.handleLocalModeStateChange(false);
|
|
|
|
|
2020-05-15 11:01:51 +00:00
|
|
|
removeStubs.forEach((s) => expect(s.remove.calledTwice).to.be.true);
|
2019-06-19 12:56:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('keeps objects from the previous snapshot on local mode exit', async () => {
|
2019-07-17 13:34:28 +00:00
|
|
|
const removeStubs = stubEngineObjectMethods(false);
|
2019-06-19 12:56:56 +00:00
|
|
|
// With this snapshot, only <object>-2 must be removed from the engine.
|
|
|
|
await storeCurrentSnapshot(
|
|
|
|
['container-1'],
|
|
|
|
['image-1'],
|
|
|
|
['volume-1'],
|
|
|
|
['network-1'],
|
|
|
|
);
|
|
|
|
|
|
|
|
await localMode.handleLocalModeStateChange(false);
|
|
|
|
|
|
|
|
expect(dockerStub.getImage.calledWithExactly('image-2')).to.be.true;
|
|
|
|
expect(dockerStub.getContainer.calledWithExactly('container-2')).to.be
|
|
|
|
.true;
|
|
|
|
expect(dockerStub.getVolume.calledWithExactly('volume-2')).to.be.true;
|
|
|
|
expect(dockerStub.getNetwork.calledWithExactly('network-2')).to.be.true;
|
2020-05-15 11:01:51 +00:00
|
|
|
removeStubs.forEach((s) => expect(s.remove.calledOnce).to.be.true);
|
2019-06-19 12:56:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('logs but consumes cleanup errors on local mode exit', async () => {
|
2019-07-17 13:34:28 +00:00
|
|
|
const removeStubs = stubEngineObjectMethods(true);
|
2019-06-19 12:56:56 +00:00
|
|
|
// This snapshot will cause the logic to remove everything.
|
|
|
|
await storeCurrentSnapshot([], [], [], []);
|
|
|
|
|
|
|
|
// This should not throw.
|
|
|
|
await localMode.handleLocalModeStateChange(false);
|
|
|
|
|
|
|
|
// Even though remove method throws, we still attempt all removals.
|
2020-05-15 11:01:51 +00:00
|
|
|
removeStubs.forEach((s) => expect(s.remove.calledTwice).to.be.true);
|
2019-06-19 12:56:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('skips cleanup without previous snapshot on local mode exit', async () => {
|
2019-07-17 13:34:28 +00:00
|
|
|
const removeStubs = stubEngineObjectMethods(false);
|
2019-06-19 12:56:56 +00:00
|
|
|
|
|
|
|
await localMode.handleLocalModeStateChange(false);
|
|
|
|
|
|
|
|
expect(dockerStub.getImage.notCalled).to.be.true;
|
|
|
|
expect(dockerStub.getContainer.notCalled).to.be.true;
|
|
|
|
expect(dockerStub.getVolume.notCalled).to.be.true;
|
|
|
|
expect(dockerStub.getNetwork.notCalled).to.be.true;
|
2020-05-15 11:01:51 +00:00
|
|
|
removeStubs.forEach((s) => expect(s.remove.notCalled).to.be.true);
|
2019-06-19 12:56:56 +00:00
|
|
|
});
|
2019-06-25 07:16:46 +00:00
|
|
|
|
|
|
|
it('can be awaited', async () => {
|
2019-07-17 13:34:28 +00:00
|
|
|
const removeStubs = stubEngineObjectMethods(false);
|
2019-06-25 07:16:46 +00:00
|
|
|
await storeCurrentSnapshot([], [], [], []);
|
|
|
|
|
|
|
|
// Run asynchronously (like on config change).
|
|
|
|
localMode.startLocalModeChangeHandling(false);
|
|
|
|
|
|
|
|
// Await like it's done by DeviceState.
|
|
|
|
await localMode.switchCompletion();
|
|
|
|
|
2020-05-15 11:01:51 +00:00
|
|
|
removeStubs.forEach((s) => expect(s.remove.calledTwice).to.be.true);
|
2019-06-25 07:16:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('cleans the last snapshot so that nothing is done on restart', async () => {
|
2019-07-17 13:34:28 +00:00
|
|
|
const removeStubs = stubEngineObjectMethods(false);
|
2019-06-25 07:16:46 +00:00
|
|
|
await storeCurrentSnapshot([], [], [], []);
|
|
|
|
|
|
|
|
await localMode.handleLocalModeStateChange(false);
|
|
|
|
|
|
|
|
// The database should be empty now.
|
|
|
|
expect(await recordsCount()).to.be.equal(0);
|
|
|
|
|
|
|
|
// This has to be no ops.
|
|
|
|
await localMode.handleLocalModeStateChange(false);
|
|
|
|
// So our stubs must be called only once.
|
|
|
|
// We delete 2 objects of each type during the first call, so number of getXXX and remove calls is 2.
|
|
|
|
expect(dockerStub.getImage.callCount).to.be.equal(2);
|
2019-07-17 13:34:28 +00:00
|
|
|
expect(dockerStub.getContainer.callCount).to.be.equal(3); // +1 for supervisor inspect call.
|
2019-06-25 07:16:46 +00:00
|
|
|
expect(dockerStub.getVolume.callCount).to.be.equal(2);
|
|
|
|
expect(dockerStub.getNetwork.callCount).to.be.equal(2);
|
2020-05-15 11:01:51 +00:00
|
|
|
removeStubs.forEach((s) => expect(s.remove.callCount).to.be.equal(2));
|
2019-06-25 07:16:46 +00:00
|
|
|
});
|
2019-06-26 11:25:40 +00:00
|
|
|
|
2019-06-27 11:56:43 +00:00
|
|
|
it('skips cleanup in case of data corruption', async () => {
|
2019-07-17 13:34:28 +00:00
|
|
|
const removeStubs = stubEngineObjectMethods(false);
|
2019-06-26 11:25:40 +00:00
|
|
|
|
|
|
|
await db.models('engineSnapshot').insert({
|
|
|
|
snapshot: 'bad json',
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
});
|
|
|
|
|
|
|
|
localMode.startLocalModeChangeHandling(false);
|
|
|
|
await localMode.switchCompletion();
|
|
|
|
|
|
|
|
expect(dockerStub.getImage.notCalled).to.be.true;
|
|
|
|
expect(dockerStub.getContainer.notCalled).to.be.true;
|
|
|
|
expect(dockerStub.getVolume.notCalled).to.be.true;
|
|
|
|
expect(dockerStub.getNetwork.notCalled).to.be.true;
|
2020-05-15 11:01:51 +00:00
|
|
|
removeStubs.forEach((s) => expect(s.remove.notCalled).to.be.true);
|
2019-06-26 11:25:40 +00:00
|
|
|
});
|
2019-07-17 13:34:28 +00:00
|
|
|
|
|
|
|
describe('with supervisor being updated', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
// We make supervisor own resources to match currently listed objects.
|
|
|
|
supervisorContainer.Id = 'container-1';
|
|
|
|
supervisorContainer.Image = 'image-1';
|
|
|
|
supervisorContainer.NetworkSettings.Networks['some-name'].NetworkID =
|
|
|
|
'network-1';
|
|
|
|
supervisorContainer.Mounts[0].Name = 'volume-1';
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not delete its own object', async () => {
|
|
|
|
const removeStubs = stubEngineObjectMethods(false);
|
|
|
|
// All the current engine objects will be new, including container-1 which is the supervisor.
|
|
|
|
await storeCurrentSnapshot(
|
|
|
|
['previous-container'],
|
|
|
|
['previous-image'],
|
|
|
|
['previous-volume'],
|
|
|
|
['previous-network'],
|
|
|
|
);
|
|
|
|
|
|
|
|
await localMode.handleLocalModeStateChange(false);
|
|
|
|
|
|
|
|
// Ensure we inspect our own container.
|
|
|
|
const [, containerStub] = removeStubs;
|
|
|
|
expect(containerStub.inspect.calledOnce).to.be.true;
|
|
|
|
|
|
|
|
// Current engine objects include 2 entities of each type.
|
|
|
|
// Container-1, network-1, image-1, and volume-1 are resources associated with currently running supervisor.
|
|
|
|
// Only xxx-2 objects must be deleted.
|
2020-05-15 11:01:51 +00:00
|
|
|
removeStubs.forEach((s) => expect(s.remove.calledOnce).to.be.true);
|
2019-07-17 13:34:28 +00:00
|
|
|
});
|
|
|
|
});
|
2019-06-19 12:56:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('engine snapshot storage', () => {
|
|
|
|
const recordSample = new EngineSnapshotRecord(
|
|
|
|
new EngineSnapshot(
|
|
|
|
['c1', 'c2'],
|
|
|
|
['i1', 'i2'],
|
|
|
|
['v1', 'v2'],
|
|
|
|
['n1', 'n2'],
|
|
|
|
),
|
|
|
|
new Date(),
|
|
|
|
);
|
|
|
|
|
|
|
|
it('returns null when snapshot is not stored', async () => {
|
|
|
|
expect(await recordsCount()).to.equal(0);
|
|
|
|
const retrieved = await localMode.retrieveLatestSnapshot();
|
|
|
|
expect(retrieved).to.be.null;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('stores snapshot and retrieves from the db', async () => {
|
|
|
|
await localMode.storeEngineSnapshot(recordSample);
|
|
|
|
const retrieved = await localMode.retrieveLatestSnapshot();
|
|
|
|
console.log(retrieved);
|
|
|
|
expect(retrieved).to.be.deep.equal(recordSample);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rewrites previous snapshot', async () => {
|
|
|
|
await localMode.storeEngineSnapshot(recordSample);
|
|
|
|
await localMode.storeEngineSnapshot(recordSample);
|
|
|
|
await localMode.storeEngineSnapshot(recordSample);
|
|
|
|
expect(await recordsCount()).to.equal(1);
|
|
|
|
});
|
2019-06-26 11:25:40 +00:00
|
|
|
|
|
|
|
describe('in case of data corruption', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await db.models('engineSnapshot').delete();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('deals with snapshot data corruption', async () => {
|
|
|
|
// Write bad data to simulate corruption.
|
|
|
|
await db.models('engineSnapshot').insert({
|
|
|
|
snapshot: 'bad json',
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2021-05-07 01:47:10 +00:00
|
|
|
const result = await localMode.retrieveLatestSnapshot();
|
|
|
|
expect(result).to.not.exist;
|
2019-06-26 11:25:40 +00:00
|
|
|
} catch (e) {
|
2021-05-07 01:47:10 +00:00
|
|
|
expect(e.message).to.match(/Cannot parse snapshot data.*"bad json"/);
|
2019-06-26 11:25:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('deals with snapshot timestamp corruption', async () => {
|
|
|
|
// Write bad data to simulate corruption.
|
|
|
|
await db.models('engineSnapshot').insert({
|
|
|
|
snapshot:
|
|
|
|
'{"containers": [], "images": [], "volumes": [], "networks": []}',
|
|
|
|
timestamp: 'bad timestamp',
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2021-05-07 01:47:10 +00:00
|
|
|
const result = await localMode.retrieveLatestSnapshot();
|
|
|
|
expect(result).to.not.exist;
|
2019-06-26 11:25:40 +00:00
|
|
|
} catch (e) {
|
2021-05-07 01:47:10 +00:00
|
|
|
expect(e.message).to.match(
|
|
|
|
/Cannot parse snapshot data.*"bad timestamp"/,
|
|
|
|
);
|
2019-06-26 11:25:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-06-19 12:56:56 +00:00
|
|
|
});
|
|
|
|
});
|