From 65edd5e37f53e3d1a0cc96e248417923e0e1dfb6 Mon Sep 17 00:00:00 2001 From: Rick Elrod Date: Thu, 5 Mar 2020 15:39:46 -0600 Subject: [PATCH] Split test_connection into individual files Signed-off-by: Rick Elrod --- .../plugins/connection/test_connection.py | 94 ------------------- test/units/plugins/connection/test_docker.py | 61 ++++++++++++ test/units/plugins/connection/test_httpapi.py | 41 ++++++++ test/units/plugins/connection/test_local.py | 40 ++++++++ test/units/plugins/connection/test_lxc.py | 40 ++++++++ test/units/plugins/connection/test_netconf.py | 10 ++ .../plugins/connection/test_network_cli.py | 12 +++ .../units/plugins/connection/test_paramiko.py | 42 +++++++++ test/units/plugins/connection/test_ssh.py | 9 ++ 9 files changed, 255 insertions(+), 94 deletions(-) create mode 100644 test/units/plugins/connection/test_docker.py create mode 100644 test/units/plugins/connection/test_httpapi.py create mode 100644 test/units/plugins/connection/test_local.py create mode 100644 test/units/plugins/connection/test_lxc.py create mode 100644 test/units/plugins/connection/test_paramiko.py diff --git a/test/units/plugins/connection/test_connection.py b/test/units/plugins/connection/test_connection.py index 1733bb57409..17c2e0850d9 100644 --- a/test/units/plugins/connection/test_connection.py +++ b/test/units/plugins/connection/test_connection.py @@ -31,40 +31,6 @@ from ansible.errors import AnsibleError from ansible.playbook.play_context import PlayContext from ansible.plugins.connection import ConnectionBase from ansible.plugins.loader import become_loader -# from ansible.plugins.connection.accelerate import Connection as AccelerateConnection -# from ansible.plugins.connection.chroot import Connection as ChrootConnection -# from ansible.plugins.connection.funcd import Connection as FuncdConnection -# from ansible.plugins.connection.jail import Connection as JailConnection -# from ansible.plugins.connection.libvirt_lxc import Connection as LibvirtLXCConnection -from ansible.plugins.connection.lxc import Connection as LxcConnection -from ansible.plugins.connection.local import Connection as LocalConnection -from ansible.plugins.connection.paramiko_ssh import Connection as ParamikoConnection -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.network_cli import Connection as NetworkCliConnection -from ansible.plugins.connection.httpapi import Connection as HttpapiConnection - -pytest.importorskip("ncclient") - -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): @@ -116,66 +82,6 @@ class TestConnectionBaseClass(unittest.TestCase): self.assertIsInstance(ConnectionModule3(self.play_context, self.in_stream), ConnectionModule3) -# def test_accelerate_connection_module(self): -# self.assertIsInstance(AccelerateConnection(), AccelerateConnection) -# -# def test_chroot_connection_module(self): -# self.assertIsInstance(ChrootConnection(), ChrootConnection) -# -# def test_funcd_connection_module(self): -# self.assertIsInstance(FuncdConnection(), FuncdConnection) -# -# def test_jail_connection_module(self): -# self.assertIsInstance(JailConnection(), JailConnection) -# -# def test_libvirt_lxc_connection_module(self): -# self.assertIsInstance(LibvirtLXCConnection(), LibvirtLXCConnection) - - def test_lxc_connection_module(self): - self.assertIsInstance(LxcConnection(self.play_context, self.in_stream), LxcConnection) - - def test_local_connection_module(self): - self.assertIsInstance(LocalConnection(self.play_context, self.in_stream), LocalConnection) - - def test_paramiko_connection_module(self): - self.assertIsInstance(ParamikoConnection(self.play_context, self.in_stream), ParamikoConnection) - - def test_ssh_connection_module(self): - self.assertIsInstance(SSHConnection(self.play_context, self.in_stream), SSHConnection) - - @mock.patch('ansible.plugins.connection.docker.Connection._old_docker_version', return_value=('false', 'garbage', '', 1)) - @mock.patch('ansible.plugins.connection.docker.Connection._new_docker_version', return_value=('docker version', '1.2.3', '', 0)) - def test_docker_connection_module_too_old(self, mock_new_docker_verison, mock_old_docker_version): - self.assertRaisesRegexp(AnsibleError, '^docker connection type requires docker 1.3 or higher$', - DockerConnection, self.play_context, self.in_stream, docker_command='/fake/docker') - - @mock.patch('ansible.plugins.connection.docker.Connection._old_docker_version', return_value=('false', 'garbage', '', 1)) - @mock.patch('ansible.plugins.connection.docker.Connection._new_docker_version', return_value=('docker version', '1.3.4', '', 0)) - def test_docker_connection_module(self, mock_new_docker_verison, mock_old_docker_version): - self.assertIsInstance(DockerConnection(self.play_context, self.in_stream, docker_command='/fake/docker'), - DockerConnection) - - # old version and new version fail - @mock.patch('ansible.plugins.connection.docker.Connection._old_docker_version', return_value=('false', 'garbage', '', 1)) - @mock.patch('ansible.plugins.connection.docker.Connection._new_docker_version', return_value=('false', 'garbage', '', 1)) - def test_docker_connection_module_wrong_cmd(self, mock_new_docker_version, mock_old_docker_version): - self.assertRaisesRegexp(AnsibleError, '^Docker version check (.*?) failed: ', - DockerConnection, self.play_context, self.in_stream, docker_command='/fake/docker') - -# def test_winrm_connection_module(self): -# self.assertIsInstance(WinRmConnection(), WinRmConnection) - - def test_network_cli_connection_module(self): - self.play_context.network_os = 'eos' - self.assertIsInstance(NetworkCliConnection(self.play_context, self.in_stream), NetworkCliConnection) - - def test_netconf_connection_module(self): - self.assertIsInstance(NetconfConnection(self.play_context, self.in_stream), NetconfConnection) - - def test_httpapi_connection_module(self): - self.play_context.network_os = 'eos' - self.assertIsInstance(HttpapiConnection(self.play_context, self.in_stream), HttpapiConnection) - def test_check_password_prompt(self): local = ( b'[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: \n' diff --git a/test/units/plugins/connection/test_docker.py b/test/units/plugins/connection/test_docker.py new file mode 100644 index 00000000000..d3715502141 --- /dev/null +++ b/test/units/plugins/connection/test_docker.py @@ -0,0 +1,61 @@ +# (c) 2020 Red Hat, Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from io import StringIO +import pytest + +from units.compat import mock +from units.compat import unittest +from ansible.errors import AnsibleError +from ansible.playbook.play_context import PlayContext +from ansible.plugins.connection.docker import Connection as DockerConnection + + +class TestDockerConnectionClass(unittest.TestCase): + + def setUp(self): + self.play_context = PlayContext() + self.play_context.prompt = ( + '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: ' + ) + self.in_stream = StringIO() + + def tearDown(self): + pass + + @mock.patch('ansible.plugins.connection.docker.Connection._old_docker_version', return_value=('false', 'garbage', '', 1)) + @mock.patch('ansible.plugins.connection.docker.Connection._new_docker_version', return_value=('docker version', '1.2.3', '', 0)) + def test_docker_connection_module_too_old(self, mock_new_docker_verison, mock_old_docker_version): + self.assertRaisesRegexp(AnsibleError, '^docker connection type requires docker 1.3 or higher$', + DockerConnection, self.play_context, self.in_stream, docker_command='/fake/docker') + + @mock.patch('ansible.plugins.connection.docker.Connection._old_docker_version', return_value=('false', 'garbage', '', 1)) + @mock.patch('ansible.plugins.connection.docker.Connection._new_docker_version', return_value=('docker version', '1.3.4', '', 0)) + def test_docker_connection_module(self, mock_new_docker_verison, mock_old_docker_version): + self.assertIsInstance(DockerConnection(self.play_context, self.in_stream, docker_command='/fake/docker'), + DockerConnection) + + # old version and new version fail + @mock.patch('ansible.plugins.connection.docker.Connection._old_docker_version', return_value=('false', 'garbage', '', 1)) + @mock.patch('ansible.plugins.connection.docker.Connection._new_docker_version', return_value=('false', 'garbage', '', 1)) + def test_docker_connection_module_wrong_cmd(self, mock_new_docker_version, mock_old_docker_version): + self.assertRaisesRegexp(AnsibleError, '^Docker version check (.*?) failed: ', + DockerConnection, self.play_context, self.in_stream, docker_command='/fake/docker') diff --git a/test/units/plugins/connection/test_httpapi.py b/test/units/plugins/connection/test_httpapi.py new file mode 100644 index 00000000000..efdc7c95a12 --- /dev/null +++ b/test/units/plugins/connection/test_httpapi.py @@ -0,0 +1,41 @@ +# +# (c) 2020 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from io import StringIO +import pytest + +from units.compat import unittest +from ansible.plugins.connection import httpapi +from ansible.playbook.play_context import PlayContext + + +class TestHttpApiConnectionClass(unittest.TestCase): + + def test_httpapi_connection_module(self): + play_context = PlayContext() + play_context.prompt = ( + '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: ' + ) + play_context.network_os = 'eos' + in_stream = StringIO() + + self.assertIsInstance(httpapi.Connection(play_context, in_stream), httpapi.Connection) diff --git a/test/units/plugins/connection/test_local.py b/test/units/plugins/connection/test_local.py new file mode 100644 index 00000000000..e55258550e5 --- /dev/null +++ b/test/units/plugins/connection/test_local.py @@ -0,0 +1,40 @@ +# +# (c) 2020 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from io import StringIO +import pytest + +from units.compat import unittest +from ansible.plugins.connection import local +from ansible.playbook.play_context import PlayContext + + +class TestLocalConnectionClass(unittest.TestCase): + + def test_local_connection_module(self): + play_context = PlayContext() + play_context.prompt = ( + '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: ' + ) + in_stream = StringIO() + + self.assertIsInstance(local.Connection(play_context, in_stream), local.Connection) diff --git a/test/units/plugins/connection/test_lxc.py b/test/units/plugins/connection/test_lxc.py new file mode 100644 index 00000000000..f754406c5df --- /dev/null +++ b/test/units/plugins/connection/test_lxc.py @@ -0,0 +1,40 @@ +# +# (c) 2020 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from io import StringIO +import pytest + +from units.compat import unittest +from ansible.plugins.connection import lxc +from ansible.playbook.play_context import PlayContext + + +class TestLXCConnectionClass(unittest.TestCase): + + def test_lxc_connection_module(self): + play_context = PlayContext() + play_context.prompt = ( + '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: ' + ) + in_stream = StringIO() + + self.assertIsInstance(lxc.Connection(play_context, in_stream), lxc.Connection) diff --git a/test/units/plugins/connection/test_netconf.py b/test/units/plugins/connection/test_netconf.py index d4b0556dc89..bf8eee03110 100644 --- a/test/units/plugins/connection/test_netconf.py +++ b/test/units/plugins/connection/test_netconf.py @@ -20,6 +20,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +from io import StringIO import sys import pytest @@ -54,6 +55,15 @@ else: class TestNetconfConnectionClass(unittest.TestCase): + def test_netconf_connection_module(self): + play_context = PlayContext() + play_context.prompt = ( + '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: ' + ) + in_stream = StringIO() + + self.assertIsInstance(netconf.Connection(play_context, in_stream), netconf.Connection) + def test_netconf_init(self): pc = PlayContext() conn = connection_loader.get('netconf', pc, '/dev/null') diff --git a/test/units/plugins/connection/test_network_cli.py b/test/units/plugins/connection/test_network_cli.py index e439db669ad..f675561ae98 100644 --- a/test/units/plugins/connection/test_network_cli.py +++ b/test/units/plugins/connection/test_network_cli.py @@ -20,6 +20,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +from io import StringIO import re import json @@ -29,11 +30,22 @@ from units.compat.mock import patch, MagicMock from ansible.module_utils._text import to_text from ansible.errors import AnsibleConnectionFailure from ansible.playbook.play_context import PlayContext +from ansible.plugins.connection import network_cli from ansible.plugins.loader import connection_loader class TestConnectionClass(unittest.TestCase): + def test_network_cli_connection_module(self): + play_context = PlayContext() + play_context.prompt = ( + '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: ' + ) + play_context.network_os = 'eos' + in_stream = StringIO() + + self.assertIsInstance(network_cli.Connection(play_context, in_stream), network_cli.Connection) + def test_network_cli__invalid_os(self): pc = PlayContext() pc.network_os = 'does not exist' diff --git a/test/units/plugins/connection/test_paramiko.py b/test/units/plugins/connection/test_paramiko.py new file mode 100644 index 00000000000..e3643b14837 --- /dev/null +++ b/test/units/plugins/connection/test_paramiko.py @@ -0,0 +1,42 @@ +# +# (c) 2020 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from io import StringIO +import pytest + +from units.compat import unittest +from ansible.plugins.connection import paramiko_ssh +from ansible.playbook.play_context import PlayContext + + +class TestParamikoConnectionClass(unittest.TestCase): + + def test_paramiko_connection_module(self): + play_context = PlayContext() + play_context.prompt = ( + '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: ' + ) + in_stream = StringIO() + + self.assertIsInstance( + paramiko_ssh.Connection(play_context, in_stream), + paramiko_ssh.Connection) diff --git a/test/units/plugins/connection/test_ssh.py b/test/units/plugins/connection/test_ssh.py index 9afc235a286..2a8f7b90cce 100644 --- a/test/units/plugins/connection/test_ssh.py +++ b/test/units/plugins/connection/test_ssh.py @@ -39,6 +39,15 @@ from ansible.plugins.loader import connection_loader, become_loader class TestConnectionBaseClass(unittest.TestCase): + def test_plugins_connection_ssh_module(self): + play_context = PlayContext() + play_context.prompt = ( + '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: ' + ) + in_stream = StringIO() + + self.assertIsInstance(ssh.Connection(play_context, in_stream), ssh.Connection) + def test_plugins_connection_ssh_basic(self): pc = PlayContext() new_stdin = StringIO()