Skip to content

Commit

Permalink
Made cluster optional and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ddegoede committed Nov 30, 2023
1 parent 7a123ba commit e20515a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 9 additions & 2 deletions live_migrate_virtual_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
@click.option('--dry-run/--exec', is_flag=True, default=True, show_default=True, help='Enable/disable dry-run')
@click_log.simple_verbosity_option(logging.getLogger(), default="INFO", show_default=True)
@click.argument('vm-name')
@click.argument('cluster')
@click.argument('cluster', required=False)
def main(profile, zwps_to_cwps, migrate_offline_with_rsync, rsync_target_host, add_affinity_group, destination_dc, is_project_vm,
avoid_storage_pool, skip_backingfile_merge, skip_within_cluster, only_within_cluster, dry_run, vm_name, cluster):
"""Live migrate VM to CLUSTER"""
"""Live migrate VM"""
"""Unless --migrate-offline-with-rsync is passed, then we migrate offline"""

click_log.basic_config()
Expand Down Expand Up @@ -79,6 +79,11 @@ def main(profile, zwps_to_cwps, migrate_offline_with_rsync, rsync_target_host, a
logging.error(f"Cannot use 'skip_within_cluster' together with 'only_within_cluster'!")
sys.exit(1)

# Need a cluster if not only migrating withing cluster
if not only_within_cluster and not cluster:
logging.error(f"We need a cluster name if you're not only migrating within the cluster!")
sys.exit(1)

# Live migrate requires running VM. Unless migrate_offline_with_rsync==True, then we stop the VM as this is offline
if not migrate_offline_with_rsync:
if not vm['state'] == 'Running':
Expand Down Expand Up @@ -324,10 +329,12 @@ def live_migrate(co, cs, cluster, vm_name, destination_dc, add_affinity_group, i

target_cluster = co.get_cluster(name=cluster)
if not target_cluster:
logging.error(f"Cannot migrate, cluster '{cluster}' not found!")
return False

vm = co.get_vm(name=vm_name, is_project_vm=is_project_vm)
if not vm:
logging.error(f"Cannot migrate, VM '{vm_name}' not found!")
return False

if not vm['state'] == 'Running':
Expand Down
16 changes: 15 additions & 1 deletion tests/test_live_migrate_virtual_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_main(self):
self.cs_instance.update_zwps_to_cwps.assert_not_called()
self.cs_instance.update_service_offering_of_vm.assert_not_called()
self.target_cluster.find_migration_host.assert_called_with(self.vm)
self.source_host.get_disks.assert_called_with(self.vm)
self.source_host.get_disks.assert_called_with(self.vm['instancename'])
self.cs_instance.get_volume_size.assert_called_with('path1')
self.cs_instance.update_volume_size.assert_not_called()
self.vm.migrate_within_cluster.assert_called()
Expand Down Expand Up @@ -259,6 +259,20 @@ def test_failures(self):
self.vm.migrate_within_cluster.assert_not_called()
self.vm.migrate.assert_called()

self._setup_mocks()
self.vm.migrate.return_value = False
self.assertEqual(1, self.runner.invoke(live_migrate_virtual_machine.main,
['--exec', '--skip-within-cluster', '--only-within-cluster', '-p', 'profile', 'vm', 'target_cluster']).exit_code)
self.vm.migrate_within_cluster.assert_not_called()
self.vm.migrate.assert_not_called()

self._setup_mocks()
self.vm.migrate.return_value = False
self.assertEqual(0, self.runner.invoke(live_migrate_virtual_machine.main,
['--exec', '--only-within-cluster', '-p', 'profile', 'vm', 'target_cluster']).exit_code)
self.vm.migrate_within_cluster.assert_called()
self.vm.migrate.assert_not_called()

self._setup_mocks()
self.vm['hostname'] = self.source_host['name']
self.assertEqual(1, self.runner.invoke(live_migrate_virtual_machine.main,
Expand Down

0 comments on commit e20515a

Please sign in to comment.