/
health.acceptance.ts
256 lines (229 loc) · 6.77 KB
/
health.acceptance.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright IBM Corp. and LoopBack contributors 2019. All Rights Reserved.
// Node module: @loopback/health
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {RestApplication, RestServer, RestServerConfig} from '@loopback/rest';
import {
Client,
createRestAppClient,
expect,
givenHttpServerConfig,
validateApiSpec,
} from '@loopback/testlab';
import {HealthComponent} from '../..';
import {HealthBindings, HealthTags} from '../../keys';
import {HealthConfig, LiveCheck, ReadyCheck} from '../../types';
describe('Health (acceptance)', () => {
let app: RestApplication;
let request: Client;
afterEach(async () => {
if (app) await app.stop();
(app as unknown) = undefined;
});
context('with default config', () => {
beforeEach(async () => {
app = givenRestApplication();
app.component(HealthComponent);
await app.start();
request = createRestAppClient(app);
});
it('exposes health at "/health/"', async () => {
await request.get('/health').expect(200, {status: 'UP', checks: []});
});
it('exposes health at "/ready/"', async () => {
await request.get('/ready').expect(200, {status: 'UP', checks: []});
});
it('exposes health at "/live/"', async () => {
await request.get('/live').expect(200, {status: 'UP', checks: []});
});
it('removes listener from the process', async () => {
const healthChecker = await app.get(HealthBindings.HEALTH_CHECKER);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const onShutdownRequest = (healthChecker as any).onShutdownRequest;
let listeners = process.listeners('SIGTERM');
expect(listeners).to.containEql(onShutdownRequest);
await app.stop();
listeners = process.listeners('SIGTERM');
expect(listeners).to.not.containEql(onShutdownRequest);
});
it('hides the endpoints from the openapi spec', async () => {
const server = await app.getServer(RestServer);
const spec = await server.getApiSpec();
expect(spec.paths).to.be.empty();
await validateApiSpec(spec);
});
});
context('with discovered live/ready checks', () => {
beforeEach(async () => {
app = givenRestApplication();
app.component(HealthComponent);
app
.bind<LiveCheck>('health.MockLiveCheck')
.to(() => Promise.resolve())
.tag(HealthTags.LIVE_CHECK);
app
.bind<ReadyCheck>('health.MockReadyCheck')
.to(() => Promise.resolve())
.tag(HealthTags.READY_CHECK);
await app.start();
request = createRestAppClient(app);
});
it('exposes health at "/health/"', async () => {
await request.get('/health').expect(200, {
status: 'UP',
checks: [
{
name: 'health.MockReadyCheck',
state: 'UP',
data: {
reason: '',
},
},
{
name: 'health.MockLiveCheck',
state: 'UP',
data: {
reason: '',
},
},
],
});
});
it('exposes health at "/ready/"', async () => {
await request.get('/ready').expect(200, {
status: 'UP',
checks: [
{
name: 'health.MockReadyCheck',
state: 'UP',
data: {
reason: '',
},
},
],
});
});
it('exposes health at "/live/"', async () => {
await request.get('/live').expect(200, {
status: 'UP',
checks: [
{
name: 'health.MockLiveCheck',
state: 'UP',
data: {
reason: '',
},
},
],
});
});
});
context('with discovered live/ready checks that fail', () => {
beforeEach(async () => {
app = givenRestApplication();
app.component(HealthComponent);
app
.bind<LiveCheck>('health.MockLiveCheck')
.to(() => Promise.reject())
.tag(HealthTags.LIVE_CHECK);
app
.bind<ReadyCheck>('health.MockReadyCheck')
.to(() => Promise.reject())
.tag(HealthTags.READY_CHECK);
await app.start();
request = createRestAppClient(app);
});
it('exposes health at "/health/"', async () => {
await request.get('/health').expect(503, {
status: 'DOWN',
checks: [
{
name: 'health.MockReadyCheck',
state: 'DOWN',
data: {
reason: '',
},
},
{
name: 'health.MockLiveCheck',
state: 'DOWN',
data: {
reason: '',
},
},
],
});
});
it('exposes health at "/ready/"', async () => {
await request.get('/ready').expect(503, {
status: 'DOWN',
checks: [
{
name: 'health.MockReadyCheck',
state: 'DOWN',
data: {
reason: '',
},
},
],
});
});
it('exposes health at "/live/"', async () => {
await request.get('/live').expect(500, {
status: 'DOWN',
checks: [
{
name: 'health.MockLiveCheck',
state: 'DOWN',
data: {
reason: '',
},
},
],
});
});
});
context('with custom endpoint basePath', () => {
it('honors prefix', async () => {
await givenAppWithCustomConfig({
healthPath: '/internal/health',
});
await request
.get('/internal/health')
.expect(200, {status: 'UP', checks: []});
await request.get('/health').expect(404);
});
});
context('with defaultHealth disabled', () => {
it('does not expose /health', async () => {
await givenAppWithCustomConfig({
disabled: true,
});
await request.get('/health').expect(404);
});
});
context('with openApiSpec enabled', () => {
beforeEach(async () => {
await givenAppWithCustomConfig({
openApiSpec: true,
});
});
it('adds the endpoints to the openapi spec', async () => {
const server = await app.getServer(RestServer);
const spec = await server.getApiSpec();
expect(spec.paths).to.have.properties('/health', '/live', '/ready');
await validateApiSpec(spec);
});
});
async function givenAppWithCustomConfig(config: HealthConfig) {
app = givenRestApplication();
app.bind(HealthBindings.CONFIG).to(config);
app.component(HealthComponent);
await app.start();
request = createRestAppClient(app);
}
function givenRestApplication(config?: RestServerConfig) {
const rest = Object.assign({}, givenHttpServerConfig(), config);
return new RestApplication({rest});
}
});