navy
Version:
Quick and powerful development environments using Docker and Docker Compose
108 lines (106 loc) • 5.76 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _chai = require("chai");
var _sinon = _interopRequireDefault(require("sinon"));
var _fs = _interopRequireDefault(require("fs"));
var httpsModule = _interopRequireWildcard(require("../https"));
var _serviceHost = require("../service-host");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/* eslint-env mocha */
describe('service-host', function () {
let sandbox;
const originalSubdomain = process.env.NAVY_EXTERNAL_SUBDOMAIN;
beforeEach(function () {
sandbox = _sinon.default.createSandbox();
sandbox.stub(httpsModule, 'getCertsPath').returns('/fake/certs');
sandbox.stub(_fs.default, 'existsSync').returns(false);
});
afterEach(function () {
sandbox.restore();
if (originalSubdomain === undefined) {
delete process.env.NAVY_EXTERNAL_SUBDOMAIN;
} else {
process.env.NAVY_EXTERNAL_SUBDOMAIN = originalSubdomain;
}
});
describe('getNIPSubdomain', function () {
it('should return an nip.io domain with the correct IP address', async function () {
(0, _chai.expect)(await (0, _serviceHost.getNIPSubdomain)('192.168.1.10')).to.equal('192.168.1.10.nip.io');
});
it('should fallback to localhost when a hostname is passed instead of an IP address', async function () {
process.env.NAVY_EXTERNAL_IP = 'somehostname';
(0, _chai.expect)(await (0, _serviceHost.getNIPSubdomain)('somehostname')).to.equal('127.0.0.1.nip.io');
delete process.env.NAVY_EXTERNAL_IP;
});
it('should treat malformed IPs (out-of-range octets) as invalid', async function () {
(0, _chai.expect)(await (0, _serviceHost.getNIPSubdomain)('999.0.0.1')).to.equal('127.0.0.1.nip.io');
});
});
describe('createHostForService', function () {
it('should return the correct host for a service', async function () {
(0, _chai.expect)(await (0, _serviceHost.createHostForService)('someservice', 'mynavy', '127.0.0.1')).to.equal('someservice.mynavy.127.0.0.1.nip.io');
(0, _chai.expect)(await (0, _serviceHost.createHostForService)('someservice', 'mynavy', '192.168.1.10')).to.equal('someservice.mynavy.192.168.1.10.nip.io');
});
it('should use NAVY_EXTERNAL_SUBDOMAIN when set, in preference to nip.io', async function () {
process.env.NAVY_EXTERNAL_SUBDOMAIN = 'dev.example.com';
(0, _chai.expect)(await (0, _serviceHost.createHostForService)('api', 'env', '10.0.0.1')).to.equal('api.env.dev.example.com');
});
});
describe('createUrlForService', function () {
it('should return an http URL when no certificate exists', async function () {
(0, _chai.expect)(await (0, _serviceHost.createUrlForService)('someservice', 'mynavy', '192.168.99.100')).to.equal('http://someservice.mynavy.192.168.99.100.nip.io');
});
it('should return an https URL when a matching .crt exists in the certs path', async function () {
_fs.default.existsSync.restore();
sandbox.stub(_fs.default, 'existsSync').callsFake(filePath => filePath === '/fake/certs/someservice.mynavy.192.168.99.100.nip.io.crt');
(0, _chai.expect)(await (0, _serviceHost.createUrlForService)('someservice', 'mynavy', '192.168.99.100')).to.equal('https://someservice.mynavy.192.168.99.100.nip.io');
});
});
describe('getUrlFromService', function () {
it('should extract the host from service ENV config', function () {
const service = {
raw: {
Config: {
Env: ['VIRTUAL_HOST=myservice.coolnavy.127.0.0.1.nip.io']
}
}
};
(0, _chai.expect)((0, _serviceHost.getUrlFromService)(service)).to.equal('http://myservice.coolnavy.127.0.0.1.nip.io');
});
it('should return https when a certificate exists for the VIRTUAL_HOST', function () {
_fs.default.existsSync.restore();
sandbox.stub(_fs.default, 'existsSync').callsFake(filePath => filePath === '/fake/certs/myservice.coolnavy.127.0.0.1.nip.io.crt');
const service = {
raw: {
Config: {
Env: ['OTHER=value', 'VIRTUAL_HOST=myservice.coolnavy.127.0.0.1.nip.io']
}
}
};
(0, _chai.expect)((0, _serviceHost.getUrlFromService)(service)).to.equal('https://myservice.coolnavy.127.0.0.1.nip.io');
});
it('should return null when env contains no VIRTUAL_HOST entry', function () {
const service = {
raw: {
Config: {
Env: ['FOO=bar', 'BAZ=qux']
}
}
};
(0, _chai.expect)((0, _serviceHost.getUrlFromService)(service)).to.equal(null);
});
it('should return null when the service object is not valid', function () {
(0, _chai.expect)((0, _serviceHost.getUrlFromService)({
raw: {
Config: {}
}
})).to.equal(null);
(0, _chai.expect)((0, _serviceHost.getUrlFromService)({
raw: {}
})).to.equal(null);
(0, _chai.expect)((0, _serviceHost.getUrlFromService)({})).to.equal(null);
(0, _chai.expect)((0, _serviceHost.getUrlFromService)(undefined)).to.equal(null);
(0, _chai.expect)((0, _serviceHost.getUrlFromService)(null)).to.equal(null);
});
});
});