Skip to content

Commit b7d024c

Browse files
committed
[exoframe-server] Fix container removal by domain, add test
1 parent 3ef3bc4 commit b7d024c

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

packages/exoframe-server/src/routes/remove.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ const removeUserContainer = async ({ username, id, reply }) => {
1616
}
1717

1818
// if not found by name - try to find by domain.
19-
const containersByUrl = allContainers.filter((c) => {
19+
const containerByUrl = allContainers.find((c) => {
2020
return (
2121
c.Labels['exoframe.user'] === username &&
2222
c.Labels[`traefik.http.routers.${c.Labels['exoframe.deployment']}.rule`].includes(id)
2323
);
2424
});
25-
26-
if (containersByUrl.length) {
27-
await Promise.all(containersByUrl.map(removeContainer));
25+
if (containerByUrl) {
26+
await removeContainer(containerByUrl);
2827
return reply.code(204).send('removed');
2928
}
3029

packages/exoframe-server/test/remove.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const generateContainerConfig = ({ name, username, project, url }) => ({
3232
},
3333
});
3434

35+
const testUrl = 'rmtest.example.com';
36+
3537
beforeAll(async () => {
3638
// start server
3739
const port = await getPort();
@@ -50,6 +52,17 @@ beforeAll(async () => {
5052
});
5153
const container = await docker.createContainer(containerConfig);
5254
await container.start();
55+
// create test container with url to remove
56+
// second project container
57+
const urlContainerConfig = generateContainerConfig({
58+
name: 'rmtest12',
59+
username: 'admin',
60+
project: 'rmtest12',
61+
baseName: 'exo-admin-rmtest12',
62+
url: testUrl,
63+
});
64+
const urlContainer = await docker.createContainer(urlContainerConfig);
65+
await urlContainer.start();
5366
// create test project to remove
5467
// first project container
5568
const prjContainerConfig1 = generateContainerConfig({
@@ -88,6 +101,22 @@ test('Should remove current deployment', async () => {
88101
expect(exContainer).toBeUndefined();
89102
});
90103

104+
test('Should remove container by url', async () => {
105+
// options base
106+
const options = Object.assign({}, baseOptions, { url: `/remove/${encodeURIComponent(testUrl)}` });
107+
108+
const response = await fastify.inject(options);
109+
// check response
110+
expect(response.statusCode).toEqual(204);
111+
112+
// check docker services
113+
const allContainers = await docker.listContainers();
114+
const urlContainers = allContainers.filter((c) =>
115+
c.Labels[`traefik.http.routers.${c.Labels['exoframe.deployment']}.rule`].includes(testUrl)
116+
);
117+
expect(urlContainers.length).toEqual(0);
118+
});
119+
91120
test('Should remove current project', async () => {
92121
// options base
93122
const options = Object.assign({}, baseOptions, { url: `/remove/${projectName}` });

0 commit comments

Comments
 (0)