mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-23 23:42:29 +00:00
Fix mock dockerode module
Tests using the mock `testWithData` method were not restoring the dockerode prototype to the default values, making some tests behave differently when run individually that when run with the suite. This commit fixes the `testWithData` method and some malfunctioning v1 API tests because of the change. It doesn't fix all the tests
This commit is contained in:
parent
e2d54e9d6c
commit
c70aedf044
@ -73,6 +73,7 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
// Clear Dockerode actions recorded for each test
|
// Clear Dockerode actions recorded for each test
|
||||||
mockedDockerode.resetHistory();
|
mockedDockerode.resetHistory();
|
||||||
|
appMock.unmockAll();
|
||||||
});
|
});
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
@ -120,7 +121,6 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
healthCheckStubs.forEach((hc) => hc.restore());
|
healthCheckStubs.forEach((hc) => hc.restore());
|
||||||
// Remove any test data generated
|
// Remove any test data generated
|
||||||
await mockedAPI.cleanUp();
|
await mockedAPI.cleanUp();
|
||||||
appMock.unmockAll();
|
|
||||||
targetStateCacheMock.restore();
|
targetStateCacheMock.restore();
|
||||||
loggerStub.restore();
|
loggerStub.restore();
|
||||||
});
|
});
|
||||||
@ -213,7 +213,6 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
|
|
||||||
describe('GET /v1/apps/:appId', () => {
|
describe('GET /v1/apps/:appId', () => {
|
||||||
it('does not return information for an application when there is more than 1 container', async () => {
|
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
|
await request
|
||||||
.get('/v1/apps/2')
|
.get('/v1/apps/2')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
@ -236,6 +235,9 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
});
|
});
|
||||||
appMock.mockManagers([container], [], []);
|
appMock.mockManagers([container], [], []);
|
||||||
appMock.mockImages([], false, [image]);
|
appMock.mockImages([], false, [image]);
|
||||||
|
await mockedDockerode.testWithData(
|
||||||
|
{ containers: [container], images: [image] },
|
||||||
|
async () => {
|
||||||
// Make request
|
// Make request
|
||||||
await request
|
await request
|
||||||
.get('/v1/apps/2')
|
.get('/v1/apps/2')
|
||||||
@ -248,12 +250,15 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
sampleResponses.V1.GET['/apps/2'].body,
|
sampleResponses.V1.GET['/apps/2'].body,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('POST /v1/apps/:appId/stop', () => {
|
describe('POST /v1/apps/:appId/stop', () => {
|
||||||
it('does not allow stopping an application when there is more than 1 container', async () => {
|
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
|
// Every test case in this suite has a 3 service release mocked so just make the request
|
||||||
|
await mockedDockerode.testWithData({ containers, images }, async () => {
|
||||||
await request
|
await request
|
||||||
.post('/v1/apps/2/stop')
|
.post('/v1/apps/2/stop')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
@ -263,6 +268,7 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
.statusCode,
|
.statusCode,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('stops a SPECIFIC application and returns a containerId', async () => {
|
it('stops a SPECIFIC application and returns a containerId', async () => {
|
||||||
// Setup single container application
|
// Setup single container application
|
||||||
@ -298,12 +304,14 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
describe('POST /v1/apps/:appId/start', () => {
|
describe('POST /v1/apps/:appId/start', () => {
|
||||||
it('does not allow starting an application when there is more than 1 container', async () => {
|
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
|
// Every test case in this suite has a 3 service release mocked so just make the request
|
||||||
|
await mockedDockerode.testWithData({ containers, images }, async () => {
|
||||||
await request
|
await request
|
||||||
.post('/v1/apps/2/start')
|
.post('/v1/apps/2/start')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
.set('Authorization', `Bearer ${apiKeys.cloudApiKey}`)
|
||||||
.expect(400);
|
.expect(400);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('starts a SPECIFIC application and returns a containerId', async () => {
|
it('starts a SPECIFIC application and returns a containerId', async () => {
|
||||||
const service = {
|
const service = {
|
||||||
@ -387,6 +395,9 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
appMock.mockManagers([container], [], []);
|
appMock.mockManagers([container], [], []);
|
||||||
appMock.mockImages([], false, [image]);
|
appMock.mockImages([], false, [image]);
|
||||||
|
|
||||||
|
await mockedDockerode.testWithData(
|
||||||
|
{ containers: [container], images: [image] },
|
||||||
|
async () => {
|
||||||
const response = await request
|
const response = await request
|
||||||
.post('/v1/reboot')
|
.post('/v1/reboot')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
@ -395,6 +406,8 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
|
|
||||||
expect(response.body).to.have.property('Data').that.is.not.empty;
|
expect(response.body).to.have.property('Data').that.is.not.empty;
|
||||||
expect(rebootMock).to.have.been.calledOnce;
|
expect(rebootMock).to.have.been.calledOnce;
|
||||||
|
},
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return 423 and reject the reboot if no locks are set', async () => {
|
it('should return 423 and reject the reboot if no locks are set', async () => {
|
||||||
@ -417,6 +430,9 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
appMock.mockManagers([container], [], []);
|
appMock.mockManagers([container], [], []);
|
||||||
appMock.mockImages([], false, [image]);
|
appMock.mockImages([], false, [image]);
|
||||||
|
|
||||||
|
await mockedDockerode.testWithData(
|
||||||
|
{ containers: [container], images: [image] },
|
||||||
|
async () => {
|
||||||
const response = await request
|
const response = await request
|
||||||
.post('/v1/reboot')
|
.post('/v1/reboot')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
@ -426,6 +442,8 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
expect(updateLock.lock).to.be.calledOnce;
|
expect(updateLock.lock).to.be.calledOnce;
|
||||||
expect(response.body).to.have.property('Error').that.is.not.empty;
|
expect(response.body).to.have.property('Error').that.is.not.empty;
|
||||||
expect(rebootMock).to.not.have.been.called;
|
expect(rebootMock).to.not.have.been.called;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
(updateLock.lock as SinonStub).restore();
|
(updateLock.lock as SinonStub).restore();
|
||||||
});
|
});
|
||||||
@ -450,6 +468,9 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
appMock.mockManagers([container], [], []);
|
appMock.mockManagers([container], [], []);
|
||||||
appMock.mockImages([], false, [image]);
|
appMock.mockImages([], false, [image]);
|
||||||
|
|
||||||
|
await mockedDockerode.testWithData(
|
||||||
|
{ containers: [container], images: [image] },
|
||||||
|
async () => {
|
||||||
const response = await request
|
const response = await request
|
||||||
.post('/v1/reboot')
|
.post('/v1/reboot')
|
||||||
.send({ force: true })
|
.send({ force: true })
|
||||||
@ -460,6 +481,8 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
expect(updateLock.lock).to.be.calledOnce;
|
expect(updateLock.lock).to.be.calledOnce;
|
||||||
expect(response.body).to.have.property('Data').that.is.not.empty;
|
expect(response.body).to.have.property('Data').that.is.not.empty;
|
||||||
expect(rebootMock).to.have.been.calledOnce;
|
expect(rebootMock).to.have.been.calledOnce;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
(updateLock.lock as SinonStub).restore();
|
(updateLock.lock as SinonStub).restore();
|
||||||
});
|
});
|
||||||
@ -488,6 +511,9 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
appMock.mockManagers([container], [], []);
|
appMock.mockManagers([container], [], []);
|
||||||
appMock.mockImages([], false, [image]);
|
appMock.mockImages([], false, [image]);
|
||||||
|
|
||||||
|
await mockedDockerode.testWithData(
|
||||||
|
{ containers: [container], images: [image] },
|
||||||
|
async () => {
|
||||||
const response = await request
|
const response = await request
|
||||||
.post('/v1/shutdown')
|
.post('/v1/shutdown')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
@ -496,6 +522,8 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
|
|
||||||
expect(response.body).to.have.property('Data').that.is.not.empty;
|
expect(response.body).to.have.property('Data').that.is.not.empty;
|
||||||
expect(shutdownMock).to.have.been.calledOnce;
|
expect(shutdownMock).to.have.been.calledOnce;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
shutdownMock.resetHistory();
|
shutdownMock.resetHistory();
|
||||||
});
|
});
|
||||||
@ -520,6 +548,9 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
appMock.mockManagers([container], [], []);
|
appMock.mockManagers([container], [], []);
|
||||||
appMock.mockImages([], false, [image]);
|
appMock.mockImages([], false, [image]);
|
||||||
|
|
||||||
|
await mockedDockerode.testWithData(
|
||||||
|
{ containers: [container], images: [image] },
|
||||||
|
async () => {
|
||||||
const response = await request
|
const response = await request
|
||||||
.post('/v1/shutdown')
|
.post('/v1/shutdown')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
@ -529,6 +560,8 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
expect(updateLock.lock).to.be.calledOnce;
|
expect(updateLock.lock).to.be.calledOnce;
|
||||||
expect(response.body).to.have.property('Error').that.is.not.empty;
|
expect(response.body).to.have.property('Error').that.is.not.empty;
|
||||||
expect(shutdownMock).to.not.have.been.called;
|
expect(shutdownMock).to.not.have.been.called;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
(updateLock.lock as SinonStub).restore();
|
(updateLock.lock as SinonStub).restore();
|
||||||
});
|
});
|
||||||
@ -553,6 +586,9 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
appMock.mockManagers([container], [], []);
|
appMock.mockManagers([container], [], []);
|
||||||
appMock.mockImages([], false, [image]);
|
appMock.mockImages([], false, [image]);
|
||||||
|
|
||||||
|
await mockedDockerode.testWithData(
|
||||||
|
{ containers: [container], images: [image] },
|
||||||
|
async () => {
|
||||||
const response = await request
|
const response = await request
|
||||||
.post('/v1/shutdown')
|
.post('/v1/shutdown')
|
||||||
.send({ force: true })
|
.send({ force: true })
|
||||||
@ -563,6 +599,8 @@ describe('SupervisorAPI [V1 Endpoints]', () => {
|
|||||||
expect(updateLock.lock).to.be.calledOnce;
|
expect(updateLock.lock).to.be.calledOnce;
|
||||||
expect(response.body).to.have.property('Data').that.is.not.empty;
|
expect(response.body).to.have.property('Data').that.is.not.empty;
|
||||||
expect(shutdownMock).to.have.been.calledOnce;
|
expect(shutdownMock).to.have.been.calledOnce;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
(updateLock.lock as SinonStub).restore();
|
(updateLock.lock as SinonStub).restore();
|
||||||
});
|
});
|
||||||
|
@ -361,6 +361,7 @@ describe('SupervisorAPI [V2 Endpoints]', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return 404 for an unknown service', async () => {
|
it('should return 404 for an unknown service', async () => {
|
||||||
|
await mockedDockerode.testWithData({}, async () => {
|
||||||
await request
|
await request
|
||||||
.post(`/v2/applications/1658654/start-service?apikey=${appScopedKey}`)
|
.post(`/v2/applications/1658654/start-service?apikey=${appScopedKey}`)
|
||||||
.send({ serviceName: 'unknown' })
|
.send({ serviceName: 'unknown' })
|
||||||
@ -369,6 +370,7 @@ describe('SupervisorAPI [V2 Endpoints]', () => {
|
|||||||
|
|
||||||
expect(applicationManagerSpy).to.not.have.been.called;
|
expect(applicationManagerSpy).to.not.have.been.called;
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should ignore locks and return 200', async () => {
|
it('should ignore locks and return 200', async () => {
|
||||||
// Turn lock on
|
// Turn lock on
|
||||||
@ -465,13 +467,17 @@ describe('SupervisorAPI [V2 Endpoints]', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return 404 for an unknown service', async () => {
|
it('should return 404 for an unknown service', async () => {
|
||||||
|
await mockedDockerode.testWithData({}, async () => {
|
||||||
await request
|
await request
|
||||||
.post(`/v2/applications/1658654/restart-service?apikey=${appScopedKey}`)
|
.post(
|
||||||
|
`/v2/applications/1658654/restart-service?apikey=${appScopedKey}`,
|
||||||
|
)
|
||||||
.send({ serviceName: 'unknown' })
|
.send({ serviceName: 'unknown' })
|
||||||
.set('Content-type', 'application/json')
|
.set('Content-type', 'application/json')
|
||||||
.expect(404);
|
.expect(404);
|
||||||
expect(applicationManagerSpy).to.not.have.been.called;
|
expect(applicationManagerSpy).to.not.have.been.called;
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should return 423 for a service with update locks', async () => {
|
it('should return 423 for a service with update locks', async () => {
|
||||||
// Turn lock on
|
// Turn lock on
|
||||||
|
@ -81,6 +81,12 @@ export function registerOverride<
|
|||||||
overrides[name] = fn;
|
overrides[name] = fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function restoreOverride<T extends DockerodeFunction>(name: T) {
|
||||||
|
if (overrides.hasOwnProperty(name)) {
|
||||||
|
delete overrides[name];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export interface TestData {
|
export interface TestData {
|
||||||
networks: Dictionary<any>;
|
networks: Dictionary<any>;
|
||||||
images: Dictionary<any>;
|
images: Dictionary<any>;
|
||||||
@ -201,6 +207,24 @@ function createMockedDockerode(data: TestData) {
|
|||||||
return mockedDockerode;
|
return mockedDockerode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Prototype = Dictionary<(...args: any[]) => any>;
|
||||||
|
function clonePrototype(prototype: Prototype): Prototype {
|
||||||
|
const clone: Prototype = {};
|
||||||
|
Object.getOwnPropertyNames(prototype).forEach((fn) => {
|
||||||
|
if (fn !== 'constructor' && _.isFunction(prototype[fn])) {
|
||||||
|
clone[fn] = prototype[fn];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
function assignPrototype(target: Prototype, source: Prototype) {
|
||||||
|
Object.keys(source).forEach((fn) => {
|
||||||
|
target[fn] = source[fn];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export async function testWithData(
|
export async function testWithData(
|
||||||
data: Partial<TestData>,
|
data: Partial<TestData>,
|
||||||
test: () => Promise<any>,
|
test: () => Promise<any>,
|
||||||
@ -216,7 +240,7 @@ export async function testWithData(
|
|||||||
};
|
};
|
||||||
|
|
||||||
// grab the original prototype...
|
// grab the original prototype...
|
||||||
const basePrototype = dockerode.prototype;
|
const basePrototype = clonePrototype(dockerode.prototype);
|
||||||
|
|
||||||
// @ts-expect-error setting a RO property
|
// @ts-expect-error setting a RO property
|
||||||
dockerode.prototype = createMockedDockerode(mockedData);
|
dockerode.prototype = createMockedDockerode(mockedData);
|
||||||
@ -226,7 +250,6 @@ export async function testWithData(
|
|||||||
await test();
|
await test();
|
||||||
} finally {
|
} finally {
|
||||||
// reset the original prototype...
|
// reset the original prototype...
|
||||||
// @ts-expect-error setting a RO property
|
assignPrototype(dockerode.prototype, basePrototype);
|
||||||
dockerode.prototype = basePrototype;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user