diff options
Diffstat (limited to 'tests/unit/client_test.py')
-rw-r--r-- | tests/unit/client_test.py | 132 |
1 files changed, 76 insertions, 56 deletions
diff --git a/tests/unit/client_test.py b/tests/unit/client_test.py index 6ceb8cb..b79c68e 100644 --- a/tests/unit/client_test.py +++ b/tests/unit/client_test.py @@ -1,14 +1,78 @@ +import datetime +import docker +from docker.utils import kwargs_from_env import os -from docker.client import Client -from .. import base +import unittest -TEST_CERT_DIR = os.path.join( - os.path.dirname(__file__), - 'testdata/certs', -) +from . import fake_api +try: + from unittest import mock +except ImportError: + import mock + + +TEST_CERT_DIR = os.path.join(os.path.dirname(__file__), 'testdata/certs') + + +class ClientTest(unittest.TestCase): + + @mock.patch('docker.api.APIClient.events') + def test_events(self, mock_func): + since = datetime.datetime(2016, 1, 1, 0, 0) + mock_func.return_value = fake_api.get_fake_events()[1] + client = docker.from_env() + assert client.events(since=since) == mock_func.return_value + mock_func.assert_called_with(since=since) + + @mock.patch('docker.api.APIClient.info') + def test_info(self, mock_func): + mock_func.return_value = fake_api.get_fake_info()[1] + client = docker.from_env() + assert client.info() == mock_func.return_value + mock_func.assert_called_with() + + @mock.patch('docker.api.APIClient.ping') + def test_ping(self, mock_func): + mock_func.return_value = True + client = docker.from_env() + assert client.ping() is True + mock_func.assert_called_with() + + @mock.patch('docker.api.APIClient.version') + def test_version(self, mock_func): + mock_func.return_value = fake_api.get_fake_version()[1] + client = docker.from_env() + assert client.version() == mock_func.return_value + mock_func.assert_called_with() + + def test_call_api_client_method(self): + client = docker.from_env() + with self.assertRaises(AttributeError) as cm: + client.create_container() + s = str(cm.exception) + assert "'DockerClient' object has no attribute 'create_container'" in s + assert "this method is now on the object APIClient" in s + + with self.assertRaises(AttributeError) as cm: + client.abcdef() + s = str(cm.exception) + assert "'DockerClient' object has no attribute 'abcdef'" in s + assert "this method is now on the object APIClient" not in s + + def test_call_containers(self): + client = docker.DockerClient(**kwargs_from_env()) + + with self.assertRaises(TypeError) as cm: + client.containers() + + s = str(cm.exception) + assert "'ContainerCollection' object is not callable" in s + assert "docker.APIClient" in s + + +class FromEnvTest(unittest.TestCase): -class ClientTest(base.BaseTestCase): def setUp(self): self.os_environ = os.environ.copy() @@ -22,57 +86,13 @@ class ClientTest(base.BaseTestCase): os.environ.update(DOCKER_HOST='tcp://192.168.59.103:2376', DOCKER_CERT_PATH=TEST_CERT_DIR, DOCKER_TLS_VERIFY='1') - client = Client.from_env() - self.assertEqual(client.base_url, "https://192.168.59.103:2376") + client = docker.from_env() + self.assertEqual(client.api.base_url, "https://192.168.59.103:2376") def test_from_env_with_version(self): os.environ.update(DOCKER_HOST='tcp://192.168.59.103:2376', DOCKER_CERT_PATH=TEST_CERT_DIR, DOCKER_TLS_VERIFY='1') - client = Client.from_env(version='2.32') - self.assertEqual(client.base_url, "https://192.168.59.103:2376") - self.assertEqual(client._version, '2.32') - - -class DisableSocketTest(base.BaseTestCase): - class DummySocket(object): - def __init__(self, timeout=60): - self.timeout = timeout - - def settimeout(self, timeout): - self.timeout = timeout - - def gettimeout(self): - return self.timeout - - def setUp(self): - self.client = Client() - - def test_disable_socket_timeout(self): - """Test that the timeout is disabled on a generic socket object.""" - socket = self.DummySocket() - - self.client._disable_socket_timeout(socket) - - self.assertEqual(socket.timeout, None) - - def test_disable_socket_timeout2(self): - """Test that the timeouts are disabled on a generic socket object - and it's _sock object if present.""" - socket = self.DummySocket() - socket._sock = self.DummySocket() - - self.client._disable_socket_timeout(socket) - - self.assertEqual(socket.timeout, None) - self.assertEqual(socket._sock.timeout, None) - - def test_disable_socket_timout_non_blocking(self): - """Test that a non-blocking socket does not get set to blocking.""" - socket = self.DummySocket() - socket._sock = self.DummySocket(0.0) - - self.client._disable_socket_timeout(socket) - - self.assertEqual(socket.timeout, None) - self.assertEqual(socket._sock.timeout, 0.0) + client = docker.from_env(version='2.32') + self.assertEqual(client.api.base_url, "https://192.168.59.103:2376") + self.assertEqual(client.api._version, '2.32') |