Skip to content

Commit fe81de1

Browse files
committed
DockerService tests
1 parent 49f4036 commit fe81de1

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

command/ServiceCommand.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = class ServiceCommand extends BaseCommand {
3636
if (this.container) {
3737
await this.container.stop();
3838
await this.container.remove();
39-
log.info(`Service removed: '${this.name}' (${image})`)
39+
log.info(`Service removed: '${this.name}'`)
4040
}
4141
}
4242
};

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
"scripts": {
66
"default": "node ./bin/www",
77
"dev": "NODE_ENV=dev node ./bin/www",
8-
"prod": "NODE_ENV=production node ./bin/www"
8+
"prod": "NODE_ENV=production node ./bin/www",
9+
"test": "mocha"
910
},
1011
"dependencies": {
12+
"chai": "^4.2.0",
1113
"debug": "~2.6.9",
1214
"dockerode": "^2.5.7",
1315
"express": "~4.16.0",
1416
"express-fileupload": "^1.0.0",
1517
"http-errors": "~1.6.2",
18+
"mocha": "^5.2.0",
1619
"morgan": "~1.9.0",
1720
"nconf": "^0.10.0",
1821
"parser-yaml": "^0.1.1",
22+
"sinon": "^7.1.0",
1923
"winston": "^3.1.0"
2024
}
2125
}

test/DockerServiceTest.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const DockerService = require('../services/DockerService');
2+
const ServiceCommand = require('../command/ServiceCommand');
3+
const timer = require('../helpers/timer');
4+
const docker = require('../docker/client');
5+
const assert = require('chai').assert;
6+
const sinon = require('sinon');
7+
const log = require('../logger');
8+
9+
10+
describe('DockerService', function () {
11+
this.timeout(0);
12+
describe('#applyDirectives()', function () {
13+
it('should create each service and remove it after 5 sec', async () => {
14+
const services = {
15+
server: {
16+
image: 'nginx'
17+
},
18+
database: {
19+
image: 'redis'
20+
}
21+
};
22+
const serviceNames = Object.keys(services);
23+
24+
await DockerService.applyDirectives({services});
25+
26+
let set = await containersSet();
27+
serviceNames.forEach(name => assert.isTrue(set.has(name)));
28+
29+
30+
await timer(6000);
31+
set = await containersSet();
32+
serviceNames.forEach(name => assert.isFalse(set.has(name)));
33+
}
34+
);
35+
36+
it('should create each dependant service after its dependency', async () => {
37+
// fuck me -- how to do this???
38+
39+
const services = {
40+
server_one: {
41+
image: 'nginx',
42+
depends_on: [
43+
'server_two'
44+
]
45+
},
46+
server_two: {
47+
image: 'nginx',
48+
depends_on: [
49+
'database'
50+
]
51+
},
52+
database: {
53+
image: 'redis',
54+
}
55+
};
56+
57+
const actualOrder = [];
58+
const expectedOrder = ['database', 'server_two', "server_one"];
59+
60+
const fake = sinon.fake(function () {
61+
return timer(500).then(() => {
62+
actualOrder.push(this.name);
63+
log.info(`Service created: ${this.name}`)
64+
})
65+
});
66+
sinon.replace(ServiceCommand.prototype, '_runInternal', fake);
67+
68+
await DockerService.applyDirectives({services});
69+
70+
assert.deepEqual(actualOrder, expectedOrder);
71+
});
72+
});
73+
});
74+
75+
async function containersSet() {
76+
const containers = await docker.listContainers();
77+
return containers
78+
.map(c => c.Names[0].replace('/', ''))
79+
.reduce((set, next) => set.add(next), new Set());
80+
}
81+
82+

0 commit comments

Comments
 (0)