Commit 19d0e9f2 by Őry Máté

vm: make Instance.migrate(to_node) optional

parent 52795ab4
...@@ -1068,10 +1068,15 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -1068,10 +1068,15 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
return local_tasks.migrate.apply_async(args=[self, to_node, user], return local_tasks.migrate.apply_async(args=[self, to_node, user],
queue="localhost.man") queue="localhost.man")
def migrate(self, to_node, user=None, task_uuid=None, timeout=120): def migrate(self, to_node=None, user=None, task_uuid=None, timeout=120):
"""Live migrate running vm to another node. """ """Live migrate running vm to another node. """
with instance_activity(code_suffix='migrate', instance=self, with instance_activity(code_suffix='migrate', instance=self,
task_uuid=task_uuid, user=user) as act: task_uuid=task_uuid, user=user) as act:
if not to_node:
with act.sub_activity('scheduling') as sa:
to_node = self.select_node()
sa.result = to_node
# Destroy networks # Destroy networks
with act.sub_activity('destroying_net'): with act.sub_activity('destroying_net'):
for net in self.interface_set.all(): for net in self.interface_set.all():
......
from django.test import TestCase from django.test import TestCase
from mock import Mock from mock import Mock, MagicMock, patch, call
from ..models.common import ( from ..models.common import (
Lease Lease
...@@ -10,6 +10,7 @@ from ..models.instance import ( ...@@ -10,6 +10,7 @@ from ..models.instance import (
from ..models.network import ( from ..models.network import (
Interface Interface
) )
from ..models.node import Node
class PortFinderTestCase(TestCase): class PortFinderTestCase(TestCase):
...@@ -39,6 +40,33 @@ class InstanceTestCase(TestCase): ...@@ -39,6 +40,33 @@ class InstanceTestCase(TestCase):
inst = Mock(state='RUNNING') inst = Mock(state='RUNNING')
assert Instance.is_running.getter(inst) assert Instance.is_running.getter(inst)
def test_migrate_with_scheduling(self):
inst = MagicMock(spec=Instance)
inst.interface_set.all.return_value = []
inst.node = MagicMock(spec=Node)
with patch('vm.models.instance.instance_activity') as ia, \
patch('vm.models.instance.vm_tasks.migrate') as migr:
Instance.migrate(inst)
migr.apply_async.assert_called()
self.assertIn(call().__enter__().sub_activity(u'scheduling'),
ia.mock_calls)
inst.select_node.assert_called()
def test_migrate_wo_scheduling(self):
inst = MagicMock(spec=Instance)
inst.interface_set.all.return_value = []
inst.node = MagicMock(spec=Node)
with patch('vm.models.instance.instance_activity') as ia, \
patch('vm.models.instance.vm_tasks.migrate') as migr:
inst.select_node.side_effect = AssertionError
Instance.migrate(inst, inst.node)
migr.apply_async.assert_called()
self.assertNotIn(call().__enter__().sub_activity(u'scheduling'),
ia.mock_calls)
class InterfaceTestCase(TestCase): class InterfaceTestCase(TestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment