Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Nov 20, 2024
1 parent 409bbf7 commit 32dc285
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
1 change: 0 additions & 1 deletion x-pack/plugins/fleet/common/types/models/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export interface PackagePolicy extends Omit<NewPackagePolicy, 'inputs'> {
updated_by: string;
created_at: string;
created_by: string;
// bump_agent_policy_revision?: boolean;
}

export type DryRunPackagePolicy = NewPackagePolicy & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,102 @@

import { loggingSystemMock } from '@kbn/core/server/mocks';

import { packagePolicyService } from '../package_policy';
import type { PackagePolicy } from '../../types';
import { agentPolicyService } from '../agent_policy';

import { appContextService } from '..';
import { getPackagePolicySavedObjectType } from '../package_policy';

import { _updatePackagePoliciesThatNeedBump } from './bump_agent_policies_task';

jest.mock('../app_context');
jest.mock('../agent_policy');
jest.mock('../package_policy');

const mockedPackagePolicyService = jest.mocked(packagePolicyService);
const mockedAgentPolicyService = jest.mocked(agentPolicyService);
const mockedAppContextService = jest.mocked(appContextService);
const mockSoClient = {
find: jest.fn(),
bulkUpdate: jest.fn(),
} as any;
const mockGetPackagePolicySavedObjectType = jest.mocked(getPackagePolicySavedObjectType);

describe('_updatePackagePoliciesThatNeedBump', () => {
beforeEach(() => {
jest.clearAllMocks();
mockedPackagePolicyService.list.mockResolvedValueOnce({
total: 1,
items: [
mockSoClient.find.mockResolvedValue({
total: 3,
saved_objects: [
{
id: 'packagePolicy1',
} as PackagePolicy,
namespaces: ['default'],
attributes: {
policy_ids: ['policy1'],
},
},
{
id: 'packagePolicy2',
namespaces: ['space'],
attributes: {
policy_ids: ['policy2'],
},
},
{
id: 'packagePolicy3',
namespaces: ['space'],
attributes: {
policy_ids: ['policy3'],
},
},
],
page: 1,
perPage: 100,
});
mockedPackagePolicyService.list.mockResolvedValueOnce({
total: 0,
items: [],
page: 1,
perPage: 100,
});
mockedAppContextService.getInternalUserSOClientWithoutSpaceExtension.mockReturnValue(
mockSoClient
);
mockedAppContextService.getInternalUserSOClientForSpaceId.mockReturnValue(mockSoClient);
mockGetPackagePolicySavedObjectType.mockResolvedValue('fleet-package-policies');
});

it('should update package policy if bump agent policy revision needed', async () => {
const logger = loggingSystemMock.createLogger();

await _updatePackagePoliciesThatNeedBump(logger, false);

expect(mockedPackagePolicyService.bulkUpdate).toHaveBeenCalledWith(undefined, undefined, [
{ bump_agent_policy_revision: false, id: 'packagePolicy1' },
expect(mockSoClient.bulkUpdate).toHaveBeenCalledWith([
{
attributes: { bump_agent_policy_revision: false },
id: 'packagePolicy1',
type: 'fleet-package-policies',
},
]);
expect(mockSoClient.bulkUpdate).toHaveBeenCalledWith([
{
attributes: { bump_agent_policy_revision: false },
id: 'packagePolicy2',
type: 'fleet-package-policies',
},
{
attributes: { bump_agent_policy_revision: false },
id: 'packagePolicy3',
type: 'fleet-package-policies',
},
]);

expect(mockedAgentPolicyService.bumpRevision).toHaveBeenCalledWith(
expect.anything(),
undefined,
'policy1'
);
expect(mockedAgentPolicyService.bumpRevision).toHaveBeenCalledWith(
expect.anything(),
undefined,
'policy2'
);
expect(mockedAgentPolicyService.bumpRevision).toHaveBeenCalledWith(
expect.anything(),
undefined,
'policy3'
);
});
});

0 comments on commit 32dc285

Please sign in to comment.