Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] Add test coverage for package policy variable overrides #113270

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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