def test_upload_local_file(self): with TempDir() as d: path = os.path.join(d, 'file1') data = six.b('hello world') write_file(path, data) self.bucket.upload_local_file(path, 'file1') self._check_file_contents('file1', data)
def test_download_by_id_progress_partial_shifted_overwrite(self): # LOCAL is # 12345678901234567890 # # and then: # # hello world # ||||||| # \\\\\\\ # \\\\\\\ # \\\\\\\ # \\\\\\\ # \\\\\\\ # ||||||| # vvvvvvv # # 1234567lo worl567890 with TempDir() as d: path = os.path.join(d, 'file2') download_dest = PreSeekedDownloadDest(seek_target=7, local_file_path=path) data = six.b('12345678901234567890') write_file(path, data) self.bucket.download_file_by_id( self.file_info.id_, download_dest, self.progress_listener, range_=(3, 9), ) self._check_local_file_contents(path, six.b('1234567lo worl567890'))
def test_write_and_set_mod_time_and_progress(self): """ Check that the file gets written and that its mod time gets set. """ with TempDir() as temp_dir: file_path = os.path.join(temp_dir, "test.txt") download_local_file = DownloadDestLocalFile(file_path) progress_listener = ProgressListenerForTest() download_dest = DownloadDestProgressWrapper( download_local_file, progress_listener) with download_dest.make_file_context("file_id", "file_name", 100, "content_type", "sha1", {}, 1500222333000) as f: f.write(b'hello world\n') with open(file_path, 'rb') as f: self.assertEqual(b'hello world\n', f.read()) self.assertEqual(1500222333, os.path.getmtime(file_path)) self.assertEqual( [ 'set_total_bytes(100)', 'bytes_completed(12)', 'close()', ], progress_listener.get_calls(), )
def test_upload_fifo(self): if platform.system().lower().startswith('java'): raise SkipTest('in Jython 2.7.1b3 there is no os.mkfifo()') with TempDir() as d: path = os.path.join(d, 'file1') os.mkfifo(path) with self.assertRaises(InvalidUploadSource): self.bucket.upload_local_file(path, 'file1')
def test_slash_sorting(self): # '/' should sort between '.' and '0' with TempDir() as tmpdir: folder = self._prepare_folder(tmpdir) self.assertEqual( self.NAMES, list(f.name for f in folder.all_files(self.reporter))) self.reporter.local_access_error.assert_not_called()
def test_broken_symlink(self): with TempDir() as tmpdir: folder = self._prepare_folder(tmpdir, broken_symlink=True) self.assertEqual( self.NAMES, list(f.name for f in folder.all_files(self.reporter))) self.reporter.local_access_error.assert_called_once_with( os.path.join(tmpdir, 'bad_symlink'))
def _check_file_filters_results(self, policies_manager, expected_scan_results): with TempDir() as tmpdir: folder = self._prepare_folder(tmpdir) self.assertEqual( expected_scan_results, list(f.name for f in folder.all_files(self.reporter, policies_manager))) self.reporter.local_access_error.assert_not_called()
def test_failed_write_deletes_partial_file(self): with TempDir() as temp_dir: download_dest, file_path = self._make_dest(temp_dir) try: with download_dest.make_file_context("file_id", "file_name", 100, "content_type", "sha1", {}, 1500222333000) as f: f.write(six.b('hello world')) raise Exception('test error') except Exception as e: self.assertEqual('test error', str(e)) self.assertFalse(os.path.exists(file_path), msg='failed download should be deleted')
def test_write_and_set_mod_time(self): """ Check that the file gets written and that its mod time gets set. """ with TempDir() as temp_dir: download_dest, file_path = self._make_dest(temp_dir) with download_dest.make_file_context("file_id", "file_name", 100, "content_type", "sha1", {}, 1500222333000) as f: f.write(six.b('hello world')) with open(file_path, 'rb') as f: self.assertEqual( six.b(self.expected_result), f.read(), ) self.assertEqual(1500222333, os.path.getmtime(file_path))
def test_invalid_permissions(self): with TempDir() as tmpdir: folder = self._prepare_folder(tmpdir, invalid_permissions=True) # tests differ depending on the user running them. "root" will # succeed in os.access(path, os.R_OK) even if the permissions of # the file are 0 as implemented on self._prepare_folder(). # use-case: running test suite inside a docker container if not os.access(os.path.join(tmpdir, self.NAMES[0]), os.R_OK): self.assertEqual( self.NAMES[1:], list(f.name for f in folder.all_files(self.reporter))) self.reporter.local_permission_error.assert_called_once_with( os.path.join(tmpdir, self.NAMES[0])) else: self.assertEqual( self.NAMES, list(f.name for f in folder.all_files(self.reporter)))
def test_upload_dead_symlink(self): with TempDir() as d: path = os.path.join(d, 'file1') os.symlink('non-existing', path) with self.assertRaises(InvalidUploadSource): self.bucket.upload_local_file(path, 'file1')