Skip to content

Commit

Permalink
test: refactor tests to use new terminus api
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunnerLivio committed Aug 27, 2021
1 parent ba6eee0 commit 5c5ffb1
Show file tree
Hide file tree
Showing 24 changed files with 21,760 additions and 1,068 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ jobs:

steps:
- uses: actions/checkout@v2
- run: docker-compose build
- run: docker-compose run lib
env:
CI: true
- name: Use Node.js 14
uses: actions/setup-node@v2
with:
node-version: 14
- run: docker-compose up -d
- run: npm ci
- run: npm run test:e2e
42 changes: 36 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ export class HealthController {
]);
}
}

```

If everything is set up correctly, you can access the healthcheck on `http://localhost:3000/health`.

```json

{
"status": "ok",
"info": {
Expand All @@ -92,21 +90,53 @@ If everything is set up correctly, you can access the healthcheck on `http://loc
}
}
}

```

For more information, [see docs](https://docs.nestjs.com/recipes/terminus).
You can find more samples in the [samples/](https://github.com/nestjs/terminus/tree/master/sample) folder of this repository.

## Contribute

In order to get started, first read through our [Contributing guidelines](https://github.com/nestjs/terminus/blob/master/CONTRIBUTING.md).

### Setup

Setup the development environment by following these instructions:

1. Fork & Clone the repository
2. Install the dependencies

```bash
npm install

# To rebuild the project, run
npm run build
```

### Test

For unit testing run the following command:

```bash
npm run test
```

For e2e testing, make sure you have docker installed

```bash
docker-compose up -d
npm run test:e2e
```

## Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).

## Stay in touch

* Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) and [Livio Brunner](https://brunnerliv.io)
* Website - [https://nestjs.com](https://nestjs.com/)
* Twitter - [@nestframework](https://twitter.com/nestframework)
- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) and [Livio Brunner](https://brunnerliv.io)
- Website - [https://nestjs.com](https://nestjs.com/)
- Twitter - [@nestframework](https://twitter.com/nestframework)

## License

Expand Down
11 changes: 0 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,5 @@ services:
ports:
- 6379:6379

lib:
build:
context: .
depends_on:
- mysql
- mongodb
networks:
- overlay
environment:
WAIT_HOSTS: mysql:3306, mongodb:27017

networks:
overlay:
46 changes: 0 additions & 46 deletions e2e/custom-logger.e2e-spec.ts

This file was deleted.

38 changes: 0 additions & 38 deletions e2e/fasitfy.e2e-spec.ts

This file was deleted.

169 changes: 95 additions & 74 deletions e2e/health-checks/disk.health.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,112 @@
import * as request from 'supertest';
import { INestApplication } from '@nestjs/common';

import Axios from 'axios';
import { DiskHealthIndicator, TerminusModuleOptions } from '../../lib';
import { bootstrapModule } from '../helper/bootstrap-module';
import * as checkDiskSpace from 'check-disk-space';

describe('Disk Health', () => {
import { bootstrapTestingModule, DynamicHealthEndpointFn } from '../helper';

describe('DiskHealthIndicator', () => {
let app: INestApplication;
let port: number;
let getTerminusOptions: (disk: DiskHealthIndicator) => TerminusModuleOptions;
beforeEach(async () => {
const { free } = await checkDiskSpace('/');
getTerminusOptions = (
disk: DiskHealthIndicator,
): TerminusModuleOptions => ({
endpoints: [
{
url: '/health',
healthIndicators: [
async () =>
disk.checkStorage('disk', { path: '/', threshold: free + 90000 }),
],
let setHealthEndpoint: DynamicHealthEndpointFn;

beforeEach(
() => (setHealthEndpoint = bootstrapTestingModule().setHealthEndpoint),
);

describe('#checkStorage', () => {
it('should check if the disk threshold has not exceeded', async () => {
const { free, size } = await checkDiskSpace('/');
app = await setHealthEndpoint(({ healthCheck, disk }) =>
healthCheck.check([
() =>
disk.checkStorage('disk', {
path: '/',
threshold: size - free + 90000,
}),
]),
).start();

const details = { disk: { status: 'up' } };

return request(app.getHttpServer()).get('/health').expect(200).expect({
status: 'ok',
info: details,
error: {},
details,
});
});

it('should check if correctly displays a threshold exceeded error', async () => {
app = await setHealthEndpoint(({ healthCheck, disk }) =>
healthCheck.check([
() => disk.checkStorage('disk', { path: '/', threshold: 0 }),
]),
).start();

const details = {
disk: {
status: 'down',
message: 'Used disk storage exceeded the set threshold',
},
],
};

return request(app.getHttpServer()).get('/health').expect(503).expect({
status: 'error',
info: {},
error: details,
details,
});
});
});

// it('should check if the disk threshold is not exceeded', async () => {
// [app, port] = await bootstrapModule({
// inject: [DiskHealthIndicator],
// useFactory: getTerminusOptions,
// });
// const response = await Axios.get(`http://0.0.0.0:${port}/health`);
// const info = { disk: { status: 'up' } };
// expect(response.status).toBe(200);
// expect(response.data).toEqual({
// status: 'ok',
// info,
// details: info,
// });
// });
it('should check if the disk thresholdPercent has not exceeded', async () => {
const { free, size } = await checkDiskSpace('/');
const thresholdPercent = (size - free) / size;
app = await setHealthEndpoint(({ healthCheck, disk }) =>
healthCheck.check([
async () =>
disk.checkStorage('disk', {
path: '/',
thresholdPercent: thresholdPercent + 0.2,
}),
]),
).start();

it('should check if correctly displays a threshold exceeded error', async () => {
[app, port] = await bootstrapModule({
inject: [DiskHealthIndicator],
useFactory: (disk: DiskHealthIndicator): TerminusModuleOptions => ({
endpoints: [
{
url: '/health',
healthIndicators: [
async () =>
disk.checkStorage('disk', { path: '/', threshold: 0 }),
],
},
],
}),
return request(app.getHttpServer())
.get('/health')
.expect(200)
.expect({
status: 'ok',
info: { disk: { status: 'up' } },
error: {},
details: { disk: { status: 'up' } },
});
});

try {
await Axios.get(`http://0.0.0.0:${port}/health`);
} catch (error) {
const details = { disk: { status: 'down', message: expect.any(String) } };
expect(error.response.status).toBe(503);
expect(error.response.data).toEqual({
it('should check if correctly displays a thresholdPercent exceeded error', async () => {
const { free, size } = await checkDiskSpace('/');
const thresholdPercent = (size - free) / size;
app = await setHealthEndpoint(({ healthCheck, disk }) =>
healthCheck.check([
async () =>
disk.checkStorage('disk', {
path: '/',
thresholdPercent: thresholdPercent - 0.2,
}),
]),
).start();

const details = {
disk: {
status: 'down',
message: 'Used disk storage exceeded the set threshold',
},
};

return request(app.getHttpServer()).get('/health').expect(503).expect({
status: 'error',
info: {},
error: details,
details,
});
}
});

it('should check if the disk threshold is not exceeded using thresholdPercent', async () => {
const { free, size } = await checkDiskSpace('/');
const thresholdPercent = (size - free) / size;
[app, port] = await bootstrapModule({
inject: [DiskHealthIndicator],
useFactory: (disk: DiskHealthIndicator): TerminusModuleOptions => ({
endpoints: [
{
url: '/health',
healthIndicators: [
async () =>
disk.checkStorage('disk', { path: '/', thresholdPercent }),
],
},
],
}),
});
});

Expand Down
Loading

0 comments on commit 5c5ffb1

Please sign in to comment.