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.

Copied from
https://review.opendev.org/c/openstack/charm-cinder-backup/+/899200

Change-Id: Ibb36344b1c30492b8a822fb48f753c6bdf6ca279
  • Loading branch information
brianphaley committed Mar 1, 2024
1 parent 871f551 commit 96d86a1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
45 changes: 27 additions & 18 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -44,52 +44,61 @@ commands =

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

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

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

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

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

[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
charm-tools==2.8.4
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
14 changes: 14 additions & 0 deletions unit_tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import os
import sys
from unittest.mock import patch

_path = os.path.dirname(os.path.realpath(__file__))
_actions = os.path.abspath(os.path.join(_path, '../actions'))
Expand All @@ -31,3 +32,16 @@ def _add_path(path):
_add_path(_hooks)
_add_path(_charmhelpers)
_add_path(_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 96d86a1

Please sign in to comment.