Skip to content

Commit

Permalink
Merge pull request #5363 from wazuh/fix/5344-windows-package-installa…
Browse files Browse the repository at this point in the history
…tion-failure

Fix E2E Vulnerability Detection Windows package installation error
  • Loading branch information
juliamagan authored May 13, 2024
2 parents 5fc3fbc + b274453 commit af0c09d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ All notable changes to this project will be documented in this file.

### Fixed

- Fix E2E Vulnerability Detection Windows package installation error ([#5363](https:/wazuh/wazuh-qa/pull/5363)) \- (Framework)
- Fix shutdown messages system test ([#5298](https:/wazuh/wazuh-qa/pull/5298)) \- (Framework + Tests)
- Fix upgrade macOS package cases for vulnerability scanner E2E ([#5334](https:/wazuh/wazuh-qa/pull/5334)) \- (Tests)
- Fix test cases in Vulnerability Detection E2E test by adding new packages ([#5349](https:/wazuh/wazuh-qa/pull/5349)) \- (Tests)
Expand Down
88 changes: 71 additions & 17 deletions deps/wazuh_testing/wazuh_testing/tools/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import sys
import tempfile
import xml.dom.minidom as minidom
import re

from threading import Thread
from typing import List, Union
from time import sleep

import testinfra
import yaml
Expand Down Expand Up @@ -477,7 +480,7 @@ def download_file(self, host, url, dest_path, mode='755'):

return result

def install_package(self, host, url, system):
def install_package(self, host, url, system, retry=3, retry_delay=5):
"""
Installs a package on the specified host.
Expand All @@ -493,32 +496,84 @@ def install_package(self, host, url, system):
Example:
host_manager.install_package('my_host', 'http://example.com/package.deb', system='ubuntu')
"""
extension = '.msi'
result = None
def install_msi_package(host, url):
result = self.get_host(host).ansible("win_package",
f"path={url} arguments=/passive", check=False)

if system == 'windows':
if url.lower().endswith(extension):
result = self.get_host(host).ansible("win_package", f"path={url} arguments=/passive", check=False)
else:
result = self.get_host(host).ansible("win_package", f"path={url} arguments=/S", check=False)
elif system == 'ubuntu':
return result

def install_exe_package(host, url):
result = self.get_host(host).ansible("win_package", f"path={url} arguments=/S", check=False)

return result

def install_apt_package(host, url):
result = self.get_host(host).ansible("apt", f"deb={url}", check=False)
elif system == 'centos':

return result

def install_yum_package(host, url):
result = self.get_host(host).ansible("yum", f"name={url} state=present "
'disable_gpg_check=True', check=False)
elif system == 'macos':
'disable_gpg_check=True', check=False)
return result

def install_pkg_package(host, url):
package_name = url.split('/')[-1]
result = self.get_host(host).ansible("command", f"curl -LO {url}", check=False)
cmd = f"installer -pkg {package_name} -target /"
result = self.get_host(host).ansible("command", cmd, check=False)
else:

return result

retry_installation_errors = [
'This installation package could not be opened',
]

result = {}
failed_installation = False

extension = url.lower().split('.')[-1]

system_supported = ['windows', 'ubuntu', 'centos', 'macos']
installer_map = {
'msi': install_msi_package,
'exe': install_exe_package,
'deb': install_apt_package,
'rpm': install_yum_package,
'pkg': install_pkg_package
}

if system not in system_supported:
raise ValueError(f"Unsupported system: {system}")

logging.info(f"Package installed result {result}")
for _ in range(retry):
installer_func = installer_map.get(extension, None)

if not installer_func:
raise ValueError(f"Unsupported extension: {extension} for {system}")

result = installer_func(host, url)

logging.debug(f"Package installation result {result}")
failed_installation = not (result.get('changed', False) or result.get('rc') == 0) \
or not (result.get('changed', False) or result.get('rc') == 0 or result.get('stderr', None) == '')

if failed_installation:
if not any(re.search(error, result.get('msg', '')) for error in retry_installation_errors):
logging.error("Installation failed. Installation will not be retried.")
raise RuntimeError(f"Failed to install package in {host}: {result}")

if not (result['changed'] or result.get('rc') == 0) or not (result['changed'] or result.get('stderr', None) == ''):
logging.error(f"Error installing {url} in {host}:"
'Corrupted package detected. Retrying installation...')
sleep(retry_delay)
else:
break

if failed_installation:
raise RuntimeError(f"Failed to install package in {host}: {result}")

return result

def install_npm_package(self, host, url, system='ubuntu'):
"""
Installs a package on the specified host using npm.
Expand Down Expand Up @@ -609,7 +664,6 @@ def remove_package(self, host, system, package_uninstall_name=None, custom_unins
host_manager.remove_package('my_host', 'my_package', system='ubuntu')
"""
logging.info(f"Removing package {package_uninstall_name} from host {host}")
logging.info(f"System: {system}")

remove_operation_result = False

Expand Down Expand Up @@ -641,7 +695,7 @@ def remove_package(self, host, system, package_uninstall_name=None, custom_unins
or not (remove_operation_result['changed'] or remove_operation_result.get('stderr', None) == ''):
raise RuntimeError(f"Failed to remove package in {host}: {remove_operation_result}")

logging.info(f"Package removed result {remove_operation_result}")
logging.debug(f"Package removed result {remove_operation_result}")

return remove_operation_result

Expand Down

0 comments on commit af0c09d

Please sign in to comment.