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_delete_missing_snapshot(self): drv = self._driver mox = self._prepare_delete_snapshot_mock(False) drv.delete_snapshot(FakeSnapshot()) mox.VerifyAll()
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' ] # 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(netapp.FLAGS, 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(netapp.FLAGS, flag)
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_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, run_as_root=True) 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(1) location = '127.0.0.1:/nfs' 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 test_check_for_setup_error(self): mox = self.mox drv = self._driver drv._client = api.NaServer("127.0.0.1") drv._client.set_api_version(1, 9) 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') mox.ReplayAll() drv.check_for_setup_error() mox.VerifyAll() # restore initial FLAGS for flag in required_flags: delattr(drv.configuration, flag)
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.assertEquals(loc, expected_result) mox.VerifyAll()
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_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_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_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 test_extract_to_calls_tar(self): mox = self.mox mox.StubOutWithMock(utils, 'execute') utils.execute('tar', '-xzf', 'archive.tgz', '-C', 'targetpath').AndReturn(('ignored', 'ignored')) mox.ReplayAll() image_utils.extract_targz('archive.tgz', 'targetpath') 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_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', 'modify', '-n', 'child', '-p', 'parent').AndReturn(('ignored', 'ignored')) mox.ReplayAll() image_utils.set_vhd_parent('child', 'parent') mox.VerifyAll()
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_upload_volume_with_raw_image(self): image_meta = {'id': 1, 'disk_format': 'raw'} mox = self._mox mox.StubOutWithMock(image_utils, 'convert_image') mox.ReplayAll() with tempfile.NamedTemporaryFile() as f: image_utils.upload_volume(context, FakeImageService(), image_meta, f.name) 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()) mox.VerifyAll()
def test_clone_volume(self): drv = self._driver mox = self._prepare_clone_mock('pass') mox.ReplayAll() volume_name = 'volume_name' clone_name = 'clone_name' volume_id = volume_name + str(hash(volume_name)) drv._clone_volume(volume_name, clone_name, volume_id) mox.VerifyAll()
def test_discovery_calls(self): mox = self.mox mox.StubOutWithMock(image_utils, 'file_exist') image_utils.file_exist('some/path/0.vhd').AndReturn(True) image_utils.file_exist('some/path/1.vhd').AndReturn(True) image_utils.file_exist('some/path/2.vhd').AndReturn(False) mox.ReplayAll() result = image_utils.discover_vhd_chain('some/path') mox.VerifyAll() self.assertEquals(['some/path/0.vhd', 'some/path/1.vhd'], result)
def test_vhd_util_call(self): mox = self.mox mox.StubOutWithMock(utils, 'execute') utils.execute('vhd-util', 'query', '-n', 'vhdfile', '-v').AndReturn( ('1024', 'ignored')) mox.ReplayAll() result = image_utils.get_vhd_size('vhdfile') mox.VerifyAll() self.assertEquals(1024, result)
def test_mount_nfs_should_not_remount(self): mox = self._mox client = self._nfsclient line = "%s on %s type nfs (rw)\n" % (self.TEST_EXPORT, self.TEST_MNT_POINT) mox.StubOutWithMock(client, '_execute') client._execute('mount', check_exit_code=0).AndReturn((line, "")) mox.ReplayAll() client.mount(self.TEST_EXPORT) mox.VerifyAll()
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_failed_clone_volume(self): drv = self._driver mox = self._prepare_clone_mock('failed') mox.ReplayAll() volume_name = 'volume_name' clone_name = 'clone_name' volume_id = volume_name + str(hash(volume_name)) self.assertRaises(exception.CinderException, drv._clone_volume, volume_name, clone_name, volume_id) mox.VerifyAll()
def test_successfull_clone_volume(self): drv = self._driver mox = self._prepare_clone_mock('passed') # set required flags setattr(drv.configuration, 'synchronous_snapshot_create', False) mox.ReplayAll() volume_name = 'volume_name' clone_name = 'clone_name' volume_id = volume_name + str(hash(volume_name)) drv._clone_volume(volume_name, clone_name, volume_id) 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_register_img_in_cache_with_share(self): volume = {'id': '1', 'name': 'testvol'} volume['provider_location'] = '10.61.170.1:/share/path' drv = self._driver mox = self.mox mox.StubOutWithMock(drv, '_do_clone_rel_img_cache') drv._do_clone_rel_img_cache('testvol', 'img-cache-12345', '10.61.170.1:/share/path', 'img-cache-12345') mox.ReplayAll() drv._register_image_in_cache(volume, '12345') mox.VerifyAll()
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_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()