2017-06-03 01:16:49 +03:00
|
|
|
const { expect } = require('code');
|
|
|
|
const Lab = require('lab');
|
|
|
|
const Package = require('../package.json');
|
2017-05-09 20:46:33 +03:00
|
|
|
const { safeLoad } = require('js-yaml');
|
|
|
|
const { Server } = require('zerorpc');
|
|
|
|
|
2017-06-03 01:16:49 +03:00
|
|
|
// Test shortcuts
|
|
|
|
|
2017-06-05 03:25:06 +03:00
|
|
|
const lab = Lab.script();
|
|
|
|
exports.lab = lab;
|
2017-06-03 01:16:49 +03:00
|
|
|
const after = lab.after;
|
|
|
|
const it = lab.it;
|
|
|
|
|
|
|
|
const projectName = Package.name;
|
|
|
|
const endpoint = 'tcp://0.0.0.0:4040';
|
2017-05-09 20:46:33 +03:00
|
|
|
const DockerComposeClient = require('../');
|
2017-06-03 01:16:49 +03:00
|
|
|
const client = new DockerComposeClient(endpoint);
|
2017-05-09 20:46:33 +03:00
|
|
|
|
|
|
|
const server = new Server({
|
2017-05-18 21:21:33 +03:00
|
|
|
// eslint-disable-next-line object-shorthand
|
2017-05-09 20:46:33 +03:00
|
|
|
up: function(options, manifest, fn) {
|
|
|
|
if (typeof options !== 'object') {
|
|
|
|
return fn(new Error('Expected options'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof options.project_name !== 'string') {
|
|
|
|
return fn(new Error('Expected project name'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof manifest !== 'string') {
|
|
|
|
return fn(new Error('Expected manifest'));
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-05-18 21:21:33 +03:00
|
|
|
safeLoad(manifest);
|
2017-05-09 20:46:33 +03:00
|
|
|
} catch (err) {
|
|
|
|
return fn(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn(null, {
|
|
|
|
projectName: options.project_name
|
|
|
|
});
|
|
|
|
},
|
2017-05-18 21:21:33 +03:00
|
|
|
// eslint-disable-next-line object-shorthand
|
2017-05-09 20:46:33 +03:00
|
|
|
scale: function(options, manifest, fn) {
|
|
|
|
if (typeof options !== 'object') {
|
|
|
|
return fn(new Error('Expected options'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof options.project_name !== 'string') {
|
|
|
|
return fn(new Error('Expected project name'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(options.services)) {
|
|
|
|
return fn(new Error('Expected services'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof manifest !== 'string') {
|
|
|
|
return fn(new Error('Expected manifest'));
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-05-18 21:21:33 +03:00
|
|
|
safeLoad(manifest);
|
2017-05-09 20:46:33 +03:00
|
|
|
} catch (err) {
|
|
|
|
return fn(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn(null, {
|
|
|
|
projectName: options.project_name,
|
|
|
|
services: options.services
|
|
|
|
});
|
2017-06-08 21:43:24 +03:00
|
|
|
},
|
|
|
|
// eslint-disable-next-line object-shorthand
|
|
|
|
config: function(options, manifest, fn) {
|
|
|
|
if (typeof options !== 'object') {
|
|
|
|
return fn(new Error('Expected options'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof options.project_name !== 'string') {
|
|
|
|
return fn(new Error('Expected project name'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof manifest !== 'string') {
|
|
|
|
return fn(new Error('Expected manifest'));
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
safeLoad(manifest);
|
|
|
|
} catch (err) {
|
|
|
|
return fn(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn(null, {
|
|
|
|
projectName: options.project_name
|
|
|
|
});
|
2017-05-09 20:46:33 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-03 01:16:49 +03:00
|
|
|
server.bind(endpoint);
|
|
|
|
|
2017-06-05 03:25:06 +03:00
|
|
|
it('provision()', done => {
|
2017-06-03 01:16:49 +03:00
|
|
|
const manifest = `
|
|
|
|
hello:
|
|
|
|
image: hello-world:latest
|
|
|
|
world:
|
|
|
|
image: consul:latest
|
|
|
|
node:
|
|
|
|
image: node:latest
|
|
|
|
`;
|
|
|
|
|
|
|
|
client.provision({ projectName, manifest }, (err, res) => {
|
|
|
|
expect(err).to.not.exist();
|
|
|
|
|
|
|
|
expect(res.projectName).to.equal(projectName);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-05 03:25:06 +03:00
|
|
|
it('scale()', done => {
|
2017-06-03 01:16:49 +03:00
|
|
|
const manifest = `
|
|
|
|
hello:
|
|
|
|
image: hello-world:latest
|
|
|
|
world:
|
|
|
|
image: consul:latest
|
|
|
|
node:
|
|
|
|
image: node:latest
|
|
|
|
`;
|
|
|
|
|
2017-06-05 03:25:06 +03:00
|
|
|
client.scale(
|
|
|
|
{
|
2017-06-03 01:16:49 +03:00
|
|
|
projectName,
|
2017-06-05 03:25:06 +03:00
|
|
|
services: {
|
|
|
|
hello: 2,
|
|
|
|
world: 3
|
|
|
|
},
|
|
|
|
manifest
|
|
|
|
},
|
|
|
|
(err, res) => {
|
|
|
|
expect(err).to.not.exist();
|
|
|
|
|
|
|
|
expect(res).to.equal({
|
|
|
|
projectName,
|
|
|
|
services: [{ name: 'hello', num: 2 }, { name: 'world', num: 3 }]
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
2017-05-09 20:46:33 +03:00
|
|
|
});
|
|
|
|
|
2017-06-08 21:43:24 +03:00
|
|
|
it('config()', done => {
|
|
|
|
const manifest = `
|
|
|
|
hello:
|
|
|
|
image: hello-world:latest
|
|
|
|
world:
|
|
|
|
image: consul:latest
|
|
|
|
node:
|
|
|
|
image: node:latest
|
|
|
|
`;
|
|
|
|
|
|
|
|
client.config(
|
|
|
|
{
|
|
|
|
projectName,
|
|
|
|
services: ['hello'],
|
|
|
|
manifest
|
|
|
|
},
|
|
|
|
(err, res) => {
|
|
|
|
expect(err).to.not.exist();
|
|
|
|
expect(res).to.exist();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-06-05 03:25:06 +03:00
|
|
|
it('handles errors', done => {
|
|
|
|
client.once('error', err => {
|
2017-06-03 01:16:49 +03:00
|
|
|
expect(err).to.exist();
|
|
|
|
done();
|
2017-05-09 20:46:33 +03:00
|
|
|
});
|
2017-06-03 01:16:49 +03:00
|
|
|
|
|
|
|
client.client.emit('error', new Error('test'));
|
2017-05-09 20:46:33 +03:00
|
|
|
});
|
|
|
|
|
2017-06-05 03:25:06 +03:00
|
|
|
after(done => {
|
2017-05-09 20:46:33 +03:00
|
|
|
client.close();
|
|
|
|
server.close();
|
2017-06-03 01:16:49 +03:00
|
|
|
done();
|
2017-05-09 20:46:33 +03:00
|
|
|
});
|