Skip to content

Commit

Permalink
[Search] Session server side functional tests (elastic#86152) (elasti…
Browse files Browse the repository at this point in the history
…c#87210)

* OSS search functional tests

* x-pack tests

* Session tests

* Update search.ts

* Update session.ts

* Update test/api_integration/apis/search/search.ts

Co-authored-by: Lukas Olson <[email protected]>

* reportServerError

* Improve response error codes

Co-authored-by: Kibana Machine <[email protected]>
Co-authored-by: Lukas Olson <[email protected]>

Co-authored-by: Kibana Machine <[email protected]>
Co-authored-by: Lukas Olson <[email protected]>
  • Loading branch information
3 people authored Jan 5, 2021
1 parent 19e9696 commit 4856675
Show file tree
Hide file tree
Showing 16 changed files with 656 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { SearchUsage } from '../collectors';
import { getDefaultSearchParams, getShardTimeout, shimAbortSignal } from './request_utils';
import { toKibanaSearchResponse } from './response_utils';
import { searchUsageObserver } from '../collectors/usage';
import { KbnServerError } from '../../../../kibana_utils/server';

export const esSearchStrategyProvider = (
config$: Observable<SharedGlobalConfig>,
Expand All @@ -35,7 +36,7 @@ export const esSearchStrategyProvider = (
// Only default index pattern type is supported here.
// See data_enhanced for other type support.
if (request.indexType) {
throw new Error(`Unsupported index pattern type ${request.indexType}`);
throw new KbnServerError(`Unsupported index pattern type ${request.indexType}`, 400);
}

const search = async () => {
Expand Down
11 changes: 2 additions & 9 deletions src/plugins/data/server/search/routes/msearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { IRouter } from 'src/core/server';
import { SearchRouteDependencies } from '../search_service';

import { getCallMsearch } from './call_msearch';
import { reportServerError } from '../../../../kibana_utils/server';

/**
* The msearch route takes in an array of searches, each consisting of header
Expand Down Expand Up @@ -69,15 +70,7 @@ export function registerMsearchRoute(router: IRouter, deps: SearchRouteDependenc
const response = await callMsearch({ body: request.body });
return res.ok(response);
} catch (err) {
return res.customError({
statusCode: err.statusCode || 500,
body: {
message: err.message,
attributes: {
error: err.body?.error || err.message,
},
},
});
return reportServerError(res, err);
}
}
);
Expand Down
21 changes: 3 additions & 18 deletions src/plugins/data/server/search/routes/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { schema } from '@kbn/config-schema';
import type { IRouter } from 'src/core/server';
import { getRequestAbortedSignal } from '../../lib';
import { shimHitsTotal } from './shim_hits_total';
import { reportServerError } from '../../../../kibana_utils/server';

export function registerSearchRoute(router: IRouter): void {
router.post(
Expand Down Expand Up @@ -74,15 +75,7 @@ export function registerSearchRoute(router: IRouter): void {
},
});
} catch (err) {
return res.customError({
statusCode: err.statusCode || 500,
body: {
message: err.message,
attributes: {
error: err.body?.error || err.message,
},
},
});
return reportServerError(res, err);
}
}
);
Expand All @@ -106,15 +99,7 @@ export function registerSearchRoute(router: IRouter): void {
await context.search!.cancel(id, { strategy });
return res.ok();
} catch (err) {
return res.customError({
statusCode: err.statusCode,
body: {
message: err.message,
attributes: {
error: err.body.error,
},
},
});
return reportServerError(res, err);
}
}
);
Expand Down
11 changes: 9 additions & 2 deletions src/plugins/data/server/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import {
import { aggShardDelay } from '../../common/search/aggs/buckets/shard_delay_fn';
import { ConfigSchema } from '../../config';
import { SessionService, IScopedSessionService, ISessionService } from './session';
import { KbnServerError } from '../../../kibana_utils/server';

declare module 'src/core/server' {
interface RequestHandlerContext {
Expand Down Expand Up @@ -305,7 +306,13 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {

private cancel = (id: string, options: ISearchOptions, deps: SearchStrategyDependencies) => {
const strategy = this.getSearchStrategy(options.strategy);
return strategy.cancel ? strategy.cancel(id, options, deps) : Promise.resolve();
if (!strategy.cancel) {
throw new KbnServerError(
`Search strategy ${options.strategy} doesn't support cancellations`,
400
);
}
return strategy.cancel(id, options, deps);
};

private getSearchStrategy = <
Expand All @@ -317,7 +324,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
this.logger.debug(`Get strategy ${name}`);
const strategy = this.searchStrategies[name];
if (!strategy) {
throw new Error(`Search strategy ${name} not found`);
throw new KbnServerError(`Search strategy ${name} not found`, 404);
}
return strategy;
};
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/kibana_utils/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ export {
Set,
url,
} from '../common';

export { KbnServerError, reportServerError } from './report_server_error';
39 changes: 39 additions & 0 deletions src/plugins/kibana_utils/server/report_server_error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { KibanaResponseFactory } from 'kibana/server';
import { KbnError } from '../common';

export class KbnServerError extends KbnError {
constructor(message: string, public readonly statusCode: number) {
super(message);
}
}

export function reportServerError(res: KibanaResponseFactory, err: any) {
return res.customError({
statusCode: err.statusCode ?? 500,
body: {
message: err.message,
attributes: {
error: err.body?.error || err.message,
},
},
});
}
1 change: 1 addition & 0 deletions test/api_integration/apis/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ import { FtrProviderContext } from '../../ftr_provider_context';
export default function ({ loadTestFile }: FtrProviderContext) {
describe('search', () => {
loadTestFile(require.resolve('./search'));
loadTestFile(require.resolve('./msearch'));
});
}
88 changes: 88 additions & 0 deletions test/api_integration/apis/search/msearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { FtrProviderContext } from '../../ftr_provider_context';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');

describe('msearch', () => {
describe('post', () => {
it('should return 200 when correctly formatted searches are provided', async () =>
await supertest
.post(`/internal/_msearch`)
.send({
searches: [
{
header: { index: 'foo' },
body: {
query: {
match_all: {},
},
},
},
],
})
.expect(200));

it('should return 400 if you provide malformed content', async () =>
await supertest
.post(`/internal/_msearch`)
.send({
foo: false,
})
.expect(400));

it('should require you to provide an index for each request', async () =>
await supertest
.post(`/internal/_msearch`)
.send({
searches: [
{ header: { index: 'foo' }, body: {} },
{ header: {}, body: {} },
],
})
.expect(400));

it('should not require optional params', async () =>
await supertest
.post(`/internal/_msearch`)
.send({
searches: [{ header: { index: 'foo' }, body: {} }],
})
.expect(200));

it('should allow passing preference as a string', async () =>
await supertest
.post(`/internal/_msearch`)
.send({
searches: [{ header: { index: 'foo', preference: '_custom' }, body: {} }],
})
.expect(200));

it('should allow passing preference as a number', async () =>
await supertest
.post(`/internal/_msearch`)
.send({
searches: [{ header: { index: 'foo', preference: 123 }, body: {} }],
})
.expect(200));
});
});
}
Loading

0 comments on commit 4856675

Please sign in to comment.