-
Notifications
You must be signed in to change notification settings - Fork 817
/
Copy pathtest_enrichments.py
737 lines (628 loc) · 24.8 KB
/
test_enrichments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# test_enrichments.py
import time
from unittest.mock import MagicMock, Mock, patch
import pytest
from keep.api.bl.enrichments_bl import EnrichmentsBl
from keep.api.core.dependencies import SINGLE_TENANT_UUID
from keep.api.models.alert import AlertDto
from keep.api.models.db.alert import ActionType
from keep.api.models.db.extraction import ExtractionRule
from keep.api.models.db.mapping import MappingRule
from keep.api.models.db.topology import TopologyService
from tests.fixtures.client import client, setup_api_key, test_app # noqa
@pytest.fixture(autouse=True)
def patch_get_tenants_configurations():
"""Automatically patch get_tenants_configurations for all tests."""
with patch(
"keep.api.core.tenant_configuration.TenantConfiguration._TenantConfiguration.get_configuration",
return_value=None,
):
yield
@pytest.fixture
def mock_session():
"""Create a mock session to simulate database operations."""
session = MagicMock()
query_mock = MagicMock()
session.query.return_value = query_mock
query_mock.filter.return_value = query_mock
query_mock.order_by.return_value = query_mock
query_mock.all.return_value = [] # Default to no rules, override in specific tests
# Patch the get_tenants_configurations function
return session
@pytest.fixture
def mock_alert_dto():
"""Fixture for creating a mock AlertDto."""
return AlertDto(
id="test_id",
name="Test Alert",
status="firing",
severity="high",
lastReceived="2021-01-01T00:00:00Z",
source=["test_source"],
fingerprint="mock_fingerprint",
labels={},
)
def test_run_extraction_rules_no_rules_applies(mock_session, mock_alert_dto):
# Assuming there are no extraction rules
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = (
[]
)
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
result_event = enrichment_bl.run_extraction_rules(mock_alert_dto)
# Check that the event has not changed (no rules to apply)
assert result_event == mock_alert_dto # Assuming no change if no rules
def test_run_extraction_rules_regex_named_groups(mock_session, mock_alert_dto):
# Setup an extraction rule that should apply based on the alert content
rule = ExtractionRule(
id=1,
tenant_id="test_tenant",
priority=1,
attribute="{{ name }}",
regex="(?P<service_name>Test) (?P<alert_type>Alert)",
disabled=False,
pre=True,
condition=None, # No condition for simplicity
)
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = [
rule
]
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
# Mocking chevron rendering to simulate template rendering
with patch("chevron.render", return_value="Test Alert"):
enriched_event = enrichment_bl.run_extraction_rules(mock_alert_dto)
# Assert that the event is now enriched with regex group names
assert enriched_event.service_name == "Test"
assert enriched_event.alert_type == "Alert"
def test_run_extraction_rules_event_is_dict(mock_session):
event = {"name": "Test Alert", "source": ["source_test"]}
rule = ExtractionRule(
id=1,
tenant_id="test_tenant",
priority=1,
attribute="{{ name }}",
regex="Test Alert",
disabled=False,
pre=False, # Rule applies to dict type events
)
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = [
rule
]
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
# Mocking chevron rendering
with patch("chevron.render", return_value="Test Alert"):
enriched_event = enrichment_bl.run_extraction_rules(event)
assert (
enriched_event["name"] == "Test Alert"
) # Ensuring the attribute is correctly processed
def test_run_extraction_rules_no_rules(mock_session, mock_alert_dto):
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = (
[]
)
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
result_event = enrichment_bl.run_extraction_rules(mock_alert_dto)
assert (
result_event == mock_alert_dto
) # Should return the original event if no rules apply
def test_run_extraction_rules_attribute_no_template(mock_session, mock_alert_dto):
rule = ExtractionRule(
id=1,
tenant_id="test_tenant",
priority=1,
attribute="name", # No {{}} in attribute
regex="Test",
disabled=False,
pre=True,
)
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = [
rule
]
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
with patch("chevron.render", return_value="Test Alert"):
enriched_event = enrichment_bl.run_extraction_rules(mock_alert_dto)
assert (
"name" not in enriched_event
) # Assuming the code does not modify the event if attribute is not in template format
def test_run_extraction_rules_empty_attribute_value(mock_session, mock_alert_dto):
rule = ExtractionRule(
id=1,
tenant_id="test_tenant",
priority=1,
attribute="{{ description }}", # Assume description is empty
regex=".*",
disabled=False,
pre=True,
)
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = [
rule
]
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
with patch("chevron.render", return_value=""):
enriched_event = enrichment_bl.run_extraction_rules(mock_alert_dto)
assert enriched_event == mock_alert_dto # Check if event is unchanged
def test_run_extraction_rules_handle_source_special_case(mock_session):
event = {"name": "Test Alert", "source": "incorrect_format"}
rule = ExtractionRule(
id=1,
tenant_id="test_tenant",
priority=1,
attribute="{{ source }}",
regex="(?P<source>incorrect_format)",
disabled=False,
pre=True,
)
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = [
rule
]
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
# We'll mock chevron to return the exact content of 'source' to simulate the template rendering
with patch("chevron.render", return_value="incorrect_format"):
# We need to mock 're.search' to return a match object with a groupdict that includes 'source'
with patch(
"re.search",
return_value=Mock(groupdict=lambda: {"source": "incorrect_format"}),
):
enriched_event = enrichment_bl.run_extraction_rules(event)
# Assert that the event's 'source' is now a list with the updated source
assert enriched_event["source"] == [
"incorrect_format"
], "Source should be updated to a list containing the new source."
#### 2. Testing `run_extraction_rules` with CEL Conditions
def test_run_extraction_rules_with_conditions(mock_session, mock_alert_dto):
rule = ExtractionRule(
id=2,
tenant_id="test_tenant",
priority=1,
attribute="{{ source[0] }}",
regex="(?P<source_name>test_source)",
disabled=False,
pre=False,
condition='source.includes("test_source")',
)
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = [
rule
]
# Mocking the CEL environment to return True for the condition
with patch("chevron.render", return_value="test_source"), patch(
"celpy.Environment"
) as mock_env, patch("celpy.celpy.json_to_cel") as mock_json_to_cel:
mock_env.return_value.compile.return_value = None
mock_program = Mock()
mock_env.return_value.program.return_value = mock_program
mock_program.evaluate.return_value = True
mock_json_to_cel.return_value = {}
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
enriched_event = enrichment_bl.run_extraction_rules(mock_alert_dto)
# Assert that the event is now enriched with the source name from regex
assert enriched_event.source_name == "test_source"
def test_run_mapping_rules_applies(mock_session, mock_alert_dto):
# Setup a mapping rule
rule = MappingRule(
id=1,
tenant_id="test_tenant",
priority=1,
matchers=["name"],
rows=[{"name": "Test Alert", "service": "new_service"}],
disabled=False,
type="csv",
)
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = [
rule
]
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
enrichment_bl.run_mapping_rules(mock_alert_dto)
# Check if the alert's service is now updated to "new_service"
assert mock_alert_dto.service == "new_service"
def test_run_mapping_rules_with_regex_match(mock_session, mock_alert_dto):
rule = MappingRule(
id=1,
tenant_id="test_tenant",
priority=1,
matchers=["name"],
rows=[
{"name": "^(keep-)?backend-service$", "service": "backend_service"},
{"name": "frontend-service", "service": "frontend_service"},
],
disabled=False,
type="csv",
)
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = [
rule
]
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
# Test case where the alert name matches the regex pattern with 'keep-' prefix
mock_alert_dto.name = "keep-backend-service"
del mock_alert_dto.service
enrichment_bl.run_mapping_rules(mock_alert_dto)
assert (
mock_alert_dto.service == "backend_service"
), "Service should match 'backend_service' for 'keep-backend-service'"
# Test case where the alert name matches the regex pattern without 'keep-' prefix
mock_alert_dto.name = "backend-service"
del mock_alert_dto.service
enrichment_bl.run_mapping_rules(mock_alert_dto)
assert (
mock_alert_dto.service == "backend_service"
), "Service should match 'backend_service' for 'backend-service'"
# Test case where the alert name does not match any regex pattern
mock_alert_dto.name = "unmatched-service"
del mock_alert_dto.service
enrichment_bl.run_mapping_rules(mock_alert_dto)
assert (
hasattr(mock_alert_dto, "service") is False
), "Service should not match any entry"
def test_run_mapping_rules_no_match(mock_session, mock_alert_dto):
rule = MappingRule(
id=1,
tenant_id="test_tenant",
priority=1,
matchers=["name"],
rows=[
{"name": "^(keep-)?backend-service$", "service": "backend_service"},
{"name": "frontend-service", "service": "frontend_service"},
],
disabled=False,
type="csv",
)
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = [
rule
]
del mock_alert_dto.service
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
# Test case where no entry matches the regex pattern
mock_alert_dto.name = "unmatched-service"
enrichment_bl.run_mapping_rules(mock_alert_dto)
assert (
hasattr(mock_alert_dto, "service") is False
), "Service should not match any entry"
def test_check_matcher_with_and_condition(mock_session, mock_alert_dto):
# Setup a mapping rule with && condition in matchers
rule = MappingRule(
id=1,
tenant_id="test_tenant",
priority=1,
matchers=["name && severity"],
rows=[{"name": "Test Alert", "severity": "high", "service": "new_service"}],
disabled=False,
type="csv",
)
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = [
rule
]
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
# Test case where alert matches both name and severity conditions
mock_alert_dto.name = "Test Alert"
mock_alert_dto.severity = "high"
matcher_exist = enrichment_bl._check_matcher(
mock_alert_dto, rule.rows[0], "name && severity"
)
assert matcher_exist
enrichment_bl.run_mapping_rules(mock_alert_dto)
assert mock_alert_dto.service == "new_service"
del mock_alert_dto.service
# Test case where alert does not match both conditions
mock_alert_dto.name = "Other Alert"
mock_alert_dto.severity = "low"
result = enrichment_bl._check_matcher(
mock_alert_dto, rule.rows[0], "name && severity"
)
assert not hasattr(mock_alert_dto, "service")
assert result is False
def test_check_matcher_with_or_condition(mock_session, mock_alert_dto):
# Setup a mapping rule with || condition in matchers
rule = MappingRule(
id=1,
tenant_id="test_tenant",
priority=1,
matchers=["name", "severity"],
rows=[
{"name": "Test Alert", "service": "new_service"},
{"severity": "high", "service": "high_severity_service"},
],
disabled=False,
type="csv",
)
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = [
rule
]
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
# Test case where alert matches name condition
mock_alert_dto.name = "Test Alert"
del mock_alert_dto.service
enrichment_bl.run_mapping_rules(mock_alert_dto)
assert mock_alert_dto.service == "new_service"
# Test case where alert matches severity condition
mock_alert_dto.name = "Other Alert"
mock_alert_dto.severity = "high"
del mock_alert_dto.service
enrichment_bl.run_mapping_rules(mock_alert_dto)
assert mock_alert_dto.service == "high_severity_service"
del mock_alert_dto.service
# Test case where alert matches neither condition
mock_alert_dto.name = "Other Alert"
mock_alert_dto.severity = "low"
enrichment_bl.run_mapping_rules(mock_alert_dto)
assert not hasattr(mock_alert_dto, "service")
@pytest.mark.parametrize(
"setup_alerts",
[
{
"alert_details": [
{"source": ["sentry"], "severity": "critical"},
{"source": ["grafana"], "severity": "critical"},
]
}
],
indirect=True,
)
def test_mapping_rule_with_elsatic(mock_session, mock_alert_dto, setup_alerts):
import os
# first, use elastic
os.environ["ELASTIC_ENABLED"] = "true"
# Setup a mapping rule with || condition in matchers
rule = MappingRule(
id=1,
tenant_id=SINGLE_TENANT_UUID,
priority=1,
matchers=["name", "severity"],
rows=[
{"name": "Test Alert", "service": "new_service"},
{"severity": "high", "service": "high_severity_service"},
],
disabled=False,
type="csv",
)
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = [
rule
]
enrichment_bl = EnrichmentsBl(tenant_id=SINGLE_TENANT_UUID, db=mock_session)
# Test case where alert matches name condition
mock_alert_dto.name = "Test Alert"
enrichment_bl.run_mapping_rules(mock_alert_dto)
assert mock_alert_dto.service == "new_service"
@pytest.mark.parametrize("test_app", ["NO_AUTH"], indirect=True)
def test_enrichment(db_session, client, test_app, mock_alert_dto, elastic_client):
# add some rule
rule = MappingRule(
id=1,
tenant_id=SINGLE_TENANT_UUID,
priority=1,
matchers=["name", "severity"],
rows=[
{"name": "Test Alert", "service": "new_service"},
{"severity": "high", "service": "high_severity_service"},
],
name="new_rule",
disabled=False,
type="csv",
)
db_session.add(rule)
db_session.commit()
# now post an alert
response = client.post(
"/alerts/event",
headers={"x-api-key": "some-key-everything-works-because-no-auth"},
json=mock_alert_dto.dict(),
)
# now query the feed preset to get the alerts
response = client.get(
"/preset/feed/alerts",
headers={"x-api-key": "some-key-everything-works-because-no-auth"},
)
alerts = response.json()
assert len(alerts) == 1
assert response.headers.get("x-search-type") == "elastic"
alert = alerts[0]
assert alert["service"] == "new_service"
@pytest.mark.parametrize("test_app", ["NO_AUTH"], indirect=True)
def test_disposable_enrichment(db_session, client, test_app, mock_alert_dto):
# SHAHAR: there is a voodoo so that you must do something with the db_session to kick it off
rule = MappingRule(
id=1,
tenant_id=SINGLE_TENANT_UUID,
priority=1,
matchers=["name", "severity"],
rows=[
{"name": "Test Alert", "service": "new_service"},
{"severity": "high", "service": "high_severity_service"},
],
name="new_rule",
disabled=False,
type="csv",
)
db_session.add(rule)
db_session.commit()
# 1. send alert
response = client.post(
"/alerts/event",
headers={"x-api-key": "some-key"},
json=mock_alert_dto.dict(),
)
while (
client.get(
f"/alerts/{mock_alert_dto.fingerprint}",
headers={"x-api-key": "some-key"},
).status_code
!= 200
):
time.sleep(0.1)
# 2. enrich with disposable alert
response = client.post(
"/alerts/enrich?dispose_on_new_alert=true",
headers={"x-api-key": "some-key"},
json={
"fingerprint": mock_alert_dto.fingerprint,
"enrichments": {
"status": "acknowledged",
},
},
)
# 3. get the alert with the new status
response = client.get(
"/preset/feed/alerts",
headers={"x-api-key": "some-key"},
)
alerts = response.json()
while alerts[0]["status"] != "acknowledged":
response = client.get(
"/preset/feed/alerts",
headers={"x-api-key": "some-key"},
)
alerts = response.json()
assert len(alerts) == 1
alert = alerts[0]
assert alert["status"] == "acknowledged"
# 4. send the alert again with firing and check that the status is reset
mock_alert_dto.status = "firing"
setattr(mock_alert_dto, "avoid_dedup", "bla")
response = client.post(
"/alerts/event",
headers={"x-api-key": "some-key"},
json=mock_alert_dto.dict(),
)
# 5. get the alert with the new status
response = client.get(
"/preset/feed/alerts",
headers={"x-api-key": "some-key"},
)
alerts = response.json()
while alerts[0]["status"] != "firing":
time.sleep(0.1)
response = client.get(
"/preset/feed/alerts",
headers={"x-api-key": "some-key"},
)
alerts = response.json()
assert len(alerts) == 1
alert = alerts[0]
assert alert["status"] == "firing"
def test_topology_mapping_rule_enrichment(mock_session, mock_alert_dto):
# Mock a TopologyService with dependencies to simulate the DB structure
mock_topology_service = TopologyService(
id=1, tenant_id="keep", service="test-service", display_name="Test Service"
)
# Create a mock MappingRule for topology
rule = MappingRule(
id=3,
tenant_id=SINGLE_TENANT_UUID,
priority=1,
matchers=["service"],
name="topology_rule",
disabled=False,
type="topology",
)
# Mock the session to return this topology mapping rule
mock_session.query.return_value.filter.return_value.all.return_value = [rule]
# Initialize the EnrichmentsBl class with the mock session
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
mock_alert_dto.service = "test-service"
# Mock the get_topology_data_by_dynamic_matcher to return the mock topology service
with patch(
"keep.api.bl.enrichments_bl.get_topology_data_by_dynamic_matcher",
return_value=mock_topology_service,
):
# Mock the enrichment database function so no actual DB actions occur
with patch(
"keep.api.bl.enrichments_bl.enrich_alert_db"
) as mock_enrich_alert_db:
# Run the mapping rule logic for the topology
result_event = enrichment_bl.run_mapping_rules(mock_alert_dto)
# Check that the topology enrichment was applied correctly
assert getattr(result_event, "display_name", None) == "Test Service"
# Verify that the DB enrichment function was called correctly
mock_enrich_alert_db.assert_called_once_with(
"test_tenant",
mock_alert_dto.fingerprint,
{
"source_provider_id": "unknown",
"service": "test-service",
"environment": "unknown",
"display_name": "Test Service",
},
action_callee="system",
action_type=ActionType.MAPPING_RULE_ENRICH,
action_description="Alert enriched with mapping from rule `topology_rule`",
session=mock_session,
force=False,
audit_enabled=True,
)
def test_run_mapping_rules_with_complex_matchers(mock_session, mock_alert_dto):
# Setup a mapping rule with complex matchers
rule = MappingRule(
id=1,
tenant_id="test_tenant",
priority=1,
matchers=["name && severity", "source"],
rows=[
{
"name": "Test Alert",
"severity": "high",
"service": "high_priority_service",
},
{
"name": "Test Alert",
"severity": "low",
"service": "low_priority_service",
},
{"source": "test_source", "service": "source_specific_service"},
],
disabled=False,
type="csv",
)
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = [
rule
]
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
# Test case 1: Matches "name && severity"
mock_alert_dto.name = "Test Alert"
mock_alert_dto.severity = "high"
enrichment_bl.run_mapping_rules(mock_alert_dto)
assert mock_alert_dto.service == "high_priority_service"
# Test case 2: Matches "name && severity" (different severity)
mock_alert_dto.severity = "low"
del mock_alert_dto.service
enrichment_bl.run_mapping_rules(mock_alert_dto)
assert mock_alert_dto.service == "low_priority_service"
# Test case 3: Matches "source"
mock_alert_dto.name = "Different Alert"
mock_alert_dto.severity = "medium"
mock_alert_dto.source = ["test_source"]
del mock_alert_dto.service
enrichment_bl.run_mapping_rules(mock_alert_dto)
assert mock_alert_dto.service == "source_specific_service"
# Test case 4: No match
mock_alert_dto.name = "Unmatched Alert"
mock_alert_dto.severity = "medium"
mock_alert_dto.source = ["different_source"]
del mock_alert_dto.service
enrichment_bl.run_mapping_rules(mock_alert_dto)
assert not hasattr(mock_alert_dto, "service")
def test_run_mapping_rules_enrichments_filtering(mock_session, mock_alert_dto):
# Setup a mapping rule with complex matchers and multiple enrichment fields
rule = MappingRule(
id=1,
tenant_id="test_tenant",
priority=1,
matchers=["name && severity"],
rows=[
{
"name": "Test Alert",
"severity": "high",
"service": "high_priority_service",
"team": "on-call",
"priority": "P1",
},
],
disabled=False,
type="csv",
)
mock_session.query.return_value.filter.return_value.filter.return_value.order_by.return_value.all.return_value = [
rule
]
enrichment_bl = EnrichmentsBl(tenant_id="test_tenant", db=mock_session)
# Test case: Matches "name && severity" and applies multiple enrichments
mock_alert_dto.name = "Test Alert"
mock_alert_dto.severity = "high"
enrichment_bl.run_mapping_rules(mock_alert_dto)
assert mock_alert_dto.service == "high_priority_service"
assert mock_alert_dto.team == "on-call"
assert mock_alert_dto.priority == "P1"