balena-supervisor/test/lib/application-state-mock.ts
Felipe Lalanne e04e64763f Improve testing for supervisor composition modules
This PR cleans up testing for supervisor compose modules. It also fixes broken
tests for application manager and removes a lot of dependencies for those tests
on DB and other unnecessary mocks. There are probably a lot of cases that tests
are missing but this should make writing new tests a lot easier.

This PR also creates a new mock dockerode (mockerode) module that should make it
easier to test operations that interact with the engine. All references
to the old mock-dockerode have not yet been removed but that should come
soon in another PR

List of squashed commits:
- Add tests for network create/remove
- Move compose service tests to test/src/compose and reorganize test descriptions
- Add support for image creation to mockerode
- Add additional tests for compose volumes
- Update mockerode so unimplemented fake methods throw. This is to ensure
  tests using mockerode fail if an unimplemented method is used
- Update tests for volume-manager with mockerode
- Update tests for compose/images
- Simplify tests using mockerode
- Clean up compose/app tests
- Create application manager tests

Change-type: minor
2021-07-05 17:50:52 -04:00

86 lines
2.8 KiB
TypeScript

import * as networkManager from '../../src/compose/network-manager';
import * as volumeManager from '../../src/compose/volume-manager';
import * as serviceManager from '../../src/compose/service-manager';
import * as imageManager from '../../src/compose/images';
import Service from '../../src/compose/service';
import Network from '../../src/compose/network';
import Volume from '../../src/compose/volume';
const originalVolGetAll = volumeManager.getAll;
const originalSvcGetAll = serviceManager.getAll;
const originalNetGetAll = networkManager.getAll;
const originalGetDl = imageManager.getDownloadingImageIds;
const originalNeedsClean = imageManager.isCleanupNeeded;
const originalImageAvailable = imageManager.getAvailable;
const originalNetworkReady = networkManager.supervisorNetworkReady;
export function mockManagers(svcs: Service[], vols: Volume[], nets: Network[]) {
// @ts-expect-error Assigning to a RO property
volumeManager.getAll = async () => vols;
// @ts-expect-error Assigning to a RO property
networkManager.getAll = async () => nets;
// @ts-expect-error Assigning to a RO property
serviceManager.getAll = async () => {
// Filter services that are being removed
svcs = svcs.filter((s) => s.status !== 'removing');
// Update Installing containers to Running
svcs = svcs.map((s) => {
if (s.status === 'Installing') {
s.status = 'Running';
}
return s;
});
return svcs;
};
}
function unmockManagers() {
// @ts-expect-error Assigning to a RO property
volumeManager.getAll = originalVolGetAll;
// @ts-expect-error Assigning to a RO property
networkManager.getAll = originalNetGetAll;
// @ts-expect-error Assigning to a RO property
serviceManager.getall = originalSvcGetAll;
}
export function mockImages(
downloading: number[],
cleanup: boolean,
available: imageManager.Image[],
) {
// @ts-expect-error Assigning to a RO property
imageManager.getDownloadingImageIds = () => {
return downloading;
};
// @ts-expect-error Assigning to a RO property
imageManager.isCleanupNeeded = async () => cleanup;
// @ts-expect-error Assigning to a RO property
imageManager.getAvailable = async () => available;
}
function unmockImages() {
// @ts-expect-error Assigning to a RO property
imageManager.getDownloadingImageIds = originalGetDl;
// @ts-expect-error Assigning to a RO property
imageManager.isCleanupNeeded = originalNeedsClean;
// @ts-expect-error Assigning to a RO property
imageManager.getAvailable = originalImageAvailable;
}
export function mockSupervisorNetwork(exists: boolean) {
// @ts-expect-error Assigning to a RO property
networkManager.supervisorNetworkReady = async () => exists;
}
function unmockSupervisorNetwork() {
// @ts-expect-error Assigning to a RO property
networkManager.supervisorNetworkReady = originalNetworkReady;
}
export function unmockAll() {
unmockManagers();
unmockImages();
unmockSupervisorNetwork();
}