Skip to content

Commit 83ad20f

Browse files
chore: ADDON-80802 PSA implementation with uuid flag
1 parent cd0c088 commit 83ad20f

File tree

11 files changed

+55
-16
lines changed

11 files changed

+55
-16
lines changed

pytest_splunk_addon/app_test_generator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ def __init__(self, pytest_config):
4848

4949
store_events = self.pytest_config.getoption("store_events")
5050
config_path = self.pytest_config.getoption("splunk_data_generator")
51+
ingest_with_uuid = self.pytest_config.getoption("ingest_with_uuid")
5152
sample_generator = SampleXdistGenerator(
52-
self.pytest_config.getoption("splunk_app"), config_path
53+
self.pytest_config.getoption("splunk_app"), ingest_with_uuid, config_path
5354
)
5455
store_sample = sample_generator.get_samples(store_events)
5556
self.tokenized_events = store_sample.get("tokenized_events")

pytest_splunk_addon/event_ingestors/hec_event_ingestor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ def ingest(self, events, thread_count):
9393
"event": event.event,
9494
"index": event.metadata.get("index", "main"),
9595
}
96+
if event.metadata["ingest_with_uuid"] == "true":
97+
event_dict["fields"] = {"unique_identifier": event.unique_identifier}
9698

9799
if event.metadata.get("host_type") in ("plugin", None):
98100
host = event.metadata.get("host")

pytest_splunk_addon/event_ingestors/ingestor_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def ingest_events(
9595
thread_count (int): number of threads to use for ingestion
9696
store_events (bool): Boolean param for generating json files with tokenised events
9797
"""
98-
sample_generator = SampleXdistGenerator(addon_path, config_path)
98+
sample_generator = SampleXdistGenerator(addon_path, ingest_meta_data["ingest_with_uuid"], config_path)
9999
store_sample = sample_generator.get_samples(store_events)
100100
tokenized_events = store_sample.get("tokenized_events")
101101
ingestor_dict = cls.get_consolidated_events(tokenized_events)

pytest_splunk_addon/fields_tests/test_generator.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,15 @@ def generate_requirements_tests(self):
261261
for field, value in requirement_fields.items()
262262
if field not in exceptions
263263
}
264-
yield pytest.param(
265-
{
264+
sample_event = {
266265
"escaped_event": escaped_event,
267266
"fields": requirement_fields,
268267
"modinput_params": modinput_params,
269-
},
268+
}
269+
if metadata["ingest_with_uuid"] == "true":
270+
sample_event["unique_identifier"] = event.unique_identifier
271+
yield pytest.param(
272+
sample_event,
270273
id=f"sample_name::{event.sample_name}::host::{event.metadata.get('host')}",
271274
)
272275

pytest_splunk_addon/fields_tests/test_templates.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,15 @@ def test_requirements_fields(
185185
if param_value is not None:
186186
basic_search += f" {param}={param_value}"
187187

188-
search = f"search {index_list} {basic_search} {escaped_event} | fields *"
188+
if splunk_searchtime_fields_requirements.get("unique_identifier"):
189+
record_property(
190+
"stanza_name", splunk_searchtime_fields_requirements["unique_identifier"]
191+
)
192+
unique_identifier = splunk_searchtime_fields_requirements["unique_identifier"]
193+
194+
search = f"search {index_list} {basic_search} unique_identifier=\"{unique_identifier}\" | fields *"
195+
else:
196+
search = f"search {index_list} {basic_search} {escaped_event} | fields *"
189197

190198
self.logger.info(f"Executing the search query: {search}")
191199

@@ -225,6 +233,7 @@ def test_requirements_fields(
225233
assert wrong_value_fields == {}, (
226234
f"\nNot all required fields have correct values or some fields are missing in Splunk. Wrong field values:\n{wrong_values_table}"
227235
f"{format_search_query_log(search)}"
236+
f"Test failed for event: {escaped_event}\n"
228237
)
229238

230239
@pytest.mark.splunk_searchtime_fields

pytest_splunk_addon/sample_generation/pytest_splunk_addon_data_parser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ class PytestSplunkAddonDataParser:
4040

4141
conf_name = " "
4242

43-
def __init__(self, addon_path: str, config_path: str):
43+
def __init__(self, addon_path: str, config_path: str, ingest_with_uuid: str):
4444
self._conf_parser = conf_parser.TABConfigParser()
4545
self.config_path = config_path
4646
self._psa_data = None
4747
self.addon_path = addon_path
4848
self.match_stanzas = set()
49+
self.ingest_with_uuid = ingest_with_uuid
4950
self._path_to_samples = self._get_path_to_samples()
5051

5152
def _get_path_to_samples(self):
@@ -106,7 +107,7 @@ def get_sample_stanzas(self):
106107
results = []
107108
for sample_name, stanza_params in sorted(_psa_data.items()):
108109
sample_path = os.path.join(self._path_to_samples, sample_name)
109-
results.append(SampleStanza(sample_path, stanza_params))
110+
results.append(SampleStanza(sample_path, stanza_params, self.ingest_with_uuid))
110111
return results
111112

112113
def _get_psa_data_stanzas(self):

pytest_splunk_addon/sample_generation/sample_event.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16+
import uuid
1617
import re
1718
import logging
1819
from ..index_tests import key_fields
@@ -67,6 +68,8 @@ def __init__(self, event_string, metadata, sample_name, requirement_test_data=No
6768
self.time_values = list()
6869
self.metadata = metadata
6970
self.sample_name = sample_name
71+
if metadata["ingest_with_uuid"] == "true":
72+
self.unique_identifier = str(uuid.uuid4())
7073
self.host_count = 0
7174
self.requirement_test_data = requirement_test_data
7275

pytest_splunk_addon/sample_generation/sample_generator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@ class SampleGenerator(object):
3333
sample_stanzas = []
3434
conf_name = " "
3535

36-
def __init__(self, addon_path, config_path=None, process_count=4):
36+
def __init__(self, addon_path, ingest_with_uuid, config_path=None, process_count=4):
3737
self.addon_path = addon_path
3838
self.process_count = process_count
3939
self.config_path = config_path
40+
self.ingest_with_uuid = ingest_with_uuid
4041

4142
def get_samples(self):
4243
"""
4344
Generate SampleEvent object
4445
"""
4546
if not SampleGenerator.sample_stanzas:
4647
psa_data_parser = PytestSplunkAddonDataParser(
47-
self.addon_path, config_path=self.config_path
48+
self.addon_path, config_path=self.config_path, ingest_with_uuid=self.ingest_with_uuid
4849
)
4950
sample_stanzas = psa_data_parser.get_sample_stanzas()
5051
SampleGenerator.conf_name = psa_data_parser.conf_name

pytest_splunk_addon/sample_generation/sample_stanza.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ class SampleStanza(object):
4747
psa_data_params (dict): Dictionary representing pytest-splunk-addon-data.conf
4848
"""
4949

50-
def __init__(self, sample_path, psa_data_params):
50+
def __init__(self, sample_path, psa_data_params, ingest_with_uuid):
5151
self.sample_path = sample_path
5252
self.sample_name = os.path.basename(sample_path)
5353
self.metadata = self._parse_meta(psa_data_params)
5454
self.sample_rules = list(self._parse_rules(psa_data_params, self.sample_path))
5555
self.input_type = self.metadata.get("input_type", "default")
56+
self.metadata["ingest_with_uuid"] = ingest_with_uuid
5657
self.host_count = 0
5758

5859
def get_raw_events(self):

pytest_splunk_addon/sample_generation/sample_xdist_generator.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ class SampleXdistGenerator:
3333
process_count (num): generate {no} process for execution
3434
"""
3535

36-
def __init__(self, addon_path, config_path=None, process_count=4):
36+
def __init__(self, addon_path, ingest_with_uuid, config_path=None, process_count=4):
3737
self.addon_path = addon_path
3838
self.process_count = process_count
3939
self.config_path = config_path
40+
self.ingest_with_uuid = ingest_with_uuid
4041

4142
def get_samples(self, store_events):
4243
"""
@@ -67,7 +68,7 @@ def get_samples(self, store_events):
6768
store_sample = pickle.load(file_obj)
6869
else:
6970
sample_generator = SampleGenerator(
70-
self.addon_path, self.config_path
71+
self.addon_path, self.ingest_with_uuid, self.config_path
7172
)
7273
tokenized_events = list(sample_generator.get_samples())
7374
store_sample = {
@@ -79,7 +80,7 @@ def get_samples(self, store_events):
7980
with open(file_path, "wb") as file_obj:
8081
pickle.dump(store_sample, file_obj)
8182
else:
82-
sample_generator = SampleGenerator(self.addon_path, self.config_path)
83+
sample_generator = SampleGenerator(self.addon_path, self.ingest_with_uuid, self.config_path)
8384
tokenized_events = list(sample_generator.get_samples())
8485
store_sample = {
8586
"conf_name": SampleGenerator.conf_name,
@@ -125,6 +126,7 @@ def store_events(self, tokenized_events):
125126
"sourcetype": each_event.metadata.get("sourcetype"),
126127
"timestamp_type": each_event.metadata.get("timestamp_type"),
127128
"input_type": each_event.metadata.get("input_type"),
129+
"ingest_with_uuid": self.ingest_with_uuid,
128130
"expected_event_count": expected_count,
129131
"index": each_event.metadata.get("index", "main"),
130132
},
@@ -137,14 +139,19 @@ def store_events(self, tokenized_events):
137139
}
138140
],
139141
}
142+
if self.ingest_with_uuid == "true":
143+
tokenized_samples_dict[each_event.sample_name]["events"][0]["unique_identifier"] = each_event.unique_identifier
140144
else:
141-
tokenized_samples_dict[each_event.sample_name]["events"].append(
142-
{
145+
sample_event = {
143146
"event": each_event.event,
144147
"key_fields": each_event.key_fields,
145148
"time_values": each_event.time_values,
146149
"requirement_test_data": each_event.requirement_test_data,
147150
}
151+
if self.ingest_with_uuid == "true":
152+
sample_event["unique_identifier"] = each_event.unique_identifier
153+
tokenized_samples_dict[each_event.sample_name]["events"].append(
154+
sample_event
148155
)
149156

150157
for sample_name, tokenized_sample in tokenized_samples_dict.items():

pytest_splunk_addon/splunk.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ def pytest_addoption(parser):
4848
by another process such as a ci/cd pipeline
4949
"""
5050
group = parser.getgroup("splunk-addon")
51+
group.addoption(
52+
"--ingest-with-uuid",
53+
action="store",
54+
dest="ingest_with_uuid",
55+
default="False",
56+
help=(
57+
"Type of ingesting and searching the events into Splunk "
58+
"with uuid or without uuid."
59+
),
60+
)
5161

5262
group.addoption(
5363
"--splunk-app",
@@ -747,6 +757,7 @@ def splunk_ingest_data(request, splunk_hec_uri, sc4s, uf, splunk_events_cleanup)
747757
"splunk_hec_uri": splunk_hec_uri[1],
748758
"sc4s_host": sc4s[0], # for sc4s
749759
"sc4s_port": sc4s[1][514], # for sc4s
760+
"ingest_with_uuid": request.config.getoption("ingest_with_uuid"),
750761
}
751762
thread_count = int(request.config.getoption("thread_count"))
752763
store_events = request.config.getoption("store_events")

0 commit comments

Comments
 (0)