From 1f16871d36a6a82fa0e353557566c719999dcd9b Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Tue, 28 Sep 2021 17:44:14 -0400 Subject: [PATCH] Add test for overridePackageInputs method (#113270) (#113320) Co-authored-by: Kyle Pollich --- .../common/types/models/preconfiguration.ts | 3 + .../server/services/package_policy.test.ts | 102 +++++++++++++++++- 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/fleet/common/types/models/preconfiguration.ts b/x-pack/plugins/fleet/common/types/models/preconfiguration.ts index 17f9b946885b1..f474606be831b 100644 --- a/x-pack/plugins/fleet/common/types/models/preconfiguration.ts +++ b/x-pack/plugins/fleet/common/types/models/preconfiguration.ts @@ -13,6 +13,9 @@ import type { import type { NewAgentPolicy } from './agent_policy'; import type { Output } from './output'; +// TODO: This type is not usable directly, and instead we typically use a type assertion +// e.g. `NewPackagePolicyInput as InputsOverride[]`. This type should be altered so that it's +// possible to use it directly in tests, etc export type InputsOverride = Partial & { vars?: Array; }; 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 fe5a3030bd95a..31f1440135436 100644 --- a/x-pack/plugins/fleet/server/services/package_policy.test.ts +++ b/x-pack/plugins/fleet/server/services/package_policy.test.ts @@ -28,11 +28,20 @@ import type { PutPackagePolicyUpdateCallback, PostPackagePolicyCreateCallback } import { createAppContextStartContractMock, xpackMocks } from '../mocks'; -import type { DeletePackagePoliciesResponse } from '../../common'; +import type { + DeletePackagePoliciesResponse, + InputsOverride, + NewPackagePolicy, + NewPackagePolicyInput, +} from '../../common'; import { IngestManagerError } from '../errors'; -import { packagePolicyService, _applyIndexPrivileges } from './package_policy'; +import { + overridePackageInputs, + packagePolicyService, + _applyIndexPrivileges, +} from './package_policy'; import { appContextService } from './app_context'; async function mockedGetAssetsData(_a: any, _b: any, dataset: string) { @@ -1074,6 +1083,95 @@ describe('Package policy service', () => { }); }); }); + + describe('overridePackageInputs', () => { + it('should override variable in base package policy', () => { + const basePackagePolicy: NewPackagePolicy = { + name: 'base-package-policy', + description: 'Base Package Policy', + namespace: 'default', + enabled: true, + policy_id: 'xxxx', + output_id: 'xxxx', + package: { + name: 'test-package', + title: 'Test Package', + version: '0.0.1', + }, + inputs: [ + { + type: 'logs', + policy_template: 'template_1', + enabled: true, + vars: { + path: { + type: 'text', + value: ['/var/log/logfile.log'], + }, + }, + streams: [], + }, + ], + }; + + const packageInfo: PackageInfo = { + name: 'test-package', + description: 'Test Package', + title: 'Test Package', + version: '0.0.1', + latestVersion: '0.0.1', + release: 'experimental', + format_version: '1.0.0', + owner: { github: 'elastic/fleet' }, + policy_templates: [ + { + name: 'template_1', + title: 'Template 1', + description: 'Template 1', + inputs: [ + { + type: 'logs', + title: 'Log', + description: 'Log Input', + vars: [ + { + name: 'path', + type: 'text', + }, + ], + }, + ], + }, + ], + // @ts-ignore + assets: {}, + }; + + const inputsOverride: NewPackagePolicyInput[] = [ + { + type: 'logs', + enabled: true, + streams: [], + vars: { + path: { + type: 'text', + value: '/var/log/new-logfile.log', + }, + }, + }, + ]; + + const result = overridePackageInputs( + basePackagePolicy, + packageInfo, + // TODO: Update this type assertion when the `InputsOverride` type is updated such + // that it no longer causes unresolvable type errors when used directly + inputsOverride as InputsOverride[], + false + ); + expect(result.inputs[0]?.vars?.path.value).toBe('/var/log/new-logfile.log'); + }); + }); }); describe('_applyIndexPrivileges()', () => {