| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# (c) 2012-2014, Michael DeHaan <[email protected]> |
| 3 |
# (c) 2016 Toshio Kuratomi <[email protected]> |
| 4 |
# (c) 2017 Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import builtins |
| 10 |
import sys |
| 11 |
import unittest |
| 12 |
|
| 13 |
from unittest.mock import patch |
| 14 |
|
| 15 |
realimport = builtins.__import__ |
| 16 |
|
| 17 |
|
| 18 |
class TestImports(unittest.TestCase): |
| 19 |
|
| 20 |
def clear_modules(self, mods): |
| 21 |
for mod in mods: |
| 22 |
if mod in sys.modules: |
| 23 |
del sys.modules[mod] |
| 24 |
|
| 25 |
def test_module_utils_basic_import_syslog(self): |
| 26 |
present = True |
| 27 |
|
| 28 |
def _mock_import(name, *args, **kwargs): |
| 29 |
if name == 'syslog': |
| 30 |
if present: |
| 31 |
return unittest.mock.MagicMock() |
| 32 |
raise ImportError |
| 33 |
return realimport(name, *args, **kwargs) |
| 34 |
|
| 35 |
with patch.object(builtins, '__import__', _mock_import): |
| 36 |
self.clear_modules(['syslog', 'ansible.module_utils._internal._logging', 'ansible.module_utils.basic']) |
| 37 |
mod = builtins.__import__('ansible.module_utils._internal._logging') |
| 38 |
self.assertTrue(mod.module_utils._internal._logging.HAS_SYSLOG) |
| 39 |
|
| 40 |
present = False |
| 41 |
|
| 42 |
self.clear_modules(['syslog', 'ansible.module_utils._internal._logging', 'ansible.module_utils.basic']) |
| 43 |
mod = builtins.__import__('ansible.module_utils._internal._logging') |
| 44 |
self.assertFalse(mod.module_utils._internal._logging.HAS_SYSLOG) |
| 45 |
|
| 46 |
self.clear_modules(['syslog', 'ansible.module_utils._internal._logging', 'ansible.module_utils.basic']) |
| 47 |
|
| 48 |
def test_module_utils_basic_import_selinux(self): |
| 49 |
present = True |
| 50 |
|
| 51 |
def _mock_import(name, globals=None, locals=None, fromlist=tuple(), level=0, **kwargs): |
| 52 |
if name == 'ansible.module_utils.compat' and fromlist == ('selinux',): |
| 53 |
if present: |
| 54 |
return unittest.mock.MagicMock() |
| 55 |
raise ImportError |
| 56 |
return realimport(name, globals=globals, locals=locals, fromlist=fromlist, level=level, **kwargs) |
| 57 |
|
| 58 |
with patch.object(builtins, '__import__', _mock_import): |
| 59 |
self.clear_modules(['ansible.module_utils.compat.selinux', 'ansible.module_utils.basic']) |
| 60 |
mod = builtins.__import__('ansible.module_utils.basic') |
| 61 |
self.assertTrue(mod.module_utils.basic.HAVE_SELINUX) |
| 62 |
|
| 63 |
present = False |
| 64 |
|
| 65 |
self.clear_modules(['ansible.module_utils.compat.selinux', 'ansible.module_utils.basic']) |
| 66 |
mod = builtins.__import__('ansible.module_utils.basic') |
| 67 |
self.assertFalse(mod.module_utils.basic.HAVE_SELINUX) |
| 68 |
|
| 69 |
self.clear_modules(['ansible.module_utils.compat.selinux', 'ansible.module_utils.basic']) |
| 70 |
|
| 71 |
# FIXME: doesn't work yet |
| 72 |
# @patch.object(builtins, 'bytes') |
| 73 |
# def test_module_utils_basic_bytes(self, mock_bytes): |
| 74 |
# mock_bytes.side_effect = NameError() |
| 75 |
# from ansible.module_utils import basic |
| 76 |
|
| 77 |
def test_module_utils_basic_import_systemd_journal(self): |
| 78 |
present = True |
| 79 |
|
| 80 |
def _mock_import(name, *args, **kwargs): |
| 81 |
try: |
| 82 |
fromlist = kwargs.get('fromlist', args[2]) |
| 83 |
except IndexError: |
| 84 |
fromlist = [] |
| 85 |
if name == 'systemd' and 'journal' in fromlist: |
| 86 |
if present: |
| 87 |
return unittest.mock.MagicMock() |
| 88 |
raise ImportError |
| 89 |
return realimport(name, *args, **kwargs) |
| 90 |
|
| 91 |
with patch.object(builtins, '__import__', _mock_import): |
| 92 |
self.clear_modules(['systemd', 'ansible.module_utils._internal._logging', 'ansible.module_utils.basic']) |
| 93 |
mod = builtins.__import__('ansible.module_utils._internal._logging') |
| 94 |
self.assertTrue(mod.module_utils._internal._logging.has_journal) |
| 95 |
|
| 96 |
present = False |
| 97 |
|
| 98 |
self.clear_modules(['systemd', 'ansible.module_utils._internal._logging', 'ansible.module_utils.basic']) |
| 99 |
mod = builtins.__import__('ansible.module_utils._internal._logging') |
| 100 |
self.assertFalse(mod.module_utils._internal._logging.has_journal) |
| 101 |
|
| 102 |
self.clear_modules(['systemd', 'ansible.module_utils._internal._logging', 'ansible.module_utils.basic']) |