Fix confusing error message on interactive run

If run a container with an incorrect command in interactive mode,
it showed a confusing error message "ERROR: Invalid websocket link when attach ...".
This error message is sub-optimal because the root cause of the failure is the container
entered an error state (because users specify an incorrect command).
We need to fix it by checking the state of the container and failed it
with a correct message if the container entered an error state.

Change-Id: I630de2414d5e36e86b246460a6d24992b06696e3
Closes-Bug: #1685521
This commit is contained in:
haobing1 2017-04-23 14:00:26 +08:00
parent fa61ba016e
commit a7fce1d200
3 changed files with 21 additions and 1 deletions

4
zunclient/common/websocketclient/exceptions.py Normal file → Executable file
View File

@ -45,3 +45,7 @@ class InvalidWebSocketLink(ContainerWebSocketException):
class ContainerFailtoStart(ContainerWebSocketException):
message = "Container fail to start"
class ContainerStateError(ContainerWebSocketException):
message = "Container state is error, can not attach container"

16
zunclient/tests/unit/v1/test_containers_shell.py Normal file → Executable file
View File

@ -13,6 +13,7 @@
import mock
from zunclient.common import utils as zun_utils
from zunclient.common.websocketclient import exceptions
from zunclient.tests.unit.v1 import shell_test_base
from zunclient.v1 import containers_shell
@ -133,3 +134,18 @@ class ShellTest(shell_test_base.TestCommandLineArgument):
'run --image-pull-policy wrong x',
self._invalid_choice_error)
self.assertFalse(mock_run.called)
@mock.patch('zunclient.v1.containers.ContainerManager.get')
@mock.patch('zunclient.v1.containers_shell._show_container')
@mock.patch('zunclient.v1.containers.ContainerManager.run')
def test_zun_container_run_interactive(self, mock_run,
mock_show_container,
mock_get_container):
fake_container = mock.MagicMock()
fake_container.uuid = 'fake_uuid'
mock_run.return_value = fake_container
fake_container.status = 'Error'
mock_get_container.return_value = fake_container
self.assertRaises(exceptions.ContainerStateError,
self.shell,
'run -i x ')

2
zunclient/v1/containers_shell.py Normal file → Executable file
View File

@ -430,7 +430,7 @@ def do_run(cs, args):
ready_for_attach = True
break
if zun_utils.check_container_status(container, 'Error'):
break
raise exceptions.ContainerStateError(container_uuid)
print("Waiting for container start")
time.sleep(1)
if ready_for_attach is True: