2015-04-16 01:33:57 +02:00
|
|
|
# (c) 2015, Toshio Kuratomi <tkuratomi@ansible.com>
|
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2016-02-27 01:42:18 +01:00
|
|
|
from io import StringIO
|
2015-04-27 19:50:08 +02:00
|
|
|
|
2016-08-22 16:39:38 +02:00
|
|
|
import mock
|
|
|
|
|
2015-04-16 01:33:57 +02:00
|
|
|
from ansible.compat.tests import unittest
|
2016-08-22 16:39:38 +02:00
|
|
|
from ansible.errors import AnsibleError
|
2015-07-21 18:12:22 +02:00
|
|
|
from ansible.playbook.play_context import PlayContext
|
2015-04-16 01:33:57 +02:00
|
|
|
|
2015-09-15 17:57:54 +02:00
|
|
|
from ansible.plugins.connection import ConnectionBase
|
|
|
|
#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
|
2015-11-13 01:28:17 +01:00
|
|
|
from ansible.plugins.connection.lxc import Connection as LxcConnection
|
2015-09-15 17:57:54 +02:00
|
|
|
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
|
2016-08-22 16:39:38 +02:00
|
|
|
from ansible.plugins.connection.docker import Connection as DockerConnection
|
2015-09-15 17:57:54 +02:00
|
|
|
#from ansible.plugins.connection.winrm import Connection as WinRmConnection
|
2015-04-16 01:33:57 +02:00
|
|
|
|
2016-08-22 16:39:38 +02:00
|
|
|
|
2015-04-16 01:33:57 +02:00
|
|
|
class TestConnectionBaseClass(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2015-07-21 18:12:22 +02:00
|
|
|
self.play_context = PlayContext()
|
2015-04-27 19:50:08 +02:00
|
|
|
self.in_stream = StringIO()
|
2015-04-16 01:33:57 +02:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_subclass_error(self):
|
|
|
|
class ConnectionModule1(ConnectionBase):
|
|
|
|
pass
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
ConnectionModule1()
|
|
|
|
|
|
|
|
class ConnectionModule2(ConnectionBase):
|
|
|
|
def get(self, key):
|
|
|
|
super(ConnectionModule2, self).get(key)
|
|
|
|
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
ConnectionModule2()
|
|
|
|
|
|
|
|
def test_subclass_success(self):
|
|
|
|
class ConnectionModule3(ConnectionBase):
|
|
|
|
@property
|
|
|
|
def transport(self):
|
|
|
|
pass
|
|
|
|
def _connect(self):
|
|
|
|
pass
|
|
|
|
def exec_command(self):
|
|
|
|
pass
|
|
|
|
def put_file(self):
|
|
|
|
pass
|
|
|
|
def fetch_file(self):
|
|
|
|
pass
|
|
|
|
def close(self):
|
|
|
|
pass
|
2015-07-21 18:12:22 +02:00
|
|
|
self.assertIsInstance(ConnectionModule3(self.play_context, self.in_stream), ConnectionModule3)
|
2015-04-16 01:33:57 +02:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2015-11-13 01:28:17 +01:00
|
|
|
def test_lxc_connection_module(self):
|
|
|
|
self.assertIsInstance(LxcConnection(self.play_context, self.in_stream), LxcConnection)
|
|
|
|
|
2015-04-16 01:33:57 +02:00
|
|
|
def test_local_connection_module(self):
|
2015-07-21 18:12:22 +02:00
|
|
|
self.assertIsInstance(LocalConnection(self.play_context, self.in_stream), LocalConnection)
|
2015-04-16 01:33:57 +02:00
|
|
|
|
2015-04-27 19:50:08 +02:00
|
|
|
def test_paramiko_connection_module(self):
|
2015-07-21 18:12:22 +02:00
|
|
|
self.assertIsInstance(ParamikoConnection(self.play_context, self.in_stream), ParamikoConnection)
|
2015-04-16 01:33:57 +02:00
|
|
|
|
|
|
|
def test_ssh_connection_module(self):
|
2015-07-21 18:12:22 +02:00
|
|
|
self.assertIsInstance(SSHConnection(self.play_context, self.in_stream), SSHConnection)
|
2015-04-16 01:33:57 +02:00
|
|
|
|
2016-08-22 16:39:38 +02:00
|
|
|
@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.assertRaises(AnsibleError, DockerConnection, self.play_context, self.in_stream)
|
|
|
|
|
|
|
|
@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), 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.assertRaises(AnsibleError, DockerConnection, self.play_context, self.in_stream)
|
|
|
|
|
2015-04-16 01:33:57 +02:00
|
|
|
# def test_winrm_connection_module(self):
|
|
|
|
# self.assertIsInstance(WinRmConnection(), WinRmConnection)
|