Skip to content

Commit 5bb9691

Browse files
authored
Use new ES client for licensing plugin (#92143)
* use new client for licensing API * add logs * adapt unit tests * Revert "add logs" This reverts commit 4a61b64 * fix some type errors * fix test types * adapt monitoring usage of `createLicensePoller` * remove test comment * fix unit test * remove createLicensePoller from setup contract * fix unit tests
1 parent 23621e6 commit 5bb9691

6 files changed

Lines changed: 123 additions & 153 deletions

File tree

x-pack/plugins/licensing/server/mocks.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,9 @@ const createSetupMock = (): jest.Mocked<LicensingPluginSetup> => {
1919
const mock = {
2020
license$: new BehaviorSubject(license),
2121
refresh: jest.fn(),
22-
createLicensePoller: jest.fn(),
2322
featureUsage: featureUsageMock.createSetup(),
2423
};
2524
mock.refresh.mockResolvedValue(license);
26-
mock.createLicensePoller.mockReturnValue({
27-
license$: mock.license$,
28-
refresh: mock.refresh,
29-
});
3025

3126
return mock;
3227
};

x-pack/plugins/licensing/server/plugin.test.ts

Lines changed: 69 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,42 @@
66
*/
77

88
import { take, toArray } from 'rxjs/operators';
9+
import { estypes } from '@elastic/elasticsearch';
910
import moment from 'moment';
1011
import { LicenseType } from '../common/types';
11-
import { ElasticsearchError, RawLicense } from './types';
12+
import { ElasticsearchError } from './types';
1213
import { LicensingPlugin } from './plugin';
1314
import {
1415
coreMock,
1516
elasticsearchServiceMock,
1617
loggingSystemMock,
1718
} from '../../../../src/core/server/mocks';
18-
import { ILegacyClusterClient } from '../../../../src/core/server/';
19+
import { IClusterClient } from '../../../../src/core/server';
1920

20-
function buildRawLicense(options: Partial<RawLicense> = {}): RawLicense {
21-
const defaultRawLicense: RawLicense = {
21+
function buildRawLicense(
22+
options: Partial<estypes.XpackInfoMinimalLicenseInformation> = {}
23+
): estypes.XpackInfoMinimalLicenseInformation {
24+
return {
2225
uid: 'uid-000000001234',
2326
status: 'active',
2427
type: 'basic',
2528
mode: 'basic',
2629
expiry_date_in_millis: 1000,
30+
...options,
2731
};
28-
return Object.assign(defaultRawLicense, options);
2932
}
3033

3134
const flushPromises = (ms = 50) => new Promise((res) => setTimeout(res, ms));
3235

33-
function createCoreSetupWith(esClient: ILegacyClusterClient) {
36+
function createCoreSetupWith(esClient: IClusterClient) {
3437
const coreSetup = coreMock.createSetup();
3538
const coreStart = coreMock.createStart();
3639
coreSetup.getStartServices.mockResolvedValue([
3740
{
3841
...coreStart,
3942
elasticsearch: {
4043
...coreStart.elasticsearch,
41-
legacy: {
42-
...coreStart.elasticsearch.legacy,
43-
client: esClient,
44-
createClient: jest.fn(),
45-
},
44+
client: esClient,
4645
},
4746
},
4847
{},
@@ -52,6 +51,16 @@ function createCoreSetupWith(esClient: ILegacyClusterClient) {
5251
}
5352

5453
describe('licensing plugin', () => {
54+
const createEsClient = (response?: Record<string, any>) => {
55+
const client = elasticsearchServiceMock.createClusterClient();
56+
if (response) {
57+
client.asInternalUser.xpack.info.mockReturnValue(
58+
elasticsearchServiceMock.createSuccessTransportRequestPromise(response as any)
59+
);
60+
}
61+
return client;
62+
};
63+
5564
describe('#start', () => {
5665
describe('#license$', () => {
5766
let plugin: LicensingPlugin;
@@ -69,8 +78,7 @@ describe('licensing plugin', () => {
6978
});
7079

7180
it('returns license', async () => {
72-
const esClient = elasticsearchServiceMock.createLegacyClusterClient();
73-
esClient.callAsInternalUser.mockResolvedValue({
81+
const esClient = createEsClient({
7482
license: buildRawLicense(),
7583
features: {},
7684
});
@@ -83,8 +91,7 @@ describe('licensing plugin', () => {
8391
});
8492

8593
it('calls `callAsInternalUser` with the correct parameters', async () => {
86-
const esClient = elasticsearchServiceMock.createLegacyClusterClient();
87-
esClient.callAsInternalUser.mockResolvedValue({
94+
const esClient = createEsClient({
8895
license: buildRawLicense(),
8996
features: {},
9097
});
@@ -94,23 +101,22 @@ describe('licensing plugin', () => {
94101
const { license$ } = await plugin.start();
95102
await license$.pipe(take(1)).toPromise();
96103

97-
expect(esClient.callAsInternalUser).toHaveBeenCalledTimes(1);
98-
expect(esClient.callAsInternalUser).toHaveBeenCalledWith('transport.request', {
99-
method: 'GET',
100-
path: '/_xpack?accept_enterprise=true',
104+
expect(esClient.asInternalUser.xpack.info).toHaveBeenCalledTimes(1);
105+
expect(esClient.asInternalUser.xpack.info).toHaveBeenCalledWith({
106+
accept_enterprise: true,
101107
});
102108
});
103109

104110
it('observable receives updated licenses', async () => {
105111
const types: LicenseType[] = ['basic', 'gold', 'platinum'];
106112

107-
const esClient = elasticsearchServiceMock.createLegacyClusterClient();
108-
esClient.callAsInternalUser.mockImplementation(() =>
109-
Promise.resolve({
113+
const esClient = createEsClient();
114+
esClient.asInternalUser.xpack.info.mockImplementation(() => {
115+
return elasticsearchServiceMock.createSuccessTransportRequestPromise({
110116
license: buildRawLicense({ type: types.shift() }),
111117
features: {},
112-
})
113-
);
118+
} as estypes.XpackInfoResponse);
119+
});
114120

115121
const coreSetup = createCoreSetupWith(esClient);
116122
await plugin.setup(coreSetup);
@@ -123,8 +129,8 @@ describe('licensing plugin', () => {
123129
});
124130

125131
it('returns a license with error when request fails', async () => {
126-
const esClient = elasticsearchServiceMock.createLegacyClusterClient();
127-
esClient.callAsInternalUser.mockRejectedValue(new Error('test'));
132+
const esClient = createEsClient();
133+
esClient.asInternalUser.xpack.info.mockRejectedValue(new Error('test'));
128134

129135
const coreSetup = createCoreSetupWith(esClient);
130136
await plugin.setup(coreSetup);
@@ -136,10 +142,10 @@ describe('licensing plugin', () => {
136142
});
137143

138144
it('generate error message when x-pack plugin was not installed', async () => {
139-
const esClient = elasticsearchServiceMock.createLegacyClusterClient();
145+
const esClient = createEsClient();
140146
const error: ElasticsearchError = new Error('reason');
141147
error.status = 400;
142-
esClient.callAsInternalUser.mockRejectedValue(error);
148+
esClient.asInternalUser.xpack.info.mockRejectedValue(error);
143149

144150
const coreSetup = createCoreSetupWith(esClient);
145151
await plugin.setup(coreSetup);
@@ -154,26 +160,35 @@ describe('licensing plugin', () => {
154160
const error1 = new Error('reason-1');
155161
const error2 = new Error('reason-2');
156162

157-
const esClient = elasticsearchServiceMock.createLegacyClusterClient();
158-
159-
esClient.callAsInternalUser
160-
.mockRejectedValueOnce(error1)
161-
.mockRejectedValueOnce(error2)
162-
.mockResolvedValue({ license: buildRawLicense(), features: {} });
163+
const esClient = createEsClient();
164+
let i = 0;
165+
esClient.asInternalUser.xpack.info.mockImplementation(() => {
166+
i++;
167+
if (i === 1) {
168+
return elasticsearchServiceMock.createErrorTransportRequestPromise(error1);
169+
}
170+
if (i === 2) {
171+
return elasticsearchServiceMock.createErrorTransportRequestPromise(error2);
172+
}
173+
return elasticsearchServiceMock.createSuccessTransportRequestPromise({
174+
license: buildRawLicense(),
175+
features: {},
176+
} as estypes.XpackInfoResponse);
177+
});
163178

164179
const coreSetup = createCoreSetupWith(esClient);
165180
await plugin.setup(coreSetup);
166181
const { license$ } = await plugin.start();
167182

168183
const [first, second, third] = await license$.pipe(take(3), toArray()).toPromise();
184+
169185
expect(first.error).toBe(error1.message);
170186
expect(second.error).toBe(error2.message);
171187
expect(third.type).toBe('basic');
172188
});
173189

174190
it('fetch license immediately without subscriptions', async () => {
175-
const esClient = elasticsearchServiceMock.createLegacyClusterClient();
176-
esClient.callAsInternalUser.mockResolvedValue({
191+
const esClient = createEsClient({
177192
license: buildRawLicense(),
178193
features: {},
179194
});
@@ -184,12 +199,11 @@ describe('licensing plugin', () => {
184199

185200
await flushPromises();
186201

187-
expect(esClient.callAsInternalUser).toHaveBeenCalledTimes(1);
202+
expect(esClient.asInternalUser.xpack.info).toHaveBeenCalledTimes(1);
188203
});
189204

190205
it('logs license details without subscriptions', async () => {
191-
const esClient = elasticsearchServiceMock.createLegacyClusterClient();
192-
esClient.callAsInternalUser.mockResolvedValue({
206+
const esClient = createEsClient({
193207
license: buildRawLicense(),
194208
features: {},
195209
});
@@ -214,13 +228,13 @@ describe('licensing plugin', () => {
214228
it('generates signature based on fetched license content', async () => {
215229
const types: LicenseType[] = ['basic', 'gold', 'basic'];
216230

217-
const esClient = elasticsearchServiceMock.createLegacyClusterClient();
218-
esClient.callAsInternalUser.mockImplementation(() =>
219-
Promise.resolve({
231+
const esClient = createEsClient();
232+
esClient.asInternalUser.xpack.info.mockImplementation(() => {
233+
return elasticsearchServiceMock.createSuccessTransportRequestPromise({
220234
license: buildRawLicense({ type: types.shift() }),
221235
features: {},
222-
})
223-
);
236+
} as estypes.XpackInfoResponse);
237+
});
224238

225239
const coreSetup = createCoreSetupWith(esClient);
226240
await plugin.setup(coreSetup);
@@ -245,8 +259,7 @@ describe('licensing plugin', () => {
245259
api_polling_frequency: moment.duration(50000),
246260
})
247261
);
248-
const esClient = elasticsearchServiceMock.createLegacyClusterClient();
249-
esClient.callAsInternalUser.mockResolvedValue({
262+
const esClient = createEsClient({
250263
license: buildRawLicense(),
251264
features: {},
252265
});
@@ -255,14 +268,14 @@ describe('licensing plugin', () => {
255268
await plugin.setup(coreSetup);
256269
const { refresh, license$ } = await plugin.start();
257270

258-
expect(esClient.callAsInternalUser).toHaveBeenCalledTimes(0);
271+
expect(esClient.asInternalUser.xpack.info).toHaveBeenCalledTimes(0);
259272

260273
await license$.pipe(take(1)).toPromise();
261-
expect(esClient.callAsInternalUser).toHaveBeenCalledTimes(1);
274+
expect(esClient.asInternalUser.xpack.info).toHaveBeenCalledTimes(1);
262275

263276
refresh();
264277
await flushPromises();
265-
expect(esClient.callAsInternalUser).toHaveBeenCalledTimes(2);
278+
expect(esClient.asInternalUser.xpack.info).toHaveBeenCalledTimes(2);
266279
});
267280
});
268281

@@ -280,17 +293,15 @@ describe('licensing plugin', () => {
280293
})
281294
);
282295

283-
const esClient = elasticsearchServiceMock.createLegacyClusterClient();
284-
esClient.callAsInternalUser.mockResolvedValue({
296+
const esClient = createEsClient({
285297
license: buildRawLicense(),
286298
features: {},
287299
});
288300
const coreSetup = createCoreSetupWith(esClient);
289301
await plugin.setup(coreSetup);
290302
const { createLicensePoller, license$ } = await plugin.start();
291303

292-
const customClient = elasticsearchServiceMock.createLegacyClusterClient();
293-
customClient.callAsInternalUser.mockResolvedValue({
304+
const customClient = createEsClient({
294305
license: buildRawLicense({ type: 'gold' }),
295306
features: {},
296307
});
@@ -300,10 +311,10 @@ describe('licensing plugin', () => {
300311
customClient,
301312
customPollingFrequency
302313
);
303-
expect(customClient.callAsInternalUser).toHaveBeenCalledTimes(0);
314+
expect(customClient.asInternalUser.xpack.info).toHaveBeenCalledTimes(0);
304315

305316
const customLicense = await customLicense$.pipe(take(1)).toPromise();
306-
expect(customClient.callAsInternalUser).toHaveBeenCalledTimes(1);
317+
expect(customClient.asInternalUser.xpack.info).toHaveBeenCalledTimes(1);
307318

308319
await flushPromises(customPollingFrequency * 1.5);
309320

@@ -324,18 +335,17 @@ describe('licensing plugin', () => {
324335
await plugin.setup(coreSetup);
325336
const { createLicensePoller } = await plugin.start();
326337

327-
const customClient = elasticsearchServiceMock.createLegacyClusterClient();
328-
customClient.callAsInternalUser.mockResolvedValue({
338+
const customClient = createEsClient({
329339
license: buildRawLicense({ type: 'gold' }),
330340
features: {},
331341
});
332342

333343
const { license$, refresh } = createLicensePoller(customClient, 10000);
334-
expect(customClient.callAsInternalUser).toHaveBeenCalledTimes(0);
344+
expect(customClient.asInternalUser.xpack.info).toHaveBeenCalledTimes(0);
335345

336346
await refresh();
337347

338-
expect(customClient.callAsInternalUser).toHaveBeenCalledTimes(1);
348+
expect(customClient.asInternalUser.xpack.info).toHaveBeenCalledTimes(1);
339349
const license = await license$.pipe(take(1)).toPromise();
340350
expect(license.type).toBe('gold');
341351
});

0 commit comments

Comments
 (0)