Skip to content

Commit

Permalink
ci: setup aggregator int tests (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
wrn14897 authored Jan 8, 2024
1 parent ea9acde commit 9e617ed
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/mean-balloons-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hyperdx/api': patch
'@hyperdx/app': patch
---

ci: setup aggregator int tests
19 changes: 18 additions & 1 deletion packages/api/src/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,24 @@ class MockServer extends Server {
}
}

export const getServer = () => new MockServer();
class MockAPIServer extends MockServer {
protected readonly appType = 'api';
}

class MockAggregatorServer extends MockServer {
protected readonly appType = 'aggregator';
}

export const getServer = (appType: 'api' | 'aggregator' = 'api') => {
switch (appType) {
case 'api':
return new MockAPIServer();
case 'aggregator':
return new MockAggregatorServer();
default:
throw new Error(`Invalid app type: ${appType}`);
}
};

export const getAgent = (server: MockServer) =>
request.agent(server.getHttpServer());
Expand Down
115 changes: 115 additions & 0 deletions packages/api/src/routers/aggregator/__tests__/root.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import _ from 'lodash';

import * as clickhouse from '@/clickhouse';
import { createTeam } from '@/controllers/team';
import {
clearClickhouseTables,
clearDBCollections,
closeDB,
getAgent,
getServer,
} from '@/fixtures';
import { sleep } from '@/utils/common';

describe('aggregator root router', () => {
const server = getServer('aggregator');

beforeAll(async () => {
await server.start();
});

afterEach(async () => {
await clearDBCollections();
await clearClickhouseTables();
});

afterAll(async () => {
await server.closeHttpServer();
await closeDB();
});

it('GET /health', async () => {
const agent = await getAgent(server);
await agent.get('/health').expect(200);
});

it('POST / -> should return 400 if no logs', async () => {
const agent = await getAgent(server);
await agent.post('/').send({}).expect(400);
});

it('POST / -> should ingest logs', async () => {
const team = await createTeam({ name: 'test-team' });
const agent = await getAgent(server);
await agent.post('/').send([
{
b: {
_hdx_body: 'Initializing ClickHouse...',
level: 'info',
message: 'Initializing ClickHouse...',
},
h: '509a8b2dea19',
hdx_platform: 'nodejs',
hdx_token: team.apiKey,
hdx_token_hash: '2f4da895de6a20c100c28daaa5c07c51',
path: '/',
r: { level: 'info', message: 'Initializing ClickHouse...' },
s_id: null,
sn: 0,
st: 'info',
sv: 'hdx-oss-dev-api',
t_id: null,
ts: 1704517334214000000,
tso: 1704517336156579600,
},
]);

// wait for data to be committed to clickhouse
await sleep(500);

const resp = await clickhouse.client.query({
query: `SELECT * FROM default.${clickhouse.TableName.LogStream}`,
format: 'JSON',
});
const result: any = await resp.json();
expect(result.data.length).toBe(1);
expect(result.data.map((row: any) => _.omit(row, ['id', '_created_at'])))
.toMatchInlineSnapshot(`
Array [
Object {
"_host": "509a8b2dea19",
"_namespace": "",
"_platform": "nodejs",
"_service": "hdx-oss-dev-api",
"_source": "{\\"level\\":\\"info\\",\\"message\\":\\"Initializing ClickHouse...\\"}",
"bool.names": Array [],
"bool.values": Array [],
"end_timestamp": "1970-01-01T00:00:00.000000000Z",
"number.names": Array [],
"number.values": Array [],
"observed_timestamp": "2024-01-06T05:02:16.156579600Z",
"parent_span_id": "",
"severity_number": 0,
"severity_text": "info",
"span_id": "",
"span_name": "",
"string.names": Array [
"_hdx_body",
"level",
"message",
],
"string.values": Array [
"Initializing ClickHouse...",
"info",
"Initializing ClickHouse...",
],
"timestamp": "2024-01-06T05:02:14.214000000Z",
"trace_id": "",
"type": "log",
},
]
`);
});

// TODO: test metrics
});
4 changes: 3 additions & 1 deletion packages/api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import logger from './utils/logger';
import redisClient from './utils/redis';

export default class Server {
protected readonly appType = config.APP_TYPE;

protected httpServer!: http.Server;

private async createServer() {
switch (config.APP_TYPE) {
switch (this.appType) {
case 'api':
return http.createServer(
// eslint-disable-next-line n/no-unsupported-features/es-syntax
Expand Down

0 comments on commit 9e617ed

Please sign in to comment.