UNPKG

dockest

Version:

Dockest is an integration testing tool aimed at alleviating the process of evaluating unit tests whilst running multi-container Docker applications.

47 lines 2.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getServiceAddress = exports.resolveServiceAddress = exports.getHostAddress = void 0; const constants_1 = require("../constants"); const errors_1 = require("../errors"); const get_run_mode_1 = require("../utils/get-run-mode"); const select_port_mapping_1 = require("../utils/select-port-mapping"); let runMode = null; const getRunMode = () => { if (!runMode) { runMode = (0, get_run_mode_1.getRunMode)(); } return runMode; }; const dockestConfig = process.env[constants_1.DOCKEST_ATTACH_TO_PROCESS]; if (!dockestConfig) { throw new errors_1.DockestError('Config not attached to process: Not executed inside dockest context'); } const config = JSON.parse(dockestConfig); const getHostAddress = () => { if (getRunMode() !== 'docker-injected-host-socket') { return constants_1.DEFAULT_HOST_NAME; } return constants_1.DOCKEST_HOST_ADDRESS; }; exports.getHostAddress = getHostAddress; const resolveServiceAddress = (serviceName, targetPort) => { const service = config.services[serviceName]; if (!service || !service.ports) { throw new errors_1.DockestError(`Service "${serviceName}" does not exist`); } const portBinding = service.ports.map(select_port_mapping_1.selectPortMapping).find((portBinding) => portBinding.target === targetPort); if (!portBinding) { throw new errors_1.DockestError(`Service "${serviceName}" has no target port ${portBinding}`); } if (getRunMode() === 'docker-injected-host-socket') { return { host: serviceName, port: portBinding.target }; } return { host: 'localhost', port: portBinding.published }; }; exports.resolveServiceAddress = resolveServiceAddress; const getServiceAddress = (serviceName, targetPort) => { const record = (0, exports.resolveServiceAddress)(serviceName, targetPort); return `${record.host}:${record.port}`; }; exports.getServiceAddress = getServiceAddress; //# sourceMappingURL=index.js.map