From ad2c223866e4a37753cad8c56dd0042fa1e15aa8 Mon Sep 17 00:00:00 2001 From: Claudio Procida Date: Mon, 6 Dec 2021 16:20:12 +0100 Subject: [PATCH] Adds test for required fields --- .../common/parse_technical_fields.test.ts | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/rule_registry/common/parse_technical_fields.test.ts b/x-pack/plugins/rule_registry/common/parse_technical_fields.test.ts index e5ec208ce9059..176256e90af85 100644 --- a/x-pack/plugins/rule_registry/common/parse_technical_fields.test.ts +++ b/x-pack/plugins/rule_registry/common/parse_technical_fields.test.ts @@ -8,10 +8,10 @@ import { parseTechnicalFields } from './parse_technical_fields'; describe('parseTechnicalFields', () => { - it('parses an alert with default fields', () => { + it('parses an alert with default fields without error', () => { const ALERT_WITH_DEFAULT_FIELDS = { - 'kibana.alert.status': ['active'], '@timestamp': ['2021-12-06T12:30:59.411Z'], + 'kibana.alert.status': ['active'], 'kibana.alert.duration.us': ['488935266000'], 'kibana.alert.reason': ['host.uptime has reported no data over the past 1m for *'], 'kibana.alert.workflow_status': ['open'], @@ -32,12 +32,36 @@ describe('parseTechnicalFields', () => { expect(() => parseTechnicalFields(ALERT_WITH_DEFAULT_FIELDS)).not.toThrow(); }); - it('parses an alert without reason', () => { - const ALERT_WITHOUT_REASON = { - '@timestamp': ['2021-12-06T12:25:47.356Z'], + it('parses an alert with missing required fields with error', () => { + const ALERT_WITH_MISSING_REQUIRED_FIELDS = { + '@timestamp': ['2021-12-06T12:30:59.411Z'], + 'kibana.alert.duration.us': ['488935266000'], + 'kibana.alert.reason': ['host.uptime has reported no data over the past 1m for *'], + 'kibana.alert.workflow_status': ['open'], + 'kibana.alert.start': ['2021-11-30T20:42:04.145Z'], + 'event.action': ['active'], + 'kibana.alert.instance.id': ['*'], + 'kibana.version': ['8.1.0'], + 'event.kind': ['signal'], + }; + expect(() => parseTechnicalFields(ALERT_WITH_MISSING_REQUIRED_FIELDS)).toThrow(); + }); + + it('parses an alert with missing optional fields without error', () => { + const ALERT_WITH_MISSING_OPTIONAL_FIELDS = { + '@timestamp': ['2021-12-06T12:30:59.411Z'], + 'kibana.alert.rule.uuid': ['c8ef4420-4604-11ec-b08c-c590e7b8c4cd'], 'kibana.alert.status': ['active'], - 'kibana.alert.duration.us': ['1806762716000'], + 'kibana.alert.rule.producer': ['infrastructure'], + 'kibana.alert.rule.consumer': ['alerts'], + 'kibana.alert.rule.category': ['Metric threshold'], + 'kibana.alert.rule.rule_type_id': ['metrics.alert.threshold'], + 'kibana.alert.instance.id': ['*'], + 'kibana.alert.rule.name': ['Uptime'], + 'kibana.alert.uuid': ['f31f5726-3c47-4c88-bc42-4e1fbde17e34'], + 'kibana.space_ids': ['default'], }; - expect(() => parseTechnicalFields(ALERT_WITHOUT_REASON)).not.toThrow(); + + expect(() => parseTechnicalFields(ALERT_WITH_MISSING_OPTIONAL_FIELDS)).not.toThrow(); }); });