Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions pytest_splunk_addon/splunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,16 @@ def splunk_search_util(splunk, request):
return search_util


def check_first_worker() -> bool:
"""
returns True if the current execution is under gw0 (first worker)
"""
return (
"PYTEST_XDIST_WORKER" not in os.environ
or os.environ.get("PYTEST_XDIST_WORKER") == "gw0"
)


@pytest.fixture(scope="session")
def ignore_internal_errors(request):
"""
Expand Down Expand Up @@ -732,10 +742,7 @@ def splunk_ingest_data(request, splunk_hec_uri, sc4s, uf, splunk_events_cleanup)
if request.config.getoption("ingest_events").lower() in ["n", "no", "false", "f"]:
return
global PYTEST_XDIST_TESTRUNUID
if (
"PYTEST_XDIST_WORKER" not in os.environ
or os.environ.get("PYTEST_XDIST_WORKER") == "gw0"
):
if check_first_worker():
addon_path = request.config.getoption("splunk_app")
config_path = request.config.getoption("splunk_data_generator")
ingest_meta_data = {
Expand Down Expand Up @@ -783,10 +790,7 @@ def splunk_events_cleanup(request, splunk_search_util):

"""
if request.config.getoption("splunk_cleanup"):
if (
"PYTEST_XDIST_WORKER" not in os.environ
or os.environ.get("PYTEST_XDIST_WORKER") == "gw0"
):
if check_first_worker():
LOGGER.info("Running the old events cleanup")
splunk_search_util.deleteEventsFromIndex()
else:
Expand All @@ -801,10 +805,7 @@ def file_system_prerequisite():
"""
UF_FILE_MONTOR_DIR = "uf_files"
monitor_dir = os.path.join(os.getcwd(), UF_FILE_MONTOR_DIR)
if (
"PYTEST_XDIST_WORKER" not in os.environ
or os.environ.get("PYTEST_XDIST_WORKER") == "gw0"
):
if check_first_worker():
if os.path.exists(monitor_dir):
shutil.rmtree(monitor_dir, ignore_errors=True)
os.mkdir(monitor_dir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ def _get_props_method(self, class_name: str):
LOGGER.info(f"Matched method of type={each_type}")
return method_mapping[each_type]
else:
LOGGER.warning(f"No parser available for {class_name}. Skipping...")
from ...splunk import check_first_worker # avoiding circular import

if check_first_worker():
LOGGER.warning(f"No parser available for {class_name}. Skipping...")

def _get_props_stanzas(self) -> Optional[Generator]:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ def __init__(self, addon_path, config_path=None, process_count=4):
self.config_path = config_path

def get_samples(self, store_events):
from ...splunk import check_first_worker # avoiding circular import

if self.tokenized_event_source == "pregenerated":
with open(self.event_path, "rb") as file_obj:
store_sample = pickle.load(file_obj)
if store_events and (
"PYTEST_XDIST_WORKER" not in os.environ
or os.environ.get("PYTEST_XDIST_WORKER") == "gw0"
):
if store_events and check_first_worker():
try:
tokenized_events = store_sample.get("tokenized_events")
self.store_events(tokenized_events)
Expand Down