2020-11-19 01:37:34 +00:00
|
|
|
import * as _ from 'lodash';
|
2020-12-03 21:19:44 +00:00
|
|
|
import * as Bluebird from 'bluebird';
|
2020-11-18 18:24:18 +00:00
|
|
|
import { expect } from 'chai';
|
2020-12-18 20:10:04 +00:00
|
|
|
import { stub, spy, SinonStub, SinonSpy } from 'sinon';
|
2020-11-18 18:24:18 +00:00
|
|
|
import * as supertest from 'supertest';
|
|
|
|
|
2020-11-19 01:37:34 +00:00
|
|
|
import * as appMock from './lib/application-state-mock';
|
|
|
|
import * as mockedDockerode from './lib/mocked-dockerode';
|
2020-11-18 18:24:18 +00:00
|
|
|
import mockedAPI = require('./lib/mocked-device-api');
|
2020-11-19 01:37:34 +00:00
|
|
|
import sampleResponses = require('./data/device-api-responses.json');
|
2020-12-18 20:10:04 +00:00
|
|
|
import * as config from '../src/config';
|
2020-11-26 06:11:47 +00:00
|
|
|
import * as logger from '../src/logger';
|
2020-11-19 01:37:34 +00:00
|
|
|
import SupervisorAPI from '../src/supervisor-api';
|
2020-11-18 18:24:18 +00:00
|
|
|
import * as apiBinder from '../src/api-binder';
|
|
|
|
import * as deviceState from '../src/device-state';
|
|
|
|
import * as apiKeys from '../src/lib/api-keys';
|
2020-12-03 21:19:44 +00:00
|
|
|
import * as dbus from '../src//lib/dbus';
|
|
|
|
import * as updateLock from '../src/lib/update-lock';
|
2020-12-18 20:10:04 +00:00
|
|
|
import * as TargetState from '../src/device-state/target-state';
|
2020-12-03 21:19:44 +00:00
|
|
|
import * as targetStateCache from '../src/device-state/target-state-cache';
|
|
|
|
|
|
|
|
import { UpdatesLockedError } from '../src/lib/errors';
|
2020-11-18 18:24:18 +00:00
|
|
|
|
|
|
|
describe('SupervisorAPI [V1 Endpoints]', () => {
|
|
|
|
let api: SupervisorAPI;
|
|
|
|
let healthCheckStubs: SinonStub[];
|
2020-12-03 21:19:44 +00:00
|
|
|
let targetStateCacheMock: SinonStub;
|
2020-11-19 01:37:34 +00:00
|
|
|
const request = supertest(
|
|
|
|
`http://127.0.0.1:${mockedAPI.mockedOptions.listenPort}`,
|
|
|
|
);
|
2020-12-03 21:19:44 +00:00
|
|
|
const services = [
|
|
|
|
{ appId: 2, serviceId: 640681, serviceName: 'one' },
|
|
|
|
{ appId: 2, serviceId: 640682, serviceName: 'two' },
|
|
|
|
{ appId: 2, serviceId: 640683, serviceName: 'three' },
|
2020-11-26 06:11:47 +00:00
|
|
|
];
|
2020-12-03 21:19:44 +00:00
|
|
|
const containers = services.map((service) => mockedAPI.mockService(service));
|
|
|
|
const images = services.map((service) => mockedAPI.mockImage(service));
|
|
|
|
|
|
|
|
let loggerStub: SinonStub;
|
2020-11-26 06:11:47 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// Mock a 3 container release
|
|
|
|
appMock.mockManagers(containers, [], []);
|
|
|
|
appMock.mockImages([], false, images);
|
|
|
|
appMock.mockSupervisorNetwork(true);
|
2020-12-03 21:19:44 +00:00
|
|
|
|
|
|
|
targetStateCacheMock.resolves({
|
|
|
|
appId: 2,
|
|
|
|
commit: 'abcdef2',
|
|
|
|
name: 'test-app2',
|
|
|
|
source: 'https://api.balena-cloud.com',
|
|
|
|
releaseId: 1232,
|
|
|
|
services: JSON.stringify(services),
|
|
|
|
networks: '{}',
|
|
|
|
volumes: '{}',
|
|
|
|
});
|
2020-11-26 06:11:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
// Clear Dockerode actions recorded for each test
|
|
|
|
mockedDockerode.resetHistory();
|
|
|
|
});
|
2020-11-18 18:24:18 +00:00
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
await apiBinder.initialized;
|
|
|
|
await deviceState.initialized;
|
2020-12-03 21:19:44 +00:00
|
|
|
await targetStateCache.initialized;
|
2020-11-18 18:24:18 +00:00
|
|
|
|
|
|
|
// Stub health checks so we can modify them whenever needed
|
|
|
|
healthCheckStubs = [
|
|
|
|
stub(apiBinder, 'healthcheck'),
|
|
|
|
stub(deviceState, 'healthcheck'),
|
|
|
|
];
|
|
|
|
|
|
|
|
// The mockedAPI contains stubs that might create unexpected results
|
|
|
|
// See the module to know what has been stubbed
|
|
|
|
api = await mockedAPI.create();
|
|
|
|
|
|
|
|
// Start test API
|
2020-11-19 01:37:34 +00:00
|
|
|
await api.listen(
|
|
|
|
mockedAPI.mockedOptions.listenPort,
|
|
|
|
mockedAPI.mockedOptions.timeout,
|
|
|
|
);
|
2020-11-18 18:24:18 +00:00
|
|
|
|
2020-12-03 21:19:44 +00:00
|
|
|
// Mock target state cache
|
|
|
|
targetStateCacheMock = stub(targetStateCache, 'getTargetApp');
|
|
|
|
|
2020-11-18 18:24:18 +00:00
|
|
|
// Create a scoped key
|
|
|
|
await apiKeys.initialized;
|
|
|
|
await apiKeys.generateCloudKey();
|
2020-12-03 21:19:44 +00:00
|
|
|
|
|
|
|
// Stub logs for all API methods
|
|
|
|
loggerStub = stub(logger, 'attach');
|
|
|
|
loggerStub.resolves();
|
2020-11-18 18:24:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
try {
|
|
|
|
await api.stop();
|
|
|
|
} catch (e) {
|
|
|
|
if (e.message !== 'Server is not running.') {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Restore healthcheck stubs
|
|
|
|
healthCheckStubs.forEach((hc) => hc.restore);
|
|
|
|
// Remove any test data generated
|
|
|
|
await mockedAPI.cleanUp();
|
2020-11-19 01:37:34 +00:00
|
|
|
appMock.unmockAll();
|
2020-12-03 21:19:44 +00:00
|
|
|
targetStateCacheMock.restore();
|
|
|
|
loggerStub.restore();
|
2020-11-19 01:37:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST /v1/restart', () => {
|
2020-11-26 06:11:47 +00:00
|
|
|
it('restarts all containers in release', async () => {
|
|
|
|
// Perform the test with our mocked release
|
2020-11-19 01:37:34 +00:00
|
|
|
await mockedDockerode.testWithData({ containers, images }, async () => {
|
|
|
|
// Perform test
|
|
|
|
await request
|
|
|
|
.post('/v1/restart')
|
2020-11-26 06:11:47 +00:00
|
|
|
.send({ appId: 2 })
|
2020-11-19 01:37:34 +00:00
|
|
|
.set('Accept', 'application/json')
|
2020-11-26 06:11:47 +00:00
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
2020-11-19 01:37:34 +00:00
|
|
|
.expect(sampleResponses.V1.POST['/restart'].statusCode)
|
|
|
|
.then((response) => {
|
|
|
|
expect(response.body).to.deep.equal(
|
|
|
|
sampleResponses.V1.POST['/restart'].body,
|
|
|
|
);
|
|
|
|
expect(response.text).to.deep.equal(
|
|
|
|
sampleResponses.V1.POST['/restart'].text,
|
|
|
|
);
|
|
|
|
});
|
2020-11-26 06:11:47 +00:00
|
|
|
// Check that mockedDockerode contains 3 stop and start actions
|
2020-11-19 01:37:34 +00:00
|
|
|
const removeSteps = _(mockedDockerode.actions)
|
|
|
|
.pickBy({ name: 'stop' })
|
|
|
|
.map()
|
|
|
|
.value();
|
2020-11-26 06:11:47 +00:00
|
|
|
expect(removeSteps).to.have.lengthOf(3);
|
2020-11-19 01:37:34 +00:00
|
|
|
const startSteps = _(mockedDockerode.actions)
|
|
|
|
.pickBy({ name: 'start' })
|
|
|
|
.map()
|
|
|
|
.value();
|
2020-11-26 06:11:47 +00:00
|
|
|
expect(startSteps).to.have.lengthOf(3);
|
2020-11-19 01:37:34 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('validates request body parameters', async () => {
|
|
|
|
await request
|
|
|
|
.post('/v1/restart')
|
|
|
|
.send({ thing: '' })
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(sampleResponses.V1.POST['/restart [Invalid Body]'].statusCode)
|
|
|
|
.then((response) => {
|
|
|
|
expect(response.body).to.deep.equal(
|
|
|
|
sampleResponses.V1.POST['/restart [Invalid Body]'].body,
|
|
|
|
);
|
|
|
|
expect(response.text).to.deep.equal(
|
|
|
|
sampleResponses.V1.POST['/restart [Invalid Body]'].text,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2020-11-18 18:24:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('GET /v1/healthy', () => {
|
|
|
|
it('returns OK because all checks pass', async () => {
|
|
|
|
// Make all healthChecks pass
|
|
|
|
healthCheckStubs.forEach((hc) => hc.resolves(true));
|
|
|
|
await request
|
|
|
|
.get('/v1/healthy')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(sampleResponses.V1.GET['/healthy'].statusCode)
|
|
|
|
.then((response) => {
|
|
|
|
expect(response.body).to.deep.equal(
|
|
|
|
sampleResponses.V1.GET['/healthy'].body,
|
|
|
|
);
|
|
|
|
expect(response.text).to.deep.equal(
|
|
|
|
sampleResponses.V1.GET['/healthy'].text,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('Fails because some checks did not pass', async () => {
|
2020-12-18 20:10:04 +00:00
|
|
|
healthCheckStubs.forEach((hc) => hc.resolves(false));
|
2020-11-18 18:24:18 +00:00
|
|
|
await request
|
|
|
|
.get('/v1/healthy')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(sampleResponses.V1.GET['/healthy [2]'].statusCode)
|
|
|
|
.then((response) => {
|
|
|
|
expect(response.body).to.deep.equal(
|
|
|
|
sampleResponses.V1.GET['/healthy [2]'].body,
|
|
|
|
);
|
|
|
|
expect(response.text).to.deep.equal(
|
|
|
|
sampleResponses.V1.GET['/healthy [2]'].text,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('GET /v1/apps/:appId', () => {
|
2020-11-26 06:11:47 +00:00
|
|
|
it('does not return information for an application when there is more than 1 container', async () => {
|
|
|
|
// Every test case in this suite has a 3 service release mocked so just make the request
|
|
|
|
await request
|
|
|
|
.get('/v1/apps/2')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(
|
|
|
|
sampleResponses.V1.GET['/apps/2 [Multiple containers running]']
|
|
|
|
.statusCode,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-11-18 18:24:18 +00:00
|
|
|
it('returns information about a specific application', async () => {
|
2020-11-26 06:11:47 +00:00
|
|
|
// Setup single container application
|
|
|
|
const container = mockedAPI.mockService({
|
|
|
|
containerId: 'abc123',
|
|
|
|
appId: 2,
|
|
|
|
releaseId: 77777,
|
|
|
|
});
|
|
|
|
const image = mockedAPI.mockImage({
|
|
|
|
appId: 2,
|
|
|
|
});
|
|
|
|
appMock.mockManagers([container], [], []);
|
|
|
|
appMock.mockImages([], false, [image]);
|
|
|
|
// Make request
|
2020-11-18 18:24:18 +00:00
|
|
|
await request
|
|
|
|
.get('/v1/apps/2')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(sampleResponses.V1.GET['/apps/2'].statusCode)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.then((response) => {
|
|
|
|
expect(response.body).to.deep.equal(
|
|
|
|
sampleResponses.V1.GET['/apps/2'].body,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST /v1/apps/:appId/stop', () => {
|
2020-11-26 06:11:47 +00:00
|
|
|
it('does not allow stopping an application when there is more than 1 container', async () => {
|
|
|
|
// Every test case in this suite has a 3 service release mocked so just make the request
|
2020-11-18 18:24:18 +00:00
|
|
|
await request
|
|
|
|
.post('/v1/apps/2/stop')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
2020-11-26 06:11:47 +00:00
|
|
|
.expect(
|
|
|
|
sampleResponses.V1.GET['/apps/2/stop [Multiple containers running]']
|
|
|
|
.statusCode,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('stops a SPECIFIC application and returns a containerId', async () => {
|
|
|
|
// Setup single container application
|
|
|
|
const container = mockedAPI.mockService({
|
|
|
|
containerId: 'abc123',
|
|
|
|
appId: 2,
|
|
|
|
});
|
|
|
|
const image = mockedAPI.mockImage({
|
|
|
|
appId: 2,
|
|
|
|
});
|
|
|
|
appMock.mockManagers([container], [], []);
|
|
|
|
appMock.mockImages([], false, [image]);
|
|
|
|
// Perform the test with our mocked release
|
|
|
|
await mockedDockerode.testWithData(
|
|
|
|
{ containers: [container], images: [image] },
|
|
|
|
async () => {
|
|
|
|
await request
|
|
|
|
.post('/v1/apps/2/stop')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(sampleResponses.V1.GET['/apps/2/stop'].statusCode)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.then((response) => {
|
|
|
|
expect(response.body).to.deep.equal(
|
|
|
|
sampleResponses.V1.GET['/apps/2/stop'].body,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2020-11-18 18:24:18 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-12-03 21:19:44 +00:00
|
|
|
describe('POST /v1/apps/:appId/start', () => {
|
|
|
|
it('does not allow starting an application when there is more than 1 container', async () => {
|
|
|
|
// Every test case in this suite has a 3 service release mocked so just make the request
|
|
|
|
await request
|
|
|
|
.post('/v1/apps/2/start')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('starts a SPECIFIC application and returns a containerId', async () => {
|
|
|
|
const service = {
|
|
|
|
serviceName: 'main',
|
|
|
|
containerId: 'abc123',
|
|
|
|
appId: 2,
|
|
|
|
serviceId: 640681,
|
|
|
|
};
|
|
|
|
// Setup single container application
|
|
|
|
const container = mockedAPI.mockService(service);
|
|
|
|
const image = mockedAPI.mockImage(service);
|
|
|
|
appMock.mockManagers([container], [], []);
|
|
|
|
appMock.mockImages([], false, [image]);
|
|
|
|
|
|
|
|
// Target state returns single service
|
|
|
|
targetStateCacheMock.resolves({
|
|
|
|
appId: 2,
|
|
|
|
commit: 'abcdef2',
|
|
|
|
name: 'test-app2',
|
|
|
|
source: 'https://api.balena-cloud.com',
|
|
|
|
releaseId: 1232,
|
|
|
|
services: JSON.stringify([service]),
|
|
|
|
volumes: '{}',
|
|
|
|
networks: '{}',
|
|
|
|
});
|
|
|
|
|
|
|
|
// Perform the test with our mocked release
|
|
|
|
await mockedDockerode.testWithData(
|
|
|
|
{ containers: [container], images: [image] },
|
|
|
|
async () => {
|
|
|
|
await request
|
|
|
|
.post('/v1/apps/2/start')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.then((response) => {
|
|
|
|
expect(response.body).to.deep.equal({ containerId: 'abc123' });
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-18 18:24:18 +00:00
|
|
|
describe('GET /v1/device', () => {
|
|
|
|
it('returns MAC address', async () => {
|
|
|
|
const response = await request
|
|
|
|
.get('/v1/device')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(response.body).to.have.property('mac_address').that.is.not.empty;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-12-03 21:19:44 +00:00
|
|
|
describe('POST /v1/reboot', () => {
|
|
|
|
let rebootMock: SinonStub;
|
|
|
|
before(() => {
|
|
|
|
rebootMock = stub(dbus, 'reboot').resolves((() => void 0) as any);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
rebootMock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
rebootMock.resetHistory();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return 202 and reboot if no locks are set', async () => {
|
|
|
|
// Setup single container application
|
|
|
|
const container = mockedAPI.mockService({
|
|
|
|
containerId: 'abc123',
|
|
|
|
appId: 2,
|
|
|
|
releaseId: 77777,
|
|
|
|
});
|
|
|
|
const image = mockedAPI.mockImage({
|
|
|
|
appId: 2,
|
|
|
|
});
|
|
|
|
appMock.mockManagers([container], [], []);
|
|
|
|
appMock.mockImages([], false, [image]);
|
|
|
|
|
|
|
|
const response = await request
|
|
|
|
.post('/v1/reboot')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(202);
|
|
|
|
|
|
|
|
expect(response.body).to.have.property('Data').that.is.not.empty;
|
|
|
|
expect(rebootMock).to.have.been.calledOnce;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return 423 and reject the reboot if no locks are set', async () => {
|
|
|
|
stub(updateLock, 'lock').callsFake((__, opts, fn) => {
|
|
|
|
if (opts.force) {
|
|
|
|
return Bluebird.resolve(fn());
|
|
|
|
}
|
|
|
|
throw new UpdatesLockedError('Updates locked');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Setup single container application
|
|
|
|
const container = mockedAPI.mockService({
|
|
|
|
containerId: 'abc123',
|
|
|
|
appId: 2,
|
|
|
|
releaseId: 77777,
|
|
|
|
});
|
|
|
|
const image = mockedAPI.mockImage({
|
|
|
|
appId: 2,
|
|
|
|
});
|
|
|
|
appMock.mockManagers([container], [], []);
|
|
|
|
appMock.mockImages([], false, [image]);
|
|
|
|
|
|
|
|
const response = await request
|
|
|
|
.post('/v1/reboot')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(423);
|
|
|
|
|
|
|
|
expect(updateLock.lock).to.be.calledOnce;
|
|
|
|
expect(response.body).to.have.property('Error').that.is.not.empty;
|
|
|
|
expect(rebootMock).to.not.have.been.called;
|
|
|
|
|
|
|
|
(updateLock.lock as SinonStub).restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return 202 and reboot if force is set to true', async () => {
|
|
|
|
stub(updateLock, 'lock').callsFake((__, opts, fn) => {
|
|
|
|
if (opts.force) {
|
|
|
|
return Bluebird.resolve(fn());
|
|
|
|
}
|
|
|
|
throw new UpdatesLockedError('Updates locked');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Setup single container application
|
|
|
|
const container = mockedAPI.mockService({
|
|
|
|
containerId: 'abc123',
|
|
|
|
appId: 2,
|
|
|
|
releaseId: 77777,
|
|
|
|
});
|
|
|
|
const image = mockedAPI.mockImage({
|
|
|
|
appId: 2,
|
|
|
|
});
|
|
|
|
appMock.mockManagers([container], [], []);
|
|
|
|
appMock.mockImages([], false, [image]);
|
|
|
|
|
|
|
|
const response = await request
|
|
|
|
.post('/v1/reboot')
|
|
|
|
.send({ force: true })
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(202);
|
|
|
|
|
|
|
|
expect(updateLock.lock).to.be.calledOnce;
|
|
|
|
expect(response.body).to.have.property('Data').that.is.not.empty;
|
|
|
|
expect(rebootMock).to.have.been.calledOnce;
|
|
|
|
|
|
|
|
(updateLock.lock as SinonStub).restore();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST /v1/shutdown', () => {
|
|
|
|
let shutdownMock: SinonStub;
|
|
|
|
before(() => {
|
|
|
|
shutdownMock = stub(dbus, 'shutdown').resolves((() => void 0) as any);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
shutdownMock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return 202 and shutdown if no locks are set', async () => {
|
|
|
|
// Setup single container application
|
|
|
|
const container = mockedAPI.mockService({
|
|
|
|
containerId: 'abc123',
|
|
|
|
appId: 2,
|
|
|
|
releaseId: 77777,
|
|
|
|
});
|
|
|
|
const image = mockedAPI.mockImage({
|
|
|
|
appId: 2,
|
|
|
|
});
|
|
|
|
appMock.mockManagers([container], [], []);
|
|
|
|
appMock.mockImages([], false, [image]);
|
|
|
|
|
|
|
|
const response = await request
|
|
|
|
.post('/v1/shutdown')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(202);
|
|
|
|
|
|
|
|
expect(response.body).to.have.property('Data').that.is.not.empty;
|
|
|
|
expect(shutdownMock).to.have.been.calledOnce;
|
|
|
|
|
|
|
|
shutdownMock.resetHistory();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return 423 and reject the reboot if no locks are set', async () => {
|
|
|
|
stub(updateLock, 'lock').callsFake((__, opts, fn) => {
|
|
|
|
if (opts.force) {
|
|
|
|
return Bluebird.resolve(fn());
|
|
|
|
}
|
|
|
|
throw new UpdatesLockedError('Updates locked');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Setup single container application
|
|
|
|
const container = mockedAPI.mockService({
|
|
|
|
containerId: 'abc123',
|
|
|
|
appId: 2,
|
|
|
|
releaseId: 77777,
|
|
|
|
});
|
|
|
|
const image = mockedAPI.mockImage({
|
|
|
|
appId: 2,
|
|
|
|
});
|
|
|
|
appMock.mockManagers([container], [], []);
|
|
|
|
appMock.mockImages([], false, [image]);
|
|
|
|
|
|
|
|
const response = await request
|
|
|
|
.post('/v1/shutdown')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(423);
|
|
|
|
|
|
|
|
expect(updateLock.lock).to.be.calledOnce;
|
|
|
|
expect(response.body).to.have.property('Error').that.is.not.empty;
|
|
|
|
expect(shutdownMock).to.not.have.been.called;
|
|
|
|
|
|
|
|
(updateLock.lock as SinonStub).restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return 202 and shutdown if force is set to true', async () => {
|
|
|
|
stub(updateLock, 'lock').callsFake((__, opts, fn) => {
|
|
|
|
if (opts.force) {
|
|
|
|
return Bluebird.resolve(fn());
|
|
|
|
}
|
|
|
|
throw new UpdatesLockedError('Updates locked');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Setup single container application
|
|
|
|
const container = mockedAPI.mockService({
|
|
|
|
containerId: 'abc123',
|
|
|
|
appId: 2,
|
|
|
|
releaseId: 77777,
|
|
|
|
});
|
|
|
|
const image = mockedAPI.mockImage({
|
|
|
|
appId: 2,
|
|
|
|
});
|
|
|
|
appMock.mockManagers([container], [], []);
|
|
|
|
appMock.mockImages([], false, [image]);
|
|
|
|
|
|
|
|
const response = await request
|
|
|
|
.post('/v1/shutdown')
|
|
|
|
.send({ force: true })
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(202);
|
|
|
|
|
|
|
|
expect(updateLock.lock).to.be.calledOnce;
|
|
|
|
expect(response.body).to.have.property('Data').that.is.not.empty;
|
|
|
|
expect(shutdownMock).to.have.been.calledOnce;
|
|
|
|
|
|
|
|
(updateLock.lock as SinonStub).restore();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-12-18 20:10:04 +00:00
|
|
|
describe('POST /v1/update', () => {
|
|
|
|
let configStub: SinonStub;
|
|
|
|
let targetUpdateSpy: SinonSpy;
|
|
|
|
|
|
|
|
before(() => {
|
|
|
|
configStub = stub(config, 'get');
|
|
|
|
targetUpdateSpy = spy(TargetState, 'update');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
targetUpdateSpy.resetHistory();
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
configStub.restore();
|
|
|
|
targetUpdateSpy.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 204 with no parameters', async () => {
|
|
|
|
// Stub response for getting instantUpdates
|
|
|
|
configStub.resolves(true);
|
|
|
|
// Make request
|
|
|
|
await request
|
|
|
|
.post('/v1/update')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(sampleResponses.V1.POST['/update [204 Response]'].statusCode);
|
|
|
|
// Check that TargetState.update was called
|
|
|
|
expect(targetUpdateSpy).to.be.called;
|
|
|
|
expect(targetUpdateSpy).to.be.calledWith(undefined, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 204 with force: true in body', async () => {
|
|
|
|
// Stub response for getting instantUpdates
|
|
|
|
configStub.resolves(true);
|
|
|
|
// Make request with force: true in the body
|
|
|
|
await request
|
|
|
|
.post('/v1/update')
|
|
|
|
.send({ force: true })
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(sampleResponses.V1.POST['/update [204 Response]'].statusCode);
|
|
|
|
// Check that TargetState.update was called
|
|
|
|
expect(targetUpdateSpy).to.be.called;
|
|
|
|
expect(targetUpdateSpy).to.be.calledWith(true, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 202 when instantUpdates are disabled', async () => {
|
|
|
|
// Stub response for getting instantUpdates
|
|
|
|
configStub.resolves(false);
|
|
|
|
// Make request
|
|
|
|
await request
|
|
|
|
.post('/v1/update')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
|
|
|
.expect(sampleResponses.V1.POST['/update [202 Response]'].statusCode);
|
|
|
|
// Check that TargetState.update was not called
|
|
|
|
expect(targetUpdateSpy).to.not.be.called;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-18 18:24:18 +00:00
|
|
|
// TODO: add tests for V1 endpoints
|
|
|
|
});
|