From 1c5c811d628675a5ba5ea1af30f19aa9bc084f92 Mon Sep 17 00:00:00 2001 From: Julia Bardi Date: Thu, 22 Feb 2024 16:52:04 +0100 Subject: [PATCH] added unit test --- .../services/agent_policy_create.test.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/x-pack/plugins/fleet/server/services/agent_policy_create.test.ts b/x-pack/plugins/fleet/server/services/agent_policy_create.test.ts index 45d42ba373a7d..f541212a51ab1 100644 --- a/x-pack/plugins/fleet/server/services/agent_policy_create.test.ts +++ b/x-pack/plugins/fleet/server/services/agent_policy_create.test.ts @@ -190,6 +190,35 @@ describe('createAgentPolicyWithPackages', () => { ); }); + it('should call deploy policy once when create policy with system package', async () => { + mockedAgentPolicyService.deployPolicy.mockClear(); + mockedAgentPolicyService.create.mockImplementation((soClient, esClient, newPolicy, options) => { + if (!options?.skipDeploy) { + mockedAgentPolicyService.deployPolicy(soClientMock, 'new_id'); + } + return Promise.resolve({ + ...newPolicy, + id: options?.id || 'new_id', + } as AgentPolicy); + }); + const response = await createAgentPolicyWithPackages({ + esClient: esClientMock, + soClient: soClientMock, + newPolicy: { name: 'Agent policy 1', namespace: 'default' }, + withSysMonitoring: true, + spaceId: 'default', + }); + + expect(response.id).toEqual('new_id'); + expect(mockedBulkInstallPackages).toHaveBeenCalledWith({ + savedObjectsClient: soClientMock, + esClient: esClientMock, + packagesToInstall: ['system'], + spaceId: 'default', + }); + expect(mockedAgentPolicyService.deployPolicy).toHaveBeenCalledTimes(1); + }); + it('should create policy with system and elastic_agent package', async () => { const response = await createAgentPolicyWithPackages({ esClient: esClientMock,