Skip to content

Commit

Permalink
Improve platform mocking
Browse files Browse the repository at this point in the history
Patch out charmhelpers.osplatform.get_platform() and
charmhelpers.core.host.lsb_release() globally in the unit tests to
insulate the unit tests from the platform that the unit tests are being
run on.

Change-Id: Ice2a2d2379349c8cf7d77027cbca78b19e9d72d1
func-test-pr: openstack-charmers/zaza-openstack-tests#1170
  • Loading branch information
ajkavanagh committed Nov 9, 2023
1 parent 068240f commit d00d011
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
38 changes: 23 additions & 15 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ skip_missing_interpreters = False
# lead to fetching the latest pip in the func* tox targets, see
# https://stackoverflow.com/a/38133283
requires =
pip < 20.3
virtualenv < 20.0
setuptools < 50.0.0
pip
virtualenv
setuptools

# NOTE: https://wiki.canonical.com/engineering/OpenStack/InstallLatestToxOnOsci
minversion = 3.18.0
Expand All @@ -34,13 +34,16 @@ minversion = 3.18.0
setenv = VIRTUAL_ENV={envdir}
PYTHONHASHSEED=0
CHARM_DIR={envdir}
install_command =
{toxinidir}/pip.sh install {opts} {packages}
commands = stestr run --slowest {posargs}
allowlist_externals =
charmcraft
rename.sh
passenv = HOME TERM CS_* OS_* TEST_*
{toxinidir}/rename.sh
passenv =
HOME
TERM
CS_*
OS_*
TEST_*
deps = -r{toxinidir}/test-requirements.txt

[testenv:build]
Expand All @@ -50,30 +53,35 @@ commands =
charmcraft clean
charmcraft -v pack
{toxinidir}/rename.sh
charmcraft clean

[testenv:py310]
basepython = python3.10
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
deps =
-r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt

[testenv:py3]
basepython = python3
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
deps =
-r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt

[testenv:pep8]
basepython = python3
deps = flake8==3.9.2
git+https://github.com/juju/charm-tools.git
deps =
flake8==3.9.2
git+https://github.com/juju/charm-tools.git
commands = flake8 {posargs} hooks unit_tests tests actions lib files
charm-proof

[testenv:cover]
# Technique based heavily upon
# https://github.com/openstack/nova/blob/master/tox.ini
basepython = python3
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
deps =
-r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
setenv =
{[testenv]setenv}
PYTHON=coverage run
Expand Down
16 changes: 16 additions & 0 deletions unit_tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,21 @@
# limitations under the License.

import sys
from unittest.mock import patch


sys.path.append('hooks')
sys.path.append('unit_tests')


# Patch out lsb_release() and get_platform() as unit tests should be fully
# insulated from the underlying platform. Unit tests assume that the system is
# ubuntu jammy.
patch(
'charmhelpers.osplatform.get_platform', return_value='ubuntu'
).start()
patch(
'charmhelpers.core.host.lsb_release',
return_value={
'DISTRIB_CODENAME': 'jammy'
}).start()

0 comments on commit d00d011

Please sign in to comment.