Skip to content

Commit

Permalink
fix: remove-pytest_exception_interact hook (#881)
Browse files Browse the repository at this point in the history
This PR reverts partially changes introduced in this PR: 

7cc8880#diff-2699a131e5dfd55b20327a39ec213c806ee847e17b6666faf0a01fe850448f47L558
This change is necessary, since exiting pytest while getting `Exception`
disturbed other tests (fr example UI test
https://splunk.atlassian.net/browse/ADDON-72764). Instead of that HEC
token is validated by ingesting simple event with it (implemented in
this PR: #877).
  • Loading branch information
kdoroszko-splunk authored Aug 28, 2024
1 parent c4f72fe commit 19f3967
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-test-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ jobs:
path: |
test-results-${{ matrix.splunk.version }}
test-splunk-matrix:
needs:
- meta
Expand All @@ -149,7 +150,6 @@ jobs:
"splunk_app_cim_broken",
"splunk_fiction_indextime",
"splunk_fiction_indextime_broken",
"splunk_fiction_indextime_wrong_hec_token",
"splunk_setup_fixture",
"splunk_app_req",
"splunk_app_req_broken",
Expand Down
14 changes: 0 additions & 14 deletions pytest_splunk_addon/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

test_generator = None

EXC_MAP = [Exception]


def pytest_configure(config):
"""
Expand Down Expand Up @@ -122,7 +120,6 @@ def pytest_sessionstart(session):
SampleXdistGenerator.tokenized_event_source = session.config.getoption(
"tokenized_event_source"
).lower()
session.__exc_limits = EXC_MAP
if (
SampleXdistGenerator.tokenized_event_source == "store_new"
and session.config.getoption("ingest_events").lower()
Expand Down Expand Up @@ -212,14 +209,3 @@ def init_pytest_splunk_addon_logger():

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


def pytest_exception_interact(node, call, report):
"""
Hook called when an exception is raised during a test.
If the number of occurrences for a specific exception exceeds the limit in session.__exc_limits, pytest exits
https://docs.pytest.org/en/stable/reference/reference.html#pytest.hookspec.pytest_exception_interact
"""
if call.excinfo.type in node.session.__exc_limits:
# pytest exits only for exceptions defined in EXC_MAP
pytest.exit(f"Exiting pytest due to: {call.excinfo.type}")
5 changes: 3 additions & 2 deletions pytest_splunk_addon/splunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ def is_responsive_hec(request, splunk):
f'{request.config.getoption("splunk_hec_scheme")}://{splunk["forwarder_host"]}:{splunk["port_hec"]}/services/collector/health/1.0',
verify=False,
)
LOGGER.debug("Status code: {}".format(response.status_code))
LOGGER.debug("Status code: %d", response.status_code)
if response.status_code in (200, 201):
LOGGER.info("Splunk HEC is responsive.")
return True
Expand Down Expand Up @@ -1040,7 +1040,8 @@ def is_valid_hec(request, splunk):
data={"event": "test_hec", "sourcetype": "hec_token_test"},
verify=False,
)
LOGGER.debug("Status code: {}".format(response.status_code))
LOGGER.debug("Status code: %d", response.status_code)

if response.status_code == 200:
LOGGER.info("Splunk HEC is valid.")
else:
Expand Down
11 changes: 7 additions & 4 deletions tests/e2e/test_splunk_addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ def empty_method():
assert result.ret == 0


@pytest.mark.docker
@pytest.mark.splunk_fiction_indextime_wrong_hec_token
@pytest.mark.external
def test_splunk_fiction_indextime_wrong_hec_token(testdir, request):
"""Make sure that pytest accepts our fixture."""

Expand Down Expand Up @@ -205,17 +205,20 @@ def empty_method():
# run pytest with the following cmd args
result = testdir.runpytest(
f"--splunk-version={request.config.getoption('splunk_version')}",
"--splunk-type=docker",
"--splunk-type=external",
"--splunk-host=splunk",
"--splunk-port=8089",
"--splunk-forwarder-host=splunk",
"--splunk-hec-token=8b741d03-43e9-4164-908b-e09102327d22",
"-v",
"--search-interval=0",
"--search-retry=0",
"--splunk-data-generator=tests/addons/TA_fiction_indextime/default",
"--search-index=*,_internal",
)

result.assert_outcomes(errors=1, passed=0, failed=0, xfailed=0)
result.stdout.fnmatch_lines(
"!!!!!! _pytest.outcomes.Exit: Exiting pytest due to: <class 'Exception'> !!!!!!!"
"*_pytest.outcomes.Exit: Exiting pytest due to invalid HEC token value."
)

assert result.ret != 0
Expand Down

0 comments on commit 19f3967

Please sign in to comment.