Browse code

Split test_connection into individual files

Signed-off-by: Rick Elrod <rick@elrod.me>

Rick Elrod authored on 2020/03/06 06:39:46
Showing 9 changed files
... ...
@@ -31,40 +31,6 @@ from ansible.errors import AnsibleError
31 31
 from ansible.playbook.play_context import PlayContext
32 32
 from ansible.plugins.connection import ConnectionBase
33 33
 from ansible.plugins.loader import become_loader
34
-# from ansible.plugins.connection.accelerate import Connection as AccelerateConnection
35
-# from ansible.plugins.connection.chroot import Connection as ChrootConnection
36
-# from ansible.plugins.connection.funcd import Connection as FuncdConnection
37
-# from ansible.plugins.connection.jail import Connection as JailConnection
38
-# from ansible.plugins.connection.libvirt_lxc import Connection as LibvirtLXCConnection
39
-from ansible.plugins.connection.lxc import Connection as LxcConnection
40
-from ansible.plugins.connection.local import Connection as LocalConnection
41
-from ansible.plugins.connection.paramiko_ssh import Connection as ParamikoConnection
42
-from ansible.plugins.connection.ssh import Connection as SSHConnection
43
-from ansible.plugins.connection.docker import Connection as DockerConnection
44
-# from ansible.plugins.connection.winrm import Connection as WinRmConnection
45
-from ansible.plugins.connection.network_cli import Connection as NetworkCliConnection
46
-from ansible.plugins.connection.httpapi import Connection as HttpapiConnection
47
-
48
-pytest.importorskip("ncclient")
49
-
50
-PY3 = sys.version_info[0] == 3
51
-builtin_import = __import__
52
-
53
-mock_ncclient = MagicMock(name='ncclient')
54
-
55
-
56
-def import_mock(name, *args):
57
-    if name.startswith('ncclient'):
58
-        return mock_ncclient
59
-    return builtin_import(name, *args)
60
-
61
-
62
-if PY3:
63
-    with patch('builtins.__import__', side_effect=import_mock):
64
-        from ansible.plugins.connection.netconf import Connection as NetconfConnection
65
-else:
66
-    with patch('__builtin__.__import__', side_effect=import_mock):
67
-        from ansible.plugins.connection.netconf import Connection as NetconfConnection
68 34
 
69 35
 
70 36
 class TestConnectionBaseClass(unittest.TestCase):
... ...
@@ -116,66 +82,6 @@ class TestConnectionBaseClass(unittest.TestCase):
116 116
 
117 117
         self.assertIsInstance(ConnectionModule3(self.play_context, self.in_stream), ConnectionModule3)
118 118
 
119
-#    def test_accelerate_connection_module(self):
120
-#        self.assertIsInstance(AccelerateConnection(), AccelerateConnection)
121
-#
122
-#    def test_chroot_connection_module(self):
123
-#        self.assertIsInstance(ChrootConnection(), ChrootConnection)
124
-#
125
-#    def test_funcd_connection_module(self):
126
-#        self.assertIsInstance(FuncdConnection(), FuncdConnection)
127
-#
128
-#    def test_jail_connection_module(self):
129
-#        self.assertIsInstance(JailConnection(), JailConnection)
130
-#
131
-#    def test_libvirt_lxc_connection_module(self):
132
-#        self.assertIsInstance(LibvirtLXCConnection(), LibvirtLXCConnection)
133
-
134
-    def test_lxc_connection_module(self):
135
-        self.assertIsInstance(LxcConnection(self.play_context, self.in_stream), LxcConnection)
136
-
137
-    def test_local_connection_module(self):
138
-        self.assertIsInstance(LocalConnection(self.play_context, self.in_stream), LocalConnection)
139
-
140
-    def test_paramiko_connection_module(self):
141
-        self.assertIsInstance(ParamikoConnection(self.play_context, self.in_stream), ParamikoConnection)
142
-
143
-    def test_ssh_connection_module(self):
144
-        self.assertIsInstance(SSHConnection(self.play_context, self.in_stream), SSHConnection)
145
-
146
-    @mock.patch('ansible.plugins.connection.docker.Connection._old_docker_version', return_value=('false', 'garbage', '', 1))
147
-    @mock.patch('ansible.plugins.connection.docker.Connection._new_docker_version', return_value=('docker version', '1.2.3', '', 0))
148
-    def test_docker_connection_module_too_old(self, mock_new_docker_verison, mock_old_docker_version):
149
-        self.assertRaisesRegexp(AnsibleError, '^docker connection type requires docker 1.3 or higher$',
150
-                                DockerConnection, self.play_context, self.in_stream, docker_command='/fake/docker')
151
-
152
-    @mock.patch('ansible.plugins.connection.docker.Connection._old_docker_version', return_value=('false', 'garbage', '', 1))
153
-    @mock.patch('ansible.plugins.connection.docker.Connection._new_docker_version', return_value=('docker version', '1.3.4', '', 0))
154
-    def test_docker_connection_module(self, mock_new_docker_verison, mock_old_docker_version):
155
-        self.assertIsInstance(DockerConnection(self.play_context, self.in_stream, docker_command='/fake/docker'),
156
-                              DockerConnection)
157
-
158
-    # old version and new version fail
159
-    @mock.patch('ansible.plugins.connection.docker.Connection._old_docker_version', return_value=('false', 'garbage', '', 1))
160
-    @mock.patch('ansible.plugins.connection.docker.Connection._new_docker_version', return_value=('false', 'garbage', '', 1))
161
-    def test_docker_connection_module_wrong_cmd(self, mock_new_docker_version, mock_old_docker_version):
162
-        self.assertRaisesRegexp(AnsibleError, '^Docker version check (.*?) failed: ',
163
-                                DockerConnection, self.play_context, self.in_stream, docker_command='/fake/docker')
164
-
165
-#    def test_winrm_connection_module(self):
166
-#        self.assertIsInstance(WinRmConnection(), WinRmConnection)
167
-
168
-    def test_network_cli_connection_module(self):
169
-        self.play_context.network_os = 'eos'
170
-        self.assertIsInstance(NetworkCliConnection(self.play_context, self.in_stream), NetworkCliConnection)
171
-
172
-    def test_netconf_connection_module(self):
173
-        self.assertIsInstance(NetconfConnection(self.play_context, self.in_stream), NetconfConnection)
174
-
175
-    def test_httpapi_connection_module(self):
176
-        self.play_context.network_os = 'eos'
177
-        self.assertIsInstance(HttpapiConnection(self.play_context, self.in_stream), HttpapiConnection)
178
-
179 119
     def test_check_password_prompt(self):
180 120
         local = (
181 121
             b'[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: \n'
182 122
new file mode 100644
... ...
@@ -0,0 +1,61 @@
0
+# (c) 2020 Red Hat, Inc.
1
+#
2
+# This file is part of Ansible
3
+#
4
+# Ansible is free software: you can redistribute it and/or modify
5
+# it under the terms of the GNU General Public License as published by
6
+# the Free Software Foundation, either version 3 of the License, or
7
+# (at your option) any later version.
8
+#
9
+# Ansible is distributed in the hope that it will be useful,
10
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
+# GNU General Public License for more details.
13
+#
14
+# You should have received a copy of the GNU General Public License
15
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
16
+
17
+# Make coding more python3-ish
18
+from __future__ import (absolute_import, division, print_function)
19
+__metaclass__ = type
20
+
21
+from io import StringIO
22
+import pytest
23
+
24
+from units.compat import mock
25
+from units.compat import unittest
26
+from ansible.errors import AnsibleError
27
+from ansible.playbook.play_context import PlayContext
28
+from ansible.plugins.connection.docker import Connection as DockerConnection
29
+
30
+
31
+class TestDockerConnectionClass(unittest.TestCase):
32
+
33
+    def setUp(self):
34
+        self.play_context = PlayContext()
35
+        self.play_context.prompt = (
36
+            '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: '
37
+        )
38
+        self.in_stream = StringIO()
39
+
40
+    def tearDown(self):
41
+        pass
42
+
43
+    @mock.patch('ansible.plugins.connection.docker.Connection._old_docker_version', return_value=('false', 'garbage', '', 1))
44
+    @mock.patch('ansible.plugins.connection.docker.Connection._new_docker_version', return_value=('docker version', '1.2.3', '', 0))
45
+    def test_docker_connection_module_too_old(self, mock_new_docker_verison, mock_old_docker_version):
46
+        self.assertRaisesRegexp(AnsibleError, '^docker connection type requires docker 1.3 or higher$',
47
+                                DockerConnection, self.play_context, self.in_stream, docker_command='/fake/docker')
48
+
49
+    @mock.patch('ansible.plugins.connection.docker.Connection._old_docker_version', return_value=('false', 'garbage', '', 1))
50
+    @mock.patch('ansible.plugins.connection.docker.Connection._new_docker_version', return_value=('docker version', '1.3.4', '', 0))
51
+    def test_docker_connection_module(self, mock_new_docker_verison, mock_old_docker_version):
52
+        self.assertIsInstance(DockerConnection(self.play_context, self.in_stream, docker_command='/fake/docker'),
53
+                              DockerConnection)
54
+
55
+    # old version and new version fail
56
+    @mock.patch('ansible.plugins.connection.docker.Connection._old_docker_version', return_value=('false', 'garbage', '', 1))
57
+    @mock.patch('ansible.plugins.connection.docker.Connection._new_docker_version', return_value=('false', 'garbage', '', 1))
58
+    def test_docker_connection_module_wrong_cmd(self, mock_new_docker_version, mock_old_docker_version):
59
+        self.assertRaisesRegexp(AnsibleError, '^Docker version check (.*?) failed: ',
60
+                                DockerConnection, self.play_context, self.in_stream, docker_command='/fake/docker')
0 61
new file mode 100644
... ...
@@ -0,0 +1,41 @@
0
+#
1
+# (c) 2020 Red Hat Inc.
2
+#
3
+# This file is part of Ansible
4
+#
5
+# Ansible is free software: you can redistribute it and/or modify
6
+# it under the terms of the GNU General Public License as published by
7
+# the Free Software Foundation, either version 3 of the License, or
8
+# (at your option) any later version.
9
+#
10
+# Ansible is distributed in the hope that it will be useful,
11
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+# GNU General Public License for more details.
14
+#
15
+# You should have received a copy of the GNU General Public License
16
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
17
+
18
+# Make coding more python3-ish
19
+from __future__ import (absolute_import, division, print_function)
20
+__metaclass__ = type
21
+
22
+from io import StringIO
23
+import pytest
24
+
25
+from units.compat import unittest
26
+from ansible.plugins.connection import httpapi
27
+from ansible.playbook.play_context import PlayContext
28
+
29
+
30
+class TestHttpApiConnectionClass(unittest.TestCase):
31
+
32
+    def test_httpapi_connection_module(self):
33
+        play_context = PlayContext()
34
+        play_context.prompt = (
35
+            '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: '
36
+        )
37
+        play_context.network_os = 'eos'
38
+        in_stream = StringIO()
39
+
40
+        self.assertIsInstance(httpapi.Connection(play_context, in_stream), httpapi.Connection)
0 41
new file mode 100644
... ...
@@ -0,0 +1,40 @@
0
+#
1
+# (c) 2020 Red Hat Inc.
2
+#
3
+# This file is part of Ansible
4
+#
5
+# Ansible is free software: you can redistribute it and/or modify
6
+# it under the terms of the GNU General Public License as published by
7
+# the Free Software Foundation, either version 3 of the License, or
8
+# (at your option) any later version.
9
+#
10
+# Ansible is distributed in the hope that it will be useful,
11
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+# GNU General Public License for more details.
14
+#
15
+# You should have received a copy of the GNU General Public License
16
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
17
+
18
+# Make coding more python3-ish
19
+from __future__ import (absolute_import, division, print_function)
20
+__metaclass__ = type
21
+
22
+from io import StringIO
23
+import pytest
24
+
25
+from units.compat import unittest
26
+from ansible.plugins.connection import local
27
+from ansible.playbook.play_context import PlayContext
28
+
29
+
30
+class TestLocalConnectionClass(unittest.TestCase):
31
+
32
+    def test_local_connection_module(self):
33
+        play_context = PlayContext()
34
+        play_context.prompt = (
35
+            '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: '
36
+        )
37
+        in_stream = StringIO()
38
+
39
+        self.assertIsInstance(local.Connection(play_context, in_stream), local.Connection)
0 40
new file mode 100644
... ...
@@ -0,0 +1,40 @@
0
+#
1
+# (c) 2020 Red Hat Inc.
2
+#
3
+# This file is part of Ansible
4
+#
5
+# Ansible is free software: you can redistribute it and/or modify
6
+# it under the terms of the GNU General Public License as published by
7
+# the Free Software Foundation, either version 3 of the License, or
8
+# (at your option) any later version.
9
+#
10
+# Ansible is distributed in the hope that it will be useful,
11
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+# GNU General Public License for more details.
14
+#
15
+# You should have received a copy of the GNU General Public License
16
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
17
+
18
+# Make coding more python3-ish
19
+from __future__ import (absolute_import, division, print_function)
20
+__metaclass__ = type
21
+
22
+from io import StringIO
23
+import pytest
24
+
25
+from units.compat import unittest
26
+from ansible.plugins.connection import lxc
27
+from ansible.playbook.play_context import PlayContext
28
+
29
+
30
+class TestLXCConnectionClass(unittest.TestCase):
31
+
32
+    def test_lxc_connection_module(self):
33
+        play_context = PlayContext()
34
+        play_context.prompt = (
35
+            '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: '
36
+        )
37
+        in_stream = StringIO()
38
+
39
+        self.assertIsInstance(lxc.Connection(play_context, in_stream), lxc.Connection)
... ...
@@ -20,6 +20,7 @@
20 20
 from __future__ import (absolute_import, division, print_function)
21 21
 __metaclass__ = type
22 22
 
23
+from io import StringIO
23 24
 import sys
24 25
 import pytest
25 26
 
... ...
@@ -54,6 +55,15 @@ else:
54 54
 
55 55
 class TestNetconfConnectionClass(unittest.TestCase):
56 56
 
57
+    def test_netconf_connection_module(self):
58
+        play_context = PlayContext()
59
+        play_context.prompt = (
60
+            '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: '
61
+        )
62
+        in_stream = StringIO()
63
+
64
+        self.assertIsInstance(netconf.Connection(play_context, in_stream), netconf.Connection)
65
+
57 66
     def test_netconf_init(self):
58 67
         pc = PlayContext()
59 68
         conn = connection_loader.get('netconf', pc, '/dev/null')
... ...
@@ -20,6 +20,7 @@
20 20
 from __future__ import (absolute_import, division, print_function)
21 21
 __metaclass__ = type
22 22
 
23
+from io import StringIO
23 24
 import re
24 25
 import json
25 26
 
... ...
@@ -29,11 +30,22 @@ from units.compat.mock import patch, MagicMock
29 29
 from ansible.module_utils._text import to_text
30 30
 from ansible.errors import AnsibleConnectionFailure
31 31
 from ansible.playbook.play_context import PlayContext
32
+from ansible.plugins.connection import network_cli
32 33
 from ansible.plugins.loader import connection_loader
33 34
 
34 35
 
35 36
 class TestConnectionClass(unittest.TestCase):
36 37
 
38
+    def test_network_cli_connection_module(self):
39
+        play_context = PlayContext()
40
+        play_context.prompt = (
41
+            '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: '
42
+        )
43
+        play_context.network_os = 'eos'
44
+        in_stream = StringIO()
45
+
46
+        self.assertIsInstance(network_cli.Connection(play_context, in_stream), network_cli.Connection)
47
+
37 48
     def test_network_cli__invalid_os(self):
38 49
         pc = PlayContext()
39 50
         pc.network_os = 'does not exist'
40 51
new file mode 100644
... ...
@@ -0,0 +1,42 @@
0
+#
1
+# (c) 2020 Red Hat Inc.
2
+#
3
+# This file is part of Ansible
4
+#
5
+# Ansible is free software: you can redistribute it and/or modify
6
+# it under the terms of the GNU General Public License as published by
7
+# the Free Software Foundation, either version 3 of the License, or
8
+# (at your option) any later version.
9
+#
10
+# Ansible is distributed in the hope that it will be useful,
11
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+# GNU General Public License for more details.
14
+#
15
+# You should have received a copy of the GNU General Public License
16
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
17
+
18
+# Make coding more python3-ish
19
+from __future__ import (absolute_import, division, print_function)
20
+__metaclass__ = type
21
+
22
+from io import StringIO
23
+import pytest
24
+
25
+from units.compat import unittest
26
+from ansible.plugins.connection import paramiko_ssh
27
+from ansible.playbook.play_context import PlayContext
28
+
29
+
30
+class TestParamikoConnectionClass(unittest.TestCase):
31
+
32
+    def test_paramiko_connection_module(self):
33
+        play_context = PlayContext()
34
+        play_context.prompt = (
35
+            '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: '
36
+        )
37
+        in_stream = StringIO()
38
+
39
+        self.assertIsInstance(
40
+            paramiko_ssh.Connection(play_context, in_stream),
41
+            paramiko_ssh.Connection)
... ...
@@ -39,6 +39,15 @@ from ansible.plugins.loader import connection_loader, become_loader
39 39
 
40 40
 class TestConnectionBaseClass(unittest.TestCase):
41 41
 
42
+    def test_plugins_connection_ssh_module(self):
43
+        play_context = PlayContext()
44
+        play_context.prompt = (
45
+            '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: '
46
+        )
47
+        in_stream = StringIO()
48
+
49
+        self.assertIsInstance(ssh.Connection(play_context, in_stream), ssh.Connection)
50
+
42 51
     def test_plugins_connection_ssh_basic(self):
43 52
         pc = PlayContext()
44 53
         new_stdin = StringIO()