Skip to content

Commit

Permalink
[Ingest Manager] prevent crash on unhandled rejection from setupInges…
Browse files Browse the repository at this point in the history
…tManager (#74300) (#74350)

* Add test to ensure setup rejects if errors thrown.

* Return the promise from setup so test passes
  • Loading branch information
John Schulz authored Aug 5, 2020
1 parent eadea24 commit 24099c3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
44 changes: 44 additions & 0 deletions x-pack/plugins/ingest_manager/server/services/setup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 { setupIngestManager } from './setup';
import { savedObjectsClientMock } from 'src/core/server/mocks';

describe('setupIngestManager', () => {
it('returned promise should reject if errors thrown', async () => {
const { savedObjectsClient, callClusterMock } = makeErrorMocks();
const setupPromise = setupIngestManager(savedObjectsClient, callClusterMock);
await expect(setupPromise).rejects.toThrow('mocked');
});
});

function makeErrorMocks() {
jest.mock('./app_context'); // else fails w/"Logger not set."
jest.mock('./epm/registry/registry_url', () => {
return {
fetchUrl: () => {
throw new Error('mocked registry#fetchUrl');
},
};
});

const callClusterMock = jest.fn();
const savedObjectsClient = savedObjectsClientMock.create();
savedObjectsClient.find = jest.fn().mockImplementation(() => {
throw new Error('mocked SO#find');
});
savedObjectsClient.get = jest.fn().mockImplementation(() => {
throw new Error('mocked SO#get');
});
savedObjectsClient.update = jest.fn().mockImplementation(() => {
throw new Error('mocked SO#update');
});

return {
savedObjectsClient,
callClusterMock,
};
}
5 changes: 5 additions & 0 deletions x-pack/plugins/ingest_manager/server/services/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ export async function setupIngestManager(
// if anything errors, reject/fail
onSetupReject(error);
}

// be sure to return the promise because it has the resolved/rejected status attached to it
// otherwise, we effectively return success every time even if there are errors
// because `return undefined` -> `Promise.resolve(undefined)` in an `async` function
return setupIngestStatus;
}

export async function setupFleet(
Expand Down

0 comments on commit 24099c3

Please sign in to comment.