From 35f79370e14b102f8d348724204d92232fcfc1ed Mon Sep 17 00:00:00 2001 From: Frederic Lepied Date: Fri, 10 Nov 2017 17:53:04 +0100 Subject: [PATCH] mock ncclient import in test_connection.py (#32786) --- .../plugins/connection/test_connection.py | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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):