diff options
Diffstat (limited to 'contrib/python/test')
-rw-r--r-- | contrib/python/test/test_client.py | 37 | ||||
-rw-r--r-- | contrib/python/test/test_system.py | 4 | ||||
-rw-r--r-- | contrib/python/test/test_tunnel.py | 79 |
3 files changed, 118 insertions, 2 deletions
diff --git a/contrib/python/test/test_client.py b/contrib/python/test/test_client.py new file mode 100644 index 000000000..e642c8add --- /dev/null +++ b/contrib/python/test/test_client.py @@ -0,0 +1,37 @@ +from __future__ import absolute_import + +import unittest +from unittest.mock import patch + +import podman +from podman.client import BaseClient, Client, LocalClient, RemoteClient + + +class TestClient(unittest.TestCase): + def setUp(self): + pass + + @patch('podman.libs.system.System.ping', return_value=True) + def test_local(self, mock_ping): + p = Client( + uri='unix:/run/podman', + interface='io.projectatomic.podman', + ) + + self.assertIsInstance(p._client, LocalClient) + self.assertIsInstance(p._client, BaseClient) + + mock_ping.assert_called_once() + + @patch('os.path.isfile', return_value=True) + @patch('podman.libs.system.System.ping', return_value=True) + def test_remote(self, mock_ping, mock_isfile): + p = Client( + uri='unix:/run/podman', + interface='io.projectatomic.podman', + remote_uri='ssh://user@hostname/run/podmain/podman', + identity_file='~/.ssh/id_rsa') + + self.assertIsInstance(p._client, BaseClient) + mock_ping.assert_called_once() + mock_isfile.assert_called_once() diff --git a/contrib/python/test/test_system.py b/contrib/python/test/test_system.py index 93fb9aded..3f6ca57a2 100644 --- a/contrib/python/test/test_system.py +++ b/contrib/python/test/test_system.py @@ -32,8 +32,8 @@ class TestSystem(unittest.TestCase): uri=local_uri, remote_uri=remote_uri, identity_file=os.path.expanduser('~/.ssh/id_rsa'), - ) as pclient: - pclient.system.ping() + ) as remote_client: + remote_client.system.ping() def test_versions(self): with podman.Client(self.host) as pclient: diff --git a/contrib/python/test/test_tunnel.py b/contrib/python/test/test_tunnel.py new file mode 100644 index 000000000..30a80289d --- /dev/null +++ b/contrib/python/test/test_tunnel.py @@ -0,0 +1,79 @@ +from __future__ import absolute_import + +import time +import unittest +from unittest.mock import MagicMock, patch + +import podman +from podman.libs.tunnel import Context, Portal, Tunnel + + +class TestTunnel(unittest.TestCase): + def setUp(self): + self.tunnel_01 = MagicMock(spec=Tunnel) + self.tunnel_02 = MagicMock(spec=Tunnel) + + def test_portal_ops(self): + portal = Portal(sweap=500) + portal['unix:/01'] = self.tunnel_01 + portal['unix:/02'] = self.tunnel_02 + + self.assertEqual(portal.get('unix:/01'), self.tunnel_01) + self.assertEqual(portal.get('unix:/02'), self.tunnel_02) + + del portal['unix:/02'] + with self.assertRaises(KeyError): + portal['unix:/02'] + self.assertEqual(len(portal), 1) + + def test_portal_reaping(self): + portal = Portal(sweap=0.5) + portal['unix:/01'] = self.tunnel_01 + portal['unix:/02'] = self.tunnel_02 + + self.assertEqual(len(portal), 2) + for entry in portal: + self.assertIn(entry, (self.tunnel_01, self.tunnel_02)) + + time.sleep(1) + portal.reap() + self.assertEqual(len(portal), 0) + + def test_portal_no_reaping(self): + portal = Portal(sweap=500) + portal['unix:/01'] = self.tunnel_01 + portal['unix:/02'] = self.tunnel_02 + + portal.reap() + self.assertEqual(len(portal), 2) + for entry in portal: + self.assertIn(entry, (self.tunnel_01, self.tunnel_02)) + + @patch('subprocess.Popen') + @patch('os.path.exists', return_value=True) + @patch('weakref.finalize') + def test_tunnel(self, mock_finalize, mock_exists, mock_Popen): + context = Context( + 'unix:/01', + 'io.projectatomic.podman', + '/tmp/user/socket', + '/run/podman/socket', + 'user', + 'hostname', + '~/.ssh/id_rsa', + ) + tunnel = Tunnel(context).bore('unix:/01') + + cmd = [ + 'ssh', + '-nNT', + '-L', + '{}:{}'.format(context.local_socket, context.remote_socket), + '-i', + context.identity_file, + 'ssh://{}@{}'.format(context.username, context.hostname), + ] + + mock_finalize.assert_called_once_with(tunnel, tunnel.close, 'unix:/01') + mock_exists.assert_called_once_with(context.local_socket) + mock_Popen.assert_called_once_with(cmd, close_fds=True) |