1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

task: update health endpoint to only say if express is ready (#1847)

This commit is contained in:
Christopher Kolstad 2022-07-22 11:47:58 +02:00 committed by GitHub
parent 5bacc7ba36
commit 2c9f4408bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 54 deletions

View File

@ -5,7 +5,6 @@ import { createTestConfig } from '../../test/config/test-config';
import createStores from '../../test/fixtures/store';
import getLogger from '../../test/fixtures/no-logger';
import getApp from '../app';
import { IUnleashStores } from '../types';
async function getSetup() {
const stores = createStores();
@ -25,12 +24,10 @@ async function getSetup() {
}
let request;
let destroy;
let stores;
beforeEach(async () => {
const setup = await getSetup();
request = setup.request;
destroy = setup.destroy;
stores = setup.stores;
});
afterEach(() => {
@ -38,45 +35,13 @@ afterEach(() => {
getLogger.setMuteError(false);
});
test('should give 500 when db is failing', async () => {
jest.spyOn(global.console, 'error').mockImplementation(() => jest.fn());
const config = createTestConfig();
const failingStores: Partial<IUnleashStores> = {
// @ts-ignore
featureTypeStore: {
getAll: () => Promise.reject(new Error('db error')),
},
clientMetricsStore: {
// @ts-ignore
on: () => {},
},
};
// @ts-ignore
const services = createServices(failingStores, config);
// @ts-ignore
const app = await getApp(createTestConfig(), failingStores, services);
request = supertest(app);
getLogger.setMuteError(true);
expect.assertions(2);
stores.featureToggleStore.getAll = () =>
Promise.reject(new Error('db error'));
return request
.get('/health')
.expect(500)
.expect((res) => {
expect(res.status).toBe(500);
expect(res.body.health).toBe('BAD');
});
test('should give 200 when ready', async () => {
await request.get('/health').expect(200);
});
test('should give 200 when db is not failing', () => {
expect.assertions(0);
return request.get('/health').expect(200);
});
test('should give health=GOOD when db is not failing', () => {
test('should give health=GOOD when ready', async () => {
expect.assertions(2);
return request
await request
.get('/health')
.expect(200)
.expect((res) => {

View File

@ -2,7 +2,6 @@ import { Request, Response } from 'express';
import { IUnleashConfig } from '../types/option';
import { IUnleashServices } from '../types/services';
import { Logger } from '../logger';
import HealthService from '../services/health-service';
import { OpenApiService } from '../services/openapi-service';
import Controller from './controller';
@ -15,19 +14,13 @@ export class HealthCheckController extends Controller {
private openApiService: OpenApiService;
private healthService: HealthService;
constructor(
config: IUnleashConfig,
{
healthService,
openApiService,
}: Pick<IUnleashServices, 'healthService' | 'openApiService'>,
{ openApiService }: Pick<IUnleashServices, 'openApiService'>,
) {
super(config);
this.logger = config.getLogger('health-check.js');
this.openApiService = openApiService;
this.healthService = healthService;
this.route({
method: 'get',
@ -51,12 +44,6 @@ export class HealthCheckController extends Controller {
_: Request,
res: Response<HealthCheckSchema>,
): Promise<void> {
try {
await this.healthService.dbIsUp();
res.status(200).json({ health: 'GOOD' });
} catch (e) {
this.logger.error('Could not select from features, error was: ', e);
res.status(500).json({ health: 'BAD' });
}
res.status(200).json({ health: 'GOOD' });
}
}