Hi,
Am Fr., 6. März 2020 um 01:04 Uhr schrieb Rich Megginson <
rmeggins(a)redhat.com>:
I'm trying to convert the storage role to use tox. I'm getting errors
like this running pylint and pytest:
pylint:
************* Module find_unused_disk
E: 67, 0: No name 'size' in module 'ansible.module_utils'
(no-name-in-module)
E: 67, 0: Unable to import 'ansible.module_utils.size' (import-error)
These need to be ignored AFAIU, same reason why pytest fails. Example from
network role:
https://github.com/linux-system-roles/network/blob/master/library/network...
pytest:
Traceback:
unit/test_unused_disk.py:2: in <module>
import find_unused_disk
../library/find_unused_disk.py:67: in <module>
from ansible.module_utils.size import Size
E ImportError: No module named size
How do you run pytest? Do you have to set PYTHONPATH?
Yes - or sys.path and something more. Here is what I did for the network
role:
https://github.com/linux-system-roles/network/blob/master/tests/unit/test...
The challenge is that Ansible accesses the module in a different namespace
than what is actually on the system.
I guess for the storage role it would be something like this:
import os
import sys
TESTS_BASEDIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(1, os.path.join(TESTS_BASEDIR, "../..", "library"))
sys.path.insert(1, os.path.join(TESTS_BASEDIR, "../..",
"module_utils"))
try:
from unittest import mock
except ImportError: # py2
import mock
sys.modules["ansible"] = mock.Mock()
sys.modules["ansible.module_utils.basic"] = mock.Mock()
sys.modules["ansible.module_utils"] = mock.Mock()
sys.modules["ansible.module_utils.size"] = __import__("size")
Kind regards
Till
--
Till Maas
He/His/Him
Associate Manager, Software Engineering
NetworkManager, Nmstate, Ansible RHEL Networking System Role
Red Hat GmbH,
http://www.de.redhat.com/, Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Laurie Krebs, Michael O'Neill, Thomas
Savage