Added zun exec state validation

Command zun exec ... returning 500 if the container is not 'Running'.
It should return 400 or 409 instead. The reason of this failure is
because we missed a validation entry for "exec" entry.

Closes-Bug: #1654391

Change-Id: I20e4dc19f6b1a76a002f5ff1ec4ddd05bf8311f4
This commit is contained in:
prameswar 2017-01-06 14:20:07 +05:30
parent 2cb6ce3cf1
commit 8bcf73fda6
2 changed files with 13 additions and 0 deletions

View File

@ -37,6 +37,7 @@ VALID_STATES = {
'pause': ['Running'],
'unpause': ['Paused'],
'kill': ['Running'],
'exec': ['Running'],
}
@ -315,6 +316,7 @@ class Manager(object):
# TODO(hongbin): support exec command interactively
LOG.debug('Executing command in container: %s', container.uuid)
try:
self._validate_container_state(container, 'exec')
return self.driver.execute(container, command)
except exception.DockerError as e:
LOG.error(_LE("Error occurred while calling docker exec API: %s"),

View File

@ -65,6 +65,9 @@ class TestManager(base.TestCase):
container.status = 'Stopped'
self.assertIsNone(self.compute_manager._validate_container_state(
container, 'reboot'))
container.status = 'Running'
self.assertIsNone(self.compute_manager._validate_container_state(
container, 'exec'))
@mock.patch.object(Container, 'save')
@mock.patch('zun.image.driver.pull_image')
@ -411,6 +414,14 @@ class TestManager(base.TestCase):
self.context, container, 'fake_cmd')
mock_execute.assert_called_once_with(container, 'fake_cmd')
@mock.patch.object(manager.Manager, '_validate_container_state')
def test_container_execute_invalid_state(self, mock_validate):
container = Container(self.context, **utils.get_test_container())
mock_validate.side_effect = exception.InvalidStateException
self.assertRaises(exception.InvalidStateException,
self.compute_manager.container_exec,
self.context, container, 'fake_cmd')
@mock.patch.object(fake_driver, 'execute')
def test_container_execute_failed(self, mock_execute):
container = Container(self.context, **utils.get_test_container())