|
| 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 { savedObjectsRepositoryMock } from '../../../../../src/core/server/mocks'; |
| 7 | +import { AlertExecutionStatusErrorReasons, HealthStatus } from '../types'; |
| 8 | +import { getHealth } from './get_health'; |
| 9 | + |
| 10 | +const savedObjectsRepository = savedObjectsRepositoryMock.create(); |
| 11 | + |
| 12 | +describe('getHealth()', () => { |
| 13 | + test('return true if some of alerts has a decryption error', async () => { |
| 14 | + const lastExecutionDateError = new Date().toISOString(); |
| 15 | + const lastExecutionDate = new Date().toISOString(); |
| 16 | + savedObjectsRepository.find.mockResolvedValueOnce({ |
| 17 | + total: 1, |
| 18 | + per_page: 1, |
| 19 | + page: 1, |
| 20 | + saved_objects: [ |
| 21 | + { |
| 22 | + id: '1', |
| 23 | + type: 'alert', |
| 24 | + attributes: { |
| 25 | + alertTypeId: 'myType', |
| 26 | + schedule: { interval: '10s' }, |
| 27 | + params: { |
| 28 | + bar: true, |
| 29 | + }, |
| 30 | + createdAt: new Date().toISOString(), |
| 31 | + actions: [ |
| 32 | + { |
| 33 | + group: 'default', |
| 34 | + actionRef: 'action_0', |
| 35 | + params: { |
| 36 | + foo: true, |
| 37 | + }, |
| 38 | + }, |
| 39 | + ], |
| 40 | + executionStatus: { |
| 41 | + status: 'error', |
| 42 | + lastExecutionDate: lastExecutionDateError, |
| 43 | + error: { |
| 44 | + reason: AlertExecutionStatusErrorReasons.Decrypt, |
| 45 | + message: 'Failed decrypt', |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + score: 1, |
| 50 | + references: [ |
| 51 | + { |
| 52 | + name: 'action_0', |
| 53 | + type: 'action', |
| 54 | + id: '1', |
| 55 | + }, |
| 56 | + ], |
| 57 | + }, |
| 58 | + ], |
| 59 | + }); |
| 60 | + savedObjectsRepository.find.mockResolvedValueOnce({ |
| 61 | + total: 0, |
| 62 | + per_page: 10, |
| 63 | + page: 1, |
| 64 | + saved_objects: [], |
| 65 | + }); |
| 66 | + |
| 67 | + savedObjectsRepository.find.mockResolvedValueOnce({ |
| 68 | + total: 0, |
| 69 | + per_page: 10, |
| 70 | + page: 1, |
| 71 | + saved_objects: [], |
| 72 | + }); |
| 73 | + |
| 74 | + savedObjectsRepository.find.mockResolvedValueOnce({ |
| 75 | + total: 1, |
| 76 | + per_page: 1, |
| 77 | + page: 1, |
| 78 | + saved_objects: [ |
| 79 | + { |
| 80 | + id: '2', |
| 81 | + type: 'alert', |
| 82 | + attributes: { |
| 83 | + alertTypeId: 'myType', |
| 84 | + schedule: { interval: '1s' }, |
| 85 | + params: { |
| 86 | + bar: true, |
| 87 | + }, |
| 88 | + createdAt: new Date().toISOString(), |
| 89 | + actions: [], |
| 90 | + executionStatus: { |
| 91 | + status: 'ok', |
| 92 | + lastExecutionDate, |
| 93 | + }, |
| 94 | + }, |
| 95 | + score: 1, |
| 96 | + references: [], |
| 97 | + }, |
| 98 | + ], |
| 99 | + }); |
| 100 | + const result = await getHealth(savedObjectsRepository); |
| 101 | + expect(result).toStrictEqual({ |
| 102 | + executionHealth: { |
| 103 | + status: HealthStatus.OK, |
| 104 | + timestamp: lastExecutionDate, |
| 105 | + }, |
| 106 | + readHealth: { |
| 107 | + status: HealthStatus.OK, |
| 108 | + timestamp: lastExecutionDate, |
| 109 | + }, |
| 110 | + decryptionHealth: { |
| 111 | + status: HealthStatus.Warning, |
| 112 | + timestamp: lastExecutionDateError, |
| 113 | + }, |
| 114 | + }); |
| 115 | + expect(savedObjectsRepository.find).toHaveBeenCalledTimes(4); |
| 116 | + }); |
| 117 | + |
| 118 | + test('return false if no alerts with a decryption error', async () => { |
| 119 | + const lastExecutionDateError = new Date().toISOString(); |
| 120 | + const lastExecutionDate = new Date().toISOString(); |
| 121 | + savedObjectsRepository.find.mockResolvedValueOnce({ |
| 122 | + total: 0, |
| 123 | + per_page: 10, |
| 124 | + page: 1, |
| 125 | + saved_objects: [], |
| 126 | + }); |
| 127 | + |
| 128 | + savedObjectsRepository.find.mockResolvedValueOnce({ |
| 129 | + total: 1, |
| 130 | + per_page: 1, |
| 131 | + page: 1, |
| 132 | + saved_objects: [ |
| 133 | + { |
| 134 | + id: '1', |
| 135 | + type: 'alert', |
| 136 | + attributes: { |
| 137 | + alertTypeId: 'myType', |
| 138 | + schedule: { interval: '10s' }, |
| 139 | + params: { |
| 140 | + bar: true, |
| 141 | + }, |
| 142 | + createdAt: new Date().toISOString(), |
| 143 | + actions: [ |
| 144 | + { |
| 145 | + group: 'default', |
| 146 | + actionRef: 'action_0', |
| 147 | + params: { |
| 148 | + foo: true, |
| 149 | + }, |
| 150 | + }, |
| 151 | + ], |
| 152 | + executionStatus: { |
| 153 | + status: 'error', |
| 154 | + lastExecutionDate: lastExecutionDateError, |
| 155 | + error: { |
| 156 | + reason: AlertExecutionStatusErrorReasons.Execute, |
| 157 | + message: 'Failed', |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + score: 1, |
| 162 | + references: [ |
| 163 | + { |
| 164 | + name: 'action_0', |
| 165 | + type: 'action', |
| 166 | + id: '1', |
| 167 | + }, |
| 168 | + ], |
| 169 | + }, |
| 170 | + ], |
| 171 | + }); |
| 172 | + savedObjectsRepository.find.mockResolvedValueOnce({ |
| 173 | + total: 0, |
| 174 | + per_page: 10, |
| 175 | + page: 1, |
| 176 | + saved_objects: [], |
| 177 | + }); |
| 178 | + |
| 179 | + savedObjectsRepository.find.mockResolvedValueOnce({ |
| 180 | + total: 1, |
| 181 | + per_page: 1, |
| 182 | + page: 1, |
| 183 | + saved_objects: [ |
| 184 | + { |
| 185 | + id: '2', |
| 186 | + type: 'alert', |
| 187 | + attributes: { |
| 188 | + alertTypeId: 'myType', |
| 189 | + schedule: { interval: '1s' }, |
| 190 | + params: { |
| 191 | + bar: true, |
| 192 | + }, |
| 193 | + createdAt: new Date().toISOString(), |
| 194 | + actions: [], |
| 195 | + executionStatus: { |
| 196 | + status: 'ok', |
| 197 | + lastExecutionDate, |
| 198 | + }, |
| 199 | + }, |
| 200 | + score: 1, |
| 201 | + references: [], |
| 202 | + }, |
| 203 | + ], |
| 204 | + }); |
| 205 | + const result = await getHealth(savedObjectsRepository); |
| 206 | + expect(result).toStrictEqual({ |
| 207 | + executionHealth: { |
| 208 | + status: HealthStatus.Warning, |
| 209 | + timestamp: lastExecutionDateError, |
| 210 | + }, |
| 211 | + readHealth: { |
| 212 | + status: HealthStatus.OK, |
| 213 | + timestamp: lastExecutionDate, |
| 214 | + }, |
| 215 | + decryptionHealth: { |
| 216 | + status: HealthStatus.OK, |
| 217 | + timestamp: lastExecutionDate, |
| 218 | + }, |
| 219 | + }); |
| 220 | + }); |
| 221 | +}); |
0 commit comments