Skip to content

Commit

Permalink
Add test for overridePackageInputs method (#113270) (#113320)
Browse files Browse the repository at this point in the history
Co-authored-by: Kyle Pollich <kyle.pollich@elastic.co>
  • Loading branch information
kibanamachine and kpollich authored Sep 28, 2021
1 parent 22b1b7f commit 1f16871
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/fleet/common/types/models/preconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NewPackagePolicyInput> & {
vars?: Array<NewPackagePolicyInput['vars'] & { name: string }>;
};
Expand Down
102 changes: 100 additions & 2 deletions x-pack/plugins/fleet/server/services/package_policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()', () => {
Expand Down

0 comments on commit 1f16871

Please sign in to comment.