def test_list_base_images(self): def fake_get_dynamic_property(vim, mobj, type, property_name): return 'fake-ds-browser' def fake_get_sub_folders(session, ds_browser, ds_path): files = set() files.add('image-ref-uuid') return files with contextlib.nested( mock.patch.object(vim_util, 'get_dynamic_property', fake_get_dynamic_property), mock.patch.object(ds_util, 'get_sub_folders', fake_get_sub_folders) ) as (_get_dynamic, _get_sub_folders): fake_ds_ref = fake.ManagedObjectReference('fake-ds-ref') datastore = ds_util.Datastore(name='ds', ref=fake_ds_ref) ds_path = datastore.build_path('base_folder') images = self._imagecache._list_datastore_images( ds_path, datastore) originals = set() originals.add('image-ref-uuid') self.assertEqual({'originals': originals, 'unexplained_images': []}, images)
def test_ds(self): ds = ds_util.Datastore("fake_ref", "ds_name", 2 * units.Gi, 1 * units.Gi) self.assertEqual('ds_name', ds.name) self.assertEqual('fake_ref', ds.ref) self.assertEqual(2 * units.Gi, ds.capacity) self.assertEqual(1 * units.Gi, ds.freespace)
def test_enlist_image(self, mock_get_ds_browser, mock_timestamp_cleanup, mock_timestamp_folder_get): image_id = "fake_image_id" dc_ref = "fake_dc_ref" fake_ds_ref = mock.Mock() ds = ds_util.Datastore( ref=fake_ds_ref, name='fake_ds', capacity=1, freespace=1) ds_browser = mock.Mock() mock_get_ds_browser.return_value = ds_browser timestamp_folder_path = mock.Mock() mock_timestamp_folder_get.return_value = timestamp_folder_path self._imagecache.enlist_image(image_id, ds, dc_ref) cache_root_folder = ds.build_path("fake-base-folder") mock_get_ds_browser.assert_called_once_with( ds.ref) mock_timestamp_folder_get.assert_called_once_with( cache_root_folder, "fake_image_id") mock_timestamp_cleanup.assert_called_once_with( dc_ref, ds_browser, timestamp_folder_path)
def test_filter_datastores_specific_match(self): data = [ ['VMFS', 'os-some-name', True, 'normal', 987654321, 1234678], ['NFS', 'another-name', True, 'normal', 9876543210, 123467890], ['BAD', 'some-name-bad', True, 'normal', 98765432100, 1234678900], ['VMFS', 'some-name-good', True, 'normal', 987654321, 12346789], ['VMFS', 'some-other-good', False, 'normal', 987654321000, 12346789000], ['VMFS', 'new-name', True, 'inMaintenance', 987654321000, 12346789000] ] # only the DS some-name-good is accessible and matches the regex datastores = self.build_result_set(data) datastore_regex = re.compile('.*-good$') best_match = ds_util.Datastore(ref='fake_ref', name='ds', capacity=0, freespace=0) rec = ds_util._select_datastore(None, datastores, best_match, datastore_regex) self.assertIsNotNone(rec, "could not find datastore!") self.assertEqual('ds-003', rec.ref.value, "didn't find the right datastore!") self.assertNotEqual('ds-004', rec.ref.value, "accepted an unreachable datastore!") self.assertEqual('some-name-good', rec.name) self.assertEqual(12346789, rec.freespace, "did not obtain correct freespace!") self.assertEqual(987654321, rec.capacity, "did not obtain correct capacity!")
def test_filter_datastores_empty(self): data = [] datastores = self.build_result_set(data) best_match = ds_util.Datastore(ref='fake_ref', name='ds', capacity=0, freespace=0) rec = ds_util._select_datastore(None, datastores, best_match) self.assertEqual(rec, best_match)
def test_age_cached_images(self): def fake_get_ds_browser(ds_ref): return 'fake-ds-browser' def fake_get_timestamp(ds_browser, ds_path): self._get_timestamp_called += 1 path = str(ds_path) if path == '[fake-ds] fake-path/fake-image-1': # No time stamp exists return if path == '[fake-ds] fake-path/fake-image-2': # Timestamp that will be valid => no deletion return 'ts-2012-11-22-10-00-00' if path == '[fake-ds] fake-path/fake-image-3': # Timestamp that will be invalid => deletion return 'ts-2012-11-20-12-00-00' self.fail() def fake_mkdir(session, ts_path, dc_ref): self.assertEqual( '[fake-ds] fake-path/fake-image-1/ts-2012-11-22-12-00-00', str(ts_path)) def fake_file_delete(session, ds_path, dc_ref): self.assertEqual('[fake-ds] fake-path/fake-image-3', str(ds_path)) def fake_timestamp_cleanup(dc_ref, ds_browser, ds_path): self.assertEqual('[fake-ds] fake-path/fake-image-4', str(ds_path)) with contextlib.nested( mock.patch.object(self._imagecache, '_get_ds_browser', fake_get_ds_browser), mock.patch.object(self._imagecache, '_get_timestamp', fake_get_timestamp), mock.patch.object(ds_util, 'mkdir', fake_mkdir), mock.patch.object(ds_util, 'file_delete', fake_file_delete), mock.patch.object(self._imagecache, 'timestamp_cleanup', fake_timestamp_cleanup), ) as (_get_ds_browser, _get_timestamp, _mkdir, _file_delete, _timestamp_cleanup): timeutils.set_time_override(override_time=self._time) datastore = ds_util.Datastore(name='ds', ref='fake-ds-ref') dc_info = vmops.DcInfo(ref='dc_ref', name='name', vmFolder='vmFolder') self._get_timestamp_called = 0 self._imagecache.originals = set(['fake-image-1', 'fake-image-2', 'fake-image-3', 'fake-image-4']) self._imagecache.used_images = set(['fake-image-4']) self._imagecache._age_cached_images( 'fake-context', datastore, dc_info, ds_util.DatastorePath('fake-ds', 'fake-path')) self.assertEqual(3, self._get_timestamp_called)
def test_filter_datastores_no_match(self): datastores = self.build_result_set(self.data) datastore_regex = re.compile('no_match.*') best_match = ds_util.Datastore(ref='fake_ref', name='ds', capacity=0, freespace=0) rec = ds_util._select_datastore(None, datastores, best_match, datastore_regex) self.assertEqual(rec, best_match, "did not match datastore properly")
def test_filter_datastores_simple(self): datastores = self.build_result_set(self.data) best_match = ds_util.Datastore(ref='fake_ref', name='ds', capacity=0, freespace=0) rec = ds_util._select_datastore(None, datastores, best_match) self.assertIsNotNone(rec.ref, "could not find datastore!") self.assertEqual('ds-001', rec.ref.value, "didn't find the right datastore!") self.assertEqual(123467890, rec.freespace, "did not obtain correct freespace!")
def test_filter_datastores_missing_props(self): data = [ ['VMFS', 'os-some-name', 987654321, 1234678], ['NFS', 'another-name', 9876543210, 123467890], ] # no matches are expected when 'summary.accessible' is missing prop_names = ['summary.type', 'summary.name', 'summary.capacity', 'summary.freeSpace'] datastores = self.build_result_set(data, prop_names) best_match = ds_util.Datastore(ref='fake_ref', name='ds', capacity=0, freespace=0) rec = ds_util._select_datastore(None, datastores, best_match) self.assertEqual(rec, best_match, "no matches were expected")
def test_update(self): def fake_list_datastore_images(ds_path, datastore): return {'unexplained_images': [], 'originals': self.images} def fake_age_cached_images(context, datastore, dc_info, ds_path): self.assertEqual('[ds] fake-base-folder', str(ds_path)) self.assertEqual(self.images, self._imagecache.used_images) self.assertEqual(self.images, self._imagecache.originals) with contextlib.nested( mock.patch.object(self._imagecache, '_list_datastore_images', fake_list_datastore_images), mock.patch.object( self._imagecache, '_age_cached_images', fake_age_cached_images)) as (_list_base, _age_and_verify): instances = [{ 'image_ref': '1', 'host': CONF.host, 'name': 'inst-1', 'uuid': '123', 'vm_state': '', 'task_state': '' }, { 'image_ref': '2', 'host': CONF.host, 'name': 'inst-2', 'uuid': '456', 'vm_state': '', 'task_state': '' }] all_instances = [] for instance in instances: all_instances.append( fake_instance.fake_instance_obj(None, **instance)) self.images = set(['1', '2']) datastore = ds_util.Datastore(name='ds', ref='fake-ds-ref') dc_info = vmops.DcInfo(ref='dc_ref', name='name', vmFolder='vmFolder') datastores_info = [(datastore, dc_info)] self._imagecache.update('context', all_instances, datastores_info)
def test_filter_datastores_best_match(self): data = [ ['VMFS', 'spam-good', True, 20 * units.Gi, 10 * units.Gi], ['NFS', 'eggs-good', True, 40 * units.Gi, 15 * units.Gi], ['BAD', 'some-name-bad', True, 30 * units.Gi, 20 * units.Gi], ['VMFS', 'some-name-good', True, 50 * units.Gi, 5 * units.Gi], ['VMFS', 'some-other-good', True, 10 * units.Gi, 10 * units.Gi], ] datastores = self.build_result_set(data) datastore_regex = re.compile('.*-good$') # the current best match is better than all candidates best_match = ds_util.Datastore(ref='ds-100', name='best-ds-good', capacity=20 * units.Gi, freespace=19 * units.Gi) rec = ds_util._select_datastore(None, datastores, best_match, datastore_regex) self.assertEqual(rec, best_match, "did not match datastore properly")
def test_build_path(self): ds = ds_util.Datastore("fake_ref", "ds_name") ds_path = ds.build_path("some_dir", "foo.vmdk") self.assertEqual('[ds_name] some_dir/foo.vmdk', str(ds_path))
def test_ds_no_capacity_no_freespace(self): ds = ds_util.Datastore("fake_ref", "ds_name") self.assertIsNone(ds.capacity) self.assertIsNone(ds.freespace)
def _test_finish_migration(self, power_on=True, resize_instance=False): """Tests the finish_migration method on vmops.""" if resize_instance: self._instance.system_metadata = {'old_instance_type_root_gb': '0'} datastore = ds_util.Datastore(ref='fake-ref', name='fake') dc_info = vmops.DcInfo(ref='fake_ref', name='fake', vmFolder='fake_folder') with contextlib.nested( mock.patch.object(self._session, "_call_method", return_value='fake-task'), mock.patch.object(self._vmops, "_update_instance_progress"), mock.patch.object(self._session, "_wait_for_task"), mock.patch.object(vm_util, "get_vm_resize_spec", return_value='fake-spec'), mock.patch.object(ds_util, "get_datastore", return_value=datastore), mock.patch.object(self._vmops, 'get_datacenter_ref_and_name', return_value=dc_info), mock.patch.object(self._vmops, '_extend_virtual_disk'), mock.patch.object( vm_util, "power_on_instance")) as (fake_call_method, fake_update_instance_progress, fake_wait_for_task, fake_vm_resize_spec, fake_get_datastore, fake_get_datacenter_ref_and_name, fake_extend_virtual_disk, fake_power_on): self._vmops.finish_migration(context=self._context, migration=None, instance=self._instance, disk_info=None, network_info=None, block_device_info=None, resize_instance=resize_instance, image_meta=None, power_on=power_on) if resize_instance: fake_vm_resize_spec.assert_called_once_with( self._session._get_vim().client.factory, self._instance) fake_call_method.assert_has_calls( mock.call(self._session._get_vim(), "ReconfigVM_Task", 'f', spec='fake-spec')) fake_wait_for_task.assert_called_once_with('fake-task') fake_extend_virtual_disk.assert_called_once_with( self._instance, self._instance['root_gb'] * units.Mi, None, dc_info.ref) else: self.assertFalse(fake_vm_resize_spec.called) self.assertFalse(fake_wait_for_task.called) self.assertFalse(fake_extend_virtual_disk.called) if power_on: fake_power_on.assert_called_once_with(self._session, self._instance, vm_ref='f') else: self.assertFalse(fake_power_on.called) fake_update_instance_progress.called_once_with( self._context, self._instance, 4, vmops.RESIZE_TOTAL_STEPS)
def setUp(self): super(VMwareVMOpsTestCase, self).setUp() vmwareapi_fake.reset() stubs.set_stubs(self.stubs) self.flags(image_cache_subdirectory_name='vmware_base', my_ip='', flat_injected=True, vnc_enabled=True) self._context = context.RequestContext('fake_user', 'fake_project') self._session = driver.VMwareAPISession() self._virtapi = mock.Mock() self._vmops = vmops.VMwareVCVMOps(self._session, self._virtapi, None) self._image_id = nova.tests.image.fake.get_valid_image_id() values = { 'name': 'fake_name', 'uuid': 'fake_uuid', 'vcpus': 1, 'memory_mb': 512, 'image_ref': self._image_id, 'root_gb': 1, 'node': 'respool-1001(MyResPoolName)' } self._instance = fake_instance.fake_instance_obj( self._context, **values) fake_ds_ref = vmwareapi_fake.ManagedObjectReference('fake-ds') self._ds = ds_util.Datastore(ref=fake_ds_ref, name='fake_ds', capacity=10 * units.Gi, freespace=10 * units.Gi) self._dc_info = vmops.DcInfo(ref='fake_dc_ref', name='fake_dc', vmFolder='fake_vm_folder') subnet_4 = network_model.Subnet( cidr='192.168.0.1/24', dns=[network_model.IP('192.168.0.1')], gateway=network_model.IP('192.168.0.1'), ips=[network_model.IP('192.168.0.100')], routes=None) subnet_6 = network_model.Subnet( cidr='dead:beef::1/64', dns=None, gateway=network_model.IP('dead:beef::1'), ips=[network_model.IP('dead:beef::dcad:beff:feef:0')], routes=None) network = network_model.Network(id=0, bridge='fa0', label='fake', subnets=[subnet_4, subnet_6], vlan=None, bridge_interface=None, injected=True) self.network_info = network_model.NetworkInfo([ network_model.VIF(id=None, address='DE:AD:BE:EF:00:00', network=network, type=None, devname=None, ovs_interfaceid=None, rxtx_cap=3) ]) pure_IPv6_network = network_model.Network(id=0, bridge='fa0', label='fake', subnets=[subnet_6], vlan=None, bridge_interface=None, injected=True) self.pure_IPv6_network_info = network_model.NetworkInfo([ network_model.VIF(id=None, address='DE:AD:BE:EF:00:00', network=pure_IPv6_network, type=None, devname=None, ovs_interfaceid=None, rxtx_cap=3) ])