Add nova profile support for vm pause and unpause operation

Change-Id: I9c6d246e4f1956d743213c12b15b50a4738e6905
Signed-off-by: Yuanbin.Chen <cybing4@gmail.com>
This commit is contained in:
Yuanbin.Chen 2018-05-16 14:08:31 +08:00
parent c75527260b
commit 112fc57aae
3 changed files with 102 additions and 2 deletions

View File

@ -308,7 +308,7 @@ LIFECYCLE_TRANSITION_TYPE = (
)
VM_STATUS = (
VS_ACTIVE, VS_ERROR, VS_SUSPENDED, VS_SHUTOFF,
VS_ACTIVE, VS_ERROR, VS_SUSPENDED, VS_SHUTOFF, VS_PAUSED,
) = (
'ACTIVE', 'ERROR', 'SUSPENDED', 'SHUTOFF',
'ACTIVE', 'ERROR', 'SUSPENDED', 'SHUTOFF', 'PAUSED',
)

View File

@ -1712,3 +1712,35 @@ class ServerProfile(base.Profile):
raise exc.EResourceOperation(op='unlock', type='server',
id=server_id,
message=six.text_type(ex))
def handle_pause(self, obj):
"""Handler for the pause operation."""
if not obj.physical_id:
return False
server_id = obj.physical_id
nova_driver = self.compute(obj)
try:
nova_driver.server_pause(server_id)
nova_driver.wait_for_server(server_id, consts.VS_PAUSED)
return True
except exc.InternalError as ex:
raise exc.EResourceOperation(op='pause', type='server',
id=server_id,
message=six.text_type(ex))
def handle_unpause(self, obj):
"""Handler for the unpause operation."""
if not obj.physical_id:
return False
server_id = obj.physical_id
nova_driver = self.compute(obj)
try:
nova_driver.server_unpause(server_id)
nova_driver.wait_for_server(server_id, consts.VS_ACTIVE)
return True
except exc.InternalError as ex:
raise exc.EResourceOperation(op='unpause', type='server',
id=server_id,
message=six.text_type(ex))

View File

@ -1803,3 +1803,71 @@ class TestNovaServerBasic(base.SenlinTestCase):
# do it
res = profile.handle_unlock(obj)
self.assertFalse(res)
def test_handle_pause(self):
obj = mock.Mock(physical_id='FAKE_ID')
profile = server.ServerProfile('t', self.spec)
profile._computeclient = mock.Mock()
# do it
res = profile.handle_pause(obj)
self.assertTrue(res)
def test_handle_pause_no_physical_id(self):
obj = mock.Mock(physical_id=None)
profile = server.ServerProfile('t', self.spec)
# do it
res = profile.handle_pause(obj)
self.assertFalse(res)
def test_handle_pause_failed_waiting(self):
profile = server.ServerProfile('t', self.spec)
cc = mock.Mock()
ex = exc.InternalError(code=500, message='timeout')
cc.wait_for_server.side_effect = ex
profile._computeclient = cc
node_obj = mock.Mock(physical_id='FAKE_ID')
ex = self.assertRaises(exc.EResourceOperation,
profile.handle_pause,
node_obj)
self.assertEqual("Failed in pause server 'FAKE_ID': "
"timeout.", six.text_type(ex))
cc.server_pause.assert_called_once_with('FAKE_ID')
cc.wait_for_server.assert_called_once_with('FAKE_ID', 'PAUSED')
def test_handle_unpause(self):
obj = mock.Mock(physical_id='FAKE_ID')
profile = server.ServerProfile('t', self.spec)
profile._computeclient = mock.Mock()
# do it
res = profile.handle_unpause(obj)
self.assertTrue(res)
def test_handle_unpause_no_physical_id(self):
obj = mock.Mock(physical_id=None)
profile = server.ServerProfile('t', self.spec)
# do it
res = profile.handle_unpause(obj)
self.assertFalse(res)
def test_handle_unpause_failed_waiting(self):
profile = server.ServerProfile('t', self.spec)
cc = mock.Mock()
ex = exc.InternalError(code=500, message='timeout')
cc.wait_for_server.side_effect = ex
profile._computeclient = cc
node_obj = mock.Mock(physical_id='FAKE_ID')
ex = self.assertRaises(exc.EResourceOperation,
profile.handle_unpause,
node_obj)
self.assertEqual("Failed in unpause server 'FAKE_ID': "
"timeout.", six.text_type(ex))
cc.server_unpause.assert_called_once_with('FAKE_ID')
cc.wait_for_server.assert_called_once_with('FAKE_ID', 'ACTIVE')