From badeaae0c3494d44ba954743215c9880688899a0 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Tue, 13 Oct 2020 11:54:09 -0400 Subject: [PATCH] [Ingest Manager] Fix agent policy bump revision to create only one POLICY_CHANGE action (#80081) (#80329) --- .../server/services/agent_policy.test.ts | 36 +++++++++++++++++++ .../server/services/agent_policy.ts | 2 -- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/ingest_manager/server/services/agent_policy.test.ts b/x-pack/plugins/ingest_manager/server/services/agent_policy.test.ts index d247b35c089e5..f9a8b63bb83ad 100644 --- a/x-pack/plugins/ingest_manager/server/services/agent_policy.test.ts +++ b/x-pack/plugins/ingest_manager/server/services/agent_policy.test.ts @@ -6,6 +6,7 @@ import { savedObjectsClientMock } from 'src/core/server/mocks'; import { agentPolicyService } from './agent_policy'; +import { agentPolicyUpdateEventHandler } from './agent_policy_update'; import { Output } from '../types'; function getSavedObjectMock(agentPolicyAttributes: any) { @@ -59,7 +60,42 @@ jest.mock('./output', () => { }; }); +jest.mock('./agent_policy_update'); + +function getAgentPolicyUpdateMock() { + return (agentPolicyUpdateEventHandler as unknown) as jest.Mock< + typeof agentPolicyUpdateEventHandler + >; +} + describe('agent policy', () => { + beforeEach(() => { + getAgentPolicyUpdateMock().mockClear(); + }); + describe('bumpRevision', () => { + it('should call agentPolicyUpdateEventHandler with updated event once', async () => { + const soClient = getSavedObjectMock({ + revision: 1, + monitoring_enabled: ['metrics'], + }); + await agentPolicyService.bumpRevision(soClient, 'agent-policy'); + + expect(agentPolicyUpdateEventHandler).toHaveBeenCalledTimes(1); + }); + }); + + describe('bumpAllAgentPolicies', () => { + it('should call agentPolicyUpdateEventHandler with updated event once', async () => { + const soClient = getSavedObjectMock({ + revision: 1, + monitoring_enabled: ['metrics'], + }); + await agentPolicyService.bumpAllAgentPolicies(soClient); + + expect(agentPolicyUpdateEventHandler).toHaveBeenCalledTimes(1); + }); + }); + describe('getFullAgentPolicy', () => { it('should return a policy without monitoring if monitoring is not enabled', async () => { const soClient = getSavedObjectMock({ diff --git a/x-pack/plugins/ingest_manager/server/services/agent_policy.ts b/x-pack/plugins/ingest_manager/server/services/agent_policy.ts index f1dcc7e5d6c99..3a28286617111 100644 --- a/x-pack/plugins/ingest_manager/server/services/agent_policy.ts +++ b/x-pack/plugins/ingest_manager/server/services/agent_policy.ts @@ -297,8 +297,6 @@ class AgentPolicyService { ): Promise { const res = await this._update(soClient, id, {}, options?.user); - await this.triggerAgentPolicyUpdatedEvent(soClient, 'updated', id); - return res; } public async bumpAllAgentPolicies(