1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-28 14:10:04 +02:00
copilot/packages/docker-compose-client/test/index.js

188 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-06-03 01:16:49 +03:00
const { expect } = require('code');
const Lab = require('lab');
const Package = require('../package.json');
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';
const DockerComposeClient = require('../');
2017-06-03 01:16:49 +03:00
const client = new DockerComposeClient(endpoint);
const server = new Server({
// eslint-disable-next-line object-shorthand
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 {
safeLoad(manifest);
} catch (err) {
return fn(err);
}
fn(null, {
projectName: options.project_name
});
},
// eslint-disable-next-line object-shorthand
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 {
safeLoad(manifest);
} 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-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-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-06-03 01:16:49 +03:00
client.client.emit('error', new Error('test'));
});
2017-06-05 03:25:06 +03:00
after(done => {
client.close();
server.close();
2017-06-03 01:16:49 +03:00
done();
});