def test_create_cachedir(self): """cachedir should be created if it doesn't exist""" assert not os.path.exists(self.cachedir) file_utils.download(url='http://localhost/file', dirname=self.dirname, cachedir=self.cachedir) assert os.path.isdir(self.cachedir)
def test_filename_missing(self): """filename should be automatically derived""" file_utils.download(url='http://localhost/file.xyz', dirname=self.dirname) self._download_mocked.assert_called_with( 'http://localhost/file.xyz', os.path.join(self.dirname, 'file.xyz'))
def test_skip_download_if_cached(self, tmpdir, monkeypatch): """if the file is already in cache, it should not be downloaded again""" tmpdir.mkdir('download').join('file').write('data') stub_same_length = lambda *args, **kwargs: True monkeypatch.setattr(file_utils, '_same_length', stub_same_length) file_utils.download(url='http://localhost/file', dirname=self.dirname) assert self._download_mocked.call_count == 0
def test_cache_used(self): """file should be downloaded to cache if cachedir is defined""" file_utils.download(url='http://localhost/file.xyz', dirname=self.dirname, cachedir=self.cachedir) self._download_mocked.assert_called_with( 'http://localhost/file.xyz', os.path.join(self.cachedir, 'file.xyz'))
def test_filename_provided(self): """filename should be respected when provided""" file_utils.download(url='http://localhost/file.xyz', dirname=self.dirname, filename='important.file') self._download_mocked.assert_called_with( 'http://localhost/file.xyz', os.path.join(self.dirname, 'important.file'))
def test_raise(self, monkeypatch): """download errors should be raised""" mock_response = mock.Mock(status_code=404) _download_mocked = mock.Mock( side_effect=(requests.exceptions.RequestException( 'fake download failed', response=mock_response))) monkeypatch.setattr(file_utils, '_download', _download_mocked) with pytest.raises(exc.CheckbRemoteError): file_utils.download(url='http://localhost/file', dirname=self.dirname)
def process(self, params, arg_data): if ('package' not in params and 'nvr' not in params) or 'path' not in params \ or 'target_dir' not in params: detected_args = ', '.join(params.keys()) raise exc.CheckbDirectiveError( "The distgit directive requires 'package' (or 'nvr') and 'path' and 'target_dir' arguments." "Detected arguments: %s" % detected_args) package = None gitref = None namespace = None if 'nvr' in params: nvr = params['nvr'] package = rpm_utils.rpmformat(nvr, fmt='n') gitref = rpm_utils.get_dist_tag(nvr).replace('c', '') rawhide_tag = yumrepoinfo.YumRepoInfo(resolve_baseurl=False).get( 'rawhide', 'tag') if gitref == rawhide_tag: gitref = 'master' namespace = 'rpms' # Assign defaults package = params.get('package', package) gitref = params.get('gitref', gitref or 'master') namespace = params.get('namespace', namespace or 'rpms') baseurl = params.get('baseurl', BASEURL) target_dir = params['target_dir'] ignore_missing = params.get('ignore_missing', False) if not python_utils.iterable(params['path']): raise exc.CheckbValueError( "Incorrect value type of the 'path' argument: " "%s" % type(params['path'])) target_path = params['path'] output_data = {} if 'localpath' in params: if not python_utils.iterable(params['localpath']): raise exc.CheckbValueError( "Incorrect value type of the 'localpath' argument: " "%s" % type(params['path'])) if not len(params['path']) == len(params['localpath']): raise exc.CheckbValueError( 'path and localpath lists must be of the same ' 'length.') target_path = params['localpath'] format_fields = { 'package': package, 'gitref': gitref, 'namespace': namespace, 'baseurl': baseurl, } output_data['downloaded_files'] = [] for path, localpath in zip(params['path'], target_path): localpath = os.path.join(target_dir, localpath) file_utils.makedirs(os.path.dirname(localpath)) url = URL_FMT.format(path=path, **format_fields) try: output_data['downloaded_files'].append( file_utils.download(url, '.', localpath)) except exc.CheckbRemoteError as e: if e.errno == 404 and ignore_missing: log.debug('File not found, ignoring: %s', url) else: raise e return output_data
def test_skip_symlink(self): """symlink should not be in dirname if the download is not cached""" file_utils.download(url='http://localhost/file', dirname=self.dirname) assert not os.path.islink(os.path.join(self.dirname, 'file'))
def test_create_symlink(self): """symlink should be created if the download is cached""" file_utils.download(url='http://localhost/file', dirname=self.dirname, cachedir=self.cachedir) assert os.path.islink(os.path.join(self.dirname, 'file'))
def test_cache_not_used(self): """file should not be downloaded to cache if cachedir is not defined""" file_utils.download(url='http://localhost/file', dirname=self.dirname) assert not self._download_mocked.call_args[0][1].startswith( self.cachedir)
def test_create_dirname(self, tmpdir): """output dir should get created if it doesn't exist, multiple levels""" dirname = tmpdir.join('parentdir/subdir').strpath assert not os.path.exists(dirname) file_utils.download(url='http://localhost/file', dirname=dirname) assert os.path.isdir(dirname)
def test_return_value(self): """downloaded file path should be returned""" path = file_utils.download(url='http://localhost/file', dirname=self.dirname) assert path == os.path.join(self.dirname, 'file')