Skip to content

Commit

Permalink
Merge branch '8.11' into backport/8.11/pr-170974
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Nov 10, 2023
2 parents 4876547 + 51bebd8 commit 3959e79
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ import { parseClientOptions } from './client_config';
import { instrumentEsQueryAndDeprecationLogger } from './log_query_and_deprecation';
import { createTransport } from './create_transport';
import type { AgentFactoryProvider } from './agent_manager';
import { patchElasticsearchClient } from './patch_client';

const noop = () => undefined;

// Apply ES client patches on module load
patchElasticsearchClient();

export const configureClient = (
config: ElasticsearchClientConfig,
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { errors } from '@elastic/elasticsearch';

export const patchElasticsearchClient = () => {
const baseErrorPrototype = errors.ElasticsearchClientError.prototype;
// @ts-expect-error
baseErrorPrototype.toJSON = function () {
return {
name: this.name,
message: this.message,
};
};
};
70 changes: 70 additions & 0 deletions src/core/server/integration_tests/elasticsearch/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import {
createTestServers,
type TestElasticsearchUtils,
type TestKibanaUtils,
} from '@kbn/core-test-helpers-kbn-server';

describe('elasticsearch clients errors', () => {
let esServer: TestElasticsearchUtils;
let kibanaServer: TestKibanaUtils;

beforeAll(async () => {
const { startES, startKibana } = createTestServers({
adjustTimeout: jest.setTimeout,
});

esServer = await startES();
kibanaServer = await startKibana();
});

afterAll(async () => {
await kibanaServer.stop();
await esServer.stop();
});

it('has the proper JSON representation', async () => {
const esClient = kibanaServer.coreStart.elasticsearch.client.asInternalUser;

try {
await esClient.search({
index: '.kibana',
// @ts-expect-error yes this is invalid
query: { someInvalidQuery: { foo: 'bar' } },
});
expect('should have thrown').toEqual('but it did not');
} catch (e) {
expect(JSON.stringify(e)).toMatchInlineSnapshot(
`"{\\"name\\":\\"ResponseError\\",\\"message\\":\\"parsing_exception\\\\n\\\\tCaused by:\\\\n\\\\t\\\\tnamed_object_not_found_exception: [1:30] unknown field [someInvalidQuery]\\\\n\\\\tRoot causes:\\\\n\\\\t\\\\tparsing_exception: unknown query [someInvalidQuery]\\"}"`
);
}
});

it('has the proper string representation', async () => {
const esClient = kibanaServer.coreStart.elasticsearch.client.asInternalUser;

try {
await esClient.search({
index: '.kibana',
// @ts-expect-error yes this is invalid
query: { someInvalidQuery: { foo: 'bar' } },
});
expect('should have thrown').toEqual('but it did not');
} catch (e) {
expect(String(e)).toMatchInlineSnapshot(`
"ResponseError: parsing_exception
Caused by:
named_object_not_found_exception: [1:30] unknown field [someInvalidQuery]
Root causes:
parsing_exception: unknown query [someInvalidQuery]"
`);
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ export class LayerGroup implements ILayer {
}

isLayerLoading(zoom: number): boolean {
if (!this.isVisible()) {
return false;
}

return this._children.some((child) => {
return child.isLayerLoading(zoom);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ describe('isLayerLoading', () => {
});

describe('joins', () => {
test('should return false when layer is not visible', () => {
const layer = new GeoJsonVectorLayer({
customIcons: [],
joins: [mockJoin],
layerDescriptor: {
visible: false,
} as unknown as VectorLayerDescriptor,
source: {} as unknown as IVectorSource,
});
expect(layer.isLayerLoading(1)).toBe(false);
});

describe('source data loaded with no features', () => {
test('should return false when join loading has not started', () => {
const layer = new GeoJsonVectorLayer({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export class GeoJsonVectorLayer extends AbstractVectorLayer {
}

isLayerLoading(zoom: number) {
if (!this.isVisible() || !this.showAtZoomLevel(zoom)) {
return false;
}

const isSourceLoading = super.isLayerLoading(zoom);
if (isSourceLoading) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,139 +110,118 @@ describe('isLayerLoading', () => {
dataRequestMetaAtStart: undefined,
dataRequestToken: undefined,
};
test('should be true when tile loading has not started', () => {
const layer = new MvtVectorLayer({
customIcons: [],
layerDescriptor: {
__dataRequests: [sourceDataRequestDescriptor],
} as unknown as VectorLayerDescriptor,
source: {
getMaxZoom: () => {
return 24;
},
getMinZoom: () => {
return 0;
},
} as unknown as IVectorSource,
const mockSource = {
getMaxZoom: () => {
return 24;
},
getMinZoom: () => {
return 0;
},
} as unknown as IVectorSource;

describe('no joins', () => {
test('should be true when tile loading has not started', () => {
const layer = new MvtVectorLayer({
customIcons: [],
layerDescriptor: {
__dataRequests: [sourceDataRequestDescriptor],
} as unknown as VectorLayerDescriptor,
source: mockSource,
});
expect(layer.isLayerLoading(1)).toBe(true);
});
expect(layer.isLayerLoading(1)).toBe(true);
});

test('should be true when tiles are loading', () => {
const layer = new MvtVectorLayer({
customIcons: [],
layerDescriptor: {
__areTilesLoaded: false,
__dataRequests: [sourceDataRequestDescriptor],
} as unknown as VectorLayerDescriptor,
source: {
getMaxZoom: () => {
return 24;
},
getMinZoom: () => {
return 0;
},
} as unknown as IVectorSource,
test('should be true when tiles are loading', () => {
const layer = new MvtVectorLayer({
customIcons: [],
layerDescriptor: {
__areTilesLoaded: false,
__dataRequests: [sourceDataRequestDescriptor],
} as unknown as VectorLayerDescriptor,
source: mockSource,
});
expect(layer.isLayerLoading(1)).toBe(true);
});
expect(layer.isLayerLoading(1)).toBe(true);
});

test('should be false when tiles are loaded', () => {
const layer = new MvtVectorLayer({
customIcons: [],
layerDescriptor: {
__areTilesLoaded: true,
__dataRequests: [sourceDataRequestDescriptor],
} as unknown as VectorLayerDescriptor,
source: {
getMaxZoom: () => {
return 24;
},
getMinZoom: () => {
return 0;
},
} as unknown as IVectorSource,
test('should be false when tiles are loaded', () => {
const layer = new MvtVectorLayer({
customIcons: [],
layerDescriptor: {
__areTilesLoaded: true,
__dataRequests: [sourceDataRequestDescriptor],
} as unknown as VectorLayerDescriptor,
source: mockSource,
});
expect(layer.isLayerLoading(1)).toBe(false);
});
expect(layer.isLayerLoading(1)).toBe(false);
});

test('should be true when tiles are loaded but join is loading', () => {
const layer = new MvtVectorLayer({
customIcons: [],
joins: [
{
hasCompleteConfig: () => {
return true;
},
getSourceDataRequestId: () => {
return 'join_source_a0b0da65-5e1a-4967-9dbe-74f24391afe2';
},
getRightJoinSource: () => {
return {} as unknown as IJoinSource;
},
} as unknown as InnerJoin,
],
layerDescriptor: {
__areTilesLoaded: true,
__dataRequests: [
sourceDataRequestDescriptor,
{
dataId: 'join_source_a0b0da65-5e1a-4967-9dbe-74f24391afe2',
dataRequestMetaAtStart: {},
dataRequestToken: Symbol('join request'),
},
],
} as unknown as VectorLayerDescriptor,
source: {
getMaxZoom: () => {
return 24;
},
getMinZoom: () => {
return 0;
},
} as unknown as IVectorSource,
describe('joins', () => {
const joinDataRequestId = 'join_source_a0b0da65-5e1a-4967-9dbe-74f24391afe2';
const mockJoin = {
hasCompleteConfig: () => {
return true;
},
getSourceDataRequestId: () => {
return joinDataRequestId;
},
getRightJoinSource: () => {
return {} as unknown as IJoinSource;
},
} as unknown as InnerJoin;

test('should be false when layer is not visible', () => {
const layer = new MvtVectorLayer({
customIcons: [],
joins: [mockJoin],
layerDescriptor: {
visible: false,
} as unknown as VectorLayerDescriptor,
source: mockSource,
});
expect(layer.isLayerLoading(1)).toBe(false);
});

test('should be true when tiles are loaded but join is loading', () => {
const layer = new MvtVectorLayer({
customIcons: [],
joins: [mockJoin],
layerDescriptor: {
__areTilesLoaded: true,
__dataRequests: [
sourceDataRequestDescriptor,
{
dataId: joinDataRequestId,
dataRequestMetaAtStart: {},
dataRequestToken: Symbol('join request'),
},
],
} as unknown as VectorLayerDescriptor,
source: mockSource,
});
expect(layer.isLayerLoading(1)).toBe(true);
});
expect(layer.isLayerLoading(1)).toBe(true);
});

test('should be false when tiles are loaded and joins are loaded', () => {
const layer = new MvtVectorLayer({
customIcons: [],
joins: [
{
hasCompleteConfig: () => {
return true;
},
getSourceDataRequestId: () => {
return 'join_source_a0b0da65-5e1a-4967-9dbe-74f24391afe2';
},
getRightJoinSource: () => {
return {} as unknown as IJoinSource;
},
} as unknown as InnerJoin,
],
layerDescriptor: {
__areTilesLoaded: true,
__dataRequests: [
sourceDataRequestDescriptor,
{
data: {},
dataId: 'join_source_a0b0da65-5e1a-4967-9dbe-74f24391afe2',
dataRequestMeta: {},
dataRequestMetaAtStart: undefined,
dataRequestToken: undefined,
},
],
} as unknown as VectorLayerDescriptor,
source: {
getMaxZoom: () => {
return 24;
},
getMinZoom: () => {
return 0;
},
} as unknown as IVectorSource,
test('should be false when tiles are loaded and joins are loaded', () => {
const layer = new MvtVectorLayer({
customIcons: [],
joins: [mockJoin],
layerDescriptor: {
__areTilesLoaded: true,
__dataRequests: [
sourceDataRequestDescriptor,
{
data: {},
dataId: joinDataRequestId,
dataRequestMeta: {},
dataRequestMetaAtStart: undefined,
dataRequestToken: undefined,
},
],
} as unknown as VectorLayerDescriptor,
source: mockSource,
});
expect(layer.isLayerLoading(1)).toBe(false);
});
expect(layer.isLayerLoading(1)).toBe(false);
});
});
Loading

0 comments on commit 3959e79

Please sign in to comment.