Skip to content

Commit

Permalink
hotfix for docker client when docker daemon is not running (#595)
Browse files Browse the repository at this point in the history
* hotfix for docker client when docker daemon is not running
  • Loading branch information
sdc50 authored Sep 14, 2020
1 parent 3631855 commit e54a2cd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 16 additions & 2 deletions tests/unit_tests/test_tethys_cli/test_docker_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
17 changes: 11 additions & 6 deletions tethys_cli/docker_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit e54a2cd

Please sign in to comment.