From af7ca56f360874d04dd7747ef32878fd25c2e9b3 Mon Sep 17 00:00:00 2001 From: kdoroszko-splunk Date: Mon, 12 Aug 2024 17:22:09 +0200 Subject: [PATCH 01/10] fix: remove-pytest_exception_interact hook --- pytest_splunk_addon/plugin.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/pytest_splunk_addon/plugin.py b/pytest_splunk_addon/plugin.py index 5b136c27e..3926774d4 100644 --- a/pytest_splunk_addon/plugin.py +++ b/pytest_splunk_addon/plugin.py @@ -26,8 +26,6 @@ test_generator = None -EXC_MAP = [Exception] - def pytest_configure(config): """ @@ -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() @@ -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}") From 568a989448b05b39c78f106361367b898afffd8c Mon Sep 17 00:00:00 2001 From: kdoroszko-splunk Date: Fri, 16 Aug 2024 15:48:22 +0200 Subject: [PATCH 02/10] chore: backport is_valid_hec from main --- pytest_splunk_addon/splunk.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pytest_splunk_addon/splunk.py b/pytest_splunk_addon/splunk.py index 0bcd046d0..de317e08d 100644 --- a/pytest_splunk_addon/splunk.py +++ b/pytest_splunk_addon/splunk.py @@ -639,6 +639,7 @@ def splunk_external(request): "Could not connect to Splunk HEC" "Please check the log file for possible errors." ) + is_valid_hec(request, splunk_info) return splunk_info @@ -1005,6 +1006,34 @@ def is_responsive(url): ) return False +def is_valid_hec(request, splunk): + """ + Verify if provided hec token is valid by sending simple post request. + + Args: + splunk (dict): details of the Splunk instance + + Returns: + None + """ + + LOGGER.info( + "Validating HEC token... splunk=%s", + json.dumps(splunk), + ) + response = requests.post( + url=f'{request.config.getoption("splunk_hec_scheme")}://{splunk["forwarder_host"]}:{splunk["port_hec"]}/services/collector/raw', + headers={ + "Authorization": f'Splunk {request.config.getoption("splunk_hec_token")}' + }, + data={"event": "test_hec", "sourcetype": "hec_token_test"}, + verify=False, + ) + LOGGER.debug("Status code: {}".format(response.status_code)) + if response.status_code == 200: + LOGGER.info("Splunk HEC is valid.") + else: + pytest.exit("Exiting pytest due to invalid HEC token value.") def pytest_unconfigure(config): if PYTEST_XDIST_TESTRUNUID: From 234b8599a8ebc1845bf9e0e930d12a5171ce1be5 Mon Sep 17 00:00:00 2001 From: kdoroszko-splunk Date: Fri, 16 Aug 2024 15:48:57 +0200 Subject: [PATCH 03/10] test: adjust tests --- tests/e2e/test_splunk_addon.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/e2e/test_splunk_addon.py b/tests/e2e/test_splunk_addon.py index a95820419..d7ddf808f 100644 --- a/tests/e2e/test_splunk_addon.py +++ b/tests/e2e/test_splunk_addon.py @@ -169,7 +169,6 @@ def empty_method(): assert result.ret == 0 -@pytest.mark.docker @pytest.mark.splunk_fiction_indextime_wrong_hec_token def test_splunk_fiction_indextime_wrong_hec_token(testdir, request): """Make sure that pytest accepts our fixture.""" @@ -205,7 +204,7 @@ 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", "-v", "--search-interval=0", "--search-retry=0", @@ -213,9 +212,8 @@ def empty_method(): "--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: !!!!!!!" + "*Exiting pytest due to invalid HEC token value." ) assert result.ret != 0 From a9c9aaa3f577d716b655087d9f1ad437c5ed96f3 Mon Sep 17 00:00:00 2001 From: kdoroszko-splunk Date: Fri, 16 Aug 2024 15:51:15 +0200 Subject: [PATCH 04/10] chore: fix lint --- pytest_splunk_addon/splunk.py | 2 ++ tests/e2e/test_splunk_addon.py | 4 +--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pytest_splunk_addon/splunk.py b/pytest_splunk_addon/splunk.py index de317e08d..3456574e8 100644 --- a/pytest_splunk_addon/splunk.py +++ b/pytest_splunk_addon/splunk.py @@ -1006,6 +1006,7 @@ def is_responsive(url): ) return False + def is_valid_hec(request, splunk): """ Verify if provided hec token is valid by sending simple post request. @@ -1035,6 +1036,7 @@ def is_valid_hec(request, splunk): else: pytest.exit("Exiting pytest due to invalid HEC token value.") + def pytest_unconfigure(config): if PYTEST_XDIST_TESTRUNUID: if os.path.exists(PYTEST_XDIST_TESTRUNUID + "_wait"): diff --git a/tests/e2e/test_splunk_addon.py b/tests/e2e/test_splunk_addon.py index d7ddf808f..88036557b 100644 --- a/tests/e2e/test_splunk_addon.py +++ b/tests/e2e/test_splunk_addon.py @@ -212,9 +212,7 @@ def empty_method(): "--search-index=*,_internal", ) - result.stdout.fnmatch_lines( - "*Exiting pytest due to invalid HEC token value." - ) + result.stdout.fnmatch_lines("*Exiting pytest due to invalid HEC token value.") assert result.ret != 0 From 3678d1ae42ebd5256e7af7f9d1820fde592ef153 Mon Sep 17 00:00:00 2001 From: kdoroszko-splunk Date: Fri, 16 Aug 2024 16:28:07 +0200 Subject: [PATCH 05/10] chore: adjust splunk external host --- tests/e2e/test_splunk_addon.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/e2e/test_splunk_addon.py b/tests/e2e/test_splunk_addon.py index 88036557b..5f14fa248 100644 --- a/tests/e2e/test_splunk_addon.py +++ b/tests/e2e/test_splunk_addon.py @@ -205,6 +205,9 @@ def empty_method(): result = testdir.runpytest( f"--splunk-version={request.config.getoption('splunk_version')}", "--splunk-type=external", + "--splunk-host=splunk", + "--splunk-port=8089", + "--splunk-forwarder-host=splunk", "-v", "--search-interval=0", "--search-retry=0", From 91b700ba4a806422b15404532b44c71f055a9c89 Mon Sep 17 00:00:00 2001 From: kdoroszko-splunk Date: Fri, 23 Aug 2024 14:27:41 +0200 Subject: [PATCH 06/10] chore: adjust hec token tests --- .github/workflows/build-test-release.yml | 40 +++++++++++++++++++++++- tests/e2e/test_splunk_addon.py | 6 ++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build-test-release.yml b/.github/workflows/build-test-release.yml index 7dcd4280c..95a271cda 100644 --- a/.github/workflows/build-test-release.yml +++ b/.github/workflows/build-test-release.yml @@ -129,6 +129,44 @@ jobs: path: | test-results-${{ matrix.splunk.version }} + test-splunk-external-wrong-hec-token: + runs-on: ubuntu-latest + needs: + - meta + - pre-commit + - fossa-scan + - compliance-copyrights + - test-splunk-unit + strategy: + fail-fast: false + matrix: + splunk: ${{ fromJson(needs.meta.outputs.matrix_supportedSplunk) }} + steps: + - uses: actions/checkout@v4 + with: + submodules: true + - uses: actions/setup-python@v5 + with: + python-version: 3.7 + - name: Setup for testing + run: | + pip install git+https://github.com/pixelb/crudini + curl -sSL https://install.python-poetry.org | python3 - --version 1.5.1 + poetry install + - name: Test + run: | + export SPLUNK_APP_PACKAGE=./tests/e2e/addons/TA_fiction_indextime + export SPLUNK_ADDON=TA_fiction_indextime + export SPLUNK_APP_ID=TA_fiction_indextime + export SPLUNK_VERSION=${{ matrix.splunk.version }} + export SPLUNK_HEC_TOKEN="1b741d03-43e9-4164-908b-e09102327d22" + export SPLUNK_PASSWORD=Chang3d! + echo $SPLUNK_VERSION + docker build -f Dockerfile.splunk --build-arg SPLUNK_VERSION=$SPLUNK_VERSION --build-arg SPLUNK_APP_ID=$SPLUNK_APP_ID --build-arg SPLUNK_APP_PACKAGE=$SPLUNK_APP_PACKAGE --build-arg SPLUNK_HEC_TOKEN=$SPLUNK_HEC_TOKEN -t splunk_instance:latest . + docker run --detach -e SPLUNK_START_ARGS=--accept-license -e SPLUNK_PASSWORD=$SPLUNK_PASSWORD -e SPLUNK_HEC_TOKEN=$SPLUNK_HEC_TOKEN -p 8000:8000 -p 8088:8088 -p 8089:8089 splunk_instance:latest + poetry run pytest -v --splunk-version=${{ matrix.splunk.version }} -m splunk_fiction_indextime_wrong_hec_token tests/e2e + + test-splunk-matrix: needs: - meta @@ -149,7 +187,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", @@ -170,6 +207,7 @@ jobs: publish: needs: - test-splunk-external + - test-splunk-external-wrong-hec-token - test-splunk-matrix runs-on: ubuntu-latest steps: diff --git a/tests/e2e/test_splunk_addon.py b/tests/e2e/test_splunk_addon.py index 5f14fa248..92f477715 100644 --- a/tests/e2e/test_splunk_addon.py +++ b/tests/e2e/test_splunk_addon.py @@ -205,9 +205,9 @@ def empty_method(): result = testdir.runpytest( f"--splunk-version={request.config.getoption('splunk_version')}", "--splunk-type=external", - "--splunk-host=splunk", + "--splunk-host=localhost", "--splunk-port=8089", - "--splunk-forwarder-host=splunk", + "--splunk-forwarder-host=localhost", "-v", "--search-interval=0", "--search-retry=0", @@ -215,7 +215,7 @@ def empty_method(): "--search-index=*,_internal", ) - result.stdout.fnmatch_lines("*Exiting pytest due to invalid HEC token value.") + result.stdout.fnmatch_lines("*_pytest.outcomes.Exit: Exiting pytest due to invalid HEC token value.") assert result.ret != 0 From dc953d37c79e7e002b6661be3ec0d35795e90858 Mon Sep 17 00:00:00 2001 From: kdoroszko-splunk Date: Fri, 23 Aug 2024 14:30:17 +0200 Subject: [PATCH 07/10] chore: fix linting --- tests/e2e/test_splunk_addon.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/e2e/test_splunk_addon.py b/tests/e2e/test_splunk_addon.py index 92f477715..0b169bd03 100644 --- a/tests/e2e/test_splunk_addon.py +++ b/tests/e2e/test_splunk_addon.py @@ -215,7 +215,9 @@ def empty_method(): "--search-index=*,_internal", ) - result.stdout.fnmatch_lines("*_pytest.outcomes.Exit: Exiting pytest due to invalid HEC token value.") + result.stdout.fnmatch_lines( + "*_pytest.outcomes.Exit: Exiting pytest due to invalid HEC token value." + ) assert result.ret != 0 From 67d51aa613bc9f47eb7906047194d94373c31dc9 Mon Sep 17 00:00:00 2001 From: kdoroszko-splunk Date: Fri, 23 Aug 2024 15:05:06 +0200 Subject: [PATCH 08/10] chore: remove unnecesary args --- .github/workflows/build-test-release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-test-release.yml b/.github/workflows/build-test-release.yml index 95a271cda..48e9f0269 100644 --- a/.github/workflows/build-test-release.yml +++ b/.github/workflows/build-test-release.yml @@ -162,7 +162,7 @@ jobs: export SPLUNK_HEC_TOKEN="1b741d03-43e9-4164-908b-e09102327d22" export SPLUNK_PASSWORD=Chang3d! echo $SPLUNK_VERSION - docker build -f Dockerfile.splunk --build-arg SPLUNK_VERSION=$SPLUNK_VERSION --build-arg SPLUNK_APP_ID=$SPLUNK_APP_ID --build-arg SPLUNK_APP_PACKAGE=$SPLUNK_APP_PACKAGE --build-arg SPLUNK_HEC_TOKEN=$SPLUNK_HEC_TOKEN -t splunk_instance:latest . + docker build -f Dockerfile.splunk --build-arg SPLUNK_VERSION=$SPLUNK_VERSION --build-arg SPLUNK_APP_ID=$SPLUNK_APP_ID --build-arg SPLUNK_APP_PACKAGE=$SPLUNK_APP_PACKAGE -t splunk_instance:latest . docker run --detach -e SPLUNK_START_ARGS=--accept-license -e SPLUNK_PASSWORD=$SPLUNK_PASSWORD -e SPLUNK_HEC_TOKEN=$SPLUNK_HEC_TOKEN -p 8000:8000 -p 8088:8088 -p 8089:8089 splunk_instance:latest poetry run pytest -v --splunk-version=${{ matrix.splunk.version }} -m splunk_fiction_indextime_wrong_hec_token tests/e2e From 2bce520db2d5a46702e0f05cad65d02fb87904b6 Mon Sep 17 00:00:00 2001 From: kdoroszko-splunk Date: Tue, 27 Aug 2024 11:40:38 +0200 Subject: [PATCH 09/10] chore: change string formatting --- pytest_splunk_addon/splunk.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytest_splunk_addon/splunk.py b/pytest_splunk_addon/splunk.py index 3456574e8..079fba22a 100644 --- a/pytest_splunk_addon/splunk.py +++ b/pytest_splunk_addon/splunk.py @@ -969,7 +969,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 @@ -1030,7 +1030,7 @@ 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: From 348e8d40a9483ba4c7bea72e960c31606be240f1 Mon Sep 17 00:00:00 2001 From: kdoroszko-splunk Date: Tue, 27 Aug 2024 18:15:07 +0200 Subject: [PATCH 10/10] chore: move hec token to external tests --- .github/workflows/build-test-release.yml | 38 ------------------------ tests/e2e/test_splunk_addon.py | 6 ++-- 2 files changed, 4 insertions(+), 40 deletions(-) diff --git a/.github/workflows/build-test-release.yml b/.github/workflows/build-test-release.yml index 48e9f0269..0e372de37 100644 --- a/.github/workflows/build-test-release.yml +++ b/.github/workflows/build-test-release.yml @@ -129,43 +129,6 @@ jobs: path: | test-results-${{ matrix.splunk.version }} - test-splunk-external-wrong-hec-token: - runs-on: ubuntu-latest - needs: - - meta - - pre-commit - - fossa-scan - - compliance-copyrights - - test-splunk-unit - strategy: - fail-fast: false - matrix: - splunk: ${{ fromJson(needs.meta.outputs.matrix_supportedSplunk) }} - steps: - - uses: actions/checkout@v4 - with: - submodules: true - - uses: actions/setup-python@v5 - with: - python-version: 3.7 - - name: Setup for testing - run: | - pip install git+https://github.com/pixelb/crudini - curl -sSL https://install.python-poetry.org | python3 - --version 1.5.1 - poetry install - - name: Test - run: | - export SPLUNK_APP_PACKAGE=./tests/e2e/addons/TA_fiction_indextime - export SPLUNK_ADDON=TA_fiction_indextime - export SPLUNK_APP_ID=TA_fiction_indextime - export SPLUNK_VERSION=${{ matrix.splunk.version }} - export SPLUNK_HEC_TOKEN="1b741d03-43e9-4164-908b-e09102327d22" - export SPLUNK_PASSWORD=Chang3d! - echo $SPLUNK_VERSION - docker build -f Dockerfile.splunk --build-arg SPLUNK_VERSION=$SPLUNK_VERSION --build-arg SPLUNK_APP_ID=$SPLUNK_APP_ID --build-arg SPLUNK_APP_PACKAGE=$SPLUNK_APP_PACKAGE -t splunk_instance:latest . - docker run --detach -e SPLUNK_START_ARGS=--accept-license -e SPLUNK_PASSWORD=$SPLUNK_PASSWORD -e SPLUNK_HEC_TOKEN=$SPLUNK_HEC_TOKEN -p 8000:8000 -p 8088:8088 -p 8089:8089 splunk_instance:latest - poetry run pytest -v --splunk-version=${{ matrix.splunk.version }} -m splunk_fiction_indextime_wrong_hec_token tests/e2e - test-splunk-matrix: needs: @@ -207,7 +170,6 @@ jobs: publish: needs: - test-splunk-external - - test-splunk-external-wrong-hec-token - test-splunk-matrix runs-on: ubuntu-latest steps: diff --git a/tests/e2e/test_splunk_addon.py b/tests/e2e/test_splunk_addon.py index 0b169bd03..006e329fd 100644 --- a/tests/e2e/test_splunk_addon.py +++ b/tests/e2e/test_splunk_addon.py @@ -170,6 +170,7 @@ def empty_method(): @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.""" @@ -205,9 +206,10 @@ def empty_method(): result = testdir.runpytest( f"--splunk-version={request.config.getoption('splunk_version')}", "--splunk-type=external", - "--splunk-host=localhost", + "--splunk-host=splunk", "--splunk-port=8089", - "--splunk-forwarder-host=localhost", + "--splunk-forwarder-host=splunk", + "--splunk-hec-token=8b741d03-43e9-4164-908b-e09102327d22", "-v", "--search-interval=0", "--search-retry=0",