mirror of
https://github.com/Unleash/unleash.git
synced 2025-06-23 01:16:27 +02:00
task: update health endpoint to only say if express is ready (#1847)
This commit is contained in:
parent
5bacc7ba36
commit
2c9f4408bc
@ -5,7 +5,6 @@ import { createTestConfig } from '../../test/config/test-config';
|
|||||||
import createStores from '../../test/fixtures/store';
|
import createStores from '../../test/fixtures/store';
|
||||||
import getLogger from '../../test/fixtures/no-logger';
|
import getLogger from '../../test/fixtures/no-logger';
|
||||||
import getApp from '../app';
|
import getApp from '../app';
|
||||||
import { IUnleashStores } from '../types';
|
|
||||||
|
|
||||||
async function getSetup() {
|
async function getSetup() {
|
||||||
const stores = createStores();
|
const stores = createStores();
|
||||||
@ -25,12 +24,10 @@ async function getSetup() {
|
|||||||
}
|
}
|
||||||
let request;
|
let request;
|
||||||
let destroy;
|
let destroy;
|
||||||
let stores;
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const setup = await getSetup();
|
const setup = await getSetup();
|
||||||
request = setup.request;
|
request = setup.request;
|
||||||
destroy = setup.destroy;
|
destroy = setup.destroy;
|
||||||
stores = setup.stores;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@ -38,45 +35,13 @@ afterEach(() => {
|
|||||||
getLogger.setMuteError(false);
|
getLogger.setMuteError(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should give 500 when db is failing', async () => {
|
test('should give 200 when ready', async () => {
|
||||||
jest.spyOn(global.console, 'error').mockImplementation(() => jest.fn());
|
await request.get('/health').expect(200);
|
||||||
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 db is not failing', () => {
|
test('should give health=GOOD when ready', async () => {
|
||||||
expect.assertions(0);
|
|
||||||
return request.get('/health').expect(200);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should give health=GOOD when db is not failing', () => {
|
|
||||||
expect.assertions(2);
|
expect.assertions(2);
|
||||||
return request
|
await request
|
||||||
.get('/health')
|
.get('/health')
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.expect((res) => {
|
.expect((res) => {
|
||||||
|
@ -2,7 +2,6 @@ import { Request, Response } from 'express';
|
|||||||
import { IUnleashConfig } from '../types/option';
|
import { IUnleashConfig } from '../types/option';
|
||||||
import { IUnleashServices } from '../types/services';
|
import { IUnleashServices } from '../types/services';
|
||||||
import { Logger } from '../logger';
|
import { Logger } from '../logger';
|
||||||
import HealthService from '../services/health-service';
|
|
||||||
import { OpenApiService } from '../services/openapi-service';
|
import { OpenApiService } from '../services/openapi-service';
|
||||||
|
|
||||||
import Controller from './controller';
|
import Controller from './controller';
|
||||||
@ -15,19 +14,13 @@ export class HealthCheckController extends Controller {
|
|||||||
|
|
||||||
private openApiService: OpenApiService;
|
private openApiService: OpenApiService;
|
||||||
|
|
||||||
private healthService: HealthService;
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
config: IUnleashConfig,
|
config: IUnleashConfig,
|
||||||
{
|
{ openApiService }: Pick<IUnleashServices, 'openApiService'>,
|
||||||
healthService,
|
|
||||||
openApiService,
|
|
||||||
}: Pick<IUnleashServices, 'healthService' | 'openApiService'>,
|
|
||||||
) {
|
) {
|
||||||
super(config);
|
super(config);
|
||||||
this.logger = config.getLogger('health-check.js');
|
this.logger = config.getLogger('health-check.js');
|
||||||
this.openApiService = openApiService;
|
this.openApiService = openApiService;
|
||||||
this.healthService = healthService;
|
|
||||||
|
|
||||||
this.route({
|
this.route({
|
||||||
method: 'get',
|
method: 'get',
|
||||||
@ -51,12 +44,6 @@ export class HealthCheckController extends Controller {
|
|||||||
_: Request,
|
_: Request,
|
||||||
res: Response<HealthCheckSchema>,
|
res: Response<HealthCheckSchema>,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
res.status(200).json({ health: 'GOOD' });
|
||||||
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' });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user