Skip to content

Commit e751d5d

Browse files
chore: ADDON-81952 Added/Updated unit test cases
1 parent 739878c commit e751d5d

File tree

9 files changed

+263
-29
lines changed

9 files changed

+263
-29
lines changed

tests/unit/tests_standard_lib/test_app_test_generator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"store_events": True,
1212
"splunk_data_generator": "psa.conf",
1313
"requirement_test": "fake_requirement_path",
14+
"ingest_with_uuid": "false",
1415
}
1516
pytest_config = namedtuple("Config", ["getoption"])
1617
test_config = pytest_config(getoption=lambda x, *y: config[x])

tests/unit/tests_standard_lib/test_event_ingestors/conftest.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ class SampleEvent:
1111
key_fields: dict = None
1212
time_values: list = None
1313

14+
def __post_init__(self):
15+
if self.metadata.get("ingest_with_uuid") == "true":
16+
self.unique_identifier = "uuid"
17+
1418

1519
@pytest.fixture()
1620
def modinput_events():
@@ -28,6 +32,7 @@ def modinput_events():
2832
"sample_count": "2",
2933
"host": "modinput_host_event_time_plugin.samples_1",
3034
"expected_event_count": 2,
35+
"ingest_with_uuid": "true",
3136
},
3237
sample_name="modinput_host_event_time_plugin.samples",
3338
),
@@ -44,6 +49,7 @@ def modinput_events():
4449
"sample_count": "2",
4550
"host": "modinput_host_event_time_plugin.samples_2",
4651
"expected_event_count": 2,
52+
"ingest_with_uuid": "false",
4753
},
4854
sample_name="modinput_host_event_time_plugin.samples",
4955
),
@@ -56,6 +62,7 @@ def modinput_events():
5662
"index": "fake_index",
5763
"timestamp_type": "event",
5864
"host": "fake host",
65+
"ingest_with_uuid": "false",
5966
},
6067
sample_name="fake.samples",
6168
time_values=[1234.5678, 1234.5679],
@@ -68,26 +75,23 @@ def modinput_posts_sent():
6875
return [
6976
(
7077
f"POST {HEC_URI}/event",
71-
"[{"
72-
'"sourcetype": "test:indextime:sourcetype:modinput_host_event_time_plugin", '
78+
'{"sourcetype": "test:indextime:sourcetype:modinput_host_event_time_plugin", '
7379
'"source": "pytest-splunk-addon:modinput", '
7480
'"event": "test_modinput_1 host=modinput_host_event_time_plugin.samples_1", '
7581
'"index": "main", '
76-
'"host": "modinput_host_event_time_plugin.samples_1"'
77-
"}, {"
78-
'"sourcetype": "test:indextime:sourcetype:modinput_host_event_time_plugin", '
82+
'"fields": {"unique_identifier": "uuid"}, '
83+
'"host": "modinput_host_event_time_plugin.samples_1"}\n'
84+
'{"sourcetype": "test:indextime:sourcetype:modinput_host_event_time_plugin", '
7985
'"source": "pytest-splunk-addon:modinput", '
8086
'"event": "test_modinput_2 host=modinput_host_event_time_plugin.samples_2", '
8187
'"index": "main", '
82-
'"host": "modinput_host_event_time_plugin.samples_2"'
83-
"}, {"
84-
'"sourcetype": "pytest_splunk_addon", '
88+
'"host": "modinput_host_event_time_plugin.samples_2"}\n'
89+
'{"sourcetype": "pytest_splunk_addon", '
8590
'"source": "pytest_splunk_addon:hec:event", '
8691
'"event": "fake event nothing happened", '
8792
'"index": "fake_index", '
8893
'"host": "fake host", '
89-
'"time": 1234.5678'
90-
"}]",
94+
'"time": 1234.5678}',
9195
)
9296
]
9397

@@ -112,6 +116,7 @@ def file_monitor_events():
112116
"sample_count": "2",
113117
"host": "file_monitor_host_prefix.sample",
114118
"expected_event_count": 1,
119+
"ingest_with_uuid": "false",
115120
},
116121
sample_name="file_monitor_host_prefix.sample",
117122
),
@@ -130,6 +135,7 @@ def file_monitor_events():
130135
"host": "failing-samples-1",
131136
"id": "failing.samples_1",
132137
"expected_event_count": 2,
138+
"ingest_with_uuid": "false",
133139
},
134140
sample_name="failing.samples",
135141
),
@@ -138,6 +144,7 @@ def file_monitor_events():
138144
metadata={
139145
"input_type": "file_monitor",
140146
"index": "fake_index",
147+
"ingest_with_uuid": "false",
141148
},
142149
sample_name="fake.samples",
143150
),
@@ -234,6 +241,7 @@ def requirement_events():
234241
"sample_count": "2",
235242
"host": "requirement_host_prefix.sample",
236243
"expected_event_count": 1,
244+
"ingest_with_uuid": "false",
237245
},
238246
sample_name="requirement_test",
239247
),
@@ -256,6 +264,7 @@ def sc4s_events():
256264
"host": "sc4s-host-plugin-time-sample-31",
257265
"id": "sc4s_host_plugin_time.sample_31",
258266
"expected_event_count": 2,
267+
"ingest_with_uuid": "false",
259268
},
260269
sample_name="sc4s_host_plugin_time.sample",
261270
),
@@ -272,6 +281,7 @@ def sc4s_events():
272281
"host": "sc4s-host-plugin-time-sample-32",
273282
"id": "sc4s-host-plugin-time-sample-32",
274283
"expected_event_count": 2,
284+
"ingest_with_uuid": "false",
275285
},
276286
sample_name="sc4s_host_plugin_time.sample",
277287
),

tests/unit/tests_standard_lib/test_event_ingestors/test_ingestor_helper.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def ingestors_mocks():
2222
) as hec_metric_event_mock, patch(
2323
f"{EVENT_INGESTOR_PATH}.sc4s_event_ingestor.SC4SEventIngestor"
2424
) as sc4s_event_mock:
25-
2625
hec_event_mock.return_value = HEC_EVENT_INGESTOR_RETURN_VALUE
2726
hec_raw_event_mock.return_value = HEC_RAW_EVENT_INGESTOR_RETURN_VALUE
2827
hec_metric_event_mock.return_value = HEC_METRIC_EVENT_INGESTOR_RETURN_VALUE
@@ -125,15 +124,19 @@ def test_events_can_be_ingested(
125124
get_ingestor_mock, sample_mock, file_monitor_events, modinput_events
126125
):
127126
event_ingestors.ingestor_helper.IngestorHelper.ingest_events(
128-
ingest_meta_data={},
127+
ingest_meta_data={"ingest_with_uuid": "false"},
129128
addon_path="fake_path",
130129
config_path="tests/unit/event_ingestors",
131130
thread_count=20,
132131
store_events=False,
133132
)
134133
assert get_ingestor_mock.call_count == 2
135134
get_ingestor_mock.assert_has_calls(
136-
[call("file_monitor", {}), call("modinput", {})], any_order=True
135+
[
136+
call("file_monitor", {"ingest_with_uuid": "false"}),
137+
call("modinput", {"ingest_with_uuid": "false"}),
138+
],
139+
any_order=True,
137140
)
138141
assert get_ingestor_mock.ingest.call_count == 2
139142
get_ingestor_mock.ingest.assert_has_calls(

tests/unit/tests_standard_lib/test_fields_tests/test_test_generator.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ def field_3():
2424
field_3.__dict__.update({"name": "field_3"})
2525

2626

27+
@pytest.fixture
28+
def mock_uuid4():
29+
with patch(
30+
"pytest_splunk_addon.sample_generation.sample_event.uuid.uuid4",
31+
return_value="uuid",
32+
) as mock_uuid:
33+
yield mock_uuid
34+
35+
2736
@pytest.fixture()
2837
def addon_parser_mock(monkeypatch):
2938
ap = MagicMock()
@@ -422,6 +431,7 @@ def test_generate_field_tests(
422431
"input_type": "modinput",
423432
"sourcetype_to_search": "dummy_sourcetype",
424433
"host": "dummy_host",
434+
"ingest_with_uuid": "false",
425435
},
426436
sample_name="file1.xml",
427437
requirement_test_data={
@@ -445,6 +455,7 @@ def test_generate_field_tests(
445455
"input_type": "syslog_tcp",
446456
"sourcetype_to_search": "dummy_sourcetype",
447457
"host": "dummy_host_syslog",
458+
"ingest_with_uuid": "false",
448459
},
449460
sample_name="file1.xml",
450461
requirement_test_data={},
@@ -455,6 +466,7 @@ def test_generate_field_tests(
455466
"input_type": "syslog_tcp",
456467
"sourcetype_to_search": "dummy_sourcetype",
457468
"host": "dummy_host_syslog",
469+
"ingest_with_uuid": "false",
458470
},
459471
sample_name="file1.xml",
460472
requirement_test_data={
@@ -522,6 +534,63 @@ def test_generate_requirement_tests(tokenised_events, expected_output):
522534
assert param_mock.call_count == len(expected_output)
523535

524536

537+
def test_generate_requirement_tests_with_uuid(mock_uuid4):
538+
tokenised_events = [
539+
SampleEvent(
540+
event_string="escaped_event",
541+
metadata={
542+
"input_type": "modinput",
543+
"sourcetype_to_search": "dummy_sourcetype",
544+
"host": "dummy_host",
545+
"ingest_with_uuid": "true",
546+
},
547+
sample_name="file1.xml",
548+
requirement_test_data={
549+
"cim_fields": {
550+
"severity": "low",
551+
"signature_id": "405001",
552+
"src": "192.168.0.1",
553+
"type": "event",
554+
},
555+
},
556+
)
557+
]
558+
559+
expected_output = [
560+
(
561+
{
562+
"escaped_event": "escaped_event",
563+
"unique_identifier": "uuid",
564+
"fields": {
565+
"severity": "low",
566+
"signature_id": "405001",
567+
"src": "192.168.0.1",
568+
"type": "event",
569+
},
570+
"modinput_params": {"sourcetype": "dummy_sourcetype"},
571+
},
572+
"sample_name::file1.xml::host::dummy_host",
573+
)
574+
]
575+
576+
with patch.object(
577+
xml_event_parser, "strip_syslog_header", return_value="escaped_event"
578+
), patch.object(
579+
xml_event_parser, "escape_char_event", return_value="escaped_event"
580+
), patch.object(
581+
pytest, "param", side_effect=lambda x, id: (x, id)
582+
) as param_mock:
583+
out = list(
584+
FieldTestGenerator(
585+
"app_path",
586+
tokenised_events,
587+
"field_bank",
588+
).generate_requirements_tests()
589+
)
590+
assert out == expected_output
591+
assert param_mock.call_count == len(expected_output)
592+
593+
525594
@pytest.mark.parametrize(
526595
"tokenised_events, expected_output",
527596
[
@@ -596,3 +665,47 @@ def test_generate_requirement_datamodel_tests(tokenised_events, expected_output)
596665
)
597666
assert out == expected_output
598667
assert param_mock.call_count == len(expected_output)
668+
669+
670+
def test_generate_requirement_datamodel_tests_with_uuid(mock_uuid4):
671+
tokenised_events = [
672+
SampleEvent(
673+
event_string="escaped_event",
674+
metadata={
675+
"input_type": "modinput",
676+
"sourcetype_to_search": "dummy_sourcetype",
677+
"host": "dummy_host",
678+
"ingest_with_uuid": "true",
679+
},
680+
sample_name="file1.xml",
681+
requirement_test_data={"datamodels": {"model": "Alerts"}},
682+
)
683+
]
684+
685+
expected_output = [
686+
(
687+
{
688+
"datamodels": ["Alerts"],
689+
"stanza": "escaped_event",
690+
"unique_identifier": "uuid",
691+
},
692+
"Alerts::sample_name::file1.xml::host::dummy_host",
693+
)
694+
]
695+
696+
with patch.object(
697+
xml_event_parser, "strip_syslog_header", return_value="escaped_event"
698+
), patch.object(
699+
xml_event_parser, "escape_char_event", return_value="escaped_event"
700+
), patch.object(
701+
pytest, "param", side_effect=lambda x, id: (x, id)
702+
) as param_mock:
703+
out = list(
704+
FieldTestGenerator(
705+
"app_path",
706+
tokenised_events,
707+
"field_bank",
708+
).generate_requirements_datamodels_tests()
709+
)
710+
assert out == expected_output
711+
assert param_mock.call_count == len(expected_output)

tests/unit/tests_standard_lib/tests_sample_generation/test_pytest_splunk_addon_data_parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
def test_psa_data_when_no_config():
1212
with tempfile.TemporaryDirectory() as tempdir:
1313
with pytest.raises(FileNotFoundError):
14-
psa_data_parser = PytestSplunkAddonDataParser(tempdir, tempdir)
14+
psa_data_parser = PytestSplunkAddonDataParser(tempdir, tempdir, "false")
1515
_ = psa_data_parser.psa_data
1616

1717

@@ -20,6 +20,7 @@ def test_path_to_samples():
2020
psa_data_parser = PytestSplunkAddonDataParser(
2121
path,
2222
path,
23+
"false",
2324
)
2425
assert os.path.join(path, "samples") == psa_data_parser._path_to_samples
2526

@@ -29,6 +30,7 @@ def test_get_psa_data_stanzas_with_samples():
2930
psa_data_parser = PytestSplunkAddonDataParser(
3031
path,
3132
path,
33+
"false",
3234
)
3335
expected_result = {
3436
"test1.samples": {
@@ -101,6 +103,7 @@ def test_get_sample_stanzas_without_samples(caplog):
101103
parser = PytestSplunkAddonDataParser(
102104
tempdir,
103105
config_path,
106+
"false",
104107
)
105108
parser.get_sample_stanzas()
106109
assert "No sample file found for stanza : test1.samples" in caplog.messages

tests/unit/tests_standard_lib/tests_sample_generation/test_sample_event.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
EVENT_STRING = "Event_string dad ad dfd ddas Value_5."
99
UPDATED_STRING = "Updated_string"
1010
SAMPLE_NAME = "Sample_name"
11-
METADATA = {"Metadata": "metadata"}
11+
METADATA = {"Metadata": "metadata", "ingest_with_uuid": "false"}
1212
RULE = "Rule"
1313
SAMPLE_HOST = "sample_host"
1414
FAKE_IPV4 = "222.222.222.222"
@@ -33,6 +33,23 @@ def samp_eve():
3333
)
3434

3535

36+
def test_sample_event_generates_uuid():
37+
METADATA["ingest_with_uuid"] = "true"
38+
with patch(
39+
"pytest_splunk_addon.sample_generation.sample_event.uuid.uuid4",
40+
return_value="uuid",
41+
) as mock_uuid:
42+
event = pytest_splunk_addon.sample_generation.sample_event.SampleEvent(
43+
event_string=EVENT_STRING,
44+
metadata=METADATA,
45+
sample_name=SAMPLE_NAME,
46+
)
47+
48+
mock_uuid.assert_called_once() # Ensures uuid4 was called
49+
assert hasattr(event, "unique_identifier") # The field was set
50+
assert event.unique_identifier == "uuid"
51+
52+
3653
def check_host_count(value):
3754
assert pytest_splunk_addon.sample_generation.sample_event.host_count == value
3855

tests/unit/tests_standard_lib/tests_sample_generation/test_sample_generator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
class TestSampleGenerator:
1313
def test_init(self):
14-
sg = SampleGenerator(ADDON_PATH, CONFIG_PATH)
14+
sg = SampleGenerator(ADDON_PATH, "false", CONFIG_PATH)
1515
assert sg.addon_path == ADDON_PATH
1616
assert sg.config_path == CONFIG_PATH
1717
assert sg.process_count == 4
18-
sg = SampleGenerator(ADDON_PATH, CONFIG_PATH, 2)
18+
sg = SampleGenerator(ADDON_PATH, "false", CONFIG_PATH, 2)
1919
assert sg.addon_path == ADDON_PATH
2020
assert sg.config_path == CONFIG_PATH
2121
assert sg.process_count == 2
@@ -36,7 +36,7 @@ def test_get_samples(self):
3636
sample_stanza_mock.get_raw_events = ["event_1", "event_2"]
3737
sample_stanza_mock.tokenize = lambda x, y: (x, y)
3838
psa_data_mock.conf_name = CONFIG_PATH
39-
sg = SampleGenerator(ADDON_PATH)
39+
sg = SampleGenerator(ADDON_PATH, "false")
4040
assert list(sg.get_samples()) == [tks_1, tks_2, tks_1, tks_2]
4141

4242
def test_clean_samples(self):

0 commit comments

Comments
 (0)