Skip to content

Commit

Permalink
Handle promise rejections when building artifacts (#73831)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
madirey and elasticmachine committed Jul 30, 2020
1 parent c21474b commit c670afa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { savedObjectsClientMock, loggingSystemMock } from 'src/core/server/mocks
import { Logger } from 'src/core/server';
import { PackageConfigServiceInterface } from '../../../../../../ingest_manager/server';
import { createPackageConfigServiceMock } from '../../../../../../ingest_manager/server/mocks';
import { ExceptionListClient } from '../../../../../../lists/server';
import { listMock } from '../../../../../../lists/server/mocks';
import LRU from 'lru-cache';
import { getArtifactClientMock } from '../artifact_client.mock';
Expand All @@ -23,22 +24,29 @@ import {

export enum ManifestManagerMockType {
InitialSystemState,
ListClientPromiseRejection,
NormalFlow,
}

export const getManifestManagerMock = (opts?: {
mockType?: ManifestManagerMockType;
cache?: LRU<string, Buffer>;
exceptionListClient?: ExceptionListClient;
packageConfigService?: jest.Mocked<PackageConfigServiceInterface>;
savedObjectsClient?: ReturnType<typeof savedObjectsClientMock.create>;
}): ManifestManager => {
let cache = new LRU<string, Buffer>({ max: 10, maxAge: 1000 * 60 * 60 });
if (opts?.cache !== undefined) {
if (opts?.cache != null) {
cache = opts.cache;
}

let exceptionListClient = listMock.getExceptionListClient();
if (opts?.exceptionListClient != null) {
exceptionListClient = opts.exceptionListClient;
}

let packageConfigService = createPackageConfigServiceMock();
if (opts?.packageConfigService !== undefined) {
if (opts?.packageConfigService != null) {
packageConfigService = opts.packageConfigService;
}
packageConfigService.list = jest.fn().mockResolvedValue({
Expand All @@ -51,7 +59,7 @@ export const getManifestManagerMock = (opts?: {
});

let savedObjectsClient = savedObjectsClientMock.create();
if (opts?.savedObjectsClient !== undefined) {
if (opts?.savedObjectsClient != null) {
savedObjectsClient = opts.savedObjectsClient;
}

Expand All @@ -61,6 +69,11 @@ export const getManifestManagerMock = (opts?: {
switch (mockType) {
case ManifestManagerMockType.InitialSystemState:
return getEmptyMockArtifacts();
case ManifestManagerMockType.ListClientPromiseRejection:
exceptionListClient.findExceptionListItem = jest
.fn()
.mockRejectedValue(new Error('unexpected thing happened'));
return super.buildExceptionListArtifacts('v1');
case ManifestManagerMockType.NormalFlow:
return getMockArtifactsWithDiff();
}
Expand All @@ -85,7 +98,7 @@ export const getManifestManagerMock = (opts?: {
artifactClient: getArtifactClientMock(savedObjectsClient),
cache,
packageConfigService,
exceptionListClient: listMock.getExceptionListClient(),
exceptionListClient,
logger: loggingSystemMock.create().get() as jest.Mocked<Logger>,
savedObjectsClient,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { savedObjectsClientMock } from 'src/core/server/mocks';
import { createPackageConfigServiceMock } from '../../../../../../ingest_manager/server/mocks';
import { ArtifactConstants, ManifestConstants, isCompleteArtifact } from '../../../lib/artifacts';

import { getManifestManagerMock } from './manifest_manager.mock';
import { getManifestManagerMock, ManifestManagerMockType } from './manifest_manager.mock';
import LRU from 'lru-cache';

describe('manifest_manager', () => {
Expand Down Expand Up @@ -204,5 +204,14 @@ describe('manifest_manager', () => {
oldArtifactId
);
});

test('ManifestManager handles promise rejections when building artifacts', async () => {
// This test won't fail on an unhandled promise rejection, but it will cause
// an UnhandledPromiseRejectionWarning to be printed.
const manifestManager = getManifestManagerMock({
mockType: ManifestManagerMockType.ListClientPromiseRejection,
});
await expect(manifestManager.buildNewManifest()).rejects.toThrow();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,17 @@ export class ManifestManager {
protected async buildExceptionListArtifacts(
artifactSchemaVersion?: string
): Promise<InternalArtifactCompleteSchema[]> {
return ArtifactConstants.SUPPORTED_OPERATING_SYSTEMS.reduce<
Promise<InternalArtifactCompleteSchema[]>
>(async (acc, os) => {
const artifacts: InternalArtifactCompleteSchema[] = [];
for (const os of ArtifactConstants.SUPPORTED_OPERATING_SYSTEMS) {
const exceptionList = await getFullEndpointExceptionList(
this.exceptionListClient,
os,
artifactSchemaVersion ?? 'v1'
);
const artifacts = await acc;
const artifact = await buildArtifact(exceptionList, os, artifactSchemaVersion ?? 'v1');
return Promise.resolve([...artifacts, artifact]);
}, Promise.resolve([]));
artifacts.push(artifact);
}
return artifacts;
}

/**
Expand Down

0 comments on commit c670afa

Please sign in to comment.