From 2abdb1dd27c83405a3cbadc7382956291c2a7fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Padilla=20=C3=81lvarez?= Date: Wed, 27 Mar 2024 11:28:11 +0100 Subject: [PATCH 1/8] npm preinstalled on macos for testing --- .../test_vulnerability_detector.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py b/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py index 3756bc15a2..5d4a5c0179 100644 --- a/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py +++ b/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py @@ -109,6 +109,29 @@ def load_vulnerability_detector_configurations(host_manager): return configurations +@pytest.fixture(scope='module') +def install_npm(host_manager: HostManager): + """Ensure npm is installed on macOS agents""" + for host in host_manager.get_group_hosts('agent'): + os_type = host_manager.get_host_variables(host).get('os') + + if os_type.startswith('macos'): + + nvm_install_and_use_command = ( + "sudo -iu vagrant /bin/bash -c '" + "export NVM_DIR=\"$HOME/.nvm\" && " + "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash && " + "echo export NVM_DIR=\\\"$HOME/.nvm\\\" >> $HOME/.zshrc && " + "echo [ -s \\\"$NVM_DIR/nvm.sh\\\" ] '&&' . \\\"$NVM_DIR/nvm.sh\\\" >> $HOME/.zshrc && " + "echo [ -s \\\"$NVM_DIR/bash_completion\\\" ] '&&' . \\\"$NVM_DIR/bash_completion\\\" >> $HOME/.zshrc && " + ". \\\"$NVM_DIR/nvm.sh\\\" && " + "nvm install 21 && " + "nvm use 21'" + ) + + logger.info(f"Installing nvm and Node.js for vagrant user on {host}") + install_result = host_manager.get_host(host).ansible("shell", nvm_install_and_use_command, check=False) + logger.info(f"nvm and Node.js installation and use result on {host}: {install_result}") @pytest.fixture(scope='module') def setup_vulnerability_tests(host_manager: HostManager) -> Generator: From a1efcb89ca6e09c313046ccf6fbe880bbadb786f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Padilla=20=C3=81lvarez?= Date: Wed, 27 Mar 2024 12:41:37 +0100 Subject: [PATCH 2/8] Condition if npm is installed --- .../test_vulnerability_detector.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py b/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py index 5d4a5c0179..cb15ae4e93 100644 --- a/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py +++ b/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py @@ -117,6 +117,11 @@ def install_npm(host_manager: HostManager): if os_type.startswith('macos'): + nvm_check_command = "source ~/.zshrc && command -v nvm" + nvm_check_result = host_manager.get_host(host).ansible("shell", f"sudo -iu vagrant /bin/zsh -c '{nvm_check_command}'", check=False) + + if nvm_check_result['stdout'] == '': + nvm_install_and_use_command = ( "sudo -iu vagrant /bin/bash -c '" "export NVM_DIR=\"$HOME/.nvm\" && " @@ -133,6 +138,9 @@ def install_npm(host_manager: HostManager): install_result = host_manager.get_host(host).ansible("shell", nvm_install_and_use_command, check=False) logger.info(f"nvm and Node.js installation and use result on {host}: {install_result}") + else: + logger.info(f"nvm is already installed on {host}") + @pytest.fixture(scope='module') def setup_vulnerability_tests(host_manager: HostManager) -> Generator: """Setup the vulnerability tests environment From 8ec19effb1e3da71b0b67b0dcb41183da624cfcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Padilla=20=C3=81lvarez?= Date: Wed, 27 Mar 2024 15:33:11 +0100 Subject: [PATCH 3/8] homebrew for npm installation --- .../test_vulnerability_detector.py | 81 +++++++++++++------ 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py b/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py index cb15ae4e93..549e33ad74 100644 --- a/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py +++ b/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py @@ -111,35 +111,68 @@ def load_vulnerability_detector_configurations(host_manager): @pytest.fixture(scope='module') def install_npm(host_manager: HostManager): - """Ensure npm is installed on macOS agents""" + """Ensure npm is installed on macOS agents.""" for host in host_manager.get_group_hosts('agent'): os_type = host_manager.get_host_variables(host).get('os') if os_type.startswith('macos'): - - nvm_check_command = "source ~/.zshrc && command -v nvm" - nvm_check_result = host_manager.get_host(host).ansible("shell", f"sudo -iu vagrant /bin/zsh -c '{nvm_check_command}'", check=False) - - if nvm_check_result['stdout'] == '': - - nvm_install_and_use_command = ( - "sudo -iu vagrant /bin/bash -c '" - "export NVM_DIR=\"$HOME/.nvm\" && " - "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash && " - "echo export NVM_DIR=\\\"$HOME/.nvm\\\" >> $HOME/.zshrc && " - "echo [ -s \\\"$NVM_DIR/nvm.sh\\\" ] '&&' . \\\"$NVM_DIR/nvm.sh\\\" >> $HOME/.zshrc && " - "echo [ -s \\\"$NVM_DIR/bash_completion\\\" ] '&&' . \\\"$NVM_DIR/bash_completion\\\" >> $HOME/.zshrc && " - ". \\\"$NVM_DIR/nvm.sh\\\" && " - "nvm install 21 && " - "nvm use 21'" - ) - - logger.info(f"Installing nvm and Node.js for vagrant user on {host}") - install_result = host_manager.get_host(host).ansible("shell", nvm_install_and_use_command, check=False) - logger.info(f"nvm and Node.js installation and use result on {host}: {install_result}") - + # Check if Homebrew is installed. + logger.info(f"Checking and installing Homebrew on {host}") + brew_check_command = "source /Users/vagrant/.zprofile && command -v brew" + brew_check_result = host_manager.get_host(host).ansible( + "shell", + brew_check_command, + become=True, + become_user='vagrant', + check=False + ) + logger.info(f"Brew check result on {host}: {brew_check_result}") + # Install Homebrew if it is not already installed. + if brew_check_result['rc'] != 0: + logger.info("Installing Homebrew") + brew_install_command = 'NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"' + install_result = host_manager.get_host(host).ansible("shell", + brew_install_command, + become=True, + become_user='vagrant', + check=False) + logger.info(f"Homebrew installation result on {host}: {install_result}") + + add_brew_to_path = """ + echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/vagrant/.zprofile + eval "$(/opt/homebrew/bin/brew shellenv)" + """ + path_result = host_manager.get_host(host).ansible("shell", + add_brew_to_path, + become=True, + become_user='vagrant', + check=False) + logger.info(f"Adding Homebrew to PATH result on {host}: {path_result}") + else: + logger.info("Homebrew is already installed.") + # Check if Node and npm is installed + logger.info(f"Checking and installing npm on {host}") + node_check_command = "PATH=/opt/homebrew/bin:$PATH && command -v node" + node_check_result = host_manager.get_host(host).ansible( + "shell", + node_check_command, + become=True, + become_user='vagrant', + check=False + ) + logger.info(f"Node check result on {host}: {node_check_result}") + # Install node if it is not already installed. + if node_check_result['rc'] != 0: + logger.info("Installing Node.js and npm") + node_install_command = "PATH=/opt/homebrew/bin:$PATH && brew install node" + node_install_result = host_manager.get_host(host).ansible("shell", + node_install_command, + become=True, + become_user='vagrant', + check=False) + logger.info(f"Node.js and npm installation result on {host}: {node_install_result}") else: - logger.info(f"nvm is already installed on {host}") + logger.info("Node.js and npm are already installed.") @pytest.fixture(scope='module') def setup_vulnerability_tests(host_manager: HostManager) -> Generator: From 2126cdd2faa6275ab3ab8daa8820b6e889bc0f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Padilla=20=C3=81lvarez?= Date: Tue, 2 Apr 2024 12:29:50 +0200 Subject: [PATCH 4/8] autouse fix --- .../test_vulnerability_detector/test_vulnerability_detector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py b/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py index 549e33ad74..8029812d03 100644 --- a/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py +++ b/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py @@ -109,7 +109,7 @@ def load_vulnerability_detector_configurations(host_manager): return configurations -@pytest.fixture(scope='module') +@pytest.fixture(scope='module', autouse=True) def install_npm(host_manager: HostManager): """Ensure npm is installed on macOS agents.""" for host in host_manager.get_group_hosts('agent'): From 998c839e00a48bccc54fa00cb95cf87b30616b0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Padilla=20=C3=81lvarez?= Date: Tue, 2 Apr 2024 12:32:45 +0200 Subject: [PATCH 5/8] Clarity comment --- .../test_vulnerability_detector/test_vulnerability_detector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py b/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py index 8029812d03..8b26638724 100644 --- a/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py +++ b/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py @@ -111,7 +111,7 @@ def load_vulnerability_detector_configurations(host_manager): @pytest.fixture(scope='module', autouse=True) def install_npm(host_manager: HostManager): - """Ensure npm is installed on macOS agents.""" + """Check and install npm if not already installed""" for host in host_manager.get_group_hosts('agent'): os_type = host_manager.get_host_variables(host).get('os') From abb850f24b579bb0561da12d822b0ec10e01d7d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Padilla=20=C3=81lvarez?= Date: Tue, 2 Apr 2024 14:25:48 +0200 Subject: [PATCH 6/8] install npm from its url not from brew --- .../test_vulnerability_detector.py | 64 ++++++------------- 1 file changed, 20 insertions(+), 44 deletions(-) diff --git a/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py b/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py index 8b26638724..caa0e728fb 100644 --- a/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py +++ b/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py @@ -112,47 +112,14 @@ def load_vulnerability_detector_configurations(host_manager): @pytest.fixture(scope='module', autouse=True) def install_npm(host_manager: HostManager): """Check and install npm if not already installed""" - for host in host_manager.get_group_hosts('agent'): - os_type = host_manager.get_host_variables(host).get('os') - - if os_type.startswith('macos'): - # Check if Homebrew is installed. - logger.info(f"Checking and installing Homebrew on {host}") - brew_check_command = "source /Users/vagrant/.zprofile && command -v brew" - brew_check_result = host_manager.get_host(host).ansible( - "shell", - brew_check_command, - become=True, - become_user='vagrant', - check=False - ) - logger.info(f"Brew check result on {host}: {brew_check_result}") - # Install Homebrew if it is not already installed. - if brew_check_result['rc'] != 0: - logger.info("Installing Homebrew") - brew_install_command = 'NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"' - install_result = host_manager.get_host(host).ansible("shell", - brew_install_command, - become=True, - become_user='vagrant', - check=False) - logger.info(f"Homebrew installation result on {host}: {install_result}") - - add_brew_to_path = """ - echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/vagrant/.zprofile - eval "$(/opt/homebrew/bin/brew shellenv)" - """ - path_result = host_manager.get_host(host).ansible("shell", - add_brew_to_path, - become=True, - become_user='vagrant', - check=False) - logger.info(f"Adding Homebrew to PATH result on {host}: {path_result}") - else: - logger.info("Homebrew is already installed.") + + target_os_groups = ['macos'] + + for group in target_os_groups: + for host in host_manager.get_group_hosts(group): # Check if Node and npm is installed logger.info(f"Checking and installing npm on {host}") - node_check_command = "PATH=/opt/homebrew/bin:$PATH && command -v node" + node_check_command = "PATH=/usr/local/bin:$PATH && command -v node" node_check_result = host_manager.get_host(host).ansible( "shell", node_check_command, @@ -164,12 +131,21 @@ def install_npm(host_manager: HostManager): # Install node if it is not already installed. if node_check_result['rc'] != 0: logger.info("Installing Node.js and npm") - node_install_command = "PATH=/opt/homebrew/bin:$PATH && brew install node" + # Download Node.js package + download_command = "curl -o /tmp/node-v21.7.1.pkg https://nodejs.org/dist/v21.7.1/node-v21.7.1.pkg" + download_result = host_manager.get_host(host).ansible("shell", + download_command, + become=True, + become_user='vagrant', + check=False) + logger.info(f"Node.js package download result on {host}: {download_result}") + # Install Node.js + node_install_command = "sudo installer -pkg /tmp/node-v21.7.1.pkg -target /" node_install_result = host_manager.get_host(host).ansible("shell", - node_install_command, - become=True, - become_user='vagrant', - check=False) + node_install_command, + become=True, + become_user='vagrant', + check=False) logger.info(f"Node.js and npm installation result on {host}: {node_install_result}") else: logger.info("Node.js and npm are already installed.") From 05dc86e69fc9af24bdfa81e30ba7ac317bb97526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Padilla=20=C3=81lvarez?= Date: Tue, 2 Apr 2024 14:30:41 +0200 Subject: [PATCH 7/8] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5083ebf02f..7ad383cf00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ### Added +- Add fix to provision macOS endpoints with npm ([#5128](https://github.com/wazuh/wazuh-qa/pull/5158)) \- (Tests) - Add documentation about markers for system tests ([#5080](https://github.com/wazuh/wazuh-qa/pull/5080)) \- (Documentation) - Add AWS Custom Buckets Integration tests ([#4675](https://github.com/wazuh/wazuh-qa/pull/4675)) \- (Framework + Tests) - Add Vulnerability Detector end to end tests ([#4878](https://github.com/wazuh/wazuh-qa/pull/4878)) \- (Framework + Tests) From 220e419bd3750d0c02c7026476a853bf9c3a17d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Padilla=20=C3=81lvarez?= Date: Thu, 4 Apr 2024 14:01:48 +0200 Subject: [PATCH 8/8] use of the install_package method --- .../test_vulnerability_detector.py | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py b/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py index caa0e728fb..b44af6990f 100644 --- a/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py +++ b/tests/end_to_end/test_vulnerability_detector/test_vulnerability_detector.py @@ -113,6 +113,9 @@ def load_vulnerability_detector_configurations(host_manager): def install_npm(host_manager: HostManager): """Check and install npm if not already installed""" + node_version = "v21.7.1" + node_package_url = f"https://nodejs.org/dist/{node_version}/node-{node_version}.pkg" + target_os_groups = ['macos'] for group in target_os_groups: @@ -130,23 +133,13 @@ def install_npm(host_manager: HostManager): logger.info(f"Node check result on {host}: {node_check_result}") # Install node if it is not already installed. if node_check_result['rc'] != 0: - logger.info("Installing Node.js and npm") - # Download Node.js package - download_command = "curl -o /tmp/node-v21.7.1.pkg https://nodejs.org/dist/v21.7.1/node-v21.7.1.pkg" - download_result = host_manager.get_host(host).ansible("shell", - download_command, - become=True, - become_user='vagrant', - check=False) - logger.info(f"Node.js package download result on {host}: {download_result}") - # Install Node.js - node_install_command = "sudo installer -pkg /tmp/node-v21.7.1.pkg -target /" - node_install_result = host_manager.get_host(host).ansible("shell", - node_install_command, - become=True, - become_user='vagrant', - check=False) - logger.info(f"Node.js and npm installation result on {host}: {node_install_result}") + logger.info(f"Installing Node.js and npm using package: {node_package_url}") + + # Use the install_package method to handle the installation. + install_result = host_manager.install_package(host, node_package_url, system='macos') + + # Logging the result of installation attempt. + logger.info(f"Node.js and npm installation result on {host}: {install_result}") else: logger.info("Node.js and npm are already installed.")