Skip to content

Commit d7f63ea

Browse files
fix: do not log .conf parser warnings from all workers (#845)
When tests are ran with multiple workers, duplicate logging is observed because every workers logs the warning. This PR fixes that. --------- Co-authored-by: Artem Rys <rysartem@gmail.com>
1 parent e524c18 commit d7f63ea

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

pytest_splunk_addon/splunk.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import configparser
3333
from filelock import FileLock
3434

35+
from pytest_splunk_addon.standard_lib import utils
36+
3537
RESPONSIVE_SPLUNK_TIMEOUT = 300 # seconds
3638

3739
LOGGER = logging.getLogger("pytest-splunk-addon")
@@ -732,10 +734,7 @@ def splunk_ingest_data(request, splunk_hec_uri, sc4s, uf, splunk_events_cleanup)
732734
if request.config.getoption("ingest_events").lower() in ["n", "no", "false", "f"]:
733735
return
734736
global PYTEST_XDIST_TESTRUNUID
735-
if (
736-
"PYTEST_XDIST_WORKER" not in os.environ
737-
or os.environ.get("PYTEST_XDIST_WORKER") == "gw0"
738-
):
737+
if utils.check_first_worker():
739738
addon_path = request.config.getoption("splunk_app")
740739
config_path = request.config.getoption("splunk_data_generator")
741740
ingest_meta_data = {
@@ -783,10 +782,7 @@ def splunk_events_cleanup(request, splunk_search_util):
783782
784783
"""
785784
if request.config.getoption("splunk_cleanup"):
786-
if (
787-
"PYTEST_XDIST_WORKER" not in os.environ
788-
or os.environ.get("PYTEST_XDIST_WORKER") == "gw0"
789-
):
785+
if utils.check_first_worker():
790786
LOGGER.info("Running the old events cleanup")
791787
splunk_search_util.deleteEventsFromIndex()
792788
else:
@@ -801,10 +797,7 @@ def file_system_prerequisite():
801797
"""
802798
UF_FILE_MONTOR_DIR = "uf_files"
803799
monitor_dir = os.path.join(os.getcwd(), UF_FILE_MONTOR_DIR)
804-
if (
805-
"PYTEST_XDIST_WORKER" not in os.environ
806-
or os.environ.get("PYTEST_XDIST_WORKER") == "gw0"
807-
):
800+
if utils.check_first_worker():
808801
if os.path.exists(monitor_dir):
809802
shutil.rmtree(monitor_dir, ignore_errors=True)
810803
os.mkdir(monitor_dir)

pytest_splunk_addon/standard_lib/addon_parser/props_parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
from .fields import convert_to_fields
3030
from .transforms_parser import TransformsParser
31+
from pytest_splunk_addon.standard_lib import utils
3132

3233
LOGGER = logging.getLogger("pytest-splunk-addon")
3334

@@ -110,7 +111,8 @@ def _get_props_method(self, class_name: str):
110111
LOGGER.info(f"Matched method of type={each_type}")
111112
return method_mapping[each_type]
112113
else:
113-
LOGGER.warning(f"No parser available for {class_name}. Skipping...")
114+
if utils.check_first_worker():
115+
LOGGER.warning(f"No parser available for {class_name}. Skipping...")
114116

115117
def _get_props_stanzas(self) -> Optional[Generator]:
116118
"""

pytest_splunk_addon/standard_lib/sample_generation/sample_xdist_generator.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import json
2121
import pytest
2222

23+
from pytest_splunk_addon.standard_lib import utils
24+
2325

2426
class SampleXdistGenerator:
2527
def __init__(self, addon_path, config_path=None, process_count=4):
@@ -28,14 +30,10 @@ def __init__(self, addon_path, config_path=None, process_count=4):
2830
self.config_path = config_path
2931

3032
def get_samples(self, store_events):
31-
3233
if self.tokenized_event_source == "pregenerated":
3334
with open(self.event_path, "rb") as file_obj:
3435
store_sample = pickle.load(file_obj)
35-
if store_events and (
36-
"PYTEST_XDIST_WORKER" not in os.environ
37-
or os.environ.get("PYTEST_XDIST_WORKER") == "gw0"
38-
):
36+
if store_events and utils.check_first_worker():
3937
try:
4038
tokenized_events = store_sample.get("tokenized_events")
4139
self.store_events(tokenized_events)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# Copyright 2024 Splunk Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
import os
17+
18+
19+
def check_first_worker() -> bool:
20+
"""
21+
returns True if the current execution is under gw0 (first worker)
22+
"""
23+
return (
24+
"PYTEST_XDIST_WORKER" not in os.environ
25+
or os.environ.get("PYTEST_XDIST_WORKER") == "gw0"
26+
)

0 commit comments

Comments
 (0)