From a1eb081ec902dab06ddd9be85145b1e710c30894 Mon Sep 17 00:00:00 2001 From: Julia Bardi Date: Tue, 22 Feb 2022 11:41:22 +0100 Subject: [PATCH] fixed tests --- x-pack/plugins/fleet/server/mocks/index.ts | 3 - .../routes/package_policy/handlers.test.ts | 7 +- .../services/managed_package_policies.test.ts | 42 +++-- .../server/services/package_policy.test.ts | 17 +- .../fleet/server/services/package_policy.ts | 154 +++++++++++++++--- 5 files changed, 161 insertions(+), 62 deletions(-) diff --git a/x-pack/plugins/fleet/server/mocks/index.ts b/x-pack/plugins/fleet/server/mocks/index.ts index 62094b1836726..2dc4dc9b4861d 100644 --- a/x-pack/plugins/fleet/server/mocks/index.ts +++ b/x-pack/plugins/fleet/server/mocks/index.ts @@ -97,14 +97,11 @@ export const xpackMocks = { createRequestHandlerContext: createCoreRequestHandlerContextMock, }; -// TODO PackagePolicyServiceInterface should not include private methods export const createPackagePolicyServiceMock = (): jest.Mocked< Partial > => { return { - _compilePackagePolicyInputs: jest.fn(), buildPackagePolicyFromPackage: jest.fn(), - buildPackagePolicyFromPackageWithVersion: jest.fn(), bulkCreate: jest.fn(), create: jest.fn(), delete: jest.fn(), diff --git a/x-pack/plugins/fleet/server/routes/package_policy/handlers.test.ts b/x-pack/plugins/fleet/server/routes/package_policy/handlers.test.ts index 8aa223cd7fa41..25d0f5c577667 100644 --- a/x-pack/plugins/fleet/server/routes/package_policy/handlers.test.ts +++ b/x-pack/plugins/fleet/server/routes/package_policy/handlers.test.ts @@ -28,17 +28,12 @@ import type { PackagePolicy } from '../../types'; import { registerRoutes } from './index'; -type PackagePolicyServicePublicInterface = Omit< - PackagePolicyServiceInterface, - 'getUpgradePackagePolicyInfo' ->; - const packagePolicyServiceMock = packagePolicyService as jest.Mocked; jest.mock( '../../services/package_policy', (): { - packagePolicyService: jest.Mocked; + packagePolicyService: jest.Mocked; } => { return { packagePolicyService: { diff --git a/x-pack/plugins/fleet/server/services/managed_package_policies.test.ts b/x-pack/plugins/fleet/server/services/managed_package_policies.test.ts index 402b65334121a..6cbb21a37a196 100644 --- a/x-pack/plugins/fleet/server/services/managed_package_policies.test.ts +++ b/x-pack/plugins/fleet/server/services/managed_package_policies.test.ts @@ -45,25 +45,24 @@ describe('upgradeManagedPackagePolicies', () => { it('should upgrade policies for managed package', async () => { const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; const soClient = savedObjectsClientMock.create(); + const packagePolicy = { + id: 'managed-package-id', + inputs: {}, + version: '', + revision: 1, + updated_at: '', + updated_by: '', + created_at: '', + created_by: '', + package: { + name: 'managed-package', + title: 'Managed Package', + version: '0.0.1', + }, + }; (packagePolicyService.list as jest.Mock).mockResolvedValueOnce({ - items: [ - { - id: 'managed-package-id', - inputs: {}, - version: '', - revision: 1, - updated_at: '', - updated_by: '', - created_at: '', - created_by: '', - package: { - name: 'managed-package', - title: 'Managed Package', - version: '0.0.1', - }, - }, - ], + items: [packagePolicy], }); (packagePolicyService.getUpgradeDryRunDiff as jest.Mock).mockResolvedValueOnce({ @@ -89,7 +88,14 @@ describe('upgradeManagedPackagePolicies', () => { { packagePolicyId: 'managed-package-id', diff: [{ id: 'foo' }, { id: 'bar' }], errors: [] }, ]); - expect(packagePolicyService.upgrade).toBeCalledWith(soClient, esClient, ['managed-package-id']); + expect(packagePolicyService.upgrade).toBeCalledWith( + soClient, + esClient, + ['managed-package-id'], + undefined, + packagePolicy, + '1.0.0' + ); }); it('should not upgrade policy if newer than installed package version', async () => { diff --git a/x-pack/plugins/fleet/server/services/package_policy.test.ts b/x-pack/plugins/fleet/server/services/package_policy.test.ts index 09271cc2b3dbd..29d10adb1b6be 100644 --- a/x-pack/plugins/fleet/server/services/package_policy.test.ts +++ b/x-pack/plugins/fleet/server/services/package_policy.test.ts @@ -49,6 +49,7 @@ import { updatePackageInputs, packagePolicyService, _applyIndexPrivileges, + _compilePackagePolicyInputs, } from './package_policy'; import { appContextService } from './app_context'; @@ -171,7 +172,7 @@ type CombinedExternalCallback = PutPackagePolicyUpdateCallback | PostPackagePoli describe('Package policy service', () => { describe('_compilePackagePolicyInputs', () => { it('should work with config variables from the stream', async () => { - const inputs = await packagePolicyService._compilePackagePolicyInputs( + const inputs = await _compilePackagePolicyInputs( { data_streams: [ { @@ -234,7 +235,7 @@ describe('Package policy service', () => { }); it('should work with a two level dataset name', async () => { - const inputs = await packagePolicyService._compilePackagePolicyInputs( + const inputs = await _compilePackagePolicyInputs( { data_streams: [ { @@ -286,7 +287,7 @@ describe('Package policy service', () => { }); it('should work with config variables at the input level', async () => { - const inputs = await packagePolicyService._compilePackagePolicyInputs( + const inputs = await _compilePackagePolicyInputs( { data_streams: [ { @@ -349,7 +350,7 @@ describe('Package policy service', () => { }); it('should work with config variables at the package level', async () => { - const inputs = await packagePolicyService._compilePackagePolicyInputs( + const inputs = await _compilePackagePolicyInputs( { data_streams: [ { @@ -417,7 +418,7 @@ describe('Package policy service', () => { }); it('should work with an input with a template and no streams', async () => { - const inputs = await packagePolicyService._compilePackagePolicyInputs( + const inputs = await _compilePackagePolicyInputs( { data_streams: [], policy_templates: [ @@ -459,7 +460,7 @@ describe('Package policy service', () => { }); it('should work with an input with a template and streams', async () => { - const inputs = await packagePolicyService._compilePackagePolicyInputs( + const inputs = await _compilePackagePolicyInputs( { data_streams: [ { @@ -564,7 +565,7 @@ describe('Package policy service', () => { }); it('should work with a package without input', async () => { - const inputs = await packagePolicyService._compilePackagePolicyInputs( + const inputs = await _compilePackagePolicyInputs( { policy_templates: [ { @@ -580,7 +581,7 @@ describe('Package policy service', () => { }); it('should work with a package with a empty inputs array', async () => { - const inputs = await packagePolicyService._compilePackagePolicyInputs( + const inputs = await _compilePackagePolicyInputs( { policy_templates: [ { diff --git a/x-pack/plugins/fleet/server/services/package_policy.ts b/x-pack/plugins/fleet/server/services/package_policy.ts index 44c882bdee2b0..cb8c7239bf4d5 100644 --- a/x-pack/plugins/fleet/server/services/package_policy.ts +++ b/x-pack/plugins/fleet/server/services/package_policy.ts @@ -86,7 +86,7 @@ export const DATA_STREAM_ALLOWED_INDEX_PRIVILEGES = new Set([ 'read_cross_cluster', ]); -class PackagePolicyService { +class PackagePolicyService implements PackagePolicyServiceInterface { public async create( soClient: SavedObjectsClientContract, esClient: ElasticsearchClient, @@ -162,7 +162,7 @@ class PackagePolicyService { } validatePackagePolicyOrThrow(packagePolicy, pkgInfo); - inputs = await this._compilePackagePolicyInputs(pkgInfo, packagePolicy.vars || {}, inputs); + inputs = await _compilePackagePolicyInputs(pkgInfo, packagePolicy.vars || {}, inputs); elasticsearch = pkgInfo.elasticsearch; } @@ -393,7 +393,7 @@ class PackagePolicyService { validatePackagePolicyOrThrow(packagePolicy, pkgInfo); - inputs = await this._compilePackagePolicyInputs(pkgInfo, packagePolicy.vars || {}, inputs); + inputs = await _compilePackagePolicyInputs(pkgInfo, packagePolicy.vars || {}, inputs); elasticsearch = pkgInfo.elasticsearch; } @@ -498,7 +498,11 @@ class PackagePolicyService { return result; } - public async getUpgradePackagePolicyInfo(soClient: SavedObjectsClientContract, id: string) { + // TODO should move out, public only for unit tests + public async getUpgradePackagePolicyInfo( + soClient: SavedObjectsClientContract, + id: string + ): Promise<{ packagePolicy: PackagePolicy; packageInfo: PackageInfo }> { const packagePolicy = await this.get(soClient, id); if (!packagePolicy) { throw new IngestManagerError( @@ -622,7 +626,7 @@ class PackagePolicyService { packageInfo, packageToPackagePolicyInputs(packageInfo) as InputsOverride[] ); - updatePackagePolicy.inputs = await this._compilePackagePolicyInputs( + updatePackagePolicy.inputs = await _compilePackagePolicyInputs( packageInfo, updatePackagePolicy.vars || {}, updatePackagePolicy.inputs as PackagePolicyInput[] @@ -688,7 +692,7 @@ class PackagePolicyService { packageToPackagePolicyInputs(packageInfo) as InputsOverride[], true ); - updatedPackagePolicy.inputs = await this._compilePackagePolicyInputs( + updatedPackagePolicy.inputs = await _compilePackagePolicyInputs( packageInfo, updatedPackagePolicy.vars || {}, updatedPackagePolicy.inputs as PackagePolicyInput[] @@ -796,7 +800,7 @@ class PackagePolicyService { return newPackagePolicy; } - public async buildPackagePolicyFromPackageWithVersion( + private async buildPackagePolicyFromPackageWithVersion( soClient: SavedObjectsClientContract, pkgName: string, pkgVersion: string @@ -834,24 +838,6 @@ class PackagePolicyService { } } - public async _compilePackagePolicyInputs( - pkgInfo: PackageInfo, - vars: PackagePolicy['vars'], - inputs: PackagePolicyInput[] - ): Promise { - const inputsPromises = inputs.map(async (input) => { - const compiledInput = await _compilePackagePolicyInput(pkgInfo, vars, input); - const compiledStreams = await _compilePackageStreams(pkgInfo, vars, input); - return { - ...input, - compiled_input: compiledInput, - streams: compiledStreams, - }; - }); - - return Promise.all(inputsPromises); - } - public async runExternalCallbacks( externalCallbackType: A, packagePolicy: A extends 'postPackagePolicyDelete' @@ -951,6 +937,24 @@ function assignStreamIdToInput(packagePolicyId: string, input: NewPackagePolicyI }; } +export async function _compilePackagePolicyInputs( + pkgInfo: PackageInfo, + vars: PackagePolicy['vars'], + inputs: PackagePolicyInput[] +): Promise { + const inputsPromises = inputs.map(async (input) => { + const compiledInput = await _compilePackagePolicyInput(pkgInfo, vars, input); + const compiledStreams = await _compilePackageStreams(pkgInfo, vars, input); + return { + ...input, + compiled_input: compiledInput, + streams: compiledStreams, + }; + }); + + return Promise.all(inputsPromises); +} + async function _compilePackagePolicyInput( pkgInfo: PackageInfo, vars: PackagePolicy['vars'], @@ -1147,8 +1151,104 @@ function _enforceFrozenVars( return resultVars; } -export type PackagePolicyServiceInterface = PackagePolicyService; -export const packagePolicyService = new PackagePolicyService(); +export interface PackagePolicyServiceInterface { + create( + soClient: SavedObjectsClientContract, + esClient: ElasticsearchClient, + packagePolicy: NewPackagePolicy, + options?: { + spaceId?: string; + id?: string; + user?: AuthenticatedUser; + bumpRevision?: boolean; + force?: boolean; + skipEnsureInstalled?: boolean; + skipUniqueNameVerification?: boolean; + overwrite?: boolean; + } + ): Promise; + + bulkCreate( + soClient: SavedObjectsClientContract, + esClient: ElasticsearchClient, + packagePolicies: NewPackagePolicy[], + agentPolicyId: string, + options?: { user?: AuthenticatedUser; bumpRevision?: boolean } + ): Promise; + + get(soClient: SavedObjectsClientContract, id: string): Promise; + + getByIDs(soClient: SavedObjectsClientContract, ids: string[]): Promise; + + list( + soClient: SavedObjectsClientContract, + options: ListWithKuery + ): Promise>; + + listIds( + soClient: SavedObjectsClientContract, + options: ListWithKuery + ): Promise>; + + update( + soClient: SavedObjectsClientContract, + esClient: ElasticsearchClient, + id: string, + packagePolicyUpdate: UpdatePackagePolicy, + options?: { user?: AuthenticatedUser }, + currentVersion?: string + ): Promise; + + delete( + soClient: SavedObjectsClientContract, + esClient: ElasticsearchClient, + ids: string[], + options?: { user?: AuthenticatedUser; skipUnassignFromAgentPolicies?: boolean; force?: boolean } + ): Promise; + + upgrade( + soClient: SavedObjectsClientContract, + esClient: ElasticsearchClient, + ids: string[], + options?: { user?: AuthenticatedUser }, + packagePolicy?: PackagePolicy, + pkgVersion?: string + ): Promise; + + getUpgradeDryRunDiff( + soClient: SavedObjectsClientContract, + id: string, + packagePolicy?: PackagePolicy, + pkgVersion?: string + ): Promise; + + enrichPolicyWithDefaultsFromPackage( + soClient: SavedObjectsClientContract, + newPolicy: NewPackagePolicy + ): Promise; + + buildPackagePolicyFromPackage( + soClient: SavedObjectsClientContract, + pkgName: string + ): Promise; + + runExternalCallbacks( + externalCallbackType: A, + packagePolicy: A extends 'postPackagePolicyDelete' + ? DeletePackagePoliciesResponse + : NewPackagePolicy, + context: RequestHandlerContext, + request: KibanaRequest + ): Promise; + + runDeleteExternalCallbacks(deletedPackagePolicies: DeletePackagePoliciesResponse): Promise; + + getUpgradePackagePolicyInfo( + soClient: SavedObjectsClientContract, + id: string + ): Promise<{ packagePolicy: PackagePolicy; packageInfo: PackageInfo }>; +} +export const packagePolicyService: PackagePolicyServiceInterface = new PackagePolicyService(); export type { PackagePolicyService };