|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { elasticsearchServiceMock } from 'src/core/server/mocks'; |
| 8 | +import { getLicenseFromLocalOrMaster } from './get_license'; |
| 9 | + |
| 10 | +describe('getLicenseFromLocalOrMaster', () => { |
| 11 | + test('return an undefined license if it fails to get the license on the first attempt and it does not have a cached license yet', async () => { |
| 12 | + const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; |
| 13 | + // The local fetch fails |
| 14 | + esClient.license.get.mockRejectedValue(new Error('Something went terribly wrong')); |
| 15 | + |
| 16 | + const license = await getLicenseFromLocalOrMaster(esClient); |
| 17 | + |
| 18 | + expect(license).toBeUndefined(); |
| 19 | + expect(esClient.license.get).toHaveBeenCalledWith({ local: true, accept_enterprise: true }); |
| 20 | + expect(esClient.license.get).toHaveBeenCalledTimes(1); |
| 21 | + }); |
| 22 | + |
| 23 | + test('returns the license it fetches from Elasticsearch', async () => { |
| 24 | + const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; |
| 25 | + // The local fetch succeeds |
| 26 | + esClient.license.get.mockResolvedValue({ body: { license: { type: 'basic' } } } as any); |
| 27 | + |
| 28 | + const license = await getLicenseFromLocalOrMaster(esClient); |
| 29 | + |
| 30 | + expect(license).toStrictEqual({ type: 'basic' }); |
| 31 | + expect(esClient.license.get).toHaveBeenCalledWith({ local: true, accept_enterprise: true }); |
| 32 | + expect(esClient.license.get).toHaveBeenCalledTimes(1); |
| 33 | + }); |
| 34 | + |
| 35 | + test('after the first successful attempt, if the local request fails, it will try with the master request (failed case)', async () => { |
| 36 | + const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; |
| 37 | + const error = new Error('Something went terribly wrong'); |
| 38 | + // The requests fail with an error |
| 39 | + esClient.license.get.mockRejectedValue(error); |
| 40 | + |
| 41 | + await expect(getLicenseFromLocalOrMaster(esClient)).rejects.toStrictEqual(error); |
| 42 | + |
| 43 | + expect(esClient.license.get).toHaveBeenCalledWith({ local: true, accept_enterprise: true }); |
| 44 | + expect(esClient.license.get).toHaveBeenCalledWith({ local: false, accept_enterprise: true }); |
| 45 | + expect(esClient.license.get).toHaveBeenCalledTimes(2); |
| 46 | + }); |
| 47 | + |
| 48 | + test('after the first successful attempt, if the local request fails, it will try with the master request (success case)', async () => { |
| 49 | + const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; |
| 50 | + // The local fetch fails |
| 51 | + esClient.license.get.mockRejectedValueOnce(new Error('Something went terribly wrong')); |
| 52 | + // The master fetch succeeds |
| 53 | + esClient.license.get.mockResolvedValue({ body: { license: { type: 'basic' } } } as any); |
| 54 | + |
| 55 | + const license = await getLicenseFromLocalOrMaster(esClient); |
| 56 | + |
| 57 | + expect(license).toStrictEqual({ type: 'basic' }); |
| 58 | + expect(esClient.license.get).toHaveBeenCalledWith({ local: true, accept_enterprise: true }); |
| 59 | + expect(esClient.license.get).toHaveBeenCalledWith({ local: false, accept_enterprise: true }); |
| 60 | + expect(esClient.license.get).toHaveBeenCalledTimes(2); |
| 61 | + }); |
| 62 | + |
| 63 | + test('after the first successful attempt, if the local request fails, it will try with the master request (clearing cached license)', async () => { |
| 64 | + const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; |
| 65 | + // The requests fail with 400 |
| 66 | + esClient.license.get.mockRejectedValue({ statusCode: 400 }); |
| 67 | + |
| 68 | + // First attempt goes through 2 requests: local and master |
| 69 | + const license = await getLicenseFromLocalOrMaster(esClient); |
| 70 | + |
| 71 | + expect(license).toBeUndefined(); |
| 72 | + expect(esClient.license.get).toHaveBeenCalledWith({ local: true, accept_enterprise: true }); |
| 73 | + expect(esClient.license.get).toHaveBeenCalledWith({ local: false, accept_enterprise: true }); |
| 74 | + expect(esClient.license.get).toHaveBeenCalledTimes(2); |
| 75 | + |
| 76 | + // Now the cached license is cleared, next request only goes for local and gives up when failed |
| 77 | + esClient.license.get.mockClear(); |
| 78 | + await expect(getLicenseFromLocalOrMaster(esClient)).resolves.toBeUndefined(); |
| 79 | + expect(esClient.license.get).toHaveBeenCalledWith({ local: true, accept_enterprise: true }); |
| 80 | + expect(esClient.license.get).toHaveBeenCalledTimes(1); |
| 81 | + }); |
| 82 | +}); |
0 commit comments