Skip to content

Commit ba0b9cf

Browse files
feat: Added options to generate ingest events and run test
1 parent d596489 commit ba0b9cf

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

pytest_splunk_addon/plugin.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
import pytest
3-
import sys
3+
from .standard_lib.sample_generation.sample_xdist_generator import SampleXdistGenerator
44
import traceback
55
from .standard_lib import AppTestGenerator
66
from .standard_lib.cim_compliance import CIMReportPlugin
@@ -78,11 +78,32 @@ def pytest_unconfigure(config):
7878
del config._markdown
7979
config.pluginmanager.unregister(markdown)
8080

81+
def pytest_sessionstart(session):
82+
83+
SampleXdistGenerator.event_path = session.config.getoption("event_path")
84+
SampleXdistGenerator.event_stored = False
85+
SampleXdistGenerator.tokenized_event_source = session.config.getoption("tokenized_event_source").lower()
86+
# For scenario (store_new, False, False) get_samples from SampleXdistGenerator will not be invoked
87+
# Because all teh tests are skipped, So event.pickle file will not be generated
88+
# For this scenarios tokenized_event is generated here
89+
if (
90+
SampleXdistGenerator.tokenized_event_source == "store_new"
91+
and session.config.getoption("ingest_events").lower() in ["no", "n", "false", "f"]
92+
and session.config.getoption("execute_test").lower() in ["no", "n", "false", "f"]
93+
):
94+
app_path = session.config.getoption("splunk_app")
95+
config_path = session.config.getoption("splunk_data_generator")
96+
store_events = session.config.getoption("store_events")
97+
sample_generator = SampleXdistGenerator(app_path, config_path)
98+
sample_generator.get_samples(store_events)
99+
81100

82101
def pytest_generate_tests(metafunc):
83102
"""
84103
Parse the fixture dynamically.
85104
"""
105+
if metafunc.config.getoption("execute_test").lower() in ["no","n","false","f"]:
106+
return
86107
global test_generator
87108
for fixture in metafunc.fixturenames:
88109
if fixture.startswith("splunk_searchtime") or fixture.startswith(
@@ -111,6 +132,13 @@ def pytest_generate_tests(metafunc):
111132
f"\nLogs:\n{log_message}"
112133
)
113134

135+
def pytest_collection_modifyitems(config, items):
136+
ingest_events_flag = config.getoption("ingest_events")
137+
is_ingest_false = ingest_events_flag.lower() in ["no","n","false","f"]
138+
execute_test_flag = config.getoption("execute_test")
139+
if execute_test_flag.lower() in ["no","n","false","f"]:
140+
for item in items.copy():
141+
item.add_marker(pytest.mark.skipif(item.name != 'test_events_with_untokenised_values' or is_ingest_false, reason=f'--execute-test={execute_test_flag} provided'))
114142

115143
def init_pytest_splunk_addon_logger():
116144
"""
@@ -128,6 +156,5 @@ def init_pytest_splunk_addon_logger():
128156
logger.setLevel(logging.INFO)
129157
return logger
130158

131-
132159
init_pytest_splunk_addon_logger()
133160
LOGGER = logging.getLogger("pytest-splunk-addon")

pytest_splunk_addon/splunk.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,34 @@ def pytest_addoption(parser):
286286
default="Chang3d!",
287287
help="Password of the Universal Forwarder user",
288288
)
289-
289+
group.addoption(
290+
"--event-file-path",
291+
action="store",
292+
dest="event_path",
293+
help="Path to tokenised event directory",
294+
default="events.pickel"
295+
)
296+
group.addoption(
297+
"--tokenized-event-source",
298+
action="store",
299+
dest="tokenized_event_source",
300+
help="One of (new|pregenerated|store_new)",
301+
default="new"
302+
)
303+
group.addoption(
304+
"--ingest-events",
305+
action="store",
306+
dest="ingest_events",
307+
help="Should ingest events or not (True|False)",
308+
default="True"
309+
)
310+
group.addoption(
311+
"--execute-test",
312+
action="store",
313+
dest="execute_test",
314+
help="Should ingest events or not (True|False)",
315+
default="True"
316+
)
290317

291318
@pytest.fixture(scope="session")
292319
def splunk_setup(splunk):
@@ -666,6 +693,8 @@ def splunk_ingest_data(request, splunk_hec_uri, sc4s, uf, splunk_events_cleanup)
666693
TODO:
667694
For splunk_type=external, data will not be ingested as manual configurations are required.
668695
"""
696+
if request.config.getoption("ingest_events").lower() in ['n','no','false','f']:
697+
return
669698
global PYTEST_XDIST_TESTRUNUID
670699
if (
671700
"PYTEST_XDIST_WORKER" not in os.environ

pytest_splunk_addon/standard_lib/sample_generation/sample_xdist_generator.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pickle
55
from filelock import FileLock
66
import json
7+
import pytest
78

89
class SampleXdistGenerator():
910

@@ -13,6 +14,21 @@ def __init__(self, addon_path, config_path=None, process_count=4):
1314
self.config_path = config_path
1415

1516
def get_samples(self, store_events):
17+
18+
if self.tokenized_event_source == 'pregenerated':
19+
with open(self.event_path,"rb") as file_obj:
20+
store_sample = pickle.load(file_obj)
21+
if store_events and (
22+
"PYTEST_XDIST_WORKER" not in os.environ
23+
or os.environ.get("PYTEST_XDIST_WORKER") == "gw0"
24+
):
25+
try:
26+
tokenized_events = store_sample.get("tokenized_events")
27+
self.store_events(tokenized_events)
28+
except Exception as e:
29+
pytest.exit(str(e))
30+
return store_sample
31+
1632
if "PYTEST_XDIST_WORKER" in os.environ:
1733
file_path = os.environ.get("PYTEST_XDIST_TESTRUNUID") + "_events"
1834
with FileLock(str(file_path) + ".lock"):
@@ -35,6 +51,10 @@ def get_samples(self, store_events):
3551
store_sample = {"conf_name" : SampleGenerator.conf_name, "tokenized_events" : tokenized_events}
3652
if store_events:
3753
self.store_events(tokenized_events)
54+
if self.tokenized_event_source == "store_new" and not self.event_stored:
55+
with open(self.event_path, "wb") as file_obj:
56+
pickle.dump(store_sample, file_obj)
57+
self.event_stored = True
3858
return store_sample
3959

4060
def store_events(self, tokenized_events):

0 commit comments

Comments
 (0)