Skip to content

Commit

Permalink
Move consumers off bsearch endpoint (elastic#196962)
Browse files Browse the repository at this point in the history
## Summary

Part of elastic#186139.

Moves direct consumers off of the `internal/bsearch` endpoint and onto
the `internal/search`.

This is in preparation for removing the `internal/bsearch` endpoint
entirely.

Updates automated tests to mock/use the correct service moving forward.

### Checklist

- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed

---------

Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: Dzmitry Lemechko <[email protected]>
  • Loading branch information
3 people authored and mbondyra committed Nov 8, 2024
1 parent 226a55d commit 255404d
Show file tree
Hide file tree
Showing 33 changed files with 177 additions and 233 deletions.
2 changes: 1 addition & 1 deletion packages/kbn-ftr-common-functional-services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type SamlAuthProviderType = ProvidedType<typeof SamlAuthProvider>;
export type { FtrProviderContext } from './services/ftr_provider_context';
export { runSavedObjInfoSvc } from './services/saved_object_info';

export type { BsearchService, SendOptions } from './services/bsearch';
export type { SearchService, SendOptions } from './services/search';
export { SavedObjectInfoService } from './services/saved_object_info';
export { DeploymentService } from './services/deployment';
export { IndexPatternsService } from './services/index_patterns';
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-ftr-common-functional-services/services/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { EsArchiverProvider } from './es_archiver';
import { EsProvider } from './es';
import { KibanaServerProvider } from './kibana_server';
import { RetryService } from './retry';
import { BsearchService } from './bsearch';
import { SearchService } from './search';
import { ConsoleProvider } from './console';
import { DeploymentService } from './deployment';
import { EsDeleteAllIndicesProvider } from './es_delete_all_indices';
Expand All @@ -27,7 +27,7 @@ export const services = {
kibanaServer: KibanaServerProvider,
esArchiver: EsArchiverProvider,
retry: RetryService,
bsearch: BsearchService,
search: SearchService,
console: ConsoleProvider,
deployment: DeploymentService,
esDeleteAllIndices: EsDeleteAllIndicesProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,11 @@
*/

import expect from '@kbn/expect';
import request from 'superagent';
import type SuperTest from 'supertest';
import type { IEsSearchResponse } from '@kbn/search-types';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import { BFETCH_ROUTE_VERSION_LATEST } from '@kbn/bfetch-plugin/common';
import { FtrService } from './ftr_provider_context';

/**
* Function copied from here:
* test/api_integration/apis/search/bsearch.ts without the compress
*
* Splits the JSON lines from bsearch
*/
const parseBfetchResponse = (resp: request.Response): Array<Record<string, any>> => {
return resp.text
.trim()
.split('\n')
.map((item) => JSON.parse(item));
};

/**
* Function copied from here:
* x-pack/test/rule_registry/common/lib/authentication/spaces.ts
Expand All @@ -48,13 +33,13 @@ export interface SendOptions {
}

/**
* Bsearch Service that can reduce flake on the CI systems when they are under
* pressure and bsearch returns an async search response or a sync response.
* Search Service that can reduce flake on the CI systems when they are under
* pressure and search returns an async search response or a sync response.
*
* @example
* const supertest = getService('supertest');
* const bsearch = getService('bsearch');
* const response = await bsearch.send<MyType>({
* const search = getService('search');
* const response = await search.send<MyType>({
* supertest,
* options: {
* defaultIndex: ['large_volume_dns_data'],
Expand All @@ -64,7 +49,7 @@ export interface SendOptions {
* expect(response).eql({ ... your value ... });
*/

export class BsearchService extends FtrService {
export class SearchService extends FtrService {
private readonly retry = this.ctx.getService('retry');

/** Send method to send in your supertest, url, options, and strategy name */
Expand All @@ -85,26 +70,13 @@ export class BsearchService extends FtrService {

const result = await this.retry.try(async () => {
const resp = await supertest
.post(`${spaceUrl}/internal/bsearch`)
.post(`${spaceUrl}/internal/search/${strategy}/${body.id}`)
.set('kbn-xsrf', 'true')
.set(ELASTIC_HTTP_VERSION_HEADER, BFETCH_ROUTE_VERSION_LATEST)
.send({
batch: [
{
request: {
id: body.id,
...options,
},
options: {
strategy,
},
},
],
})
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send()
.expect(200);
const [parsedResponse] = parseBfetchResponse(resp);
expect(parsedResponse.result.isRunning).equal(false);
return parsedResponse.result as T;
expect(resp.body.isRunning).equal(false);
return resp.body as T;
});
return result;
}
Expand Down
1 change: 0 additions & 1 deletion packages/kbn-ftr-common-functional-services/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"@kbn/expect",
"@kbn/search-types",
"@kbn/core-http-common",
"@kbn/bfetch-plugin",
"@kbn/data-plugin",
"@kbn/dev-cli-runner",
"@kbn/dev-cli-errors",
Expand Down
4 changes: 2 additions & 2 deletions test/common/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const {
esDeleteAllIndices,
savedObjectInfo,
indexPatterns,
bsearch,
search,
console,
supertest,
esSupertest,
Expand All @@ -42,7 +42,7 @@ export const services = {
esDeleteAllIndices,
savedObjectInfo,
indexPatterns,
bsearch,
search,
console,
supertest,
esSupertest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export const generateFindingHit = (finding: CspFinding) => {
};
};

const getFindingsBsearchResponse = (findings: CspFinding[]) => {
const getFindingsSearchResponse = (findings: CspFinding[]) => {
const buckets = findings.reduce(
(acc, finding) => {
if (finding.result.evaluation === 'failed') {
Expand All @@ -174,28 +174,26 @@ const getFindingsBsearchResponse = (findings: CspFinding[]) => {
);

return {
id: 0,
result: {
rawResponse: {
took: 1,
timed_out: false,
_shards: {
total: 1,
successful: 1,
skipped: 0,
failed: 0,
},
hits: {
total: findings.length,
max_score: null,
hits: findings.map(generateFindingHit),
},
aggregations: {
count: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets,
},
id: '1',
rawResponse: {
took: 1,
timed_out: false,
_shards: {
total: 1,
successful: 1,
skipped: 0,
failed: 0,
},
hits: {
total: findings.length,
max_score: null,
hits: findings.map(generateFindingHit),
},
aggregations: {
count: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets,
},
},
isPartial: false,
Expand All @@ -214,8 +212,8 @@ export const rulesGetStatesHandler = http.get(
}
);

export const bsearchFindingsHandler = (findings: CspFinding[]) =>
http.post('internal/bsearch', async ({ request }) => {
export const searchFindingsHandler = (findings: CspFinding[]) =>
http.post('internal/search', async ({ request }) => {
const jsonRequest = (await request.json()) as Partial<estypes.SearchRequest>;

const filter = jsonRequest?.query?.bool?.filter;
Expand All @@ -233,7 +231,7 @@ export const bsearchFindingsHandler = (findings: CspFinding[]) =>
return finding.rule.section === termValue;
});

return HttpResponse.json(getFindingsBsearchResponse(filteredFindings));
return HttpResponse.json(getFindingsSearchResponse(filteredFindings));
}

const hasRuleSectionFilter =
Expand All @@ -244,7 +242,7 @@ export const bsearchFindingsHandler = (findings: CspFinding[]) =>
return finding.rule.section === filter?.[0]?.match_phrase?.['rule.section'];
});

return HttpResponse.json(getFindingsBsearchResponse(filteredFindings));
return HttpResponse.json(getFindingsSearchResponse(filteredFindings));
}

const hasResultEvaluationFilter =
Expand All @@ -255,8 +253,8 @@ export const bsearchFindingsHandler = (findings: CspFinding[]) =>
return finding.result.evaluation === filter?.[0]?.match_phrase?.['result.evaluation'];
});

return HttpResponse.json(getFindingsBsearchResponse(filteredFindings));
return HttpResponse.json(getFindingsSearchResponse(filteredFindings));
}

return HttpResponse.json(getFindingsBsearchResponse(findings));
return HttpResponse.json(getFindingsSearchResponse(findings));
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { FilterManager } from '@kbn/data-plugin/public';
import { CspClientPluginStartDeps } from '@kbn/cloud-security-posture';
import * as statusHandlers from '../../../server/routes/status/status.handlers.mock';
import {
bsearchFindingsHandler,
searchFindingsHandler,
generateCspFinding,
generateMultipleCspFindings,
rulesGetStatesHandler,
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('<Findings />', () => {
const finding2 = generateCspFinding('0004', 'passed');

server.use(statusHandlers.notInstalledHasMisconfigurationsFindingsHandler);
server.use(bsearchFindingsHandler([finding1, finding2]));
server.use(searchFindingsHandler([finding1, finding2]));
renderFindingsPage();

// Loading while checking the status API and fetching the findings
Expand Down Expand Up @@ -89,7 +89,7 @@ describe('<Findings />', () => {
const finding2 = generateCspFinding('0002', 'passed');

server.use(statusHandlers.indexedHandler);
server.use(bsearchFindingsHandler([finding1, finding2]));
server.use(searchFindingsHandler([finding1, finding2]));
renderFindingsPage();

// Loading while checking the status API
Expand Down Expand Up @@ -118,7 +118,7 @@ describe('<Findings />', () => {
const finding2 = generateCspFinding('0002', 'passed');

server.use(statusHandlers.indexedHandler);
server.use(bsearchFindingsHandler([finding1, finding2]));
server.use(searchFindingsHandler([finding1, finding2]));

renderFindingsPage();

Expand Down Expand Up @@ -148,7 +148,7 @@ describe('<Findings />', () => {
const finding2 = generateCspFinding('0002', 'passed');

server.use(statusHandlers.indexedHandler);
server.use(bsearchFindingsHandler([finding1, finding2]));
server.use(searchFindingsHandler([finding1, finding2]));

renderFindingsPage();

Expand Down Expand Up @@ -180,7 +180,7 @@ describe('<Findings />', () => {
const finding2 = generateCspFinding('0002', 'passed');

server.use(statusHandlers.indexedHandler);
server.use(bsearchFindingsHandler([finding1, finding2]));
server.use(searchFindingsHandler([finding1, finding2]));

renderFindingsPage();

Expand Down Expand Up @@ -259,7 +259,7 @@ describe('<Findings />', () => {
};

server.use(statusHandlers.indexedHandler);
server.use(bsearchFindingsHandler([finding1, finding2]));
server.use(searchFindingsHandler([finding1, finding2]));

renderFindingsPage(mockDependenciesWithFilter);

Expand All @@ -286,7 +286,7 @@ describe('<Findings />', () => {
it('renders the distribution bar', async () => {
server.use(statusHandlers.indexedHandler);
server.use(
bsearchFindingsHandler(
searchFindingsHandler(
generateMultipleCspFindings({
count: 10,
failedCount: 3,
Expand Down Expand Up @@ -316,7 +316,7 @@ describe('<Findings />', () => {
it('filters by passed findings when clicking on the passed findings button', async () => {
server.use(statusHandlers.indexedHandler);
server.use(
bsearchFindingsHandler(
searchFindingsHandler(
generateMultipleCspFindings({
count: 2,
failedCount: 1,
Expand Down Expand Up @@ -352,7 +352,7 @@ describe('<Findings />', () => {
it('filters by failed findings when clicking on the failed findings button', async () => {
server.use(statusHandlers.indexedHandler);
server.use(
bsearchFindingsHandler(
searchFindingsHandler(
generateMultipleCspFindings({
count: 2,
failedCount: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jest.mock('rxjs', () => {
...actual,
lastValueFrom: async (source: Promise<any>) => {
const value = await source;
return value.result;
return value;
},
};
});
Expand Down Expand Up @@ -97,7 +97,7 @@ export const getMockServerDependencies = () => {
search: {
...getMockDependencies().data.search,
search: async ({ params }: { params: any }) => {
const response = await fetch(`${MOCK_SERVER_BASE_URL}/internal/bsearch`, {
const response = await fetch(`${MOCK_SERVER_BASE_URL}/internal/search`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const useEsSearch = <DocumentSource extends unknown, TParams extends esty
operationName: name,
kibanaRequest: {
route: {
path: '/internal/bsearch',
path: '/internal/search',
method: 'POST',
},
} as any,
Expand Down Expand Up @@ -95,7 +95,7 @@ export const useEsSearch = <DocumentSource extends unknown, TParams extends esty
operationName: name,
kibanaRequest: {
route: {
path: '/internal/bsearch',
path: '/internal/search',
method: 'POST',
},
} as any,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const executeEsQueryAPI = async ({
operationName: name,
kibanaRequest: {
route: {
path: '/internal/bsearch',
path: '/internal/search',
method: 'POST',
},
} as any,
Expand Down Expand Up @@ -82,7 +82,7 @@ export const executeEsQueryAPI = async ({
operationName: name,
kibanaRequest: {
route: {
path: '/internal/bsearch',
path: '/internal/search',
method: 'POST',
},
} as any,
Expand Down
4 changes: 2 additions & 2 deletions x-pack/test/common/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { commonFunctionalServices } from '@kbn/ftr-common-functional-services';
import { commonFunctionalUIServices } from '@kbn/ftr-common-functional-ui-services';
import { InfraLogViewsServiceProvider } from './infra_log_views';
import { SpacesServiceProvider } from './spaces';
import { BsearchSecureService } from './bsearch_secure';
import { SearchSecureService } from './search_secure';
import { ApmSynthtraceKibanaClientProvider } from './apm_synthtrace_kibana_client';
import { InfraSynthtraceKibanaClientProvider } from './infra_synthtrace_kibana_client';

Expand All @@ -20,7 +20,7 @@ export const services = {
infraLogViews: InfraLogViewsServiceProvider,
supertest: kibanaApiIntegrationServices.supertest,
spaces: SpacesServiceProvider,
secureBsearch: BsearchSecureService,
secureSearch: SearchSecureService,
apmSynthtraceKibanaClient: ApmSynthtraceKibanaClientProvider,
infraSynthtraceKibanaClient: InfraSynthtraceKibanaClientProvider,
};
Loading

0 comments on commit 255404d

Please sign in to comment.