forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break up tests & modules for easier testing.
Deal with issue described in jestjs/jest#1075 (comment) epm/packages/install has functions a, b, c which are independent but a can also call b and c function a() { b(); c(); } The linked FB issue describes the cause and rationale (Jest works on "module" boundary) but TL;DR: it's easier if you split up your files Some related links I found during this journey * https://medium.com/@qjli/how-to-mock-specific-module-function-in-jest-715e39a391f4 * https://stackoverflow.com/questions/52650367/jestjs-how-to-test-function-being-called-inside-another-function * https://stackoverflow.com/questions/50854440/spying-on-an-imported-function-that-calls-another-function-in-jest/50855968#50855968
- Loading branch information
John Schulz
committed
Sep 25, 2020
1 parent
037ba98
commit a76006f
Showing
6 changed files
with
187 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
x-pack/plugins/ingest_manager/server/services/epm/packages/bulk_install_packages.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* 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 * as Registry from '../registry'; | ||
import { getInstallationObject } from './index'; | ||
import { | ||
BulkInstallPackagesParams, | ||
BulkInstallResponse, | ||
upgradePackage, | ||
bulkInstallErrorToOptions, | ||
} from './install'; | ||
|
||
export async function bulkInstallPackages({ | ||
savedObjectsClient, | ||
packagesToUpgrade, | ||
callCluster, | ||
}: BulkInstallPackagesParams): Promise<BulkInstallResponse[]> { | ||
const installedAndLatestPromises = packagesToUpgrade.map((pkgToUpgrade) => | ||
Promise.all([ | ||
getInstallationObject({ savedObjectsClient, pkgName: pkgToUpgrade }), | ||
Registry.fetchFindLatestPackage(pkgToUpgrade), | ||
]) | ||
); | ||
const installedAndLatestResults = await Promise.allSettled(installedAndLatestPromises); | ||
const installResponsePromises = installedAndLatestResults.map(async (result, index) => { | ||
const pkgToUpgrade = packagesToUpgrade[index]; | ||
if (result.status === 'fulfilled') { | ||
const [installedPkg, latestPkg] = result.value; | ||
return upgradePackage({ | ||
savedObjectsClient, | ||
callCluster, | ||
installedPkg, | ||
latestPkg, | ||
pkgToUpgrade, | ||
}); | ||
} else { | ||
return bulkInstallErrorToOptions({ pkgToUpgrade, error: result.reason }); | ||
} | ||
}); | ||
const installResults = await Promise.allSettled(installResponsePromises); | ||
const installResponses = installResults.map((result, index) => { | ||
const pkgToUpgrade = packagesToUpgrade[index]; | ||
if (result.status === 'fulfilled') { | ||
return result.value; | ||
} else { | ||
return bulkInstallErrorToOptions({ pkgToUpgrade, error: result.reason }); | ||
} | ||
}); | ||
|
||
return installResponses; | ||
} |
101 changes: 101 additions & 0 deletions
101
x-pack/plugins/ingest_manager/server/services/epm/packages/get_install_type.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* 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 { SavedObject } from 'src/core/server'; | ||
import { ElasticsearchAssetType, Installation, KibanaAssetType } from '../../../types'; | ||
import { getInstallType } from './install'; | ||
|
||
const mockInstallation: SavedObject<Installation> = { | ||
id: 'test-pkg', | ||
references: [], | ||
type: 'epm-packages', | ||
attributes: { | ||
id: 'test-pkg', | ||
installed_kibana: [{ type: KibanaAssetType.dashboard, id: 'dashboard-1' }], | ||
installed_es: [{ type: ElasticsearchAssetType.ingestPipeline, id: 'pipeline' }], | ||
es_index_patterns: { pattern: 'pattern-name' }, | ||
name: 'test packagek', | ||
version: '1.0.0', | ||
install_status: 'installed', | ||
install_version: '1.0.0', | ||
install_started_at: new Date().toISOString(), | ||
}, | ||
}; | ||
const mockInstallationUpdateFail: SavedObject<Installation> = { | ||
id: 'test-pkg', | ||
references: [], | ||
type: 'epm-packages', | ||
attributes: { | ||
id: 'test-pkg', | ||
installed_kibana: [{ type: KibanaAssetType.dashboard, id: 'dashboard-1' }], | ||
installed_es: [{ type: ElasticsearchAssetType.ingestPipeline, id: 'pipeline' }], | ||
es_index_patterns: { pattern: 'pattern-name' }, | ||
name: 'test packagek', | ||
version: '1.0.0', | ||
install_status: 'installing', | ||
install_version: '1.0.1', | ||
install_started_at: new Date().toISOString(), | ||
}, | ||
}; | ||
|
||
describe('getInstallType', () => { | ||
it('should return correct type when installing and no other version is currently installed', () => { | ||
const installTypeInstall = getInstallType({ pkgVersion: '1.0.0', installedPkg: undefined }); | ||
expect(installTypeInstall).toBe('install'); | ||
|
||
// @ts-expect-error can only be 'install' if no installedPkg given | ||
expect(installTypeInstall === 'update').toBe(false); | ||
// @ts-expect-error can only be 'install' if no installedPkg given | ||
expect(installTypeInstall === 'reinstall').toBe(false); | ||
// @ts-expect-error can only be 'install' if no installedPkg given | ||
expect(installTypeInstall === 'reupdate').toBe(false); | ||
// @ts-expect-error can only be 'install' if no installedPkg given | ||
expect(installTypeInstall === 'rollback').toBe(false); | ||
}); | ||
|
||
it('should return correct type when installing the same version', () => { | ||
const installTypeReinstall = getInstallType({ | ||
pkgVersion: '1.0.0', | ||
installedPkg: mockInstallation, | ||
}); | ||
expect(installTypeReinstall).toBe('reinstall'); | ||
|
||
// @ts-expect-error cannot be 'install' if given installedPkg | ||
expect(installTypeReinstall === 'install').toBe(false); | ||
}); | ||
|
||
it('should return correct type when moving from one version to another', () => { | ||
const installTypeUpdate = getInstallType({ | ||
pkgVersion: '1.0.1', | ||
installedPkg: mockInstallation, | ||
}); | ||
expect(installTypeUpdate).toBe('update'); | ||
|
||
// @ts-expect-error cannot be 'install' if given installedPkg | ||
expect(installTypeUpdate === 'install').toBe(false); | ||
}); | ||
|
||
it('should return correct type when update fails and trys again', () => { | ||
const installTypeReupdate = getInstallType({ | ||
pkgVersion: '1.0.1', | ||
installedPkg: mockInstallationUpdateFail, | ||
}); | ||
expect(installTypeReupdate).toBe('reupdate'); | ||
|
||
// @ts-expect-error cannot be 'install' if given installedPkg | ||
expect(installTypeReupdate === 'install').toBe(false); | ||
}); | ||
|
||
it('should return correct type when attempting to rollback from a failed update', () => { | ||
const installTypeRollback = getInstallType({ | ||
pkgVersion: '1.0.0', | ||
installedPkg: mockInstallationUpdateFail, | ||
}); | ||
expect(installTypeRollback).toBe('rollback'); | ||
|
||
// @ts-expect-error cannot be 'install' if given installedPkg | ||
expect(installTypeRollback === 'install').toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.