Improve platform mocking

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: I5336677dd9e7fbfa50cc6507b0d5ac5ef52b0070
This commit is contained in:
Alex Kavanagh 2023-10-24 15:47:45 +01:00
parent 674d761c9a
commit 9c4f136b9d
2 changed files with 57 additions and 21 deletions

62
tox.ini
View File

@ -18,9 +18,9 @@ skip_missing_interpreters = False
# * It is necessary to declare setuptools as a dependency otherwise tox will # * It is necessary to declare setuptools as a dependency otherwise tox will
# fail very early at not being able to load it. The version pinning is in # fail very early at not being able to load it. The version pinning is in
# line with `pip.sh`. # line with `pip.sh`.
requires = pip < 20.3 requires = pip
virtualenv < 20.0 virtualenv
setuptools < 50.0.0 setuptools
# NOTE: https://wiki.canonical.com/engineering/OpenStack/InstallLatestToxOnOsci # NOTE: https://wiki.canonical.com/engineering/OpenStack/InstallLatestToxOnOsci
minversion = 3.2.0 minversion = 3.2.0
@ -35,8 +35,13 @@ allowlist_externals =
git git
bash bash
charmcraft charmcraft
rename.sh {toxinidir}/rename.sh
passenv = HOME TERM CS_* OS_* TEST_* passenv =
HOME
TERM
CS_*
OS_*
TEST_*
deps = -r{toxinidir}/test-requirements.txt deps = -r{toxinidir}/test-requirements.txt
[testenv:py35] [testenv:py35]
@ -46,46 +51,60 @@ commands = /bin/true
[testenv:py36] [testenv:py36]
basepython = python3.6 basepython = python3.6
deps = -r{toxinidir}/requirements.txt deps =
-r{toxinidir}/test-requirements.txt -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
[testenv:py37] [testenv:py37]
basepython = python3.7 basepython = python3.7
deps = -r{toxinidir}/requirements.txt deps =
-r{toxinidir}/test-requirements.txt -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
[testenv:py38] [testenv:py38]
basepython = python3.8 basepython = python3.8
deps = -r{toxinidir}/requirements.txt deps =
-r{toxinidir}/test-requirements.txt -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
[testenv:py39] [testenv:py39]
basepython = python3.9 basepython = python3.9
deps = -r{toxinidir}/requirements.txt deps =
-r{toxinidir}/test-requirements.txt -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
[testenv:py310] [testenv:py310]
basepython = python3.10 basepython = python3.10
deps = -r{toxinidir}/requirements.txt deps =
-r{toxinidir}/test-requirements.txt -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
[testenv:py311]
basepython = python3.11
deps =
-r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
[testenv:py3] [testenv:py3]
basepython = python3 basepython = python3
deps = -r{toxinidir}/requirements.txt deps =
-r{toxinidir}/test-requirements.txt -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
[testenv:pep8] [testenv:pep8]
basepython = python3 basepython = python3
deps = -r{toxinidir}/requirements.txt deps =
-r{toxinidir}/test-requirements.txt -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
commands = flake8 {posargs} src unit_tests tests commands = flake8 {posargs} src unit_tests tests
[testenv:cover] [testenv:cover]
# Technique based heavily upon # Technique based heavily upon
# https://github.com/openstack/nova/blob/master/tox.ini # https://github.com/openstack/nova/blob/master/tox.ini
basepython = python3 basepython = python3
deps = -r{toxinidir}/requirements.txt deps =
-r{toxinidir}/test-requirements.txt -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
setenv = setenv =
{[testenv]setenv} {[testenv]setenv}
PYTHON=coverage run PYTHON=coverage run
@ -124,6 +143,7 @@ commands =
charmcraft clean charmcraft clean
charmcraft -v pack charmcraft -v pack
{toxinidir}/rename.sh {toxinidir}/rename.sh
charmcraft clean
[testenv:func-noop] [testenv:func-noop]
basepython = python3 basepython = python3

View File

@ -11,3 +11,19 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# 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.
from unittest.mock import patch
patch(
'charmhelpers.osplatform.get_platform', return_value='ubuntu'
).start()
patch(
'charmhelpers.core.host.lsb_release',
return_value={
'DISTRIB_CODENAME': 'jammy'
}).start()