Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
40 changes: 39 additions & 1 deletion .github/workflows/build-test-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 -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
Expand All @@ -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",
Expand All @@ -170,6 +207,7 @@ jobs:
publish:
needs:
- test-splunk-external
- test-splunk-external-wrong-hec-token
- test-splunk-matrix
runs-on: ubuntu-latest
steps:
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}")
31 changes: 31 additions & 0 deletions pytest_splunk_addon/splunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -1006,6 +1007,36 @@ 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:
if os.path.exists(PYTEST_XDIST_TESTRUNUID + "_wait"):
Expand Down
9 changes: 5 additions & 4 deletions tests/e2e/test_splunk_addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -205,17 +204,19 @@ 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=localhost",
"--splunk-port=8089",
"--splunk-forwarder-host=localhost",
"-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