示例#1
0
 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)
示例#2
0
 def test_invalid_permissions(self):
     with TempDir() as tmpdir:
         folder = self._prepare_folder(tmpdir, invalid_permissions=True)
         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])
         )
    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(),
         )
示例#5
0
 def test_broken_symlink(self):
     with TempDir() as tmpdir:
         folder = self._prepare_folder(tmpdir, broken_symlink=True)
         for _ in folder.all_files(self.reporter):
             pass  # just generate all the files
         self.reporter.local_access_error.assert_called_once_with(
             os.path.join(tmpdir, 'bad_symlink'))
 def test_sync_empty_folder_when_not_enabled(self):
     self._authorize_account()
     self._create_my_bucket()
     with TempDir() as temp_dir:
         command = ['sync', '--threads', '1', '--noProgress', temp_dir, 'b2://my-bucket']
         expected_stderr = 'ERROR: Directory %s is empty.  Use --allowEmptySource to sync anyway.\n' % temp_dir
         self._run_command(command, '', expected_stderr, 1)
示例#7
0
    def test_upload_large_file(self):
        self._authorize_account()
        self._create_my_bucket()
        min_part_size = self.account_info.get_minimum_part_size()
        file_size = min_part_size * 3

        with TempDir() as temp_dir:
            file_path = os.path.join(temp_dir, 'test.txt')
            text = six.u('*') * file_size
            with open(file_path, 'wb') as f:
                f.write(text.encode('utf-8'))
            expected_stdout = '''
            URL by file name: http://download.example.com/file/my-bucket/test.txt
            URL by fileId: http://download.example.com/b2api/v1/b2_download_file_by_id?fileId=9999
            {
              "action": "upload",
              "fileId": "9999",
              "fileName": "test.txt",
              "size": 600,
              "uploadTimestamp": 5000
            }
            '''

            self._run_command([
                'upload_file', '--noProgress', '--threads', '5', 'my-bucket',
                file_path, 'test.txt'
            ], expected_stdout, '', 0)
示例#8
0
 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')
         )
示例#9
0
 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')
示例#10
0
 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()
示例#11
0
 def test_slash_sorting(self):
     # '/' should sort between '.' and '0'
     with TempDir() as tmpdir:
         create_files(tmpdir, ['hello.', 'hello/a', 'hello/b', 'hello0'])
         folder = LocalFolder(tmpdir)
         files = list(folder.all_files())
         names = [f.name for f in files]
         self.assertEqual(['hello.', 'hello/a', 'hello/b', 'hello0'], names)
示例#12
0
 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()
示例#13
0
 def test_sync_empty_folder_when_enabled(self):
     self._authorize_account()
     self._create_my_bucket()
     with TempDir() as temp_dir:
         command = [
             'sync', '--threads', '1', '--noProgress', '--allowEmptySource',
             temp_dir, 'b2://my-bucket'
         ]
         self._run_command(command, '', '', 0)
示例#14
0
    def test_sync_dry_run(self):
        self._authorize_account()
        self._create_my_bucket()

        with TempDir() as temp_dir:
            temp_file = self._make_local_file(temp_dir, 'test-dry-run.txt')

            # dry-run
            expected_stdout = '''
            upload test-dry-run.txt
            '''
            command = [
                'sync', '--noProgress', '--dryRun', temp_dir, 'b2://my-bucket'
            ]
            self._run_command(command, expected_stdout, '', 0)

            # file should not have been uploaded
            expected_stdout = '''
            {
              "files": [],
              "nextFileName": null
            }
            '''
            self._run_command(['list_file_names', 'my-bucket'],
                              expected_stdout, '', 0)

            # upload file
            expected_stdout = '''
            upload test-dry-run.txt
            '''
            command = ['sync', '--noProgress', temp_dir, 'b2://my-bucket']
            self._run_command(command, expected_stdout, '', 0)

            # file should have been uploaded
            mtime = file_mod_time_millis(temp_file)
            expected_stdout = '''
            {
              "files": [
                {
                  "action": "upload",
                  "contentSha1": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed",
                  "contentType": "b2/x-auto",
                  "fileId": "9999",
                  "fileInfo": {
                    "src_last_modified_millis": "%d"
                  },
                  "fileName": "test-dry-run.txt",
                  "size": 11,
                  "uploadTimestamp": 5000
                }
              ],
              "nextFileName": null
            }
            ''' % (mtime)
            self._run_command(['list_file_names', 'my-bucket'],
                              expected_stdout, '', 0)
示例#15
0
 def test_slash_sorting(self):
     # '/' should sort between '.' and '0'
     names = [
         six.u('.dot_file'), six.u('hello.'), six.u('hello/a/1'), six.u('hello/a/2'),
         six.u('hello/b'), six.u('hello0'), six.u('\u81ea\u7531')
     ]
     with TempDir() as tmpdir:
         create_files(tmpdir, names)
         folder = LocalFolder(tmpdir)
         actual_names = list(f.name for f in folder.all_files())
         self.assertEqual(names, actual_names)
 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')
示例#17
0
    def test_sync(self):
        self._authorize_account()
        self._create_my_bucket()

        with TempDir() as temp_dir:
            file_path = os.path.join(temp_dir, 'test.txt')
            with open(file_path, 'wb') as f:
                f.write(six.u('hello world').encode('utf-8'))
            expected_stdout = '''
            upload test.txt
            '''

            command = ['sync', '--threads', '5', '--noProgress', temp_dir, 'b2://my-bucket']
            self._run_command(command, expected_stdout, '', 0)
示例#18
0
 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:
         file_path = os.path.join(temp_dir, "test.txt")
         download_dest = DownloadDestLocalFile(file_path)
         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))
 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))
示例#20
0
 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)))
示例#21
0
    def test_get_bucket_one_item_show_size(self):
        self._authorize_account()
        self._create_my_bucket()
        with TempDir() as temp_dir:
            # Upload a standard test file.
            local_file1 = self._make_local_file(temp_dir, 'file1.txt')
            expected_stdout = '''
            URL by file name: http://download.example.com/file/my-bucket/file1.txt
            URL by fileId: http://download.example.com/b2api/v1/b2_download_file_by_id?fileId=9999
            {
              "action": "upload",
              "fileId": "9999",
              "fileName": "file1.txt",
              "size": 11,
              "uploadTimestamp": 5000
            }
            '''
            self._run_command([
                'upload_file', '--noProgress', 'my-bucket', local_file1,
                'file1.txt'
            ], expected_stdout, '', 0)

            # Now check the output of get-bucket against the canon.
            expected_stdout = '''
            {
                "accountId": "my-account",
                "bucketId": "bucket_0",
                "bucketInfo": {},
                "bucketName": "my-bucket",
                "bucketType": "allPublic",
                "corsRules": [],
                "fileCount": 1,
                "lifecycleRules": [],
                "revision": 1,
                "totalSize": 11
            }
            '''
            self._run_command(['get-bucket', '--showSize', 'my-bucket'],
                              expected_stdout, '', 0)
示例#22
0
    def test_files(self):

        self._authorize_account()
        self._run_command(['create_bucket', 'my-bucket', 'allPublic'],
                          'bucket_0\n', '', 0)

        with TempDir() as temp_dir:
            local_file1 = self._make_local_file(temp_dir, 'file1.txt')

            # Upload a file
            expected_stdout = '''
            URL by file name: http://download.example.com/file/my-bucket/file1.txt
            URL by fileId: http://download.example.com/b2api/v1/b2_download_file_by_id?fileId=9999
            {
              "action": "upload",
              "fileId": "9999",
              "fileName": "file1.txt",
              "size": 11,
              "uploadTimestamp": 5000
            }
            '''

            self._run_command([
                'upload_file', '--noProgress', 'my-bucket', local_file1,
                'file1.txt'
            ], expected_stdout, '', 0)

            # Download by name
            local_download1 = os.path.join(temp_dir, 'download1.txt')
            expected_stdout = '''
            File name:    file1.txt
            File id:      9999
            File size:    11
            Content type: b2/x-auto
            Content sha1: 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
            checksum matches
            '''

            self._run_command([
                'download_file_by_name', '--noProgress', 'my-bucket',
                'file1.txt', local_download1
            ], expected_stdout, '', 0)
            self.assertEquals(six.b('hello world'),
                              self._read_file(local_download1))

            # Download file by ID.  (Same expected output as downloading by name)
            local_download2 = os.path.join(temp_dir, 'download2.txt')
            self._run_command([
                'download_file_by_id', '--noProgress', '9999', local_download2
            ], expected_stdout, '', 0)
            self.assertEquals(six.b('hello world'),
                              self._read_file(local_download2))

            # Hide the file
            expected_stdout = '''
            {
              "action": "hide",
              "fileId": "9998",
              "fileName": "file1.txt",
              "size": 0,
              "uploadTimestamp": 5001
            }
            '''

            self._run_command(['hide_file', 'my-bucket', 'file1.txt'],
                              expected_stdout, '', 0)

            # List the file versions
            expected_stdout = '''
            {
              "files": [
                {
                  "action": "hide",
                  "contentSha1": "none",
                  "contentType": null,
                  "fileId": "9998",
                  "fileInfo": {},
                  "fileName": "file1.txt",
                  "size": 0,
                  "uploadTimestamp": 5001
                },
                {
                  "action": "upload",
                  "contentSha1": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed",
                  "contentType": "b2/x-auto",
                  "fileId": "9999",
                  "fileInfo": {},
                  "fileName": "file1.txt",
                  "size": 11,
                  "uploadTimestamp": 5000
                }
              ],
              "nextFileId": null,
              "nextFileName": null
            }
            '''

            self._run_command(['list_file_versions', 'my-bucket'],
                              expected_stdout, '', 0)

            # List the file names
            expected_stdout = '''
            {
              "files": [],
              "nextFileName": null
            }
            '''

            self._run_command(['list_file_names', 'my-bucket'],
                              expected_stdout, '', 0)

            # Delete one file version, passing the name in
            expected_stdout = '''
            {
              "action": "delete",
              "fileId": "9998",
              "fileName": "file1.txt"
            }
            '''

            self._run_command(['delete_file_version', 'file1.txt', '9998'],
                              expected_stdout, '', 0)

            # Delete one file version, not passing the name in
            expected_stdout = '''
            {
              "action": "delete",
              "fileId": "9999",
              "fileName": "file1.txt"
            }
            '''

            self._run_command(['delete_file_version', '9999'], expected_stdout,
                              '', 0)
示例#23
0
 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')
示例#24
0
    def test_files(self):

        self._authorize_account()
        self._run_command(['create_bucket', 'my-bucket', 'allPublic'],
                          'bucket_0\n', '', 0)

        with TempDir() as temp_dir:
            local_file1 = self._make_local_file(temp_dir, 'file1.txt')
            # For this test, use a mod time without millis.  My mac truncates
            # millis and just leaves seconds.
            mod_time = 1500111222
            os.utime(local_file1, (mod_time, mod_time))
            self.assertEqual(1500111222, os.path.getmtime(local_file1))

            # Upload a file
            expected_stdout = '''
            URL by file name: http://download.example.com/file/my-bucket/file1.txt
            URL by fileId: http://download.example.com/b2api/v1/b2_download_file_by_id?fileId=9999
            {
              "action": "upload",
              "fileId": "9999",
              "fileName": "file1.txt",
              "size": 11,
              "uploadTimestamp": 5000
            }
            '''

            self._run_command([
                'upload_file', '--noProgress', 'my-bucket', local_file1,
                'file1.txt'
            ], expected_stdout, '', 0)

            # Get file info
            mod_time_str = str(int(os.path.getmtime(local_file1) * 1000))
            expected_stdout = '''
            {
              "accountId": "my-account",
              "action": "upload",
              "bucketId": "bucket_0",
              "contentLength": 11,
              "contentSha1": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed",
              "contentType": "b2/x-auto",
              "fileId": "9999",
              "fileInfo": {
                "src_last_modified_millis": "1500111222000"
              },
              "fileName": "file1.txt",
              "uploadTimestamp": 5000
            }
            '''

            self._run_command(['get_file_info', '9999'], expected_stdout, '',
                              0)

            # Download by name
            local_download1 = os.path.join(temp_dir, 'download1.txt')
            expected_stdout = '''
            File name:    file1.txt
            File id:      9999
            File size:    11
            Content type: b2/x-auto
            Content sha1: 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
            INFO src_last_modified_millis: 1500111222000
            checksum matches
            '''

            self._run_command([
                'download_file_by_name', '--noProgress', 'my-bucket',
                'file1.txt', local_download1
            ], expected_stdout, '', 0)
            self.assertEquals(six.b('hello world'),
                              self._read_file(local_download1))
            self.assertEquals(mod_time, os.path.getmtime(local_download1))

            # Download file by ID.  (Same expected output as downloading by name)
            local_download2 = os.path.join(temp_dir, 'download2.txt')
            self._run_command([
                'download_file_by_id', '--noProgress', '9999', local_download2
            ], expected_stdout, '', 0)
            self.assertEquals(six.b('hello world'),
                              self._read_file(local_download2))

            # Hide the file
            expected_stdout = '''
            {
              "action": "hide",
              "fileId": "9998",
              "fileName": "file1.txt",
              "size": 0,
              "uploadTimestamp": 5001
            }
            '''

            self._run_command(['hide_file', 'my-bucket', 'file1.txt'],
                              expected_stdout, '', 0)

            # List the file versions
            expected_stdout = '''
            {
              "files": [
                {
                  "action": "hide",
                  "contentSha1": "none",
                  "contentType": null,
                  "fileId": "9998",
                  "fileInfo": {},
                  "fileName": "file1.txt",
                  "size": 0,
                  "uploadTimestamp": 5001
                },
                {
                  "action": "upload",
                  "contentSha1": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed",
                  "contentType": "b2/x-auto",
                  "fileId": "9999",
                  "fileInfo": {
                    "src_last_modified_millis": "%s"
                  },
                  "fileName": "file1.txt",
                  "size": 11,
                  "uploadTimestamp": 5000
                }
              ],
              "nextFileId": null,
              "nextFileName": null
            }
            ''' % (mod_time_str, )

            self._run_command(['list_file_versions', 'my-bucket'],
                              expected_stdout, '', 0)

            # List the file names
            expected_stdout = '''
            {
              "files": [],
              "nextFileName": null
            }
            '''

            self._run_command(['list_file_names', 'my-bucket'],
                              expected_stdout, '', 0)

            # Delete one file version, passing the name in
            expected_stdout = '''
            {
              "action": "delete",
              "fileId": "9998",
              "fileName": "file1.txt"
            }
            '''

            self._run_command(['delete_file_version', 'file1.txt', '9998'],
                              expected_stdout, '', 0)

            # Delete one file version, not passing the name in
            expected_stdout = '''
            {
              "action": "delete",
              "fileId": "9999",
              "fileName": "file1.txt"
            }
            '''

            self._run_command(['delete_file_version', '9999'], expected_stdout,
                              '', 0)