Skip to content

Commit

Permalink
Merge pull request #48 from wazuh/47-fix-truncate-file
Browse files Browse the repository at this point in the history
Fix HostManager methods to handle paths with whitespaces
  • Loading branch information
pro-akim authored Jun 7, 2023
2 parents 76ac7ba + 6b80037 commit 5fb449e
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/wazuh_qa_framework/system/host_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def copy_file(self, host, src_path, dest_path, remote_src=False, become=None, ig
ansible_command = 'win_copy' if self.get_host_variables(host)['os_name'] == 'windows' else 'copy'
remote_source = 'yes' if remote_src else 'no'

command_parameters = f"src={src_path} dest={dest_path} remote_src={remote_source}"
command_parameters = f"src='{src_path}' dest='{dest_path}' remote_src={remote_source}"
result = testinfra_host.ansible(ansible_command, command_parameters, check=False, become=become)

if result.get('msg', None) and not ignore_errors:
Expand All @@ -202,7 +202,7 @@ def get_file_content(self, host, path, become=None, ignore_errors=False):
become = self.get_host_variables(host).get('become', False) if become is None else become

testinfra_host = self.get_host(host)
result = testinfra_host.ansible("slurp", f"src={path}", check=False, become=become)
result = testinfra_host.ansible("slurp", f"src='{path}'", check=False, become=become)

if result.get('msg', None) and not ignore_errors:
raise Exception(f"Error reading file {path} on host {host}: {result}")
Expand Down Expand Up @@ -245,7 +245,8 @@ def synchronize_linux_directory(self, host, dest_path, src_path=None, filesystem
with open(file_path, 'w') as file_operator:
file_operator.write(file['content'])

result = testinfra_host.ansible(ansible_command, f"src={src_path} dest={dest_path}", check=False, become=become)
result = testinfra_host.ansible(ansible_command, f"src='{src_path}' dest='{dest_path}'", check=False,
become=become)

if (result['rc'] != 0 or not result) and not ignore_errors:
raise Exception(f"Error creating file structure on host {host}: {result}")
Expand Down Expand Up @@ -276,10 +277,11 @@ def truncate_file(self, host, file_path, recreate=True, become=None, ignore_erro

if recreate:
ansible_command = 'win_copy' if self.get_host_variables(host)['os_name'] == 'windows' else 'copy'
result = testinfra_host.ansible(ansible_command, f"dest={file_path} content=''", check=False, become=become)
result = testinfra_host.ansible(ansible_command, f"dest='{file_path}' content=''", check=False,
become=become)
else:
ansible_command = 'win_file' if self.get_host_variables(host)['os_name'] == 'windows' else 'file'
result = testinfra_host.ansible(ansible_command, f"path={file_path} state=touch", check=False,
result = testinfra_host.ansible(ansible_command, f"path='{file_path}' state=touch", check=False,
become=become)
if result.get('msg', None) and not ignore_errors:
raise Exception(f"Error truncating file {file_path} on host {host}: {result}")
Expand All @@ -306,7 +308,7 @@ def remove_file(self, host, file_path, become=None, ignore_errors=False):

testinfra_host = self.get_host(host)
ansible_command = 'win_file' if self.get_host_variables(host)['os_name'] == 'windows' else 'file'
result = testinfra_host.ansible(ansible_command, f"path={file_path} state=absent", check=False, become=become)
result = testinfra_host.ansible(ansible_command, f"path='{file_path}' state=absent", check=False, become=become)

if result.get('msg', None) and not ignore_errors:
raise Exception(f"Error removing file {file_path} on host {host}: {result}")
Expand Down Expand Up @@ -373,7 +375,7 @@ def create_file(self, host, path, content, directory=False, owner=None, group=No

ansible_command = 'win_copy' if self.get_host_variables(host)['os_name'] == 'windows' else 'copy'

ansible_parameters = f"src={tmp_file.name} dest={path}"
ansible_parameters = f"src='{tmp_file.name}' dest='{path}'"
ansible_parameters += f" owner={owner}" if owner else ''
ansible_parameters += f" group={group}" if group else ''
ansible_parameters += f" mode={mode}" if mode else ''
Expand Down Expand Up @@ -536,7 +538,7 @@ def get_file_stats(self, host, path, become=None, ignore_errors=False):
else:
ansible_command = 'stat'

result = testinfra_host.ansible(ansible_command, f"path={path}", check=False, become=become)
result = testinfra_host.ansible(ansible_command, f"path='{path}'", check=False, become=become)

if 'stat' not in result and not ignore_errors:
raise Exception(f"Error getting stats of {path} on host {host}: {result}")
Expand Down Expand Up @@ -629,7 +631,7 @@ def append_block_in_file(self, host, path, block, become=None, ignore_errors=Fal
become = self.get_host_variables(host).get('become', False) if become is None else become

testinfra_host = self.get_host(host)
ansible_arguments = f"path={path} block={block}"
ansible_arguments = f"path='{path}' block='{block}'"
ansible_command = 'blockinfile'

result = testinfra_host.ansible(ansible_command, ansible_arguments, check=False, become=become)
Expand Down

0 comments on commit 5fb449e

Please sign in to comment.