UNPKG

navy

Version:

Quick and powerful development environments using Docker and Docker Compose

257 lines (254 loc) 8.05 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _chai = require("chai"); var _sinon = _interopRequireDefault(require("sinon")); var _proxyquire = _interopRequireDefault(require("proxyquire")); var _stripAnsi = _interopRequireDefault(require("strip-ansi")); /* eslint-env mocha */ // NOTE (potential source bugs in health.js, not locked in by these tests): // 1. `getStatus` and `getHistory` both compute `service.raw && service.raw.State.Health`. // If `service.raw` is truthy but `service.raw.State` is undefined, this throws a // TypeError reading 'Health' of undefined. The guard chain is incomplete; it // should be `service.raw && service.raw.State && service.raw.State.Health`. // 2. Both helpers accept a `state` parameter that is never used (dead parameter). describe('cli/health', function () { let sandbox; let getNavyStub; let navyStub; let consoleLogStub; let healthCli; function makeService(overrides = {}) { return { id: 'svc-1', name: 'api', raw: undefined, ...overrides }; } beforeEach(function () { sandbox = _sinon.default.createSandbox(); navyStub = { ps: sandbox.stub().resolves([]), getState: sandbox.stub().resolves(null) }; getNavyStub = sandbox.stub().returns(navyStub); consoleLogStub = sandbox.stub(console, 'log'); healthCli = _proxyquire.default.noCallThru()('../health', { '../': { getNavy: getNavyStub } }); }); afterEach(function () { sandbox.restore(); }); function lastLoggedString() { const last = consoleLogStub.lastCall; return last ? String(last.args[0]) : ''; } describe('default export', function () { it('should resolve the navy instance using opts.navy', async function () { await healthCli({ navy: 'env-1' }); (0, _chai.expect)(getNavyStub.calledOnce).to.equal(true); (0, _chai.expect)(getNavyStub.firstCall.args[0]).to.equal('env-1'); }); it('should print the table headers even when there are no services', async function () { navyStub.ps.resolves([]); await healthCli({ navy: 'env-1' }); (0, _chai.expect)(consoleLogStub.calledOnce).to.equal(true); const text = (0, _stripAnsi.default)(consoleLogStub.firstCall.args[0]); (0, _chai.expect)(text).to.contain('NAME'); (0, _chai.expect)(text).to.contain('STATUS'); (0, _chai.expect)(text).to.contain('HISTORY'); }); it('should call ps and getState exactly once each', async function () { navyStub.ps.resolves([makeService()]); await healthCli({ navy: 'env-1' }); (0, _chai.expect)(navyStub.ps.calledOnce).to.equal(true); (0, _chai.expect)(navyStub.getState.calledOnce).to.equal(true); }); }); describe('getStatus (via default export)', function () { it('should render - when the service has no raw container info', async function () { navyStub.ps.resolves([makeService({ name: 'api', raw: undefined })]); await healthCli({ navy: 'env-1' }); const text = (0, _stripAnsi.default)(lastLoggedString()); (0, _chai.expect)(text).to.contain('api'); (0, _chai.expect)(text).to.contain('-'); }); it('should render - when the service has raw but no Health record', async function () { navyStub.ps.resolves([makeService({ name: 'api', raw: { State: { Health: null } } })]); await healthCli({ navy: 'env-1' }); const text = (0, _stripAnsi.default)(lastLoggedString()); (0, _chai.expect)(text).to.contain('api'); (0, _chai.expect)(text).to.contain('-'); }); it('should render Healthy when health status is "healthy"', async function () { navyStub.ps.resolves([makeService({ raw: { State: { Health: { Status: 'healthy', Log: [] } } } })]); await healthCli({ navy: 'env-1' }); const text = (0, _stripAnsi.default)(lastLoggedString()); (0, _chai.expect)(text).to.contain('Healthy'); }); it('should render Unhealthy when health status is anything other than healthy', async function () { navyStub.ps.resolves([makeService({ raw: { State: { Health: { Status: 'starting', Log: [] } } } })]); await healthCli({ navy: 'env-1' }); const text = (0, _stripAnsi.default)(lastLoggedString()); (0, _chai.expect)(text).to.contain('Unhealthy'); }); }); describe('getHistory (via default export)', function () { it('should render - when the service has no raw container info', async function () { navyStub.ps.resolves([makeService({ name: 'api', raw: undefined })]); await healthCli({ navy: 'env-1' }); const text = (0, _stripAnsi.default)(lastLoggedString()); (0, _chai.expect)(text).to.contain('-'); }); it('should render - when the service has raw but no Health record', async function () { navyStub.ps.resolves([makeService({ raw: { State: { Health: null } } })]); await healthCli({ navy: 'env-1' }); const text = (0, _stripAnsi.default)(lastLoggedString()); (0, _chai.expect)(text).to.contain('-'); }); it('should render only success markers when every entry has ExitCode 0', async function () { navyStub.ps.resolves([makeService({ raw: { State: { Health: { Status: 'healthy', Log: [{ ExitCode: 0 }, { ExitCode: 0 }] } } } })]); await healthCli({ navy: 'env-1' }); const text = (0, _stripAnsi.default)(lastLoggedString()); (0, _chai.expect)(text).to.contain('█ █'); (0, _chai.expect)(text).not.to.contain('x'); }); it('should render only failure markers when every entry has a non-zero ExitCode', async function () { navyStub.ps.resolves([makeService({ raw: { State: { Health: { Status: 'unhealthy', Log: [{ ExitCode: 1 }, { ExitCode: 2 }] } } } })]); await healthCli({ navy: 'env-1' }); const text = (0, _stripAnsi.default)(lastLoggedString()); (0, _chai.expect)(text).to.contain('x x'); (0, _chai.expect)(text).not.to.contain('█'); }); it('should render mixed success and failure markers based on each entry ExitCode', async function () { navyStub.ps.resolves([makeService({ raw: { State: { Health: { Status: 'healthy', Log: [{ ExitCode: 0 }, { ExitCode: 1 }, { ExitCode: 0 }] } } } })]); await healthCli({ navy: 'env-1' }); const text = (0, _stripAnsi.default)(lastLoggedString()); (0, _chai.expect)(text).to.contain('█ x █'); }); it('should render an empty history string when Log is an empty array', async function () { navyStub.ps.resolves([makeService({ raw: { State: { Health: { Status: 'healthy', Log: [] } } } })]); await healthCli({ navy: 'env-1' }); const text = (0, _stripAnsi.default)(lastLoggedString()); (0, _chai.expect)(text).to.contain('Healthy'); (0, _chai.expect)(text).not.to.contain('█'); (0, _chai.expect)(text).not.to.contain('x'); }); }); });