Example #1
0
class TestUploadFileTask(TestUpTaskBase):
    def setUp(self):
        self.initialize_obj()
        self.task = UploadFileTask(self.parent_task, '/', self.data['name'])
        self.parent_task.drive.upload_file = mock.MagicMock(
            return_value=self.item)

    def test_properties(self):
        self.assertEqual(self.data['name'], self.task.item_name)
        self.assertEqual(
            self.parent_task.drive.config.local_root + '/' + self.data['name'],
            self.task.local_path)

    def test_handle(self):
        os.path.getsize = lambda p: self.data['size']
        os.path.getmtime = lambda p: 123412341234
        m = mock.mock_open()
        m.return_value = io.BytesIO()
        with mock.patch('builtins.open', m, create=True):
            self.task.handle()
        self.assertEqual(
            1,
            len(self.task.items_store.get_items_by_id(item_id=self.item.id)))

    def test_handle_error(self):
        m = mock.mock_open()
        m.side_effect = OSError()
        with mock.patch('builtins.open', m, create=True):
            self.task.handle()
Example #2
0
 def handle(self):
     try:
         self._async_status.update_status()
         if self._async_status.status == options.AsyncOperationStatuses.FAILED:
             # If failed, fall back to an upload task.
             if os.path.isfile(self.local_path):
                 self.logger.info(
                     'Server failed to copy to item "%s". Upload the file instead.',
                     self.remote_path)
                 self.task_pool.add_task(
                     UploadFileTask(self,
                                    rel_parent_path=self.rel_parent_path,
                                    item_name=self.item_name))
             else:
                 self.logger.error(
                     'Failed to copy file "%s" to server and local entry is no longer a file. Abort.',
                     self.local_path)
         elif self._async_status.status == options.AsyncOperationStatuses.COMPLETED:
             # If completed, update the item.
             item = self._async_status.get_item()
             self.items_store.update_item(item, ItemRecordStatuses.OK)
             self.logger.info('Successfully copied file "%s" to server',
                              self.local_path)
         else:
             # Put the task back to task pool.
             th = threading.Thread(target=self.put_back, args=())
             th.daemon = True
             th.start()
     except errors.OneDriveError as e:
         self.logger.error(
             'API error when polling copy status for file "%s": %s.',
             self.local_path, e)
Example #3
0
 def _create_upload_task(self, local_item_name, is_dir):
     if is_dir:
         self._create_remote_dir(local_item_name)
     else:
         self.logger.debug('Created task uploading file "%s/%s".',
                           self.rel_path, local_item_name)
         self.task_pool.add_task(
             UploadFileTask(self, self.rel_path + '/', local_item_name))
Example #4
0
class TestUploadFileTask(TestUpTaskBase):
    def setUp(self):
        self.initialize_obj()
        self.task = UploadFileTask(self.parent_task, "/", self.data["name"])
        self.parent_task.drive.upload_file = mock.MagicMock(return_value=self.item)

    def test_properties(self):
        self.assertEqual(self.data["name"], self.task.item_name)
        self.assertEqual(self.parent_task.drive.config.local_root + "/" + self.data["name"], self.task.local_path)

    def test_handle(self):
        os.path.getsize = lambda p: self.data["size"]
        os.path.getmtime = lambda p: 123412341234
        m = mock.mock_open()
        m.return_value = io.BytesIO()
        with mock.patch("builtins.open", m, create=True):
            self.task.handle()
        self.assertEqual(1, len(self.task.items_store.get_items_by_id(item_id=self.item.id)))

    def test_handle_error(self):
        m = mock.mock_open()
        m.side_effect = OSError()
        with mock.patch("builtins.open", m, create=True):
            self.task.handle()
Example #5
0
 def setUp(self):
     self.initialize_obj()
     self.task = UploadFileTask(self.parent_task, '/', self.data['name'])
     self.parent_task.drive.upload_file = mock.MagicMock(
         return_value=self.item)
Example #6
0
 def setUp(self):
     self.initialize_obj()
     self.task = UploadFileTask(self.parent_task, "/", self.data["name"])
     self.parent_task.drive.upload_file = mock.MagicMock(return_value=self.item)