Skip to content

Commit 5e34b91

Browse files
chore: adjusting host variants handling
1 parent ca5f891 commit 5e34b91

4 files changed

Lines changed: 46 additions & 19 deletions

File tree

pytest_splunk_addon/fields_tests/test_generator.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,29 @@ def generate_requirements_tests(self):
264264
for field, value in requirement_fields.items()
265265
if field not in exceptions
266266
}
267-
sample_event = {
268-
"escaped_event": escaped_event,
269-
"fields": requirement_fields,
270-
"modinput_params": modinput_params,
271-
}
272-
if metadata.get("ingest_with_uuid") == "true":
273-
sample_event["unique_identifier"] = event.unique_identifier
267+
# Prefer UUID if present and include variant_id for disambiguation
268+
# Fetch UUID from the SampleEvent when ingest_with_uuid is enabled
269+
unique_identifier = (
270+
getattr(event, "unique_identifier", None)
271+
if metadata.get("ingest_with_uuid") == "true"
272+
else None
273+
)
274+
variant_id = metadata.get("variant_id")
275+
search_selector = (
276+
{
277+
"unique_identifier": unique_identifier,
278+
"escaped_event": escaped_event,
279+
"variant_id": variant_id,
280+
}
281+
if unique_identifier is not None
282+
else {"escaped_event": escaped_event, "variant_id": variant_id}
283+
)
274284
yield pytest.param(
275-
sample_event,
285+
{
286+
**search_selector,
287+
"fields": requirement_fields,
288+
"modinput_params": modinput_params,
289+
},
276290
id=f"sample_name::{event.sample_name}::host::{event.metadata.get('host')}",
277291
)
278292

pytest_splunk_addon/fields_tests/test_templates.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ def test_requirements_fields(
170170
"modinput_params", splunk_searchtime_fields_requirements["modinput_params"]
171171
)
172172

173-
escaped_event = splunk_searchtime_fields_requirements["escaped_event"]
173+
escaped_event = splunk_searchtime_fields_requirements.get("escaped_event")
174+
unique_identifier = splunk_searchtime_fields_requirements.get(
175+
"unique_identifier"
176+
)
177+
variant_id = splunk_searchtime_fields_requirements.get("variant_id")
174178
fields = splunk_searchtime_fields_requirements["fields"]
175179
modinput_params = splunk_searchtime_fields_requirements["modinput_params"]
176180

@@ -185,15 +189,18 @@ def test_requirements_fields(
185189
if param_value is not None:
186190
basic_search += f" {param}={param_value}"
187191

188-
if splunk_searchtime_fields_requirements.get("unique_identifier"):
189-
unique_identifier = splunk_searchtime_fields_requirements[
190-
"unique_identifier"
191-
]
192-
record_property("Event_with", unique_identifier)
193-
194-
search = f'search {index_list} {basic_search} unique_identifier="{unique_identifier}" | fields *'
192+
# Prefer UUID+variant_id, fallback to escaped_event+variant_id
193+
if unique_identifier is not None:
194+
selector = f'fields.unique_identifier="{unique_identifier}"'
195+
elif escaped_event is not None:
196+
selector = escaped_event
195197
else:
196-
search = f"search {index_list} {basic_search} {escaped_event} | fields *"
198+
selector = ""
199+
variant_clause = f" variant_id={variant_id}" if variant_id is not None else ""
200+
201+
search = (
202+
f"search {index_list} {basic_search} {selector}{variant_clause} | fields *"
203+
)
197204

198205
self.logger.info(f"Executing the search query: {search}")
199206

pytest_splunk_addon/sample_generation/sample_stanza.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ def get_eventmetadata(self):
267267
self.host_count += 1
268268
event_host = self.metadata.get("host") + "_" + str(self.host_count)
269269
event_metadata = copy.deepcopy(self.metadata)
270+
# Add variant_id only when UUID ingestion is enabled
271+
if event_metadata.get("ingest_with_uuid") == "true":
272+
event_metadata.update(variant_id=self.host_count)
270273
event_metadata.update(host=event_host)
271274
LOGGER.info("event metadata: {}".format(event_metadata))
272275
return event_metadata
@@ -306,8 +309,7 @@ def _get_raw_sample(self):
306309
if "transport" in each_event.keys():
307310
static_host = each_event["transport"].get("@host")
308311
if static_host:
309-
# Preserve per-event uniqueness by appending variant counter
310-
event_metadata.update(host=f"{static_host}-{self.host_count}")
312+
event_metadata.update(host=static_host)
311313
static_source = each_event["transport"].get("@source")
312314
if static_source:
313315
event_metadata.update(source=static_source)

tests/unit/tests_standard_lib/test_fields_tests/test_test_generator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ def test_generate_field_tests(
486486
(
487487
{
488488
"escaped_event": "escaped_event",
489+
"variant_id": None,
489490
"fields": {
490491
"severity": "low",
491492
"signature_id": "405001",
@@ -501,6 +502,7 @@ def test_generate_field_tests(
501502
(
502503
{
503504
"escaped_event": "escaped_event",
505+
"variant_id": None,
504506
"fields": {
505507
"src": "192.168.0.1",
506508
"type": "event",
@@ -543,6 +545,7 @@ def test_generate_requirement_tests_with_uuid(mock_uuid4):
543545
"sourcetype_to_search": "dummy_sourcetype",
544546
"host": "dummy_host",
545547
"ingest_with_uuid": "true",
548+
"unique_identifier": "uuid",
546549
},
547550
sample_name="file1.xml",
548551
requirement_test_data={
@@ -561,6 +564,7 @@ def test_generate_requirement_tests_with_uuid(mock_uuid4):
561564
{
562565
"escaped_event": "escaped_event",
563566
"unique_identifier": "uuid",
567+
"variant_id": None,
564568
"fields": {
565569
"severity": "low",
566570
"signature_id": "405001",

0 commit comments

Comments
 (0)