def test_create_export(self, mock_generate_password, mock_generate_username, mock_get_target_name): tgt_utils = self._driver._tgt_utils fake_volume = db_fakes.get_fake_volume_info() self._driver.configuration.chap_username = None self._driver.configuration.chap_password = None self._driver.configuration.use_chap_auth = True fake_chap_username = "******" fake_chap_password = "******" mock_get_target_name.return_value = mock.sentinel.target_name mock_generate_username.return_value = fake_chap_username mock_generate_password.return_value = fake_chap_password tgt_utils.iscsi_target_exists.return_value = False vol_updates = self._driver.create_export(mock.sentinel.context, fake_volume, mock.sentinel.connector) mock_get_target_name.assert_called_once_with(fake_volume) tgt_utils.iscsi_target_exists.assert_called_once_with(mock.sentinel.target_name) tgt_utils.set_chap_credentials.assert_called_once_with( mock.sentinel.target_name, fake_chap_username, fake_chap_password ) tgt_utils.add_disk_to_target.assert_called_once_with(fake_volume["name"], mock.sentinel.target_name) expected_provider_auth = " ".join(("CHAP", fake_chap_username, fake_chap_password)) expected_vol_updates = dict(provider_location=mock.sentinel.target_name, provider_auth=expected_provider_auth) self.assertEqual(expected_vol_updates, vol_updates)
def test_copy_image_to_volume(self, mock_unlink, mock_fetch_to_vhd, mock_tmp_file, mock_local_path): tgt_utils = self._driver._tgt_utils fake_volume = db_fakes.get_fake_volume_info() mock_tmp_file.return_value.__enter__.return_value = ( mock.sentinel.tmp_vhd_path) mock_local_path.return_value = mock.sentinel.vol_vhd_path self._driver.copy_image_to_volume(mock.sentinel.context, fake_volume, mock.sentinel.image_service, mock.sentinel.image_id) mock_local_path.assert_called_once_with(fake_volume) mock_tmp_file.assert_called_once_with(suffix='.vhd') image_utils.fetch_to_vhd.assert_called_once_with( mock.sentinel.context, mock.sentinel.image_service, mock.sentinel.image_id, mock.sentinel.tmp_vhd_path, self._driver.configuration.volume_dd_blocksize) mock_unlink.assert_called_once_with(mock.sentinel.vol_vhd_path) self._driver._vhdutils.convert_vhd.assert_called_once_with( mock.sentinel.tmp_vhd_path, mock.sentinel.vol_vhd_path, tgt_utils.get_supported_vhd_type.return_value) self._driver._vhdutils.resize_vhd.assert_called_once_with( mock.sentinel.vol_vhd_path, fake_volume['size'] * units.Gi, is_file_max_size=False) tgt_utils.change_wt_disk_status.assert_has_calls([ mock.call(fake_volume['name'], enabled=False), mock.call(fake_volume['name'], enabled=True) ])
def test_copy_volume_to_image(self, mock_delete_if_exists, mock_upload_volume, mock_tmp_snap): tgt_utils = self._driver._tgt_utils disk_format = 'vhd' fake_image_meta = db_fakes.get_fake_image_meta() fake_volume = db_fakes.get_fake_volume_info() fake_img_conv_dir = 'fake_img_conv_dir' self.flags(image_conversion_dir=fake_img_conv_dir) tgt_utils.get_supported_disk_format.return_value = disk_format mock_tmp_snap.return_value.__enter__.return_value = ( mock.sentinel.tmp_snap_name) expected_tmp_vhd_path = os.path.join( fake_img_conv_dir, fake_image_meta['id'] + '.' + disk_format) self._driver.copy_volume_to_image(mock.sentinel.context, fake_volume, mock.sentinel.image_service, fake_image_meta) mock_tmp_snap.assert_called_once_with(fake_volume['name']) tgt_utils.export_snapshot.assert_called_once_with( mock.sentinel.tmp_snap_name, expected_tmp_vhd_path) mock_upload_volume.assert_called_once_with(mock.sentinel.context, mock.sentinel.image_service, fake_image_meta, expected_tmp_vhd_path, 'vhd') mock_delete_if_exists.assert_called_once_with(expected_tmp_vhd_path)
def test_get_host_information(self, mock_get_target_name): tgt_utils = self._driver._tgt_utils fake_auth_meth = "CHAP" fake_chap_username = "******" fake_chap_password = "******" fake_host_info = {"fake_prop": "fake_value"} fake_volume = db_fakes.get_fake_volume_info() fake_volume["provider_auth"] = "%s %s %s" % (fake_auth_meth, fake_chap_username, fake_chap_password) mock_get_target_name.return_value = mock.sentinel.target_name tgt_utils.get_portal_locations.return_value = [mock.sentinel.portal_location] tgt_utils.get_target_information.return_value = fake_host_info expected_host_info = dict( fake_host_info, auth_method=fake_auth_meth, auth_username=fake_chap_username, auth_password=fake_chap_password, target_discovered=False, target_portal=mock.sentinel.portal_location, target_lun=0, volume_id=fake_volume["id"], ) host_info = self._driver._get_host_information(fake_volume) self.assertEqual(expected_host_info, host_info) mock_get_target_name.assert_called_once_with(fake_volume) tgt_utils.get_portal_locations.assert_called_once_with() tgt_utils.get_target_information.assert_called_once_with(mock.sentinel.target_name)
def test_get_host_information(self, mock_get_target_name): tgt_utils = self._driver._tgt_utils fake_auth_meth = 'CHAP' fake_chap_username = '******' fake_chap_password = '******' fake_host_info = {'fake_prop': 'fake_value'} fake_volume = db_fakes.get_fake_volume_info() fake_volume['provider_auth'] = "%s %s %s" % ( fake_auth_meth, fake_chap_username, fake_chap_password) mock_get_target_name.return_value = mock.sentinel.target_name tgt_utils.get_portal_locations.return_value = [ mock.sentinel.portal_location ] tgt_utils.get_target_information.return_value = fake_host_info expected_host_info = dict(fake_host_info, auth_method=fake_auth_meth, auth_username=fake_chap_username, auth_password=fake_chap_password, target_discovered=False, target_portal=mock.sentinel.portal_location, target_lun=0, volume_id=fake_volume['id']) host_info = self._driver._get_host_information(fake_volume) self.assertEqual(expected_host_info, host_info) mock_get_target_name.assert_called_once_with(fake_volume) tgt_utils.get_portal_locations.assert_called_once_with() tgt_utils.get_target_information.assert_called_once_with( mock.sentinel.target_name)
def test_remove_export(self, mock_get_target_name): fake_volume = db_fakes.get_fake_volume_info() self._driver.remove_export(mock.sentinel.context, fake_volume) mock_get_target_name.assert_called_once_with(fake_volume) self._driver._tgt_utils.delete_iscsi_target.assert_called_once_with(mock_get_target_name.return_value)
def test_create_export(self, mock_generate_password, mock_generate_username, mock_get_target_name): tgt_utils = self._driver._tgt_utils fake_volume = db_fakes.get_fake_volume_info() self._driver.configuration.chap_username = None self._driver.configuration.chap_password = None self._driver.configuration.use_chap_auth = True fake_chap_username = '******' fake_chap_password = '******' mock_get_target_name.return_value = mock.sentinel.target_name mock_generate_username.return_value = fake_chap_username mock_generate_password.return_value = fake_chap_password tgt_utils.iscsi_target_exists.return_value = False vol_updates = self._driver.create_export(mock.sentinel.context, fake_volume, mock.sentinel.connector) mock_get_target_name.assert_called_once_with(fake_volume) tgt_utils.iscsi_target_exists.assert_called_once_with( mock.sentinel.target_name) tgt_utils.set_chap_credentials.assert_called_once_with( mock.sentinel.target_name, fake_chap_username, fake_chap_password) tgt_utils.add_disk_to_target.assert_called_once_with( fake_volume['name'], mock.sentinel.target_name) expected_provider_auth = ' '.join( ('CHAP', fake_chap_username, fake_chap_password)) expected_vol_updates = dict( provider_location=mock.sentinel.target_name, provider_auth=expected_provider_auth) self.assertEqual(expected_vol_updates, vol_updates)
def test_copy_volume_to_image(self, mock_delete_if_exists, mock_upload_volume, mock_tmp_snap): tgt_utils = self._driver._tgt_utils disk_format = 'vhd' fake_image_meta = db_fakes.get_fake_image_meta() fake_volume = db_fakes.get_fake_volume_info() fake_img_conv_dir = 'fake_img_conv_dir' self.flags(image_conversion_dir=fake_img_conv_dir) tgt_utils.get_supported_disk_format.return_value = disk_format mock_tmp_snap.return_value.__enter__.return_value = ( mock.sentinel.tmp_snap_name) expected_tmp_vhd_path = os.path.join( fake_img_conv_dir, fake_image_meta['id'] + '.' + disk_format) self._driver.copy_volume_to_image( mock.sentinel.context, fake_volume, mock.sentinel.image_service, fake_image_meta) mock_tmp_snap.assert_called_once_with(fake_volume['name']) tgt_utils.export_snapshot.assert_called_once_with( mock.sentinel.tmp_snap_name, expected_tmp_vhd_path) mock_upload_volume.assert_called_once_with( mock.sentinel.context, mock.sentinel.image_service, fake_image_meta, expected_tmp_vhd_path, 'vhd') mock_delete_if_exists.assert_called_once_with( expected_tmp_vhd_path)
def test_copy_image_to_volume(self, mock_unlink, mock_fetch_to_vhd, mock_tmp_file, mock_local_path): tgt_utils = self._driver._tgt_utils fake_volume = db_fakes.get_fake_volume_info() mock_tmp_file.return_value.__enter__.return_value = mock.sentinel.tmp_vhd_path mock_local_path.return_value = mock.sentinel.vol_vhd_path self._driver.copy_image_to_volume( mock.sentinel.context, fake_volume, mock.sentinel.image_service, mock.sentinel.image_id ) mock_local_path.assert_called_once_with(fake_volume) mock_tmp_file.assert_called_once_with(suffix=".vhd") image_utils.fetch_to_vhd.assert_called_once_with( mock.sentinel.context, mock.sentinel.image_service, mock.sentinel.image_id, mock.sentinel.tmp_vhd_path, self._driver.configuration.volume_dd_blocksize, ) mock_unlink.assert_called_once_with(mock.sentinel.vol_vhd_path) self._driver._vhdutils.convert_vhd.assert_called_once_with( mock.sentinel.tmp_vhd_path, mock.sentinel.vol_vhd_path, tgt_utils.get_supported_vhd_type.return_value ) self._driver._vhdutils.resize_vhd.assert_called_once_with( mock.sentinel.vol_vhd_path, fake_volume["size"] * units.Gi, is_file_max_size=False ) tgt_utils.change_wt_disk_status.assert_has_calls( [mock.call(fake_volume["name"], enabled=False), mock.call(fake_volume["name"], enabled=True)] )
def test_create_cloned_volume(self): drv = self._driver volume = db_fakes.get_fake_volume_info() volume_cloned = db_fakes.get_fake_volume_info_cloned() new_vhd_path = self.fake_local_path(volume) src_vhd_path = self.fake_local_path(volume_cloned) self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'copy_vhd_disk') self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'import_wt_disk') self.mox.StubOutWithMock(vhdutils.VHDUtils, 'resize_vhd') self.stubs.Set(drv.utils, 'is_resize_needed', lambda vhd_path, new_size, old_size: True) self.stubs.Set(drv, 'local_path', self.fake_local_path) windows_utils.WindowsUtils.copy_vhd_disk(src_vhd_path, new_vhd_path) drv.utils.is_resize_needed(new_vhd_path, volume['size'], volume_cloned['size']) vhdutils.VHDUtils.resize_vhd(new_vhd_path, volume['size'] << 30) windows_utils.WindowsUtils.import_wt_disk(new_vhd_path, volume['name']) self.mox.ReplayAll() drv.create_cloned_volume(volume, volume_cloned)
def test_delete_volume(self, mock_delete_if_exists, mock_local_path): fake_volume = db_fakes.get_fake_volume_info() self._driver.delete_volume(fake_volume) mock_local_path.assert_called_once_with(fake_volume) self._driver._tgt_utils.remove_wt_disk.assert_called_once_with(fake_volume["name"]) mock_delete_if_exists.assert_called_once_with(mock_local_path.return_value)
def test_get_target_name(self): fake_volume = db_fakes.get_fake_volume_info() expected_target_name = "%s%s" % ( self._driver.configuration.iscsi_target_prefix, fake_volume['name']) target_name = self._driver._get_target_name(fake_volume) self.assertEqual(expected_target_name, target_name)
def test_terminate_connection(self): fake_volume = db_fakes.get_fake_volume_info() fake_initiator = db_fakes.get_fake_connector_info() self._driver.terminate_connection(fake_volume, fake_initiator) self._driver._tgt_utils.deassociate_initiator.assert_called_once_with( fake_initiator['initiator'], fake_volume['provider_location'])
def test_extend_volume(self): fake_volume = db_fakes.get_fake_volume_info() new_size_gb = 2 expected_additional_sz_mb = 1024 self._driver.extend_volume(fake_volume, new_size_gb) self._driver._tgt_utils.extend_wt_disk.assert_called_once_with(fake_volume["name"], expected_additional_sz_mb)
def test_remove_export(self, mock_get_target_name): fake_volume = db_fakes.get_fake_volume_info() self._driver.remove_export(mock.sentinel.context, fake_volume) mock_get_target_name.assert_called_once_with(fake_volume) self._driver._tgt_utils.delete_iscsi_target.assert_called_once_with( mock_get_target_name.return_value)
def test_create_volume(self, mock_local_path): fake_volume = db_fakes.get_fake_volume_info() self._driver.create_volume(fake_volume) mock_local_path.assert_called_once_with(fake_volume) self._driver._tgt_utils.create_wt_disk.assert_called_once_with( mock_local_path.return_value, fake_volume["name"], size_mb=fake_volume["size"] * 1024 )
def test_extend_volume(self): fake_volume = db_fakes.get_fake_volume_info() new_size_gb = 2 expected_additional_sz_mb = 1024 self._driver.extend_volume(fake_volume, new_size_gb) self._driver._tgt_utils.extend_wt_disk.assert_called_once_with( fake_volume['name'], expected_additional_sz_mb)
def test_create_volume_from_snapshot(self, mock_local_path): fake_volume = db_fakes.get_fake_volume_info() fake_snapshot = db_fakes.get_fake_snapshot_info() self._driver.create_volume_from_snapshot(fake_volume, fake_snapshot) self._driver._tgt_utils.export_snapshot.assert_called_once_with( fake_snapshot['name'], mock_local_path.return_value) self._driver._tgt_utils.import_wt_disk.assert_called_once_with( mock_local_path.return_value, fake_volume['name'])
def test_delete_volume(self, mock_delete_if_exists, mock_local_path): fake_volume = db_fakes.get_fake_volume_info() self._driver.delete_volume(fake_volume) mock_local_path.assert_called_once_with(fake_volume) self._driver._tgt_utils.remove_wt_disk.assert_called_once_with( fake_volume['name']) mock_delete_if_exists.assert_called_once_with( mock_local_path.return_value)
def test_create_volume(self, mock_local_path): fake_volume = db_fakes.get_fake_volume_info() self._driver.create_volume(fake_volume) mock_local_path.assert_called_once_with(fake_volume) self._driver._tgt_utils.create_wt_disk.assert_called_once_with( mock_local_path.return_value, fake_volume['name'], size_mb=fake_volume['size'] * 1024)
def test_create_volume_from_snapshot(self, mock_local_path): fake_volume = db_fakes.get_fake_volume_info() fake_snapshot = db_fakes.get_fake_snapshot_info() self._driver.create_volume_from_snapshot(fake_volume, fake_snapshot) self._driver._tgt_utils.export_snapshot.assert_called_once_with( fake_snapshot["name"], mock_local_path.return_value ) self._driver._tgt_utils.import_wt_disk.assert_called_once_with( mock_local_path.return_value, fake_volume["name"] )
def test_create_volume(self): drv = self._driver vol = db_fakes.get_fake_volume_info() self.stubs.Set(drv, 'local_path', self.fake_local_path) self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'create_volume') windows_utils.WindowsUtils.create_volume(self.fake_local_path(vol), vol['name'], vol['size']) self.mox.ReplayAll() drv.create_volume(vol)
def test_create_volume_from_snapshot(self): drv = self._driver snapshot = db_fakes.get_fake_snapshot_info() volume = db_fakes.get_fake_volume_info() self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'create_volume_from_snapshot') windows_utils.WindowsUtils.\ create_volume_from_snapshot(volume, snapshot['name']) self.mox.ReplayAll() drv.create_volume_from_snapshot(volume, snapshot)
def test_remove_export(self): drv = self._driver volume = db_fakes.get_fake_volume_info() target_name = volume['provider_location'] self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'remove_iscsi_target') windows_utils.WindowsUtils.remove_iscsi_target(target_name) self.mox.ReplayAll() drv.remove_export(None, volume)
def test_create_snapshot(self): drv = self._driver self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'create_snapshot') volume = db_fakes.get_fake_volume_info() snapshot = db_fakes.get_fake_snapshot_info() self.stubs.Set(drv, 'local_path', self.fake_local_path(snapshot)) windows_utils.WindowsUtils.create_snapshot(volume['name'], snapshot['name']) self.mox.ReplayAll() drv.create_snapshot(snapshot)
def test_delete_volume(self): """delete_volume simple test case.""" drv = self._driver vol = db_fakes.get_fake_volume_info() self.mox.StubOutWithMock(drv, 'local_path') drv.local_path(vol).AndReturn(self.fake_local_path(vol)) self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'delete_volume') windows_utils.WindowsUtils.delete_volume(vol['name'], self.fake_local_path(vol)) self.mox.ReplayAll() drv.delete_volume(vol)
def test_terminate_connection(self): drv = self._driver volume = db_fakes.get_fake_volume_info() initiator_name = "%s%s" % (CONF.iscsi_target_prefix, volume['name']) connector = db_fakes.get_fake_connector_info() self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'delete_iscsi_target') windows_utils.WindowsUtils.delete_iscsi_target( initiator_name, volume['provider_location']) self.mox.ReplayAll() drv.terminate_connection(volume, connector)
def test_initialize_connection(self, mock_get_host_info): tgt_utils = self._driver._tgt_utils fake_volume = db_fakes.get_fake_volume_info() fake_initiator = db_fakes.get_fake_connector_info() fake_host_info = {"fake_host_prop": "fake_value"} mock_get_host_info.return_value = fake_host_info expected_conn_info = {"driver_volume_type": "iscsi", "data": fake_host_info} conn_info = self._driver.initialize_connection(fake_volume, fake_initiator) self.assertEqual(expected_conn_info, conn_info) mock_associate = tgt_utils.associate_initiator_with_iscsi_target mock_associate.assert_called_once_with(fake_initiator["initiator"], fake_volume["provider_location"])
def test_local_path(self): fake_volume = db_fakes.get_fake_volume_info() fake_lun_path = 'fake_lun_path' self.flags(windows_iscsi_lun_path=fake_lun_path) disk_format = 'vhd' mock_get_fmt = self._driver._tgt_utils.get_supported_disk_format mock_get_fmt.return_value = disk_format disk_path = self._driver.local_path(fake_volume) expected_fname = "%s.%s" % (fake_volume['name'], disk_format) expected_disk_path = os.path.join(fake_lun_path, expected_fname) self.assertEqual(expected_disk_path, disk_path) mock_get_fmt.assert_called_once_with()
def test_local_path(self): fake_volume = db_fakes.get_fake_volume_info() fake_lun_path = "fake_lun_path" self.flags(windows_iscsi_lun_path=fake_lun_path) disk_format = "vhd" mock_get_fmt = self._driver._tgt_utils.get_supported_disk_format mock_get_fmt.return_value = disk_format disk_path = self._driver.local_path(fake_volume) expected_fname = "%s.%s" % (fake_volume["name"], disk_format) expected_disk_path = os.path.join(fake_lun_path, expected_fname) self.assertEqual(expected_disk_path, disk_path) mock_get_fmt.assert_called_once_with()
def test_create_cloned_volume(self, mock_local_path, mock_tmp_snap): tgt_utils = self._driver._tgt_utils fake_volume = db_fakes.get_fake_volume_info() fake_src_volume = db_fakes.get_fake_volume_info_cloned() mock_tmp_snap.return_value.__enter__.return_value = mock.sentinel.tmp_snap_name mock_local_path.return_value = mock.sentinel.vol_vhd_path self._driver.create_cloned_volume(fake_volume, fake_src_volume) mock_tmp_snap.assert_called_once_with(fake_src_volume["name"]) tgt_utils.export_snapshot.assert_called_once_with(mock.sentinel.tmp_snap_name, mock.sentinel.vol_vhd_path) self._driver._vhdutils.resize_vhd.assert_called_once_with( mock.sentinel.vol_vhd_path, fake_volume["size"] * units.Gi, is_file_max_size=False ) tgt_utils.import_wt_disk.assert_called_once_with(mock.sentinel.vol_vhd_path, fake_volume["name"])
def test_copy_image_to_volume(self): """resize_image common case usage.""" drv = self._driver volume = db_fakes.get_fake_volume_info() fake_get_supported_type = lambda x: constants.VHD_TYPE_FIXED self.stubs.Set(drv, 'local_path', self.fake_local_path) self.stubs.Set(windows_utils.WindowsUtils, 'get_supported_vhd_type', fake_get_supported_type) self.mox.StubOutWithMock(os, 'unlink') self.mox.StubOutWithMock(image_utils, 'create_temporary_file') self.mox.StubOutWithMock(image_utils, 'fetch_to_vhd') self.mox.StubOutWithMock(vhdutils.VHDUtils, 'convert_vhd') self.mox.StubOutWithMock(vhdutils.VHDUtils, 'resize_vhd') self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'change_disk_status') fake_temp_path = r'C:\fake\temp\file' if (CONF.image_conversion_dir and not os.path.exists(CONF.image_conversion_dir)): os.makedirs(CONF.image_conversion_dir) image_utils.create_temporary_file(suffix='.vhd').AndReturn( fake_temp_path) fake_volume_path = self.fake_local_path(volume) image_utils.fetch_to_vhd(None, None, None, fake_temp_path, mox.IgnoreArg()) windows_utils.WindowsUtils.change_disk_status(volume['name'], mox.IsA(bool)) vhdutils.VHDUtils.convert_vhd(fake_temp_path, fake_volume_path, constants.VHD_TYPE_FIXED) vhdutils.VHDUtils.resize_vhd(fake_volume_path, volume['size'] << 30) windows_utils.WindowsUtils.change_disk_status(volume['name'], mox.IsA(bool)) os.unlink(mox.IsA(str)) self.mox.ReplayAll() drv.copy_image_to_volume(None, volume, None, None)
def test_ensure_export(self): drv = self._driver volume = db_fakes.get_fake_volume_info() initiator_name = "%s%s" % (CONF.iscsi_target_prefix, volume['name']) self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'create_iscsi_target') windows_utils.WindowsUtils.create_iscsi_target(initiator_name, True) self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'add_disk_to_target') windows_utils.WindowsUtils.add_disk_to_target(volume['name'], initiator_name) self.mox.ReplayAll() drv.ensure_export(None, volume)
def test_extend_volume(self): drv = self._driver volume = db_fakes.get_fake_volume_info() TEST_VOLUME_ADDITIONAL_SIZE_MB = 1024 TEST_VOLUME_ADDITIONAL_SIZE_GB = 1 self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'extend') windows_utils.WindowsUtils.extend(volume['name'], TEST_VOLUME_ADDITIONAL_SIZE_MB) new_size = volume['size'] + TEST_VOLUME_ADDITIONAL_SIZE_GB self.mox.ReplayAll() drv.extend_volume(volume, new_size)
def _test_create_export(self, chap_enabled=False): drv = self._driver volume = db_fakes.get_fake_volume_info() initiator_name = "%s%s" % (CONF.iscsi_target_prefix, volume['name']) fake_chap_username = '******' fake_chap_password = '******' self.flags(use_chap_auth=chap_enabled) self.flags(chap_username=fake_chap_username) self.flags(chap_password=fake_chap_password) self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'add_disk_to_target') self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'create_iscsi_target') self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'set_chap_credentials') self.mox.StubOutWithMock(self._driver, 'remove_export') self._driver.remove_export(mox.IgnoreArg(), mox.IgnoreArg()) windows_utils.WindowsUtils.create_iscsi_target(initiator_name) if chap_enabled: windows_utils.WindowsUtils.set_chap_credentials( mox.IgnoreArg(), fake_chap_username, fake_chap_password) windows_utils.WindowsUtils.add_disk_to_target(volume['name'], initiator_name) self.mox.ReplayAll() export_info = drv.create_export(None, volume, {}) self.assertEqual(initiator_name, export_info['provider_location']) if chap_enabled: expected_provider_auth = ' '.join(('CHAP', fake_chap_username, fake_chap_password)) self.assertEqual(expected_provider_auth, export_info['provider_auth'])
def test_initialize_connection(self, mock_get_host_info): tgt_utils = self._driver._tgt_utils fake_volume = db_fakes.get_fake_volume_info() fake_initiator = db_fakes.get_fake_connector_info() fake_host_info = {'fake_host_prop': 'fake_value'} mock_get_host_info.return_value = fake_host_info expected_conn_info = {'driver_volume_type': 'iscsi', 'data': fake_host_info} conn_info = self._driver.initialize_connection(fake_volume, fake_initiator) self.assertEqual(expected_conn_info, conn_info) mock_associate = tgt_utils.associate_initiator_with_iscsi_target mock_associate.assert_called_once_with( fake_initiator['initiator'], fake_volume['provider_location'])
def test_create_export(self): drv = self._driver volume = db_fakes.get_fake_volume_info() initiator_name = "%s%s" % (CONF.iscsi_target_prefix, volume['name']) self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'create_iscsi_target') windows_utils.WindowsUtils.create_iscsi_target(initiator_name) self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'add_disk_to_target') windows_utils.WindowsUtils.add_disk_to_target(volume['name'], initiator_name) self.mox.ReplayAll() export_info = drv.create_export(None, volume) self.assertEqual(export_info['provider_location'], initiator_name)
def test_initialize_connection(self, mock_get_host_info): tgt_utils = self._driver._tgt_utils fake_volume = db_fakes.get_fake_volume_info() fake_initiator = db_fakes.get_fake_connector_info() fake_host_info = {'fake_host_prop': 'fake_value'} mock_get_host_info.return_value = fake_host_info expected_conn_info = { 'driver_volume_type': 'iscsi', 'data': fake_host_info } conn_info = self._driver.initialize_connection(fake_volume, fake_initiator) self.assertEqual(expected_conn_info, conn_info) mock_associate = tgt_utils.associate_initiator_with_iscsi_target mock_associate.assert_called_once_with( fake_initiator['initiator'], fake_volume['provider_location'])
def _test_copy_volume_to_image(self, supported_format): drv = self._driver vol = db_fakes.get_fake_volume_info() image_meta = db_fakes.get_fake_image_meta() fake_get_supported_format = lambda x: supported_format self.stubs.Set(os.path, 'exists', lambda x: False) self.stubs.Set(drv, 'local_path', self.fake_local_path) self.stubs.Set(windows_utils.WindowsUtils, 'get_supported_format', fake_get_supported_format) self.mox.StubOutWithMock(fileutils, 'ensure_tree') self.mox.StubOutWithMock(fileutils, 'delete_if_exists') self.mox.StubOutWithMock(image_utils, 'upload_volume') self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'copy_vhd_disk') self.mox.StubOutWithMock(vhdutils.VHDUtils, 'convert_vhd') fileutils.ensure_tree(CONF.image_conversion_dir) temp_vhd_path = os.path.join(CONF.image_conversion_dir, str(image_meta['id']) + "." + supported_format) upload_image = temp_vhd_path windows_utils.WindowsUtils.copy_vhd_disk(self.fake_local_path(vol), temp_vhd_path) if supported_format == 'vhdx': upload_image = upload_image[:-1] vhdutils.VHDUtils.convert_vhd(temp_vhd_path, upload_image, constants.VHD_TYPE_DYNAMIC) image_utils.upload_volume(None, None, image_meta, upload_image, 'vhd') fileutils.delete_if_exists(temp_vhd_path) fileutils.delete_if_exists(upload_image) self.mox.ReplayAll() drv.copy_volume_to_image(None, vol, None, image_meta)
def test_initialize_connection(self): drv = self._driver volume = db_fakes.get_fake_volume_info() initiator_name = "%s%s" % (CONF.iscsi_target_prefix, volume['name']) connector = db_fakes.get_fake_connector_info() self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'associate_initiator_with_iscsi_target') windows_utils.WindowsUtils.associate_initiator_with_iscsi_target( volume['provider_location'], initiator_name, ) self.mox.StubOutWithMock(windows_utils.WindowsUtils, 'get_host_information') windows_utils.WindowsUtils.get_host_information( volume, volume['provider_location']) self.mox.ReplayAll() drv.initialize_connection(volume, connector)
def test_create_cloned_volume(self, mock_local_path, mock_tmp_snap): tgt_utils = self._driver._tgt_utils fake_volume = db_fakes.get_fake_volume_info() fake_src_volume = db_fakes.get_fake_volume_info_cloned() mock_tmp_snap.return_value.__enter__.return_value = ( mock.sentinel.tmp_snap_name) mock_local_path.return_value = mock.sentinel.vol_vhd_path self._driver.create_cloned_volume(fake_volume, fake_src_volume) mock_tmp_snap.assert_called_once_with(fake_src_volume['name']) tgt_utils.export_snapshot.assert_called_once_with( mock.sentinel.tmp_snap_name, mock.sentinel.vol_vhd_path) self._driver._vhdutils.resize_vhd.assert_called_once_with( mock.sentinel.vol_vhd_path, fake_volume['size'] * units.Gi, is_file_max_size=False) tgt_utils.import_wt_disk.assert_called_once_with( mock.sentinel.vol_vhd_path, fake_volume['name'])
def test_copy_volume_to_image(self, mock_delete_if_exists, mock_upload_volume, mock_tmp_snap): tgt_utils = self._driver._tgt_utils disk_format = "vhd" fake_image_meta = db_fakes.get_fake_image_meta() fake_volume = db_fakes.get_fake_volume_info() fake_img_conv_dir = "fake_img_conv_dir" self._driver.configuration.image_conversion_dir = fake_img_conv_dir tgt_utils.get_supported_disk_format.return_value = disk_format mock_tmp_snap.return_value.__enter__.return_value = mock.sentinel.tmp_snap_name expected_tmp_vhd_path = os.path.join(fake_img_conv_dir, fake_image_meta["id"] + "." + disk_format) self._driver.copy_volume_to_image( mock.sentinel.context, fake_volume, mock.sentinel.image_service, fake_image_meta ) mock_tmp_snap.assert_called_once_with(fake_volume["name"]) tgt_utils.export_snapshot.assert_called_once_with(mock.sentinel.tmp_snap_name, expected_tmp_vhd_path) mock_upload_volume.assert_called_once_with( mock.sentinel.context, mock.sentinel.image_service, fake_image_meta, expected_tmp_vhd_path, "vhd" ) mock_delete_if_exists.assert_called_once_with(expected_tmp_vhd_path)