Skip to content

Commit

Permalink
Fixed pytests
Browse files Browse the repository at this point in the history
  • Loading branch information
ddegoede committed Oct 11, 2023
1 parent 029115b commit 2f2168b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 25 deletions.
6 changes: 3 additions & 3 deletions live_migrate_virtual_machine_volumes.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
@click.option('--is-project-vm', is_flag=True, help='The specified VM is a project VM')
@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')
@click.argument('vm-name')
@click.argument('storage_pool')
def main(profile, max_iops, zwps_to_cwps, is_router, is_project_vm, dry_run, vm, storage_pool):
def main(profile, max_iops, zwps_to_cwps, is_router, is_project_vm, dry_run, vm_name, storage_pool):
"""Live migrate VM volumes to STORAGE_POOL"""

click_log.basic_config()
Expand All @@ -51,7 +51,7 @@ def main(profile, max_iops, zwps_to_cwps, is_router, is_project_vm, dry_run, vm,

cs = CosmicSQL(server=profile, dry_run=dry_run)

if not live_migrate_volumes(storage_pool, co, cs, dry_run, is_router, is_project_vm, log_to_slack, max_iops, vm,
if not live_migrate_volumes(storage_pool, co, cs, dry_run, is_router, is_project_vm, log_to_slack, max_iops, vm_name,
zwps_to_cwps):
sys.exit(1)

Expand Down
4 changes: 0 additions & 4 deletions migrate_offline_volumes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ def main(profile, dry_run, ignore_volumes, zwps_to_cwps, skip_disk_offerings, sk
logging.error('Destination cluster cannot be the source cluster!')
sys.exit(1)

if source_pool_name == destination_pool_name:
logging.error('Destination cluster cannot be the source cluster!')
sys.exit(1)

if dry_run:
logging.warning('Running in dry-run mode, will only show changes')

Expand Down
21 changes: 11 additions & 10 deletions tests/test_cosmichost.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,8 @@ def test_wait_until_offline_dry_run(self):

def test_wait_until_online(self):
self.host.execute = Mock()
self.host.execute.side_effect = [Mock(return_code=1), Mock(return_code=0)]
self.socket_context.connect_ex.side_effect = [1, 0]
self.host.execute.side_effect = [Mock(return_code=1), Mock(return_code=0), Mock(return_code=1), Mock(return_code=0), Mock(return_code=1), Mock(return_code=0)]
self.socket_context.connect_ex.side_effect = [1, 0, 1, 0, 1, 0]

self.host.wait_until_online()
self.socket_context.connect_ex.assert_called_with(('host1', 22))
Expand All @@ -568,7 +568,7 @@ def test_wait_until_online_retry_on_failure(self):
self.host.execute.side_effect = [Mock(return_code=1), ConnectionResetError,
UnexpectedExit('mock unexpected exit'),
CommandTimedOut('mock command timeout', 10), Mock(return_code=0)]
self.socket_context.connect_ex.side_effect = [1, 0]
self.socket_context.connect_ex.side_effect = [1, 0, 1, 0, 1, 0]

self.host.wait_until_online()
self.socket_context.connect_ex.assert_called_with(('host1', 22))
Expand Down Expand Up @@ -626,7 +626,7 @@ def test_get_disks(self):
vm = CosmicVM(Mock(), {
'id': 'vm1',
'name': 'vm',
'instancename': 'vm'
'instancename': 'i-1-vm'
})

xml_desc = """
Expand Down Expand Up @@ -672,22 +672,23 @@ def test_get_disks(self):
domain.XMLDesc.return_value = xml_desc
domain.blockInfo.return_value = (10737418240, 567148544, 567148544)

self.assertDictEqual(disk_data, self.host.get_disks(vm))
self.assertDictEqual(disk_data, self.host.get_disks(vm['instancename']))

mock_libvirt.assert_called_with('qemu+tcp://host1/system')
mock_libvirt.return_value.lookupByName.assert_called_with(vm['name'])
mock_libvirt.return_value.lookupByName.assert_called_with(vm['instancename'])
mock_libvirt.return_value.close.assert_called()

def test_set_iops_limit(self):
self.host.execute = Mock(return_value=Mock(return_code=0))
vm = CosmicVM(Mock(), {
'id': 'v1',
'name': 'vm1'
'name': 'vm1',
'instancename': 'i-1-vm'
})

self.assertTrue(self.host.set_iops_limit(vm, 100))
self.assertTrue(self.host.set_iops_limit(vm['instancename'], 100))
command = self.host.execute.call_args[0][0]
self.assertIn("--details 'vm1'", command)
self.assertIn("--details 'i-1-vm'", command)
self.assertIn('--total-iops-sec 100', command)

self.host.execute.return_value.return_code = 1
Expand All @@ -701,7 +702,7 @@ def test_merge_backing_files(self):
'instancename': 'i-1-VM'
})

self.assertTrue(self.host.merge_backing_files(vm))
self.assertTrue(self.host.merge_backing_files(vm['instancename']))
command = self.host.execute.call_args[0][0]
self.assertIn("--details 'i-1-VM'", command)
self.assertIn("blockpull 'i-1-VM'", command)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_live_migrate_virtual_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def test_root_migration_to_zwps(self):

self.co_instance.get_storage_pool.assert_has_calls([call(name='zwps_pool'), call(name='root_pool')])
self.root_volume.migrate.assert_called_with(self.zwps_storage_pool, live_migrate=True,
source_host=self.source_host, vm=self.vm)
source_host=self.source_host, vm_instance=self.vm['instancename'])

def test_root_migration_to_zwps_dry_run(self):
self.vm.get_volumes.return_value = [self.zwps_volume, self.root_volume]
Expand Down Expand Up @@ -388,4 +388,4 @@ def test_root_migration_to_zwps_failure(self):

self.co_instance.get_storage_pool.assert_called_with(name='zwps_pool')
self.root_volume.migrate.assert_called_with(self.zwps_storage_pool, live_migrate=True,
source_host=self.source_host, vm=self.vm)
source_host=self.source_host, vm_instance=self.vm['instancename'])
13 changes: 7 additions & 6 deletions tests/test_live_migrate_virtual_machine_volumes.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def _setup_mocks(self):
'zonename': 'zone',
'hostid': 'sh1',
'maintenancepolicy': 'LiveMigrate',
'instancename': 'i-VM-1'
'instancename': 'i-VM-1',
'state': 'Running'
})
self.host = CosmicHost(Mock(), {
'id': 'sh1',
Expand Down Expand Up @@ -106,13 +107,13 @@ def test_main(self):
self.co_instance.get_host.assert_called_with(id=self.vm['hostid'])
self.co_instance.get_cluster.assert_called_with(id=self.host['clusterid'])
self.cs_instance.update_zwps_to_cwps.assert_not_called()
self.host.get_disks.assert_called_with(self.vm)
self.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.host.set_iops_limit.assert_has_calls([call(self.vm, 1000), call(self.vm, 0)])
self.host.merge_backing_files.assert_called_with(self.vm)
self.host.set_iops_limit.assert_has_calls([call(self.vm['instancename'], 1000), call(self.vm['instancename'], 0)])
self.host.merge_backing_files.assert_called_with(self.vm['instancename'])
self.vm.get_volumes.assert_called()
self.volume.migrate.assert_called_with(self.target_storage_pool, live_migrate=True, source_host=self.host, vm=self.vm)
self.volume.migrate.assert_called_with(self.target_storage_pool, live_migrate=True, source_host=self.host, vm_instance=self.vm['instancename'])
self.volume.refresh.assert_called()

def test_main_dry_run(self):
Expand Down Expand Up @@ -186,7 +187,7 @@ def test_continues(self):
self.volume.migrate.assert_not_called()

self._setup_mocks()
self.source_storage_pool['scope'] = 'Host'
self.source_storage_pool['scope'] = 'HOST'
self.assertEqual(0, self.runner.invoke(live_migrate_virtual_machine_volumes.main,
['--exec', '-p', 'profile', 'vm', 'target_pool']).exit_code)
self.assertEqual(2, self.co_instance.get_storage_pool.call_count)
Expand Down

0 comments on commit 2f2168b

Please sign in to comment.