def test_download(self, tmpdir, filename, httpauth): url = 'http://example.org/test/file.dat' filesize = 2 * 1024 * 1024 + 1 self.mox.StubOutWithMock(fileutils, 'open_url') if httpauth: fileutils.open_url(url, httpuser='******', httppassword='******' ).AndReturn(self.MockResponse(filesize)) else: fileutils.open_url(url).AndReturn(self.MockResponse(filesize)) self.mox.ReplayAll() with tmpdir.as_cwd(): if filename: output = 'test.name' if httpauth: f = f = fileutils.download(url, output, httpuser='******', httppassword='******') else: f = fileutils.download(url, output) else: output = 'file.dat' if httpauth: f = f = fileutils.download(url, output, httpuser='******', httppassword='******') else: f = fileutils.download(url) assert f == output assert os.path.exists(output) assert os.path.getsize(output) == filesize self.mox.VerifyAll()
def test_get_as_local_path(self, exists, remote, overwrite, httpauth): if remote: p = 'http://example.org/test.zip' expectedp = 'test.zip' else: p = '/example/test.zip' expectedp = p self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(os.path, 'isdir') self.mox.StubOutWithMock(fileutils, 'rename_backup') self.mox.StubOutWithMock(fileutils, 'download') if httpauth: kwargs = {'httpuser': '******', 'httppassword': '******'} else: kwargs = {'httpuser': None, 'httppassword': None} if remote: os.path.exists(expectedp).AndReturn(exists) if remote and exists and overwrite == 'backup': fileutils.rename_backup(expectedp) if (remote and exists and overwrite == 'backup') or ( remote and not exists): fileutils.download(p, expectedp, 0, **kwargs) if not remote or (remote and not exists) or ( remote and exists and overwrite != 'error'): os.path.isdir(expectedp).AndReturn(False) os.path.exists(expectedp).AndReturn(True) self.mox.ReplayAll() if remote and exists and overwrite == 'error': with pytest.raises(fileutils.FileException): fileutils.get_as_local_path(p, overwrite, **kwargs) else: ptype, lpath = fileutils.get_as_local_path(p, overwrite, **kwargs) assert ptype == 'file' assert lpath == expectedp self.mox.VerifyAll()