Add nova profile support vm start and stop operation

Change-Id: I5df621e35745395504f14a8bfac31df9471af203
Signed-off-by: Yuanbin.Chen <cybing4@gmail.com>
This commit is contained in:
Yuanbin.Chen 2018-05-11 15:31:17 +08:00
parent bb23514980
commit 9865d71b33
3 changed files with 103 additions and 2 deletions

View File

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

View File

@ -1652,3 +1652,35 @@ class ServerProfile(base.Profile):
raise exc.EResourceOperation(op='resume', type='server',
id=server_id,
message=six.text_type(ex))
def handle_start(self, obj):
"""Handler for the start operation."""
if not obj.physical_id:
return False
server_id = obj.physical_id
nova_driver = self.compute(obj)
try:
nova_driver.server_start(server_id)
nova_driver.wait_for_server(server_id, consts.VS_ACTIVE)
return True
except exc.InternalError as ex:
raise exc.EResourceOperation(op='start', type='server',
id=server_id,
message=six.text_type(ex))
def handle_stop(self, obj):
"""Handler for the stop operation."""
if not obj.physical_id:
return False
server_id = obj.physical_id
nova_driver = self.compute(obj)
try:
nova_driver.server_stop(server_id)
nova_driver.wait_for_server(server_id, consts.VS_SHUTOFF)
return True
except exc.InternalError as ex:
raise exc.EResourceOperation(op='stop', type='server',
id=server_id,
message=six.text_type(ex))

View File

@ -1699,3 +1699,72 @@ class TestNovaServerBasic(base.SenlinTestCase):
"timeout.", six.text_type(ex))
cc.server_resume.assert_called_once_with('FAKE_ID')
cc.wait_for_server.assert_called_once_with('FAKE_ID', 'ACTIVE')
def test_handle_start(self):
obj = mock.Mock(physical_id='FAKE_ID')
profile = server.ServerProfile('t', self.spec)
profile._computeclient = mock.Mock()
# do it
res = profile.handle_start(obj)
self.assertTrue(res)
def test_handle_start_no_physical_id(self):
obj = mock.Mock(physical_id=None)
profile = server.ServerProfile('t', self.spec)
# do it
res = profile.handle_start(obj)
self.assertFalse(res)
def test_handle_start_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_start,
node_obj)
self.assertEqual("Failed in start server 'FAKE_ID': "
"timeout.", six.text_type(ex))
cc.server_start.assert_called_once_with('FAKE_ID')
cc.wait_for_server.assert_called_once_with('FAKE_ID', 'ACTIVE')
def test_handle_stop(self):
obj = mock.Mock(physical_id='FAKE_ID')
profile = server.ServerProfile('t', self.spec)
profile._computeclient = mock.Mock()
# do it
res = profile.handle_stop(obj)
self.assertTrue(res)
def test_handle_stop_no_physical_id(self):
obj = mock.Mock(physical_id=None)
profile = server.ServerProfile('t', self.spec)
profile._computeclient = mock.Mock()
# do it
res = profile.handle_stop(obj)
self.assertFalse(res)
def test_handle_stop_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_stop,
node_obj)
self.assertEqual("Failed in stop server 'FAKE_ID': "
"timeout.", six.text_type(ex))
cc.server_stop.assert_called_once_with('FAKE_ID')
cc.wait_for_server.assert_called_once_with('FAKE_ID', 'SHUTOFF')