2121
2222import logging
2323import time
24+ from collections .abc import Iterable
2425
2526from marvin .cloudstackTestCase import cloudstackTestCase
27+ from marvin .cloudstackAPI import (migrateSystemVm , listRouters , listSystemVms )
2628from marvin .lib .base import (Cluster , Configurations , Host , Network , NetworkOffering , ServiceOffering , VirtualMachine ,
2729 Zone )
2830from marvin .lib .common import (get_domain , get_zone , get_template )
@@ -98,6 +100,41 @@ def setUpClass(cls):
98100 )
99101 cls ._cleanup .append (cls .network )
100102
103+ cls .migrateSvms (cls .cluster )
104+
105+ @classmethod
106+ def migrateSvms (cls , cluster ):
107+ """
108+ for testing the balanced algorithm we must make sure there is at least as more free memory on host[1] than on
109+ host[0]. As a grude measure we migrate any and all system vms to host[0] before the testing commences
110+
111+ :param cluster: the cluser to check
112+ :return: None
113+ """
114+
115+ systemVmIds = []
116+ cmds = listSystemVms .listSystemVmsCmd ()
117+ responseS = cls .apiclient .listSystemVms (cmds )
118+ if isinstance (responseS , Iterable ):
119+ for svm in responseS :
120+ if svm .hostid != cls .hosts [0 ].id :
121+ systemVmIds .append (svm .id )
122+ cmdv = listRouters .listRoutersCmd ()
123+ responseR = cls .apiclient .listRouters (cmdv )
124+ if isinstance (responseR , Iterable ):
125+ for svm in responseR :
126+ if svm .hostid != cls .hosts [0 ].id :
127+ systemVmIds .append (svm .id )
128+ numToMigrate = len (systemVmIds )
129+ cls .logger .debug (f'system vms and routers to migrate -- { numToMigrate } ' )
130+ cmdM = migrateSystemVm .migrateSystemVmCmd ()
131+ cmdM .hostId = cls .hosts [0 ].id
132+ for id in systemVmIds :
133+ cmdM .virtualmachineid = id
134+ responseM = cls .apiclient .migrateSystemVm (cmdM )
135+ cls .logger .debug (f'migrated { responseM } ' )
136+
137+
101138 @classmethod
102139 def tearDownClass (cls ):
103140 super (TestClusterDRS , cls ).tearDownClass ()
@@ -111,7 +148,6 @@ def setUp(self):
111148 def tearDown (self ):
112149 super (TestClusterDRS , self ).tearDown ()
113150
114- @classmethod
115151 def get_vm_host_id (cls , vm_id ):
116152 list_vms = VirtualMachine .list (cls .apiclient , id = vm_id )
117153 vm = list_vms [0 ]
@@ -188,8 +224,8 @@ def test_01_condensed_drs_algorithm(self):
188224 serviceofferingid = self .service_offering .id ,
189225 templateid = self .template .id , zoneid = self .zone .id ,
190226 networkids = self .network .id , hostid = self .hosts [1 ].id )
191- vm_2_host_id = self .get_vm_host_id (self .virtual_machine_2 .id )
192227 self .cleanup .append (self .virtual_machine_2 )
228+ vm_2_host_id = self .get_vm_host_id (self .virtual_machine_2 .id )
193229
194230 self .assertNotEqual (vm_1_host_id , vm_2_host_id , msg = "Both VMs should be on different hosts" )
195231 self .wait_for_vm_start (self .virtual_machine_1 )
@@ -216,13 +252,15 @@ def test_01_condensed_drs_algorithm(self):
216252
217253 @attr (tags = ["advanced" ], required_hardware = "false" )
218254 def test_02_balanced_drs_algorithm (self ):
219- """ Verify DRS algorithm - balanced"""
220-
221- # 1. Deploy vm-1 on host 1
222- # 2. Deploy vm-2 on host 2
223- # 3. Execute DRS to move all VMs on different hosts
255+ """
256+ Verify DRS algorithm - balanced
224257
258+ # 1. Deploy vm-1 on host 1
259+ # 2. Deploy vm-2 on host 2
260+ # 3. Execute DRS to move all VMs on different hosts
261+ """
225262 self .logger .debug ("=== Running test_02_balanced_drs_algorithm ===" )
263+
226264 # 1. Deploy vm-1 on host 1
227265 self .services ["virtual_machine" ]["name" ] = "virtual-machine-1"
228266 self .services ["virtual_machine" ]["displayname" ] = "virtual-machine-1"
@@ -240,8 +278,8 @@ def test_02_balanced_drs_algorithm(self):
240278 serviceofferingid = self .service_offering .id ,
241279 templateid = self .template .id , zoneid = self .zone .id ,
242280 networkids = self .network .id , hostid = self .hosts [0 ].id )
243- vm_2_host_id = self .get_vm_host_id (self .virtual_machine_2 .id )
244281 self .cleanup .append (self .virtual_machine_2 )
282+ vm_2_host_id = self .get_vm_host_id (self .virtual_machine_2 .id )
245283
246284 self .assertEqual (vm_1_host_id , vm_2_host_id , msg = "Both VMs should be on same hosts" )
247285 self .wait_for_vm_start (self .virtual_machine_1 )
@@ -256,12 +294,15 @@ def test_02_balanced_drs_algorithm(self):
256294 migration ["virtualmachineid" ]: migration ["destinationhostid" ] for migration in migrations
257295 }
258296
259- self .assertEqual (len (vm_to_dest_host_map ), 1 , msg = "DRS plan should have 1 migrations" )
297+ # this is one if no svm is considered to be migrated, it might be higher
298+ self .assertTrue (len (vm_to_dest_host_map ) >= 1 , msg = "DRS plan should have at least 1 migrations" )
260299
261300 executed_plan = self .cluster .executeDrsPlan (self .apiclient , vm_to_dest_host_map )
262301 self .wait_for_plan_completion (executed_plan )
263302
264303 vm_1_host_id = self .get_vm_host_id (self .virtual_machine_1 .id )
265304 vm_2_host_id = self .get_vm_host_id (self .virtual_machine_2 .id )
266305
267- self .assertNotEqual (vm_1_host_id , vm_2_host_id , msg = "Both VMs should be on different hosts" )
306+ self .assertTrue (
307+ vm_1_host_id != self .virtual_machine_1 .hostid or vm_2_host_id != self .virtual_machine_2 .hostid ,
308+ msg = "At least one VM should have been migrated to a different host" )
0 commit comments