Beispiel #1
0
 def rename(self, share_drive_path: str, initial_file_path: str,
            new_file_path: str) -> None:
     if self.rename_failure:
         raise OperationFailure("Mock for rename failure.", [])
     SMBConnectionMock.stored_files[(
         share_drive_path,
         new_file_path)] = SMBConnectionMock.stored_files.pop(
             (share_drive_path, initial_file_path), None)
def test_smb_share_agent_operation_failure(mock_connect, mock_get_files,
                                           mock_write_section):
    mock_write_section.side_effect = OperationFailure(
        "Operation failure happened", [])
    args = parse_arguments([
        "hostname", "127.0.0.1", "--username", "username", "--password",
        "password"
    ], )
    with pytest.raises(OperationFailure, match="Operation failure happened"):
        smb_share_agent(args)
Beispiel #3
0
    def retrieveFile(self, share_drive_path: str, file_path: str,
                     file) -> (int, int):
        if self.path(share_drive_path, file_path).exists():
            file.write(self.path(share_drive_path, file_path).read_bytes())
            return 0, 0

        raise OperationFailure(
            f"Failed to retrieve {file_path} on {share_drive_path}: Unable to open file",
            [],
        )
Beispiel #4
0
 def createDirectory(self,
                     share_drive_path: str,
                     folder_path: str,
                     timeout=30):
     try:
         self.path(share_drive_path, folder_path).mkdir()
     except (FileNotFoundError, FileExistsError):
         raise OperationFailure(
             f"Failed to create directory {folder_path} on {share_drive_path}: Create failed",
             [],
         )
Beispiel #5
0
 def storeFile(self, share_drive_path: str, file_path: str, file) -> int:
     if self.storeFile_failure:
         raise OperationFailure("Mock for storeFile failure.", [])
     file_content = file.read()
     try:
         # Try to store string in order to compare it easily
         file_content = file_content.decode().replace("\r\n", "\n")
     except UnicodeDecodeError:
         pass  # Keep bytes when content is not str compatible (eg. Zip file)
     SMBConnectionMock.stored_files[(share_drive_path,
                                     file_path)] = file_content
     return 0
Beispiel #6
0
 def listPath(self,
              service_name: str,
              path: str,
              pattern: str = "*") -> List[SharedFile]:
     files_list = [
         SharedFileMock(os.path.basename(file_path))
         for _, file_path in SMBConnectionMock.stored_files
         if re.search(pattern, os.path.basename(file_path))
     ]
     if not files_list:
         raise OperationFailure("Mock for listPath failure.", [])
     return files_list
Beispiel #7
0
    def storeFile(self,
                  share_drive_path: str,
                  file_path: str,
                  file,
                  timeout=30) -> int:
        if self.path(share_drive_path, file_path).parent.exists():
            self.path(share_drive_path, file_path).write_bytes(file.read())
            return 0

        raise OperationFailure(
            f"Failed to store {file_path} on {share_drive_path}: Unable to open file",
            [],
        )
Beispiel #8
0
 def listPath(self, share, path):
     if path.endswith('missing'):
         raise OperationFailure('msg1', 'msg2')
     if str(path).endswith('example_dir') or str(path).endswith('sub'):
         return [self.SharedFileMock('sub_file', False)]
     return [
         self.SharedFileMock('.', True),
         self.SharedFileMock('..', True),
         self.SharedFileMock('example_dir', True),
         self.SharedFileMock('example.txt', False),
         self.SharedFileMock('other_example.txt', False),
         self.SharedFileMock('sub', True),
         self.SharedFileMock('last_file', False),
     ]
Beispiel #9
0
 def retrieveFile(self, share_drive_path: str, file_path: str,
                  file) -> (int, int):
     file_id = (share_drive_path, file_path)
     if file_id not in SMBConnectionMock.files_to_retrieve:
         retrieved_file_content = SMBConnectionMock.stored_files.get(
             file_id)
     else:
         retrieved_file_content = SMBConnectionMock.files_to_retrieve.pop(
             (share_drive_path, file_path), None)
     if retrieved_file_content is not None:
         if os.path.isfile(retrieved_file_content):
             with open(retrieved_file_content, mode="rb") as retrieved_file:
                 file.write(retrieved_file.read())
         else:
             try:
                 # Try to store string in order to compare it easily
                 file.write(str.encode(retrieved_file_content))
             except TypeError:
                 # Keep bytes when content is not str compatible (eg. Zip file)
                 file.write(retrieved_file_content)
         return 0, 0
     raise OperationFailure("Mock for retrieveFile failure.", [])
Beispiel #10
0
    def listPath(
        self,
        service_name: str,
        path: str,
        search: int = SMB_FILE_ATTRIBUTE_DIRECTORY,
        pattern: str = "*",
    ) -> List[SharedFile]:
        files = [
            SharedFileMock(file.name, file.is_dir())
            for file in self.path(service_name, path).glob(pattern) if search
            | SMB_FILE_ATTRIBUTE_DIRECTORY == search or file.is_file()
        ]

        if files:
            if pattern in ["", "*"]:
                files.extend(
                    [SharedFileMock(".", False),
                     SharedFileMock("..", False)])
            # sort it for testing purposes only in order to ensure that the order is always the same
            return sorted(set(files), key=lambda file: file.filename)

        raise OperationFailure(
            f"Failed to list {path} on {service_name}: Unable to open directory",
            [])
Beispiel #11
0
 def raise_exception(*args):
     raise OperationFailure("Mock for echo failure.", [])
Beispiel #12
0
 def retrieveFile(self, share, path, fileobj):
     if path.endswith('missing'):
         raise OperationFailure('msg1', 'msg2')
     fileobj.write(b'HELLO KITOVU')
Beispiel #13
0
 def getAttributes(self, share, path):
     if path.endswith('missing'):
         raise OperationFailure('msg1', 'msg2')
     return self.AttributesMock(1024, 988824605.56)
Beispiel #14
0
 def echo(self, data, timeout: int = 10):
     echo_response = SMBConnectionMock.echo_responses.pop(data, None)
     if echo_response is None:
         raise OperationFailure("Mock for echo failure.", [])
     return echo_response
Beispiel #15
0
 def raise_failure(*args):
     raise OperationFailure("Mock for rename failure.", [])
Beispiel #16
0
 def raise_failure(*args):
     raise OperationFailure("Mock for storeFile failure.", [])
Beispiel #17
0
 def raise_failure(*args):
     raise OperationFailure("Unable to create directory", [])