Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Logs UI] Complete test suite for #70413 #70729

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 87 additions & 4 deletions x-pack/plugins/infra/public/utils/logs_overview_fetches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,36 @@
import { coreMock } from 'src/core/public/mocks';
import { dataPluginMock } from 'src/plugins/data/public/mocks';
import { CoreStart } from 'kibana/public';
import { getLogsHasDataFetcher } from './logs_overview_fetchers';
import { getLogsHasDataFetcher, getLogsOverviewDataFetcher } from './logs_overview_fetchers';
import { InfraClientStartDeps, InfraClientStartExports } from '../types';
import { callFetchLogSourceStatusAPI } from '../containers/logs/log_source/api/fetch_log_source_status';
import { callFetchLogSourceConfigurationAPI } from '../containers/logs/log_source/api/fetch_log_source_configuration';

// Note
// Calls to `.mock*` functions will fail the typecheck because how jest does the mocking.
// The calls will be preluded with a `@ts-expect-error`
jest.mock('../containers/logs/log_source/api/fetch_log_source_status');
jest.mock('../containers/logs/log_source/api/fetch_log_source_configuration');

function setup() {
const core = coreMock.createStart();
const data = dataPluginMock.createStartContract();

// `dataResponder.mockReturnValue()` will be the `response` in
//
// const searcher = data.search.getSearchStrategy('sth');
// searcher.search(...).subscribe((**response**) => {});
//
const dataResponder = jest.fn();

(data.search.getSearchStrategy as jest.Mock).mockReturnValue({
search: () => ({
subscribe: (success: Function) => {
success(dataResponder());
},
}),
});

const mockedGetStartServices = jest.fn(() => {
const deps = { data };
return Promise.resolve([
Expand All @@ -27,7 +44,7 @@ function setup() {
void 0 as InfraClientStartExports,
]) as Promise<[CoreStart, InfraClientStartDeps, InfraClientStartExports]>;
});
return { core, mockedGetStartServices };
return { core, mockedGetStartServices, dataResponder };
}

describe('Logs UI Observability Homepage Functions', () => {
Expand Down Expand Up @@ -68,8 +85,74 @@ describe('Logs UI Observability Homepage Functions', () => {
});

describe('getLogsOverviewDataFetcher()', () => {
it.skip('should work', async () => {
// Pending
it('should work', async () => {
// Two minute range, to test the normalization
const params = {
startTime: '2020-06-29T11:38:00.000Z',
endTime: '2020-06-29T11:40:00.000Z',
bucketSize: '30s',
};

const { mockedGetStartServices, dataResponder } = setup();

// @ts-expect-error
callFetchLogSourceConfigurationAPI.mockResolvedValue({
data: { configuration: { logAlias: 'filebeat', fields: { timestamp: '@timestamp' } } },
});

dataResponder.mockReturnValue({
rawResponse: {
aggregations: {
stats: {
// Stats are for two minutes. The normalized rate per minute will be half
buckets: [{ key: 'nginx.access', doc_count: 100 }],
},
series: {
// Buckets every 30 seconds. The normalized rate per minute will be double
buckets: [
{
key: 1593430680000,
dataset: { buckets: [{ key: 'nginx.access', doc_count: 25 }] },
},
{
key: 1593430710000,
dataset: { buckets: [{ key: 'nginx.access', doc_count: 50 }] },
},
{
key: 1593430740000,
dataset: { buckets: [{ key: 'nginx.access', doc_count: 75 }] },
},
{
key: 1593430770000,
dataset: { buckets: [{ key: 'nginx.access', doc_count: 100 }] },
},
],
},
},
},
});

const fetchData = getLogsOverviewDataFetcher(mockedGetStartServices);
const response = await fetchData(params);

expect(response).toMatchObject({
title: 'Logs',
appLink: `/app/logs/stream?logPosition=(end:'${params.endTime}',start:'${params.startTime}')`,
stats: {
'nginx.access': { label: 'nginx.access', type: 'number', value: 50 },
},
series: {
'nginx.access': {
label: 'nginx.access',
coordinates: [
{ x: 1593430680000, y: 50 },
{ x: 1593430710000, y: 100 },
{ x: 1593430740000, y: 150 },
{ x: 1593430770000, y: 200 },
],
},
},
});
});
});
});