2020-07-29 11:46:02 +00:00
|
|
|
import * as _ from 'lodash';
|
2020-05-08 20:04:21 +00:00
|
|
|
import { Router } from 'express';
|
2020-12-18 20:10:04 +00:00
|
|
|
import rewire = require('rewire');
|
2020-05-08 20:04:21 +00:00
|
|
|
|
2021-04-26 19:54:04 +00:00
|
|
|
import { unlinkAll } from '../../src/lib/fs-utils';
|
2020-08-13 12:25:39 +00:00
|
|
|
import * as applicationManager from '../../src/compose/application-manager';
|
2020-06-15 10:31:26 +00:00
|
|
|
import * as serviceManager from '../../src/compose/service-manager';
|
2020-06-11 10:43:03 +00:00
|
|
|
import * as volumeManager from '../../src/compose/volume-manager';
|
2020-11-04 17:11:19 +00:00
|
|
|
import * as commitStore from '../../src/compose/commit';
|
2020-06-02 13:29:05 +00:00
|
|
|
import * as config from '../../src/config';
|
2020-05-28 17:15:33 +00:00
|
|
|
import * as db from '../../src/db';
|
2020-05-08 20:04:21 +00:00
|
|
|
import { createV1Api } from '../../src/device-api/v1';
|
|
|
|
import { createV2Api } from '../../src/device-api/v2';
|
2020-07-21 15:25:47 +00:00
|
|
|
import * as deviceState from '../../src/device-state';
|
2020-05-08 20:04:21 +00:00
|
|
|
import SupervisorAPI from '../../src/supervisor-api';
|
2020-11-19 01:37:34 +00:00
|
|
|
import { Service } from '../../src/compose/service';
|
|
|
|
import { Image } from '../../src/compose/images';
|
2020-05-08 20:04:21 +00:00
|
|
|
|
2020-12-18 20:10:04 +00:00
|
|
|
const apiBinder = rewire('../../src/api-binder');
|
|
|
|
|
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 {
|
|
|
|
...{
|
|
|
|
name:
|
|
|
|
'registry2.balena-cloud.com/v2/e2bf6410ffc30850e96f5071cdd1dca8@sha256:e2e87a8139b8fc14510095b210ad652d7d5badcc64fdc686cbf749d399fba15e',
|
|
|
|
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({
|
2020-08-13 12:25:39 +00:00
|
|
|
routers: [deviceState.router, buildRoutes()],
|
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> {
|
2020-05-28 17:15:33 +00:00
|
|
|
await db.initialized;
|
2020-07-21 15:25:47 +00:00
|
|
|
await deviceState.initialized;
|
|
|
|
|
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
|
|
|
|
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-08-13 12:25:39 +00:00
|
|
|
function buildRoutes(): Router {
|
2020-12-18 20:10:04 +00:00
|
|
|
// Add to existing apiBinder router (it contains additional middleware and endpoints)
|
|
|
|
const router = apiBinder.router;
|
2020-05-08 20:04:21 +00:00
|
|
|
// Add V1 routes
|
2020-08-13 12:25:39 +00:00
|
|
|
createV1Api(applicationManager.router);
|
2020-05-08 20:04:21 +00:00
|
|
|
// Add V2 routes
|
2020-08-13 12:25:39 +00:00
|
|
|
createV2Api(applicationManager.router);
|
2020-05-08 20:04:21 +00:00
|
|
|
// Return modified Router
|
|
|
|
return router;
|
|
|
|
}
|
|
|
|
|
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;
|
2020-12-18 20:10:04 +00:00
|
|
|
const originalReadyForUpdates = apiBinder.__get__('readyForUpdates');
|
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,
|
|
|
|
};
|