def test_save_content(self):
     fs = FileServicer()
     test_data = b'This is a test file for save content test.'
     content = fmsg.SaveFileContentRequest()
     content.file.path = self.test_save_content_path
     content.file.mtime = 1.0  # something not zero to update a not existing file
     content.file.size = len(test_data)
     content.file.data = test_data
     save_response = next(fs.SaveFileContent([content], DummyContext()))
     self.assertEqual(save_response.status.code, fmsg.ReturnStatus.StatusType.Value('REMOVED_FILE'), "wrong status code '%d' if file was removed in meantime." % save_response.status.code)
     # save new file
     content.file.mtime = 0
     save_response = next(fs.SaveFileContent([content], DummyContext()))
     self.assertEqual(save_response.status.code, fmsg.ReturnStatus.StatusType.Value('OK'), 'new file was not saved')
     self.assertTrue(os.path.exists(self.test_save_content_path), 'new file was not saved to %s' % self.test_save_content_path)
     self.assertEqual(save_response.ack.mtime, os.path.getmtime(self.test_save_content_path), 'wrong mtime returned after create a new file')
     # sleep to change the mtime in file
     time.sleep(0.3)
     # change file in meantime
     new_utime = time.time()
     os.utime(self.test_save_content_path, (new_utime, new_utime))
     new_test_data = b'This is a changed text for save content test.'
     content.file.mtime = save_response.ack.mtime
     content.file.size = len(new_test_data)
     content.file.data = new_test_data
     save_response = next(fs.SaveFileContent([content], DummyContext()))
     self.assertEqual(save_response.status.code, fmsg.ReturnStatus.StatusType.Value('CHANGED_FILE'), 'wrong status code if file was changed in meantime.')
     # overwrite the changed file
     content.overwrite = True
     save_response = next(fs.SaveFileContent([content], DummyContext()))
     self.assertEqual(save_response.status.code, fmsg.ReturnStatus.StatusType.Value('OK'), 'file was not overwritten')
     self.assertEqual(save_response.ack.mtime, os.path.getmtime(self.test_save_content_path), 'wrong mtime returned after overwrite file')
     with FileIO(self.test_save_content_path, 'r') as outfile:
         self.assertEqual(new_test_data, outfile.read(), 'wrong content in file')
     # try to save in root folder
     content.file.path = '/content_test.txt'
     content.file.mtime = 0
     save_response = next(fs.SaveFileContent([content], DummyContext()))
     if save_response.status.code == fmsg.ReturnStatus.StatusType.Value('OK'):
         # test in industrial ci, use source folder
         content.file.path = interpret_path('$(find fkie_node_manager_daemon)/') + content.file.path
         save_response = next(fs.SaveFileContent([content], DummyContext()))
     self.assertEqual(save_response.status.code, fmsg.ReturnStatus.StatusType.Value('IO_ERROR'), 'save in root folder returns a wrong result: %d, expected: %d' % (save_response.status.code, fmsg.ReturnStatus.StatusType.Value('IO_ERROR')))
     # save file in more chunks
     test_data = [b'First line.\n', b'Second line.\n', b'Third line.\n']
     for resp in fs.SaveFileContent(self._read_from_list(test_data, self.test_save_content_path), DummyContext()):
         self.assertEqual(resp.status.code, fmsg.ReturnStatus.StatusType.Value('OK'), 'file was not overwritten, result code: %d' % resp.status.code)
         self.assertEqual(resp.ack.size, self.current_pose, 'incorrect transferred file size: %d, expected: %d' % (resp.ack.size, self.current_pose))
     with FileIO(self.test_save_content_path, 'r') as outfile:
         self.assertEqual(str(test_data), str(outfile.readlines()), 'wrong content in file')
 def _read_from_list(self, test_data, path):
     data_len = 0
     for l in test_data:
         data_len += len(l)
     self.current_pose = 0
     for line in test_data:
         content = fmsg.SaveFileContentRequest()
         content.overwrite = True
         content.file.path = path
         content.file.mtime = 0.0  # something not zero to update a not existing file
         content.file.size = data_len
         content.file.offset = self.current_pose
         content.file.data = line
         self.current_pose += len(line)
         yield content
 def _gen_save_content_list(self, path, content, mtime, package=''):
     send_content = content
     while send_content:
         chunk = send_content
         # split into small parts on big files
         if len(chunk) > self.FILE_CHUNK_SIZE:
             chunk = send_content[0:self.FILE_CHUNK_SIZE]
             send_content = send_content[self.FILE_CHUNK_SIZE:]
         else:
             send_content = ''
         msg = fms.SaveFileContentRequest()
         msg.overwrite = mtime == 0
         msg.file.path = path
         msg.file.mtime = mtime  # something not zero to update a not existing file
         msg.file.size = len(content)
         msg.file.data = chunk
         msg.file.package = package
         yield msg
Exemple #4
0
 def _gen_save_content_list(self, path, content, mtime, package=''):
     minone = True
     try:
         send_content = content  # .encode('utf-8')
         while send_content or minone:
             minone = False
             chunk = send_content
             # split into small parts on big files
             if len(chunk) > self.FILE_CHUNK_SIZE:
                 chunk = send_content[0:self.FILE_CHUNK_SIZE]
                 send_content = send_content[self.FILE_CHUNK_SIZE:]
             else:
                 send_content = ''
             msg = fmsg.SaveFileContentRequest()
             msg.overwrite = mtime == 0
             msg.file.path = path
             msg.file.mtime = mtime  # something not zero to update a not existing file
             msg.file.size = len(content)
             msg.file.data = chunk
             msg.file.package = package
             yield msg
     except Exception:
         import traceback
         print(traceback.format_exc())