Skip to content

Commit

Permalink
Merge pull request ComplianceAsCode#11146 from Honny1/unit-tests-with…
Browse files Browse the repository at this point in the history
…-tox

Running locally unit tests of `ssg` module using python2 and 3
  • Loading branch information
Mab879 authored Sep 27, 2023
2 parents 9e934b3 + 44b1209 commit c1b0203
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ ocp4/profiles/test.profile

# Ignore build profiling data
.build_profiling/

# Ignore coverage files
.coverage
coverage.xml
1 change: 0 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ macro(ssg_python_unit_tests PYTHON_COMPONENT_ID RELATIVE_PYTHONPATH)
endmacro()

if(PY_PYTEST)
ssg_python_unit_tests("build-scripts" "build-scripts")
ssg_python_unit_tests("utils" "utils")
ssg_python_unit_tests("ssg-module" ".")
ssg_python_unit_tests("ssg_test_suite" "tests")
Expand Down
4 changes: 0 additions & 4 deletions tests/unit/build-scripts/test_relabel_ids.py

This file was deleted.

3 changes: 3 additions & 0 deletions tests/unit/ssg-module/test_build_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import collections
import os
import tempfile
import sys

import yaml
import pytest
Expand Down Expand Up @@ -437,6 +438,7 @@ def rule_accounts_tmout():
return ssg.build_yaml.Rule.from_yaml(rule_file)


@pytest.mark.skipif(sys.version_info[0] < 3, reason="requires python3 or higher")
def test_rule_to_xml_element(rule_accounts_tmout):
xmldiff_main = pytest.importorskip("xmldiff.main")
rule_el = rule_accounts_tmout.to_xml_element()
Expand Down Expand Up @@ -469,6 +471,7 @@ def value_system_crypto_policy():
return ssg.build_yaml.Value.from_yaml(value_file)


@pytest.mark.skipif(sys.version_info[0] < 3, reason="requires python3 or higher")
def test_value_to_xml_element(value_system_crypto_policy):
xmldiff_main = pytest.importorskip("xmldiff.main")
value_el = value_system_crypto_policy.to_xml_element()
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/ssg-module/test_controls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import logging
import os
import sys

import ssg.controls
import ssg.build_yaml
Expand Down Expand Up @@ -98,6 +99,7 @@ def test_controls_load(controls_manager):
_load_test(controls_manager, "abcd")


@pytest.mark.skipif(sys.version_info[0] < 3, reason="requires python3 or higher")
def test_controls_invalid_rules(env_yaml):
existing_rules = {"accounts_tmout", "configure_crypto_policy"}
controls_manager = ssg.controls.ControlsManager(
Expand All @@ -107,6 +109,8 @@ def test_controls_invalid_rules(env_yaml):
assert str(exc.value) == \
"Control abcd:R1 contains nonexisting rule(s) sshd_set_idle_timeout"


@pytest.mark.skipif(sys.version_info[0] < 3, reason="requires python3 or higher")
def test_controls_levels(controls_manager):
# Default level is the lowest level
c_1 = controls_manager.get_control("abcd-levels", "S1")
Expand Down
58 changes: 37 additions & 21 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,44 +1,60 @@
[tox]
envlist = py27,py3{6,7,8,9,10,11},flake8,docs
envlist = coverage_clean, py27, py3{6, 8, 9}, py3, coverage_report, flake8
skip_missing_interpreters = true
isolated_build = true
skipsdist = true

[flake8]
max-line-length = 99

# Unit tests
[testenv]
setenv =
tests_dir = {toxinidir}{/}tests/unit
PYTHONPATH = {toxinidir}
commands =
python -m pytest --cov-append --cov-report=xml --cov=ssg "{env:tests_dir}{/}ssg-module"

deps =
pyyaml
Jinja2
-r test-requirements.txt

# Coverage
[testenv:coverage_report]
deps = coverage
skip_install = true
install_command = pip install {opts} {packages}
commands =
coverage html
coverage xml
coverage report -m

[testenv:coverage_clean]
deps = coverage
skip_install = true
commands = coverage erase


# Code style
[flake8]
max-line-length = 99
per-file-ignores =__init__.py:F401

[testenv:flake8]
basepython = python3
commands =
flake8 ssg utils
flake8 ssg utils tests build-scripts
deps =
flake8

# Documentation
[testenv:docs]
skip_install=false
skipsdist=true
basepython = python3
allowlist_externals =
make
rm
deps =
-r docs/requirements.txt
commands_pre =
make -C docs clean
rm -rf docs/api
commands =
make -C docs html
commands_post =
make -C docs linkcheck

[testenv:clean]
skip_install = true
allowlist_externals =
bash

deps =
pip>=21.1

commands =
bash -c 'make -C docs/ clean'
bash -c 'rm -rf dist/ build/* docs/api/'

0 comments on commit c1b0203

Please sign in to comment.