diff --git a/test/units/plugins/connection/test_connection.py b/test/units/plugins/connection/test_connection.py index 0705e495d7..ea5f540fcd 100644 --- a/test/units/plugins/connection/test_connection.py +++ b/test/units/plugins/connection/test_connection.py @@ -20,9 +20,12 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type from io import StringIO +import sys from ansible.compat.tests import mock from ansible.compat.tests import unittest +from ansible.compat.tests.mock import MagicMock +from ansible.compat.tests.mock import patch from ansible.errors import AnsibleError from ansible.playbook.play_context import PlayContext from ansible.plugins.connection import ConnectionBase @@ -37,9 +40,27 @@ from ansible.plugins.connection.paramiko_ssh import Connection as ParamikoConnec from ansible.plugins.connection.ssh import Connection as SSHConnection from ansible.plugins.connection.docker import Connection as DockerConnection # from ansible.plugins.connection.winrm import Connection as WinRmConnection -from ansible.plugins.connection.netconf import Connection as NetconfConnection from ansible.plugins.connection.network_cli import Connection as NetworkCliConnection +PY3 = sys.version_info[0] == 3 + +builtin_import = __import__ + +mock_ncclient = MagicMock(name='ncclient') + + +def import_mock(name, *args): + if name.startswith('ncclient'): + return mock_ncclient + return builtin_import(name, *args) + +if PY3: + with patch('builtins.__import__', side_effect=import_mock): + from ansible.plugins.connection.netconf import Connection as NetconfConnection +else: + with patch('__builtin__.__import__', side_effect=import_mock): + from ansible.plugins.connection.netconf import Connection as NetconfConnection + class TestConnectionBaseClass(unittest.TestCase):