Skip to content

Commit cc26c4b

Browse files
Feature/unit tests/infra 29960 (#268)
Initial unit tests
1 parent c72c9f6 commit cc26c4b

22 files changed

+2507
-5
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Python Code Style Check
2+
3+
on: [push]
4+
5+
jobs:
6+
style-check:
7+
runs-on: ubuntu-latest
8+
container: python:latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- run: |
12+
pip install black
13+
black --check ./tests/unit
14+
name: Install and run black style checker

.github/workflows/test_matrix.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
- name: Test with pytest
3434
run: |
3535
export PATH="~/.pyenv/bin:$PATH"
36-
eval "$(pyenv init -)"
36+
eval "$(pyenv init -)"
3737
source ~/.poetry/env
3838
poetry install -E docker
3939
poetry run pytest -v --splunk-version=${{ matrix.splunk-version }} -m docker

.github/workflows/unit_tests.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Unit tests
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
fail-fast: false
10+
11+
steps:
12+
- uses: actions/checkout@v2
13+
with:
14+
submodules: true
15+
- name: Set up Python
16+
uses: actions/setup-python@v1
17+
with:
18+
python-version: 3.7
19+
- name: Install dependencies
20+
run: |
21+
sudo apt-get install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
22+
curl https://pyenv.run | bash
23+
export PATH="~/.pyenv/bin:$PATH"
24+
eval "$(pyenv init -)"
25+
pyenv install 3.7.8
26+
pyenv local 3.7.8
27+
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
28+
source ~/.poetry/env
29+
- name: Test with pytest
30+
run: |
31+
export PATH="~/.pyenv/bin:$PATH"
32+
eval "$(pyenv init -)"
33+
source ~/.poetry/env
34+
poetry install
35+
poetry run coverage run --source=./pytest_splunk_addon/standard_lib -m pytest -v tests/unit
36+
poetry run coverage html
37+
- name: Archive test coverage results
38+
uses: actions/upload-artifact@v2
39+
with:
40+
name: code-coverage-report-unit-tests
41+
path: htmlcov

poetry.lock

Lines changed: 136 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ poetry-dynamic-versioning = "0.12.1"
3939
sphinx_rtd_theme = "0.5.0"
4040
sphinx-panels = "*"
4141
lovely-pytest-docker = "*"
42+
pytest-mock = "^3.5.1"
43+
pytest-cov = "^2.11.1"
44+
requests-mock = "^1.8.0"
45+
recordtype = "^1.3"
4246

4347
[tool.poetry.plugins]
4448
pytest11 = { plugin = "pytest_splunk_addon.plugin", "splunk" = "pytest_splunk_addon.splunk" }
@@ -53,4 +57,4 @@ enable = true
5357

5458
[build-system]
5559
requires = ["poetry>=1.0.2", "poetry-dynamic-versioning"]
56-
build-backend = "poetry.masonry.api"
60+
build-backend = "poetry.masonry.api"

pytest_splunk_addon/standard_lib/sample_generation/sample_event.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,6 @@ def update_metadata(self, event, metadata, key_fields):
301301

302302
return event, metadata, key_fields
303303

304-
except IndexError as error:
304+
except KeyError as error:
305305
LOGGER.error(f"Unexpected data found. Error: {error}")
306-
raise Exception(error)
306+
raise error

tests/unit/pytest.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pytest]
2+
addopts = -v --tb=long --log-level=INFO
3+
# --force-flaky --max-runs=3 --min-passes=1
4+
filterwarnings =
5+
ignore::DeprecationWarning
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pytest
2+
from collections import namedtuple
3+
from unittest.mock import Mock
4+
5+
6+
@pytest.fixture
7+
def parser(configuration_file):
8+
def create_parser(
9+
parser_class, func_to_be_mocked, parsed_output, headers=None, props_conf=None
10+
):
11+
headers = headers if headers else []
12+
FakeApp = Mock()
13+
attrs = {
14+
"{}.return_value".format(func_to_be_mocked): configuration_file(
15+
headers=headers, sects=parsed_output, errors=[]
16+
)
17+
}
18+
FakeApp.configure_mock(**attrs)
19+
if props_conf is not None:
20+
FakeApp.props_conf.return_value = props_conf
21+
return parser_class("fake_path", FakeApp)
22+
23+
return create_parser
24+
25+
26+
@pytest.fixture
27+
def configuration_file():
28+
def func(headers, sects, errors):
29+
ConfigurationFile = namedtuple(
30+
"ConfigurationFile", ["headers", "sects", "errors"]
31+
)
32+
return ConfigurationFile(headers, sects, errors)
33+
34+
return func
35+
36+
37+
@pytest.fixture(scope="session")
38+
def build_parsed_output():
39+
def parsed_output(output_elements):
40+
"""
41+
builds expected parser output from provided dict
42+
:param output_elements: dictionary with {stanza: {option: value, ...}, ...}
43+
:return: parsed_output
44+
"""
45+
parsed_output = {}
46+
for stanza, stanza_value in output_elements.items():
47+
fake_section = Mock()
48+
fake_section.options = {}
49+
fake_section.name = stanza
50+
parsed_output.update({stanza: fake_section})
51+
for option, value in stanza_value.items():
52+
fake_setting = Mock()
53+
fake_setting.name = option
54+
fake_setting.value = value
55+
parsed_output[stanza].options.update({option: fake_setting})
56+
return parsed_output
57+
58+
return parsed_output

0 commit comments

Comments
 (0)