def test_create_volume_from_snapshot(self): """Tests volume creation from snapshot.""" drv = self._driver mox = self.mox location = '127.0.0.1:/nfs' host = 'hostname@backend#' + location volume = FakeVolume(host, 1) snapshot = FakeSnapshot(1) expected_result = {'provider_location': location} mox.StubOutWithMock(drv, '_clone_volume') mox.StubOutWithMock(drv, '_get_volume_location') mox.StubOutWithMock(drv, 'local_path') mox.StubOutWithMock(drv, '_discover_file_till_timeout') mox.StubOutWithMock(drv, '_set_rw_permissions_for_all') drv._clone_volume(IgnoreArg(), IgnoreArg(), IgnoreArg()) drv._get_volume_location(IgnoreArg()).AndReturn(location) drv.local_path(IgnoreArg()).AndReturn('/mnt') drv._discover_file_till_timeout(IgnoreArg()).AndReturn(True) drv._set_rw_permissions_for_all(IgnoreArg()) mox.ReplayAll() loc = drv.create_volume_from_snapshot(volume, snapshot) self.assertEqual(loc, expected_result) mox.VerifyAll()
def _prepare_clone_mock(self, status): drv = self._driver mox = self.mox volume = FakeVolume() setattr(volume, 'provider_location', '127.0.0.1:/nfs') mox.StubOutWithMock(drv, '_get_export_ip_path') mox.StubOutWithMock(drv, '_get_actual_path_for_export') mox.StubOutWithMock(drv, '_start_clone') mox.StubOutWithMock(drv, '_wait_for_clone_finish') if status == 'fail': mox.StubOutWithMock(drv, '_clear_clone') drv._get_export_ip_path(IgnoreArg(), IgnoreArg()).AndReturn( ('127.0.0.1', '/nfs')) drv._get_actual_path_for_export(IgnoreArg()).AndReturn('/vol/vol1/nfs') drv._start_clone(IgnoreArg(), IgnoreArg()).AndReturn(('1', '2')) if status == 'fail': drv._wait_for_clone_finish('1', '2').AndRaise( api.NaApiError('error', 'error')) drv._clear_clone('1') else: drv._wait_for_clone_finish('1', '2') return mox
def test_convert_image(self, mock_stat): mox = self._mox mox.StubOutWithMock(utils, 'execute') mox.StubOutWithMock(utils, 'is_blk_device') TEST_OUT_FORMAT = 'vmdk' TEST_SOURCE = 'img/qemu.img' TEST_DEST = '/img/vmware.vmdk' utils.is_blk_device(TEST_DEST).AndReturn(True) utils.execute('dd', 'count=0', 'if=img/qemu.img', 'of=/img/vmware.vmdk', 'oflag=direct', run_as_root=True) utils.execute('qemu-img', 'convert', '-t', 'none', '-O', TEST_OUT_FORMAT, TEST_SOURCE, TEST_DEST, run_as_root=True) mox.ReplayAll() image_utils.convert_image(TEST_SOURCE, TEST_DEST, TEST_OUT_FORMAT) mox.VerifyAll()
def test_fetch_verify_image_without_file_format(self): TEST_RETURN = "image: qemu.qcow2\n"\ "virtual_size: 50M (52428800 bytes)\n"\ "cluster_size: 65536\n"\ "disk_size: 196K (200704 bytes)\n"\ "Snapshot list:\n"\ "ID TAG VM SIZE DATE VM CLOCK\n"\ "1 snap1 1.7G 2011-10-04 19:04:00 32:06:34.974" fake_image_service = FakeImageService() mox = self._mox mox.StubOutWithMock(image_utils, 'fetch') mox.StubOutWithMock(utils, 'execute') image_utils.fetch(context, fake_image_service, self.TEST_IMAGE_ID, self.TEST_DEV_PATH, None, None) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', self.TEST_DEV_PATH, run_as_root=True).AndReturn((TEST_RETURN, 'ignored')) mox.ReplayAll() self.assertRaises(exception.ImageUnacceptable, image_utils.fetch_verify_image, context, fake_image_service, self.TEST_IMAGE_ID, self.TEST_DEV_PATH)
def test_fetch_to_raw(self): TEST_RET = "image: qemu.qcow2\n"\ "file_format: qcow2 \n"\ "virtual_size: 50M (52428800 bytes)\n"\ "cluster_size: 65536\n"\ "disk_size: 196K (200704 bytes)" TEST_RETURN_RAW = "image: qemu.raw\n"\ "file_format: raw\n"\ "virtual_size: 50M (52428800 bytes)\n"\ "cluster_size: 65536\n"\ "disk_size: 196K (200704 bytes)\n"\ fake_image_service = FakeImageService() mox = self._mox mox.StubOutWithMock(image_utils, 'create_temporary_file') mox.StubOutWithMock(utils, 'execute') mox.StubOutWithMock(image_utils, 'fetch') image_utils.create_temporary_file().AndReturn(self.TEST_DEV_PATH) image_utils.fetch(context, fake_image_service, self.TEST_IMAGE_ID, self.TEST_DEV_PATH, None, None) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', self.TEST_DEV_PATH, run_as_root=True).AndReturn((TEST_RET, 'ignored')) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', self.TEST_DEV_PATH, run_as_root=True).AndReturn((TEST_RET, 'ignored')) utils.execute('qemu-img', 'convert', '-O', 'raw', self.TEST_DEV_PATH, self.TEST_DEV_PATH, run_as_root=True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', self.TEST_DEV_PATH, run_as_root=True).AndReturn((TEST_RETURN_RAW, 'ignored')) mox.ReplayAll() image_utils.fetch_to_raw(context, fake_image_service, self.TEST_IMAGE_ID, self.TEST_DEV_PATH) mox.VerifyAll()
def test_create_volume_from_snapshot(self): """Tests volume creation from snapshot.""" drv = self._driver mox = self.mox volume = FakeVolume(1) snapshot = FakeSnapshot(2) self.assertRaises(exception.CinderException, drv.create_volume_from_snapshot, volume, snapshot) snapshot = FakeSnapshot(1) location = '127.0.0.1:/nfs' expected_result = {'provider_location': location} mox.StubOutWithMock(drv, '_clone_volume') mox.StubOutWithMock(drv, '_get_volume_location') drv._clone_volume(IgnoreArg(), IgnoreArg(), IgnoreArg()) drv._get_volume_location(IgnoreArg()).AndReturn(location) mox.ReplayAll() loc = drv.create_volume_from_snapshot(volume, snapshot) self.assertEqual(loc, expected_result) mox.VerifyAll()
def _prepare_delete_snapshot_mock(self, snapshot_exists): drv = self._driver mox = self.mox mox.StubOutWithMock(drv, '_get_provider_location') mox.StubOutWithMock(drv, '_volume_not_present') mox.StubOutWithMock(drv, '_post_prov_deprov_in_ssc') if snapshot_exists: mox.StubOutWithMock(drv, '_execute') mox.StubOutWithMock(drv, '_get_volume_path') drv._get_provider_location(IgnoreArg()) drv._get_provider_location(IgnoreArg()) drv._volume_not_present(IgnoreArg(), IgnoreArg())\ .AndReturn(not snapshot_exists) if snapshot_exists: drv._get_volume_path(IgnoreArg(), IgnoreArg()) drv._execute('rm', None, run_as_root=True) drv._post_prov_deprov_in_ssc(IgnoreArg()) mox.ReplayAll() return mox
def test_find_old_cache_files_exists(self): drv = self._driver mox = self.mox cmd = [ 'find', '/mnt', '-maxdepth', '1', '-name', 'img-cache*', '-amin', '+720' ] setattr(drv.configuration, 'expiry_thres_minutes', '720') files = '/mnt/img-id1\n/mnt/img-id2\n' r_files = ['img-id1', 'img-id2'] mox.StubOutWithMock(drv, '_get_mount_point_for_share') mox.StubOutWithMock(drv, '_execute') mox.StubOutWithMock(drv, '_shortlist_del_eligible_files') drv._get_mount_point_for_share('share').AndReturn('/mnt') drv._execute(*cmd, run_as_root=True).AndReturn((files, None)) drv._shortlist_del_eligible_files(IgnoreArg(), r_files).AndReturn(r_files) mox.ReplayAll() res = drv._find_old_cache_files('share') mox.VerifyAll() if len(res) == len(r_files): for f in res: r_files.remove(f) else: self.fail('Returned files not same as expected.')
def test_clone_image_cloneableshare_raw(self): drv = self._driver mox = self.mox volume = {'name': 'vol', 'size': '20'} mox.StubOutWithMock(drv, '_find_image_in_cache') mox.StubOutWithMock(drv, '_is_cloneable_share') mox.StubOutWithMock(drv, '_get_mount_point_for_share') mox.StubOutWithMock(image_utils, 'qemu_img_info') mox.StubOutWithMock(drv, '_clone_volume') mox.StubOutWithMock(drv, '_discover_file_till_timeout') mox.StubOutWithMock(drv, '_set_rw_permissions_for_all') mox.StubOutWithMock(drv, '_resize_image_file') mox.StubOutWithMock(drv, '_is_share_vol_compatible') drv._find_image_in_cache(IgnoreArg()).AndReturn([]) drv._is_cloneable_share(IgnoreArg()).AndReturn('127.0.0.1:/share') drv._is_share_vol_compatible(IgnoreArg(), IgnoreArg()).AndReturn(True) drv._get_mount_point_for_share(IgnoreArg()).AndReturn('/mnt') image_utils.qemu_img_info('/mnt/img-id').AndReturn( self.get_img_info('raw')) drv._clone_volume('img-id', 'vol', share='127.0.0.1:/share', volume_id=None) drv._get_mount_point_for_share(IgnoreArg()).AndReturn('/mnt') drv._discover_file_till_timeout(IgnoreArg()).AndReturn(True) drv._set_rw_permissions_for_all('/mnt/vol') drv._resize_image_file({'name': 'vol'}, IgnoreArg()) mox.ReplayAll() drv.clone_image(volume, ('nfs://127.0.0.1:/share/img-id', None), 'image_id', {}) mox.VerifyAll()
def test_file_unlinked(self): mox = self.mox mox.StubOutWithMock(image_utils, 'create_temporary_file') mox.StubOutWithMock(image_utils.os, 'unlink') image_utils.create_temporary_file().AndReturn('somefile') image_utils.os.unlink('somefile') mox.ReplayAll() with image_utils.temporary_file(): pass
def test_single_vhd(self): mox = self.mox mox.StubOutWithMock(image_utils, 'get_vhd_size') mox.StubOutWithMock(image_utils, 'resize_vhd') mox.StubOutWithMock(image_utils, 'coalesce_vhd') mox.ReplayAll() result = image_utils.coalesce_chain(['0.vhd']) mox.VerifyAll() self.assertEquals('0.vhd', result)
def test_file_unlinked(self): mox = self.mox mox.StubOutWithMock(image_utils, 'create_temporary_file') mox.StubOutWithMock(fileutils, 'delete_if_exists') image_utils.create_temporary_file().AndReturn('somefile') fileutils.delete_if_exists('somefile') mox.ReplayAll() with image_utils.temporary_file(): pass
def _test_fetch_to_raw(self, has_qemu=True, src_inf=None, dest_inf=None): mox = self._mox mox.StubOutWithMock(image_utils, 'create_temporary_file') mox.StubOutWithMock(utils, 'execute') mox.StubOutWithMock(image_utils, 'fetch') TEST_INFO = ("image: qemu.qcow2\n" "file format: raw\n" "virtual size: 0 (0 bytes)\n" "disk size: 0") image_utils.create_temporary_file().AndReturn(self.TEST_DEV_PATH) test_qemu_img = utils.execute('env', 'LC_ALL=C', 'qemu-img', 'info', self.TEST_DEV_PATH, run_as_root=True) if has_qemu: test_qemu_img.AndReturn((TEST_INFO, 'ignored')) image_utils.fetch(context, self._image_service, self.TEST_IMAGE_ID, self.TEST_DEV_PATH, None, None) else: test_qemu_img.AndRaise(processutils.ProcessExecutionError()) if has_qemu and src_inf: utils.execute('env', 'LC_ALL=C', 'qemu-img', 'info', self.TEST_DEV_PATH, run_as_root=True).AndReturn((src_inf, 'ignored')) if has_qemu and dest_inf: utils.execute('qemu-img', 'convert', '-O', 'raw', self.TEST_DEV_PATH, self.TEST_DEV_PATH, run_as_root=True) utils.execute('env', 'LC_ALL=C', 'qemu-img', 'info', self.TEST_DEV_PATH, run_as_root=True).AndReturn((dest_inf, 'ignored')) self._mox.ReplayAll()
def test_do_setup(self): mox = self.mox drv = self._driver mox.StubOutWithMock(drv, 'check_for_setup_error') mox.StubOutWithMock(netapp_nfs.NetAppNFSDriver, '_get_client') drv.check_for_setup_error() netapp_nfs.NetAppNFSDriver._get_client() mox.ReplayAll() drv.do_setup(IsA(context.RequestContext))
def test_delete_files_till_bytes_free_success(self): drv = self._driver mox = self.mox files = [('img-cache-1', 230), ('img-cache-2', 380)] mox.StubOutWithMock(drv, '_get_mount_point_for_share') mox.StubOutWithMock(drv, '_delete_file') drv._get_mount_point_for_share(IgnoreArg()).AndReturn('/mnt') drv._delete_file('/mnt/img-cache-2').AndReturn(True) drv._delete_file('/mnt/img-cache-1').AndReturn(True) mox.ReplayAll() drv._delete_files_till_bytes_free(files, 'share', bytes_to_free=1024) mox.VerifyAll()
def test_check_share_in_use_success(self): drv = self._driver mox = self.mox drv._mounted_shares = ['127.0.0.1:/dir/share'] mox.StubOutWithMock(utils, 'resolve_hostname') mox.StubOutWithMock(drv, '_share_match_for_ip') utils.resolve_hostname(IgnoreArg()).AndReturn('10.22.33.44') drv._share_match_for_ip('10.22.33.44', ['127.0.0.1:/dir/share']).AndReturn('share') mox.ReplayAll() share = drv._check_share_in_use('127.0.0.1:8989', '/dir/share') mox.VerifyAll() if not share: self.fail('Expected share not detected')
def test_do_setup(self): mox = self.mox drv = self._driver mox.StubOutWithMock(netapp_nfs.NetAppNFSDriver, 'do_setup') mox.StubOutWithMock(drv, '_get_client') mox.StubOutWithMock(drv, '_do_custom_setup') netapp_nfs.NetAppNFSDriver.do_setup(IgnoreArg()) drv._get_client() drv._do_custom_setup(IgnoreArg()) mox.ReplayAll() drv.do_setup(IsA(context.RequestContext)) mox.VerifyAll()
def test_file_unlinked_on_error(self): mox = self.mox mox.StubOutWithMock(image_utils, 'create_temporary_file') mox.StubOutWithMock(image_utils.os, 'unlink') image_utils.create_temporary_file().AndReturn('somefile') image_utils.os.unlink('somefile') mox.ReplayAll() def sut(): with image_utils.temporary_file(): raise test.TestingException() self.assertRaises(test.TestingException, sut)
def test_do_setup(self): mox = self._mox drv = self._driver mox.StubOutWithMock(drv, 'check_for_setup_error') mox.StubOutWithMock(drv, '_get_client') drv.check_for_setup_error() drv._get_client() mox.ReplayAll() drv.do_setup(IsA(context.RequestContext)) mox.VerifyAll()
def test_check_for_setup_error(self): mox = self.mox drv = self._driver required_flags = [ 'netapp_transport_type', 'netapp_login', 'netapp_password', 'netapp_server_hostname', 'netapp_server_port' ] # set required flags for flag in required_flags: setattr(drv.configuration, flag, None) # check exception raises when flags are not set self.assertRaises(exception.CinderException, drv.check_for_setup_error) # set required flags for flag in required_flags: setattr(drv.configuration, flag, 'val') setattr(drv, 'ssc_enabled', False) mox.StubOutWithMock(netapp_nfs.NetAppDirectNfsDriver, '_check_flags') netapp_nfs.NetAppDirectNfsDriver._check_flags() mox.ReplayAll() drv.check_for_setup_error() mox.VerifyAll() # restore initial FLAGS for flag in required_flags: delattr(drv.configuration, flag)
def test_check_for_setup_error(self): mox = self._mox drv = self._driver required_flags = [ 'netapp_wsdl_url', 'netapp_login', 'netapp_password', 'netapp_server_hostname', 'netapp_server_port' ] # set required flags for flag in required_flags: setattr(drv.configuration, flag, None) # check exception raises when flags are not set self.assertRaises(exception.CinderException, drv.check_for_setup_error) # set required flags for flag in required_flags: setattr(drv.configuration, flag, 'val') mox.StubOutWithMock(nfs.NfsDriver, 'check_for_setup_error') nfs.NfsDriver.check_for_setup_error() mox.ReplayAll() drv.check_for_setup_error() mox.VerifyAll() # restore initial FLAGS for flag in required_flags: delattr(drv.configuration, flag)
def test_chain_with_two_elements(self): mox = self.mox mox.StubOutWithMock(image_utils, 'set_vhd_parent') image_utils.set_vhd_parent('0.vhd', '1.vhd') mox.ReplayAll() image_utils.fix_vhd_chain(['0.vhd', '1.vhd'])
def test_is_cloneable_share_goodformat5(self): drv = self._driver mox = self.mox strg = 'nfs://netapp.com/img' mox.StubOutWithMock(drv, '_check_share_in_use') drv._check_share_in_use(IgnoreArg(), IgnoreArg()).AndReturn('share') mox.ReplayAll() drv._is_cloneable_share(strg) mox.VerifyAll()
def test_clone_image_fromcache(self): drv = self._driver mox = self.mox volume = {'name': 'vol', 'size': '20'} mox.StubOutWithMock(drv, '_find_image_in_cache') mox.StubOutWithMock(drv, '_do_clone_rel_img_cache') mox.StubOutWithMock(drv, '_post_clone_image') mox.StubOutWithMock(drv, '_is_share_vol_compatible') drv._find_image_in_cache(IgnoreArg()).AndReturn([('share', 'file_name') ]) drv._is_share_vol_compatible(IgnoreArg(), IgnoreArg()).AndReturn(True) drv._do_clone_rel_img_cache('file_name', 'vol', 'share', 'file_name') drv._post_clone_image(volume) mox.ReplayAll() drv.clone_image(volume, ('image_location', None), 'image_id', {}) mox.VerifyAll()
def test_create_snapshot(self): """Test snapshot can be created and deleted""" mox = self.mox drv = self._driver mox.StubOutWithMock(drv, '_clone_volume') drv._clone_volume(IgnoreArg(), IgnoreArg(), IgnoreArg()) mox.ReplayAll() drv.create_snapshot(FakeSnapshot())
def test_check_share_in_use_incorrect_host(self): drv = self._driver mox = self.mox mox.StubOutWithMock(utils, 'resolve_hostname') utils.resolve_hostname(IgnoreArg()).AndRaise(Exception()) mox.ReplayAll() share = drv._check_share_in_use('incorrect:8989', '/dir') mox.VerifyAll() if share: self.fail('Unexpected share detected.')
def _prepare_clone_mock(self, status): drv = self._driver mox = self._mox volume = FakeVolume() setattr(volume, 'provider_location', '127.0.0.1:/nfs') mox.StubOutWithMock(drv, '_get_host_ip') mox.StubOutWithMock(drv, '_get_export_path') mox.StubOutWithMock(drv, '_get_if_info_by_ip') mox.StubOutWithMock(drv, '_get_vol_by_junc_vserver') mox.StubOutWithMock(drv, '_clone_file') drv._get_host_ip(IgnoreArg()).AndReturn('127.0.0.1') drv._get_export_path(IgnoreArg()).AndReturn('/nfs') drv._get_if_info_by_ip('127.0.0.1').AndReturn( self._prepare_info_by_ip_response()) drv._get_vol_by_junc_vserver('openstack', '/nfs').AndReturn('nfsvol') drv._clone_file('nfsvol', 'volume_name', 'clone_name', 'openstack') return mox
def test_vhd_util_call(self): mox = self.mox mox.StubOutWithMock(utils, 'execute') utils.execute('vhd-util', 'resize', '-n', 'vhdfile', '-s', '1024', '-j', 'journal').AndReturn(('ignored', 'ignored')) mox.ReplayAll() image_utils.resize_vhd('vhdfile', 1024, 'journal') mox.VerifyAll()
def test_vhd_util_call(self): mox = self.mox mox.StubOutWithMock(utils, 'execute') utils.execute('vhd-util', 'coalesce', '-n', 'vhdfile').AndReturn( ('ignored', 'ignored')) mox.ReplayAll() image_utils.coalesce_vhd('vhdfile') mox.VerifyAll()
def test_find_old_cache_files_notexists(self): drv = self._driver mox = self.mox cmd = [ 'find', '/mnt', '-maxdepth', '1', '-name', 'img-cache*', '-amin', '+720' ] setattr(drv.configuration, 'expiry_thres_minutes', 720) mox.StubOutWithMock(drv, '_get_mount_point_for_share') mox.StubOutWithMock(drv, '_execute') drv._get_mount_point_for_share(IgnoreArg()).AndReturn('/mnt') drv._execute(*cmd, run_as_root=True).AndReturn((None, '')) mox.ReplayAll() res = drv._find_old_cache_files('share') mox.VerifyAll() if len(res) == 0: pass else: self.fail('No files expected but got return values.')