From e54a2cd012e232e9df2c269220ff87ac536b071c Mon Sep 17 00:00:00 2001 From: sdc50 Date: Mon, 14 Sep 2020 10:04:28 -0600 Subject: [PATCH] hotfix for docker client when docker daemon is not running (#595) * hotfix for docker client when docker daemon is not running --- setup.cfg | 2 +- .../test_tethys_cli/test_docker_commands.py | 18 ++++++++++++++++-- tethys_cli/docker_commands.py | 17 +++++++++++------ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/setup.cfg b/setup.cfg index c5729325c..0bac8c14e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = tethys-platform -version = 3.1.0 +version = 3.1.1 license = BSD 2-Clause License summary = Primary Tethys Platform Django Site Project description-file = README.rst diff --git a/tests/unit_tests/test_tethys_cli/test_docker_commands.py b/tests/unit_tests/test_tethys_cli/test_docker_commands.py index 40fa93c9a..05b104bc3 100644 --- a/tests/unit_tests/test_tethys_cli/test_docker_commands.py +++ b/tests/unit_tests/test_tethys_cli/test_docker_commands.py @@ -10,12 +10,16 @@ def setUp(self): dc_patcher = mock.patch('tethys_cli.docker_commands.docker.from_env', return_value=self.mock_dc) self.mock_from_env = dc_patcher.start() self.addCleanup(dc_patcher.stop) + + # this is required to reset the mock_dc on the docker commands module + cli_docker_commands.ContainerMetadata.get_docker_client() + input_patcher = mock.patch('tethys_cli.docker_commands.input', return_value=mock.MagicMock(name='input')) self.mock_input = input_patcher.start() self.addCleanup(input_patcher.stop) def tearDown(self): - pass + cli_docker_commands.ContainerMetadata._docker_client = None def test_curses_import_error(self): with mock.patch.dict('sys.modules', {'curses': None}): @@ -572,7 +576,7 @@ def test_cm_stop(self, mock_pretty_output): self.mock_dc.containers.get().stop.assert_called() @mock.patch('tethys_cli.docker_commands.write_pretty_output') - def test_cm_stop_excpetion(self, mock_pretty_output): + def test_cm_stop_exception(self, mock_pretty_output): self.mock_dc.containers.get().stop.side_effect = Exception container = cli_docker_commands.PostGisContainerMetadata() msg = container.stop() @@ -1092,3 +1096,13 @@ def test_log_pull_stream_windows(self, mock_platform_system, mock_pretty_output) po_call_args = mock_pretty_output.call_args_list self.assertEqual(1, len(po_call_args)) self.assertEqual('358464:Downloading bar', po_call_args[0][0][0]) + + @mock.patch('tethys_cli.docker_commands.exit') + @mock.patch('tethys_cli.docker_commands.write_error') + @mock.patch('tethys_cli.docker_commands.docker.from_env', + side_effect=cli_docker_commands.docker.errors.DockerException) + def test_docker_deamon_not_running(self, _, mock_write_error, mock_exit): + cli_docker_commands.ContainerMetadata._docker_client = None + cli_docker_commands.ContainerMetadata.get_docker_client() + mock_write_error.assert_called() + mock_exit.assert_called_with(1) diff --git a/tethys_cli/docker_commands.py b/tethys_cli/docker_commands.py index 461e90838..a1f4e2029 100644 --- a/tethys_cli/docker_commands.py +++ b/tethys_cli/docker_commands.py @@ -20,7 +20,7 @@ import docker from docker.types import Mount from docker.errors import NotFound as DockerNotFound -from tethys_cli.cli_colors import write_pretty_output +from tethys_cli.cli_colors import write_pretty_output, write_error from tethys_apps.utilities import get_tethys_home_dir @@ -66,21 +66,26 @@ class ContainerMetadata(ABC): container_port = None default_host = '127.0.0.1' - _docker_client = docker.from_env() + _docker_client = None all_containers = None def __init__(self, docker_client=None): self._docker_client = docker_client self._container = None - @staticmethod - def get_docker_client(): + @classmethod + def get_docker_client(cls): """ Configure DockerClient """ - docker_client = docker.from_env() + if cls._docker_client is None: + try: + cls._docker_client = docker.from_env() + except docker.errors.DockerException: + write_error('The Docker daemon must be running to use the tethys docker command.') + exit(1) - return docker_client + return cls._docker_client @property def docker_client(self):