Skip to content

Commit

Permalink
Merge branch 'master' into test-telemetry-for-metrics-and-logs
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticmachine authored Jul 7, 2020
2 parents fb7f0d9 + e58cc17 commit 91c8cf8
Show file tree
Hide file tree
Showing 149 changed files with 2,259 additions and 977 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export declare class IndexPattern implements IIndexPattern
| [\_fetchFields()](./kibana-plugin-plugins-data-public.indexpattern._fetchfields.md) | | |
| [addScriptedField(name, script, fieldType, lang)](./kibana-plugin-plugins-data-public.indexpattern.addscriptedfield.md) | | |
| [create(allowOverride)](./kibana-plugin-plugins-data-public.indexpattern.create.md) | | |
| [destroy()](./kibana-plugin-plugins-data-public.indexpattern.destroy.md) | | |
| [getAggregationRestrictions()](./kibana-plugin-plugins-data-public.indexpattern.getaggregationrestrictions.md) | | |
| [getComputedFields()](./kibana-plugin-plugins-data-public.indexpattern.getcomputedfields.md) | | |
| [getFieldByName(name)](./kibana-plugin-plugins-data-public.indexpattern.getfieldbyname.md) | | |
Expand Down
105 changes: 20 additions & 85 deletions src/core/server/elasticsearch/elasticsearch_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,14 @@
*/

import { BehaviorSubject } from 'rxjs';
import { Client } from 'elasticsearch';
import {
ILegacyClusterClient,
ILegacyCustomClusterClient,
ILegacyScopedClusterClient,
} from './legacy';
import { ILegacyClusterClient, ILegacyCustomClusterClient } from './legacy';
import { legacyClientMock } from './legacy/mocks';
import { ElasticsearchConfig } from './elasticsearch_config';
import { ElasticsearchService } from './elasticsearch_service';
import { InternalElasticsearchServiceSetup, ElasticsearchStatusMeta } from './types';
import { NodesVersionCompatibility } from './version_check/ensure_es_version';
import { ServiceStatus, ServiceStatusLevels } from '../status';

const createScopedClusterClientMock = (): jest.Mocked<ILegacyScopedClusterClient> => ({
callAsInternalUser: jest.fn(),
callAsCurrentUser: jest.fn(),
});

const createCustomClusterClientMock = (): jest.Mocked<ILegacyCustomClusterClient> => ({
...createClusterClientMock(),
close: jest.fn(),
});

function createClusterClientMock() {
const client: jest.Mocked<ILegacyClusterClient> = {
callAsInternalUser: jest.fn(),
asScoped: jest.fn(),
};
client.asScoped.mockReturnValue(createScopedClusterClientMock());
return client;
}

interface MockedElasticSearchServiceSetup {
legacy: {
createClient: jest.Mock<ILegacyCustomClusterClient, any>;
Expand All @@ -60,11 +37,13 @@ const createSetupContractMock = () => {
const setupContract: MockedElasticSearchServiceSetup = {
legacy: {
createClient: jest.fn(),
client: createClusterClientMock(),
client: legacyClientMock.createClusterClient(),
},
};
setupContract.legacy.createClient.mockReturnValue(createCustomClusterClientMock());
setupContract.legacy.client.asScoped.mockReturnValue(createScopedClusterClientMock());
setupContract.legacy.createClient.mockReturnValue(legacyClientMock.createCustomClusterClient());
setupContract.legacy.client.asScoped.mockReturnValue(
legacyClientMock.createScopedClusterClient()
);
return setupContract;
};

Expand All @@ -74,11 +53,14 @@ const createStartContractMock = () => {
const startContract: MockedElasticSearchServiceStart = {
legacy: {
createClient: jest.fn(),
client: createClusterClientMock(),
client: legacyClientMock.createClusterClient(),
},
};
startContract.legacy.createClient.mockReturnValue(createCustomClusterClientMock());
startContract.legacy.client.asScoped.mockReturnValue(createScopedClusterClientMock());
startContract.legacy.createClient.mockReturnValue(legacyClientMock.createCustomClusterClient());
startContract.legacy.client.asScoped.mockReturnValue(
legacyClientMock.createScopedClusterClient()
);

return startContract;
};

Expand All @@ -104,7 +86,9 @@ const createInternalSetupContractMock = () => {
...createSetupContractMock().legacy,
},
};
setupContract.legacy.client.asScoped.mockReturnValue(createScopedClusterClientMock());
setupContract.legacy.client.asScoped.mockReturnValue(
legacyClientMock.createScopedClusterClient()
);
return setupContract;
};

Expand All @@ -121,62 +105,13 @@ const createMock = () => {
return mocked;
};

const createElasticsearchClientMock = () => {
const mocked: jest.Mocked<Client> = {
cat: {} as any,
cluster: {} as any,
indices: {} as any,
ingest: {} as any,
nodes: {} as any,
snapshot: {} as any,
tasks: {} as any,
bulk: jest.fn(),
clearScroll: jest.fn(),
count: jest.fn(),
create: jest.fn(),
delete: jest.fn(),
deleteByQuery: jest.fn(),
deleteScript: jest.fn(),
deleteTemplate: jest.fn(),
exists: jest.fn(),
explain: jest.fn(),
fieldStats: jest.fn(),
get: jest.fn(),
getScript: jest.fn(),
getSource: jest.fn(),
getTemplate: jest.fn(),
index: jest.fn(),
info: jest.fn(),
mget: jest.fn(),
msearch: jest.fn(),
msearchTemplate: jest.fn(),
mtermvectors: jest.fn(),
ping: jest.fn(),
putScript: jest.fn(),
putTemplate: jest.fn(),
reindex: jest.fn(),
reindexRethrottle: jest.fn(),
renderSearchTemplate: jest.fn(),
scroll: jest.fn(),
search: jest.fn(),
searchShards: jest.fn(),
searchTemplate: jest.fn(),
suggest: jest.fn(),
termvectors: jest.fn(),
update: jest.fn(),
updateByQuery: jest.fn(),
close: jest.fn(),
};
return mocked;
};

export const elasticsearchServiceMock = {
create: createMock,
createInternalSetup: createInternalSetupContractMock,
createSetup: createSetupContractMock,
createStart: createStartContractMock,
createClusterClient: createClusterClientMock,
createCustomClusterClient: createCustomClusterClientMock,
createScopedClusterClient: createScopedClusterClientMock,
createElasticsearchClient: createElasticsearchClientMock,
createLegacyClusterClient: legacyClientMock.createClusterClient,
createLegacyCustomClusterClient: legacyClientMock.createCustomClusterClient,
createLegacyScopedClusterClient: legacyClientMock.createScopedClusterClient,
createLegacyElasticsearchClient: legacyClientMock.createElasticsearchClient,
};
8 changes: 4 additions & 4 deletions src/core/server/elasticsearch/elasticsearch_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('#setup', () => {
});

it('returns elasticsearch client as a part of the contract', async () => {
const mockClusterClientInstance = elasticsearchServiceMock.createClusterClient();
const mockClusterClientInstance = elasticsearchServiceMock.createLegacyClusterClient();
MockClusterClient.mockImplementationOnce(() => mockClusterClientInstance);

const setupContract = await elasticsearchService.setup(deps);
Expand Down Expand Up @@ -209,7 +209,7 @@ describe('#setup', () => {
});

it('esNodeVersionCompatibility$ only starts polling when subscribed to', async (done) => {
const clusterClientInstance = elasticsearchServiceMock.createClusterClient();
const clusterClientInstance = elasticsearchServiceMock.createLegacyClusterClient();
MockClusterClient.mockImplementationOnce(() => clusterClientInstance);

clusterClientInstance.callAsInternalUser.mockRejectedValue(new Error());
Expand All @@ -225,7 +225,7 @@ describe('#setup', () => {
});

it('esNodeVersionCompatibility$ stops polling when unsubscribed from', async (done) => {
const mockClusterClientInstance = elasticsearchServiceMock.createClusterClient();
const mockClusterClientInstance = elasticsearchServiceMock.createLegacyClusterClient();
MockClusterClient.mockImplementationOnce(() => mockClusterClientInstance);

mockClusterClientInstance.callAsInternalUser.mockRejectedValue(new Error());
Expand Down Expand Up @@ -255,7 +255,7 @@ describe('#stop', () => {

it('stops pollEsNodeVersions even if there are active subscriptions', async (done) => {
expect.assertions(2);
const mockClusterClientInstance = elasticsearchServiceMock.createCustomClusterClient();
const mockClusterClientInstance = elasticsearchServiceMock.createLegacyCustomClusterClient();

MockClusterClient.mockImplementationOnce(() => mockClusterClientInstance);

Expand Down
97 changes: 97 additions & 0 deletions src/core/server/elasticsearch/legacy/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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 { Client } from 'elasticsearch';
import { ILegacyScopedClusterClient } from './scoped_cluster_client';
import { ILegacyClusterClient, ILegacyCustomClusterClient } from './cluster_client';

const createScopedClusterClientMock = (): jest.Mocked<ILegacyScopedClusterClient> => ({
callAsInternalUser: jest.fn(),
callAsCurrentUser: jest.fn(),
});

const createCustomClusterClientMock = (): jest.Mocked<ILegacyCustomClusterClient> => ({
...createClusterClientMock(),
close: jest.fn(),
});

function createClusterClientMock() {
const client: jest.Mocked<ILegacyClusterClient> = {
callAsInternalUser: jest.fn(),
asScoped: jest.fn(),
};
client.asScoped.mockReturnValue(createScopedClusterClientMock());
return client;
}

const createElasticsearchClientMock = () => {
const mocked: jest.Mocked<Client> = {
cat: {} as any,
cluster: {} as any,
indices: {} as any,
ingest: {} as any,
nodes: {} as any,
snapshot: {} as any,
tasks: {} as any,
bulk: jest.fn(),
clearScroll: jest.fn(),
count: jest.fn(),
create: jest.fn(),
delete: jest.fn(),
deleteByQuery: jest.fn(),
deleteScript: jest.fn(),
deleteTemplate: jest.fn(),
exists: jest.fn(),
explain: jest.fn(),
fieldStats: jest.fn(),
get: jest.fn(),
getScript: jest.fn(),
getSource: jest.fn(),
getTemplate: jest.fn(),
index: jest.fn(),
info: jest.fn(),
mget: jest.fn(),
msearch: jest.fn(),
msearchTemplate: jest.fn(),
mtermvectors: jest.fn(),
ping: jest.fn(),
putScript: jest.fn(),
putTemplate: jest.fn(),
reindex: jest.fn(),
reindexRethrottle: jest.fn(),
renderSearchTemplate: jest.fn(),
scroll: jest.fn(),
search: jest.fn(),
searchShards: jest.fn(),
searchTemplate: jest.fn(),
suggest: jest.fn(),
termvectors: jest.fn(),
update: jest.fn(),
updateByQuery: jest.fn(),
close: jest.fn(),
};
return mocked;
};

export const legacyClientMock = {
createScopedClusterClient: createScopedClusterClientMock,
createCustomClusterClient: createCustomClusterClientMock,
createClusterClient: createClusterClientMock,
createElasticsearchClient: createElasticsearchClientMock,
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { elasticsearchServiceMock } from '../../elasticsearch/elasticsearch_serv
export const clusterClientMock = jest.fn();
jest.doMock('../../elasticsearch/legacy/scoped_cluster_client', () => ({
LegacyScopedClusterClient: clusterClientMock.mockImplementation(function () {
return elasticsearchServiceMock.createScopedClusterClient();
return elasticsearchServiceMock.createLegacyScopedClusterClient();
}),
}));

Expand All @@ -31,7 +31,7 @@ jest.doMock('elasticsearch', () => {
...realES,
// eslint-disable-next-line object-shorthand
Client: function () {
return elasticsearchServiceMock.createElasticsearchClient();
return elasticsearchServiceMock.createLegacyElasticsearchClient();
},
};
});
2 changes: 1 addition & 1 deletion src/core/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function createCoreRequestHandlerContextMock() {
},
elasticsearch: {
legacy: {
client: elasticsearchServiceMock.createScopedClusterClient(),
client: elasticsearchServiceMock.createLegacyScopedClusterClient(),
},
},
uiSettings: {
Expand Down
9 changes: 8 additions & 1 deletion src/legacy/core_plugins/kibana/server/ui_setting_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ export function getUiSettingDefaults() {
},
}),
type: 'select',
options: ['Browser', ...moment.tz.names()],
options: [
'Browser',
...moment.tz
.names()
// We need to filter out some time zones, that moment.js knows about, but Elasticsearch
// does not understand and would fail thus with a 400 bad request when using them.
.filter((tz) => !['America/Nuuk', 'EST', 'HST', 'ROC', 'MST'].includes(tz)),
],
requiresPageReload: true,
},
'dateFormat:scaled': {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class IndexPattern implements IIndexPattern {
this.sourceFilters = spec.sourceFilters;

// ignoring this because the same thing happens elsewhere but via _.assign
// @ts-ignore
// @ts-expect-error
this.fields = spec.fields || [];
this.typeMeta = spec.typeMeta;
this.fieldFormatMap = _.mapValues(fieldFormatMap, (mapping) => {
Expand Down Expand Up @@ -473,21 +473,8 @@ export class IndexPattern implements IIndexPattern {
async create(allowOverride: boolean = false) {
const _create = async (duplicateId?: string) => {
if (duplicateId) {
const duplicatePattern = new IndexPattern(duplicateId, {
getConfig: this.getConfig,
savedObjectsClient: this.savedObjectsClient,
apiClient: this.apiClient,
patternCache: this.patternCache,
fieldFormats: this.fieldFormats,
onNotification: this.onNotification,
onError: this.onError,
uiSettingsValues: {
shortDotsEnable: this.shortDotsEnable,
metaFields: this.metaFields,
},
});

await duplicatePattern.destroy();
this.patternCache.clear(duplicateId);
await this.savedObjectsClient.delete(savedObjectType, duplicateId);
}

const body = this.prepBody();
Expand Down Expand Up @@ -634,11 +621,4 @@ export class IndexPattern implements IIndexPattern {
toString() {
return '' + this.toJSON();
}

destroy() {
if (this.id) {
this.patternCache.clear(this.id);
return this.savedObjectsClient.delete(savedObjectType, this.id);
}
}
}
Loading

0 comments on commit 91c8cf8

Please sign in to comment.