2020-07-29 11:46:02 +00:00
|
|
|
import * as _ from 'lodash';
|
2020-12-18 20:10:04 +00:00
|
|
|
import rewire = require('rewire');
|
2020-05-08 20:04:21 +00:00
|
|
|
|
2022-08-17 23:35:08 +00:00
|
|
|
import { unlinkAll } from '~/lib/fs-utils';
|
|
|
|
import * as serviceManager from '~/src/compose/service-manager';
|
|
|
|
import * as volumeManager from '~/src/compose/volume-manager';
|
|
|
|
import * as commitStore from '~/src/compose/commit';
|
|
|
|
import * as config from '~/src/config';
|
|
|
|
import * as db from '~/src/db';
|
2022-09-26 22:19:37 +00:00
|
|
|
import * as v1 from '~/src/device-api/v1';
|
2022-09-27 02:22:18 +00:00
|
|
|
import * as v2 from '~/src/device-api/v2';
|
2022-08-17 23:35:08 +00:00
|
|
|
import * as deviceState from '~/src/device-state';
|
2022-09-16 01:51:17 +00:00
|
|
|
import SupervisorAPI from '~/src/device-api';
|
2024-02-29 22:00:39 +00:00
|
|
|
import type { Service } from '~/src/compose/service';
|
|
|
|
import type { Image } from '~/src/compose/images';
|
2020-05-08 20:04:21 +00:00
|
|
|
|
2022-08-17 23:35:08 +00:00
|
|
|
const apiBinder = rewire('~/src/api-binder');
|
2020-12-18 20:10:04 +00:00
|
|
|
|
2020-05-08 20:04:21 +00:00
|
|
|
const DB_PATH = './test/data/supervisor-api.sqlite';
|
2020-11-19 01:37:34 +00:00
|
|
|
|
2020-05-15 19:18:20 +00:00
|
|
|
// Holds all values used for stubbing
|
|
|
|
const STUBBED_VALUES = {
|
2020-11-04 17:11:19 +00:00
|
|
|
commits: {
|
|
|
|
1: '7fc9c5bea8e361acd49886fe6cc1e1cd',
|
|
|
|
2: '4e380136c2cf56cd64197d51a1ab263a',
|
2020-05-15 19:18:20 +00:00
|
|
|
},
|
|
|
|
services: [
|
|
|
|
{
|
|
|
|
appId: 1,
|
|
|
|
imageId: 1111,
|
|
|
|
status: 'Running',
|
|
|
|
releaseId: 99999,
|
|
|
|
createdAt: new Date('2020-04-25T04:15:23.111Z'),
|
|
|
|
serviceName: 'main',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
appId: 1,
|
|
|
|
imageId: 2222,
|
|
|
|
status: 'Running',
|
|
|
|
releaseId: 99999,
|
|
|
|
createdAt: new Date('2020-04-25T04:15:23.111Z'),
|
|
|
|
serviceName: 'redis',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
appId: 2,
|
|
|
|
imageId: 3333,
|
2020-07-29 11:46:02 +00:00
|
|
|
containerId: 'abc123',
|
2020-05-15 19:18:20 +00:00
|
|
|
status: 'Running',
|
|
|
|
releaseId: 77777,
|
|
|
|
createdAt: new Date('2020-05-15T19:33:06.088Z'),
|
|
|
|
serviceName: 'main',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
images: [],
|
|
|
|
networks: [],
|
|
|
|
volumes: [],
|
|
|
|
};
|
|
|
|
|
2020-11-19 01:37:34 +00:00
|
|
|
// Useful for creating mock services in the ServiceManager
|
|
|
|
const mockService = (overrides?: Partial<Service>) => {
|
|
|
|
return {
|
|
|
|
...{
|
|
|
|
appId: 1658654,
|
|
|
|
status: 'Running',
|
|
|
|
serviceName: 'main',
|
|
|
|
imageId: 2885946,
|
|
|
|
serviceId: 640681,
|
|
|
|
containerId:
|
|
|
|
'f93d386599d1b36e71272d46ad69770cff333842db04e2e4c64dda7b54da07c6',
|
|
|
|
createdAt: '2020-11-13T20:29:44.143Z',
|
|
|
|
config: {
|
|
|
|
labels: {},
|
|
|
|
},
|
2020-11-26 06:11:47 +00:00
|
|
|
toDockerContainer: () => {
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
extraNetworksToJoin: () => {
|
|
|
|
return [];
|
|
|
|
},
|
2020-12-03 21:19:44 +00:00
|
|
|
isEqualConfig: (service: Service) => {
|
|
|
|
return _.isEqual(
|
|
|
|
_.pick(mockService, ['imageId', 'containerId', 'serviceId']),
|
|
|
|
_.pick(service, ['imageId', 'containerId', 'serviceId']),
|
|
|
|
);
|
|
|
|
},
|
2020-11-19 01:37:34 +00:00
|
|
|
},
|
|
|
|
...overrides,
|
|
|
|
} as Service;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Useful for creating mock images that are returned from Images.getStatus
|
|
|
|
const mockImage = (overrides?: Partial<Image>) => {
|
|
|
|
return {
|
|
|
|
...{
|
2022-09-19 15:33:52 +00:00
|
|
|
name: 'registry2.balena-cloud.com/v2/e2bf6410ffc30850e96f5071cdd1dca8@sha256:e2e87a8139b8fc14510095b210ad652d7d5badcc64fdc686cbf749d399fba15e',
|
2020-11-19 01:37:34 +00:00
|
|
|
appId: 1658654,
|
|
|
|
serviceName: 'main',
|
|
|
|
imageId: 2885946,
|
|
|
|
dockerImageId:
|
|
|
|
'sha256:4502983d72e2c72bc292effad1b15b49576da3801356f47fd275ba274d409c1a',
|
|
|
|
status: 'Downloaded',
|
|
|
|
downloadProgress: null,
|
|
|
|
},
|
|
|
|
...overrides,
|
|
|
|
} as Image;
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockedOptions = {
|
|
|
|
listenPort: 54321,
|
|
|
|
timeout: 30000,
|
|
|
|
};
|
|
|
|
|
2020-05-15 19:18:20 +00:00
|
|
|
/**
|
|
|
|
* THIS MOCKED API CONTAINS STUBS THAT MIGHT CAUSE UNEXPECTED RESULTS
|
|
|
|
* IF YOU WANT TO ADD/MODIFY STUBS THAT INVOLVE API OPERATIONS
|
|
|
|
* AND MULTIPLE TEST CASES WILL USE THEM THEN ADD THEM HERE
|
|
|
|
* OTHERWISE YOU CAN ADD STUBS ON A PER TEST CASE BASIS
|
|
|
|
*
|
2020-07-29 15:55:22 +00:00
|
|
|
* EXAMPLE: We stub ApplicationManager so there is at least 1 running app
|
2020-05-15 19:18:20 +00:00
|
|
|
*
|
|
|
|
* You can see all the stubbed values convientiely in STUBBED_VALUES.
|
|
|
|
*
|
|
|
|
*/
|
2020-05-08 20:04:21 +00:00
|
|
|
|
2022-01-26 21:56:08 +00:00
|
|
|
async function create(
|
|
|
|
healthchecks = [deviceState.healthcheck, apiBinder.healthcheck],
|
|
|
|
): Promise<SupervisorAPI> {
|
2020-05-08 20:04:21 +00:00
|
|
|
// Get SupervisorAPI construct options
|
2020-07-21 15:25:47 +00:00
|
|
|
await createAPIOpts();
|
|
|
|
|
2020-05-15 19:18:20 +00:00
|
|
|
// Stub functions
|
|
|
|
setupStubs();
|
2020-08-13 12:25:39 +00:00
|
|
|
|
2020-05-08 20:04:21 +00:00
|
|
|
// Create SupervisorAPI
|
|
|
|
const api = new SupervisorAPI({
|
2022-09-27 02:22:18 +00:00
|
|
|
routers: [v1.router, v2.router],
|
2022-01-26 21:56:08 +00:00
|
|
|
healthchecks,
|
2020-05-08 20:04:21 +00:00
|
|
|
});
|
2020-06-05 13:16:43 +00:00
|
|
|
|
|
|
|
const macAddress = await config.get('macAddress');
|
|
|
|
deviceState.reportCurrentState({
|
|
|
|
mac_address: macAddress,
|
|
|
|
});
|
|
|
|
|
2020-05-08 20:04:21 +00:00
|
|
|
// Return SupervisorAPI that is not listening yet
|
|
|
|
return api;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanUp(): Promise<void> {
|
2021-04-26 19:54:04 +00:00
|
|
|
// Clean up test data
|
|
|
|
await unlinkAll(DB_PATH);
|
2020-05-15 19:18:20 +00:00
|
|
|
// Restore created SinonStubs
|
|
|
|
return restoreStubs();
|
2020-05-08 20:04:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 15:25:47 +00:00
|
|
|
async function createAPIOpts(): Promise<void> {
|
2022-09-06 18:03:23 +00:00
|
|
|
await db.initialized();
|
|
|
|
await deviceState.initialized();
|
2020-07-21 15:25:47 +00:00
|
|
|
|
2020-05-15 19:18:20 +00:00
|
|
|
// Initialize and set values for mocked Config
|
2020-06-02 13:29:05 +00:00
|
|
|
await initConfig();
|
2020-05-08 20:04:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 13:29:05 +00:00
|
|
|
async function initConfig(): Promise<void> {
|
|
|
|
// Initialize this config
|
2022-09-06 18:03:23 +00:00
|
|
|
await config.initialized();
|
2020-09-16 14:19:23 +00:00
|
|
|
|
2020-05-15 19:18:20 +00:00
|
|
|
// Set a currentCommit
|
2020-11-04 17:11:19 +00:00
|
|
|
for (const [id, commit] of Object.entries(STUBBED_VALUES.commits)) {
|
|
|
|
await commitStore.upsertCommitForApp(parseInt(id, 10), commit);
|
|
|
|
}
|
2020-05-15 19:18:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:10:04 +00:00
|
|
|
// TO-DO: Create a cleaner way to restore previous values.
|
2020-06-11 10:43:03 +00:00
|
|
|
const originalVolGetAll = volumeManager.getAllByAppId;
|
2021-07-28 16:35:21 +00:00
|
|
|
const originalSvcGetStatus = serviceManager.getState;
|
2022-09-26 22:19:37 +00:00
|
|
|
const originalReadyForUpdates = apiBinder.isReadyForUpdates();
|
2020-07-29 11:46:02 +00:00
|
|
|
|
2020-05-15 19:18:20 +00:00
|
|
|
function setupStubs() {
|
2020-12-18 20:10:04 +00:00
|
|
|
apiBinder.__set__('readyForUpdates', true);
|
2020-06-11 10:05:43 +00:00
|
|
|
// @ts-expect-error Assigning to a RO property
|
2020-06-15 10:31:26 +00:00
|
|
|
volumeManager.getAllByAppId = async () => STUBBED_VALUES.volumes;
|
2020-06-11 10:43:03 +00:00
|
|
|
// @ts-expect-error Assigning to a RO property
|
2021-07-28 16:35:21 +00:00
|
|
|
serviceManager.getState = async () => STUBBED_VALUES.services;
|
2020-05-15 19:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function restoreStubs() {
|
2020-12-18 20:10:04 +00:00
|
|
|
apiBinder.__set__('readyForUpdates', originalReadyForUpdates);
|
2020-06-11 10:05:43 +00:00
|
|
|
// @ts-expect-error Assigning to a RO property
|
2020-06-11 10:43:03 +00:00
|
|
|
volumeManager.getAllByAppId = originalVolGetAll;
|
2020-06-15 10:31:26 +00:00
|
|
|
// @ts-expect-error Assigning to a RO property
|
2021-07-28 16:35:21 +00:00
|
|
|
serviceManager.getState = originalSvcGetStatus;
|
2020-05-15 19:18:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 01:37:34 +00:00
|
|
|
export = {
|
|
|
|
create,
|
|
|
|
cleanUp,
|
|
|
|
STUBBED_VALUES,
|
|
|
|
mockService,
|
|
|
|
mockImage,
|
|
|
|
mockedOptions,
|
|
|
|
};
|