|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +import { savedObjectsClientMock } from 'src/core/server/mocks'; |
| 7 | +import { createAgentActionFromPolicyAction } from './state_new_actions'; |
| 8 | +import { OutputType, Agent, AgentPolicyAction } from '../../../types'; |
| 9 | + |
| 10 | +jest.mock('../../app_context', () => ({ |
| 11 | + appContextService: { |
| 12 | + getEncryptedSavedObjects: () => ({ |
| 13 | + getDecryptedAsInternalUser: () => ({ |
| 14 | + attributes: { |
| 15 | + default_api_key: 'MOCK_API_KEY', |
| 16 | + }, |
| 17 | + }), |
| 18 | + }), |
| 19 | + }, |
| 20 | +})); |
| 21 | + |
| 22 | +describe('test agent checkin new action services', () => { |
| 23 | + describe('createAgentActionFromPolicyAction()', () => { |
| 24 | + const mockSavedObjectsClient = savedObjectsClientMock.create(); |
| 25 | + const mockAgent: Agent = { |
| 26 | + id: 'agent1', |
| 27 | + active: true, |
| 28 | + type: 'PERMANENT', |
| 29 | + local_metadata: { elastic: { agent: { version: '7.10.0' } } }, |
| 30 | + user_provided_metadata: {}, |
| 31 | + current_error_events: [], |
| 32 | + packages: [], |
| 33 | + enrolled_at: '2020-03-14T19:45:02.620Z', |
| 34 | + }; |
| 35 | + const mockPolicyAction: AgentPolicyAction = { |
| 36 | + id: 'action1', |
| 37 | + type: 'POLICY_CHANGE', |
| 38 | + policy_id: 'policy1', |
| 39 | + policy_revision: 1, |
| 40 | + sent_at: '2020-03-14T19:45:02.620Z', |
| 41 | + created_at: '2020-03-14T19:45:02.620Z', |
| 42 | + data: { |
| 43 | + policy: { |
| 44 | + id: 'policy1', |
| 45 | + outputs: { |
| 46 | + default: { |
| 47 | + type: OutputType.Elasticsearch, |
| 48 | + hosts: [], |
| 49 | + ca_sha256: undefined, |
| 50 | + api_key: undefined, |
| 51 | + }, |
| 52 | + }, |
| 53 | + inputs: [], |
| 54 | + }, |
| 55 | + }, |
| 56 | + }; |
| 57 | + |
| 58 | + it('should return POLICY_CHANGE and data.policy for agent version >= 7.10', async () => { |
| 59 | + const expectedResult = [ |
| 60 | + { |
| 61 | + agent_id: 'agent1', |
| 62 | + created_at: '2020-03-14T19:45:02.620Z', |
| 63 | + data: { |
| 64 | + policy: { |
| 65 | + id: 'policy1', |
| 66 | + inputs: [], |
| 67 | + outputs: { default: { api_key: 'MOCK_API_KEY', hosts: [], type: 'elasticsearch' } }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + id: 'action1', |
| 71 | + sent_at: '2020-03-14T19:45:02.620Z', |
| 72 | + type: 'POLICY_CHANGE', |
| 73 | + }, |
| 74 | + ]; |
| 75 | + |
| 76 | + expect( |
| 77 | + await createAgentActionFromPolicyAction(mockSavedObjectsClient, mockAgent, mockPolicyAction) |
| 78 | + ).toEqual(expectedResult); |
| 79 | + |
| 80 | + expect( |
| 81 | + await createAgentActionFromPolicyAction( |
| 82 | + mockSavedObjectsClient, |
| 83 | + { ...mockAgent, local_metadata: { elastic: { agent: { version: '7.10.1-SNAPSHOT' } } } }, |
| 84 | + mockPolicyAction |
| 85 | + ) |
| 86 | + ).toEqual(expectedResult); |
| 87 | + |
| 88 | + expect( |
| 89 | + await createAgentActionFromPolicyAction( |
| 90 | + mockSavedObjectsClient, |
| 91 | + { ...mockAgent, local_metadata: { elastic: { agent: { version: '8.0.0' } } } }, |
| 92 | + mockPolicyAction |
| 93 | + ) |
| 94 | + ).toEqual(expectedResult); |
| 95 | + |
| 96 | + expect( |
| 97 | + await createAgentActionFromPolicyAction( |
| 98 | + mockSavedObjectsClient, |
| 99 | + { ...mockAgent, local_metadata: { elastic: { agent: { version: '8.0.0-SNAPSHOT' } } } }, |
| 100 | + mockPolicyAction |
| 101 | + ) |
| 102 | + ).toEqual(expectedResult); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should return CONNFIG_CHANGE and data.config for agent version <= 7.9', async () => { |
| 106 | + const expectedResult = [ |
| 107 | + { |
| 108 | + agent_id: 'agent1', |
| 109 | + created_at: '2020-03-14T19:45:02.620Z', |
| 110 | + data: { |
| 111 | + config: { |
| 112 | + id: 'policy1', |
| 113 | + inputs: [], |
| 114 | + outputs: { default: { api_key: 'MOCK_API_KEY', hosts: [], type: 'elasticsearch' } }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + id: 'action1', |
| 118 | + sent_at: '2020-03-14T19:45:02.620Z', |
| 119 | + type: 'CONFIG_CHANGE', |
| 120 | + }, |
| 121 | + ]; |
| 122 | + |
| 123 | + expect( |
| 124 | + await createAgentActionFromPolicyAction( |
| 125 | + mockSavedObjectsClient, |
| 126 | + { ...mockAgent, local_metadata: { elastic: { agent: { version: '7.9.0' } } } }, |
| 127 | + mockPolicyAction |
| 128 | + ) |
| 129 | + ).toEqual(expectedResult); |
| 130 | + |
| 131 | + expect( |
| 132 | + await createAgentActionFromPolicyAction( |
| 133 | + mockSavedObjectsClient, |
| 134 | + { ...mockAgent, local_metadata: { elastic: { agent: { version: '7.9.1-SNAPSHOT' } } } }, |
| 135 | + mockPolicyAction |
| 136 | + ) |
| 137 | + ).toEqual(expectedResult); |
| 138 | + |
| 139 | + expect( |
| 140 | + await createAgentActionFromPolicyAction( |
| 141 | + mockSavedObjectsClient, |
| 142 | + { ...mockAgent, local_metadata: { elastic: { agent: { version: '7.9.3' } } } }, |
| 143 | + mockPolicyAction |
| 144 | + ) |
| 145 | + ).toEqual(expectedResult); |
| 146 | + |
| 147 | + expect( |
| 148 | + await createAgentActionFromPolicyAction( |
| 149 | + mockSavedObjectsClient, |
| 150 | + { ...mockAgent, local_metadata: { elastic: { agent: { version: '7.8.2' } } } }, |
| 151 | + mockPolicyAction |
| 152 | + ) |
| 153 | + ).toEqual(expectedResult); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments