Skip to content

Commit

Permalink
Add tests for the license getter
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo committed Nov 30, 2020
1 parent 1b97de1 commit 9008eb5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 8 deletions.
8 changes: 0 additions & 8 deletions x-pack/plugins/telemetry_collection_xpack/common/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { elasticsearchServiceMock } from 'src/core/server/mocks';
import { getLicenseFromLocalOrMaster } from './get_license';

describe('getLicenseFromLocalOrMaster', () => {
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 () => {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
// The local fetch fails
esClient.license.get.mockRejectedValue(new Error('Something went terribly wrong'));

const license = await getLicenseFromLocalOrMaster(esClient);

expect(license).toBeUndefined();
expect(esClient.license.get).toHaveBeenCalledWith({ local: true, accept_enterprise: true });
expect(esClient.license.get).toHaveBeenCalledTimes(1);
});

test('returns the license it fetches from Elasticsearch', async () => {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
// The local fetch succeeds
esClient.license.get.mockResolvedValue({ body: { license: { type: 'basic' } } } as any);

const license = await getLicenseFromLocalOrMaster(esClient);

expect(license).toStrictEqual({ type: 'basic' });
expect(esClient.license.get).toHaveBeenCalledWith({ local: true, accept_enterprise: true });
expect(esClient.license.get).toHaveBeenCalledTimes(1);
});

test('after the first successful attempt, if the local request fails, it will try with the master request (failed case)', async () => {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
const error = new Error('Something went terribly wrong');
// The requests fail with an error
esClient.license.get.mockRejectedValue(error);

await expect(getLicenseFromLocalOrMaster(esClient)).rejects.toStrictEqual(error);

expect(esClient.license.get).toHaveBeenCalledWith({ local: true, accept_enterprise: true });
expect(esClient.license.get).toHaveBeenCalledWith({ local: false, accept_enterprise: true });
expect(esClient.license.get).toHaveBeenCalledTimes(2);
});

test('after the first successful attempt, if the local request fails, it will try with the master request (success case)', async () => {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
// The local fetch fails
esClient.license.get.mockRejectedValueOnce(new Error('Something went terribly wrong'));
// The master fetch succeeds
esClient.license.get.mockResolvedValue({ body: { license: { type: 'basic' } } } as any);

const license = await getLicenseFromLocalOrMaster(esClient);

expect(license).toStrictEqual({ type: 'basic' });
expect(esClient.license.get).toHaveBeenCalledWith({ local: true, accept_enterprise: true });
expect(esClient.license.get).toHaveBeenCalledWith({ local: false, accept_enterprise: true });
expect(esClient.license.get).toHaveBeenCalledTimes(2);
});

test('after the first successful attempt, if the local request fails, it will try with the master request (clearing cached license)', async () => {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
// The requests fail with 400
esClient.license.get.mockRejectedValue({ statusCode: 400 });

// First attempt goes through 2 requests: local and master
const license = await getLicenseFromLocalOrMaster(esClient);

expect(license).toBeUndefined();
expect(esClient.license.get).toHaveBeenCalledWith({ local: true, accept_enterprise: true });
expect(esClient.license.get).toHaveBeenCalledWith({ local: false, accept_enterprise: true });
expect(esClient.license.get).toHaveBeenCalledTimes(2);

// Now the cached license is cleared, next request only goes for local and gives up when failed
esClient.license.get.mockClear();
await expect(getLicenseFromLocalOrMaster(esClient)).resolves.toBeUndefined();
expect(esClient.license.get).toHaveBeenCalledWith({ local: true, accept_enterprise: true });
expect(esClient.license.get).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 9008eb5

Please sign in to comment.