|
| 1 | +import chai from 'chai' |
| 2 | +import sinon from 'sinon' |
| 3 | +import sinonChai from 'sinon-chai' |
| 4 | + |
| 5 | +import * as adminHealth from '../../../../src/utils/admin-health' |
| 6 | +import { |
| 7 | + buildReadyzSnapshot, |
| 8 | + getReadyzRequestHandler, |
| 9 | + resetReadyzSnapshotCache, |
| 10 | +} from '../../../../src/handlers/request-handlers/get-readyz-request-handler' |
| 11 | + |
| 12 | +chai.use(sinonChai) |
| 13 | +const { expect } = chai |
| 14 | + |
| 15 | +describe('buildReadyzSnapshot', () => { |
| 16 | + it('returns ok when database and redis are healthy', () => { |
| 17 | + expect(buildReadyzSnapshot({ ok: true }, { ok: true })).to.deep.equal({ |
| 18 | + status: 'ok', |
| 19 | + database: { ok: true }, |
| 20 | + redis: { ok: true }, |
| 21 | + }) |
| 22 | + }) |
| 23 | + |
| 24 | + it('returns unavailable when either dependency is unhealthy', () => { |
| 25 | + expect(buildReadyzSnapshot({ ok: false }, { ok: true })).to.deep.equal({ |
| 26 | + status: 'unavailable', |
| 27 | + database: { ok: false }, |
| 28 | + redis: { ok: true }, |
| 29 | + }) |
| 30 | + }) |
| 31 | +}) |
| 32 | + |
| 33 | +describe('getReadyzRequestHandler', () => { |
| 34 | + let sandbox: sinon.SinonSandbox |
| 35 | + let collectAdminHealthSnapshotStub: sinon.SinonStub |
| 36 | + |
| 37 | + const healthyAdminSnapshot = { |
| 38 | + status: 'ok' as const, |
| 39 | + uptimeSeconds: 42, |
| 40 | + worker: { type: 'primary' }, |
| 41 | + database: { ok: true }, |
| 42 | + redis: { ok: true }, |
| 43 | + } |
| 44 | + |
| 45 | + const createResponse = () => |
| 46 | + ({ |
| 47 | + status: sinon.stub().returnsThis(), |
| 48 | + setHeader: sinon.stub().returnsThis(), |
| 49 | + send: sinon.stub().returnsThis(), |
| 50 | + }) as any |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + sandbox = sinon.createSandbox() |
| 54 | + collectAdminHealthSnapshotStub = sandbox.stub(adminHealth, 'collectAdminHealthSnapshot') |
| 55 | + resetReadyzSnapshotCache() |
| 56 | + }) |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + sandbox.restore() |
| 60 | + resetReadyzSnapshotCache() |
| 61 | + }) |
| 62 | + |
| 63 | + it('responds with 200 JSON when dependencies are ready', async () => { |
| 64 | + collectAdminHealthSnapshotStub.resolves(healthyAdminSnapshot) |
| 65 | + |
| 66 | + const res = createResponse() |
| 67 | + const next = sinon.stub() |
| 68 | + |
| 69 | + await getReadyzRequestHandler({} as any, res, next) |
| 70 | + |
| 71 | + expect(res.status).to.have.been.calledOnceWithExactly(200) |
| 72 | + expect(res.setHeader).to.have.been.calledWith('content-type', 'application/json; charset=utf-8') |
| 73 | + expect(res.setHeader).to.have.been.calledWith('cache-control', 'no-store') |
| 74 | + expect(res.send).to.have.been.calledOnceWithExactly({ |
| 75 | + status: 'ok', |
| 76 | + database: { ok: true }, |
| 77 | + redis: { ok: true }, |
| 78 | + }) |
| 79 | + expect(next).to.have.been.calledOnce |
| 80 | + }) |
| 81 | + |
| 82 | + it('responds with 503 JSON when redis is unavailable', async () => { |
| 83 | + collectAdminHealthSnapshotStub.resolves({ |
| 84 | + ...healthyAdminSnapshot, |
| 85 | + status: 'degraded', |
| 86 | + redis: { ok: false }, |
| 87 | + }) |
| 88 | + |
| 89 | + const res = createResponse() |
| 90 | + const next = sinon.stub() |
| 91 | + |
| 92 | + await getReadyzRequestHandler({} as any, res, next) |
| 93 | + |
| 94 | + expect(res.status).to.have.been.calledOnceWithExactly(503) |
| 95 | + expect(res.send).to.have.been.calledOnceWithExactly({ |
| 96 | + status: 'unavailable', |
| 97 | + database: { ok: true }, |
| 98 | + redis: { ok: false }, |
| 99 | + }) |
| 100 | + expect(next).to.have.been.calledOnce |
| 101 | + }) |
| 102 | + |
| 103 | + it('responds with 503 JSON when database is unavailable', async () => { |
| 104 | + collectAdminHealthSnapshotStub.resolves({ |
| 105 | + ...healthyAdminSnapshot, |
| 106 | + status: 'degraded', |
| 107 | + database: { ok: false }, |
| 108 | + }) |
| 109 | + |
| 110 | + const res = createResponse() |
| 111 | + const next = sinon.stub() |
| 112 | + |
| 113 | + await getReadyzRequestHandler({} as any, res, next) |
| 114 | + |
| 115 | + expect(res.status).to.have.been.calledOnceWithExactly(503) |
| 116 | + expect(res.send).to.have.been.calledOnceWithExactly({ |
| 117 | + status: 'unavailable', |
| 118 | + database: { ok: false }, |
| 119 | + redis: { ok: true }, |
| 120 | + }) |
| 121 | + expect(next).to.have.been.calledOnce |
| 122 | + }) |
| 123 | + |
| 124 | + it('responds with 503 JSON when dependency collection throws', async () => { |
| 125 | + collectAdminHealthSnapshotStub.rejects(new Error('boom')) |
| 126 | + |
| 127 | + const res = createResponse() |
| 128 | + const next = sinon.stub() |
| 129 | + |
| 130 | + await getReadyzRequestHandler({} as any, res, next) |
| 131 | + |
| 132 | + expect(res.status).to.have.been.calledOnceWithExactly(503) |
| 133 | + expect(res.setHeader).to.have.been.calledWith('cache-control', 'no-store') |
| 134 | + expect(res.send).to.have.been.calledOnceWithExactly({ |
| 135 | + status: 'unavailable', |
| 136 | + database: { ok: false }, |
| 137 | + redis: { ok: false }, |
| 138 | + }) |
| 139 | + expect(next).to.have.been.calledOnce |
| 140 | + }) |
| 141 | + |
| 142 | + it('reuses a cached snapshot within the 1s TTL', async () => { |
| 143 | + collectAdminHealthSnapshotStub.resolves(healthyAdminSnapshot) |
| 144 | + |
| 145 | + const res = createResponse() |
| 146 | + const next = sinon.stub() |
| 147 | + |
| 148 | + await getReadyzRequestHandler({} as any, res, next) |
| 149 | + await getReadyzRequestHandler({} as any, res, next) |
| 150 | + |
| 151 | + expect(collectAdminHealthSnapshotStub).to.have.been.calledOnce |
| 152 | + }) |
| 153 | +}) |
0 commit comments