kibana/x-pack/plugins/apm/server/lib/helpers/setup_request.test.ts
Justin Kambic 2f7603de18
[UX Dashboard] Unify processor event (#135367)
* Migrate page load distribution query to use data plugin.

* Migrate breakdowns to use data plugin.

* Delete page load dist breakdown from APM server.

* Update comments.

* Unify `ProcessorEvent` enum.

* Fix linting and delete old code.

* Fix import.

* Delete obsolete snapshots.

* [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix'

* Fix remaining conflicts

* Fix remaining conflicts

* Fix import

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: emilioalvap <emilio.alvarezpineiro@elastic.co>
2022-08-02 15:22:19 -04:00

200 lines
5.2 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { elasticsearchServiceMock } from '@kbn/core/server/mocks';
import { setupRequest } from './setup_request';
import { APMConfig } from '../..';
import { APMRouteHandlerResources } from '../../routes/typings';
import { ProcessorEvent } from '@kbn/observability-plugin/common';
import { getApmIndices } from '../../routes/settings/apm_indices/get_apm_indices';
jest.mock('../../routes/settings/apm_indices/get_apm_indices', () => ({
getApmIndices: async () =>
({
sourcemap: 'apm-*',
error: 'apm-*',
onboarding: 'apm-*',
span: 'apm-*',
transaction: 'apm-*',
metric: 'apm-*',
apmAgentConfigurationIndex: 'apm-*',
} as Awaited<ReturnType<typeof getApmIndices>>),
}));
function getMockResources() {
const esClientMock = elasticsearchServiceMock.createScopedClusterClient();
// @ts-expect-error incomplete definition
esClientMock.asCurrentUser.search.mockResponse({});
// @ts-expect-error incomplete definition
esClientMock.asInternalUser.search.mockResponse({});
const mockResources = {
config: new Proxy(
{},
{
get: () => 'apm-*',
}
) as APMConfig,
params: {
query: {
_inspect: false,
},
},
context: {
core: {
elasticsearch: {
client: esClientMock,
},
uiSettings: {
client: {
get: jest.fn().mockResolvedValue(false),
},
},
savedObjects: {
client: {
get: jest.fn(),
},
},
},
},
plugins: {
ml: undefined,
},
request: {
url: '',
events: {
aborted$: {
subscribe: jest.fn().mockReturnValue({ unsubscribe: jest.fn() }),
},
},
},
} as unknown as APMRouteHandlerResources & {
context: {
core: {
elasticsearch: {
client: typeof esClientMock;
};
uiSettings: {
client: {
get: jest.Mock<any, any>;
};
};
savedObjects: {
client: {
get: jest.Mock<any, any>;
};
};
};
};
};
return mockResources;
}
describe('setupRequest', () => {
describe('with default args', () => {
it('calls callWithRequest', async () => {
const mockResources = getMockResources();
const { apmEventClient } = await setupRequest(mockResources);
await apmEventClient.search('foo', {
apm: { events: [ProcessorEvent.transaction] },
body: { size: 10 },
});
expect(
mockResources.context.core.elasticsearch.client.asCurrentUser.search
).toHaveBeenCalledWith(
{
index: ['apm-*'],
body: {
size: 10,
query: {
bool: {
filter: [{ terms: { 'processor.event': ['transaction'] } }],
},
},
},
ignore_unavailable: true,
preference: 'any',
},
{
signal: expect.any(Object),
meta: true,
}
);
});
it('calls callWithInternalUser', async () => {
const mockResources = getMockResources();
const { internalClient } = await setupRequest(mockResources);
await internalClient.search('foo', {
index: ['apm-*'],
body: { foo: 'bar' },
} as any);
expect(
mockResources.context.core.elasticsearch.client.asInternalUser.search
).toHaveBeenCalledWith(
{
index: ['apm-*'],
body: {
foo: 'bar',
},
},
{
signal: expect.any(Object),
meta: true,
}
);
});
});
});
describe('with includeFrozen=false', () => {
it('should NOT send "ignore_throttled:true" in the request', async () => {
const mockResources = getMockResources();
// mock includeFrozen to return false
mockResources.context.core.uiSettings.client.get.mockResolvedValue(false);
const { apmEventClient } = await setupRequest(mockResources);
await apmEventClient.search('foo', {
apm: {
events: [],
},
body: { size: 10 },
});
const params =
mockResources.context.core.elasticsearch.client.asCurrentUser.search.mock
.calls[0][0];
// @ts-expect-error missing body definition
expect(params.ignore_throttled).toBe(undefined);
});
});
describe('with includeFrozen=true', () => {
it('sets `ignore_throttled=false`', async () => {
const mockResources = getMockResources();
// mock includeFrozen to return true
mockResources.context.core.uiSettings.client.get.mockResolvedValue(true);
const { apmEventClient } = await setupRequest(mockResources);
await apmEventClient.search('foo', {
apm: { events: [] },
body: { size: 10 },
});
const params =
mockResources.context.core.elasticsearch.client.asCurrentUser.search.mock
.calls[0][0];
// @ts-expect-error missing body definition
expect(params.ignore_throttled).toBe(false);
});
});