Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename legacy ES mock accessors #70432

Merged
merged 12 commits into from
Jul 7, 2020
Merged
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
2 changes: 1 addition & 1 deletion x-pack/plugins/actions/server/actions_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { KibanaRequest } from 'kibana/server';

const defaultKibanaIndex = '.kibana';
const savedObjectsClient = savedObjectsClientMock.create();
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient();
const scopedClusterClient = elasticsearchServiceMock.createLegacyScopedClusterClient();
const actionExecutor = actionExecutorMock.create();
const executionEnqueuer = jest.fn();
const request = {} as KibanaRequest;
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/actions/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const createServicesMock = () => {
savedObjectsClient: ReturnType<typeof savedObjectsClientMock.create>;
}
> = {
callCluster: elasticsearchServiceMock.createScopedClusterClient().callAsCurrentUser,
callCluster: elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser,
getScopedCallCluster: jest.fn(),
savedObjectsClient: savedObjectsClientMock.create(),
};
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/alerts/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const createAlertServicesMock = () => {
alertInstanceFactory: jest
.fn<jest.Mocked<AlertInstance>, [string]>()
.mockReturnValue(alertInstanceFactoryMock),
callCluster: elasticsearchServiceMock.createScopedClusterClient().callAsCurrentUser,
callCluster: elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser,
getScopedCallCluster: jest.fn(),
savedObjectsClient: savedObjectsClientMock.create(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function mockHandlerArguments(
{
alertsClient = alertsClientMock.create(),
listTypes: listTypesRes = [],
esClient = elasticsearchServiceMock.createClusterClient(),
esClient = elasticsearchServiceMock.createLegacyClusterClient(),
}: {
alertsClient?: AlertsClientMock;
listTypes?: AlertType[];
Expand Down
Loading