raise exception when create container with invalid bay status

raise exception when create container with invalid bay status
instead of just print out the error message.

Added unit test for create container with invalid bay status.

Change-Id: I821a9b1e814c45a4f197579740794fdb48285e68
This commit is contained in:
Yang Hongyang 2016-01-14 11:48:59 +08:00
parent c915e5425b
commit 70dea1147c
2 changed files with 22 additions and 4 deletions

View File

@ -14,6 +14,7 @@
import mock import mock
from magnumclient import exceptions
from magnumclient.tests.v1 import shell_test_base from magnumclient.tests.v1 import shell_test_base
@ -58,6 +59,20 @@ class ShellTest(shell_test_base.TestCommandLineArgument):
self._mandatory_arg_error) self._mandatory_arg_error)
self.assertFalse(mock_create.called) self.assertFalse(mock_create.called)
@mock.patch('magnumclient.v1.bays.BayManager.get')
@mock.patch('magnumclient.v1.containers.ContainerManager.create')
def test_container_create_failure_invalid_bay_status(self, mock_create,
mock_bay_get):
mock_bay = mock.MagicMock()
mock_bay.status = "CREATE_IN_PROGRESS"
mock_bay_get.return_value = mock_bay
self.assertRaises(exceptions.InvalidAttribute, self._test_arg_failure,
'container-create '
'--image test-image '
'--bay test-bay',
self._bay_status_error)
self.assertFalse(mock_create.called)
@mock.patch('magnumclient.v1.containers.ContainerManager.delete') @mock.patch('magnumclient.v1.containers.ContainerManager.delete')
def test_container_delete_success(self, mock_delete): def test_container_delete_success(self, mock_delete):
self._test_arg_success('container-delete xxx') self._test_arg_success('container-delete xxx')

View File

@ -15,6 +15,7 @@
import json import json
from magnumclient.common import utils as magnum_utils from magnumclient.common import utils as magnum_utils
from magnumclient import exceptions
from magnumclient.openstack.common import cliutils as utils from magnumclient.openstack.common import cliutils as utils
@ -43,10 +44,12 @@ def _show_container(container):
def do_container_create(cs, args): def do_container_create(cs, args):
"""Create a container.""" """Create a container."""
bay = cs.bays.get(args.bay) bay = cs.bays.get(args.bay)
if bay.status not in ['CREATE_COMPLETE', 'UPDATE_COMPLETE']: if bay.status not in ['CREATE_COMPLETE',
print('Bay status for %s is: %s. We can not create a %s there' 'UPDATE_IN_PROGRESS', 'UPDATE_COMPLETE']:
' until the status is CREATE_COMPLETE or UPDATE_COMPLETE.' % raise exceptions.InvalidAttribute(
(bay.uuid, bay.status, "container")) 'Bay status for %s is: %s. We cannot create a %s'
' unless the status is CREATE_COMPLETE, UPDATE_IN_PROGRESS'
' or UPDATE_COMPLETE.' % (bay.uuid, bay.status, "container"))
return return
opts = {} opts = {}
opts['name'] = args.name opts['name'] = args.name