-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
npm preinstalled on macos for testing #5158
npm preinstalled on macos for testing #5158
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GJ. Some changes are required
@@ -109,6 +109,70 @@ def load_vulnerability_detector_configurations(host_manager): | |||
|
|||
return configurations | |||
|
|||
@pytest.fixture(scope='module') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixture will never be used in the test. Fixtures should be included in the desired tests or marked as autouse. Check the fixture documentation https://docs.pytest.org/en/6.2.x/fixture.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here: 2126cdd
@@ -109,6 +109,70 @@ 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.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixture not only ensures npm is installed but also performs the installation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here: 998c839
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("Node.js and npm are already installed.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not simplify the process by installing npm directly through its URL instead of using brew? This approach reduces complexity and minimizes the potential for errors in the block of code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified here: abb850f and check in this comment.
for host in host_manager.get_group_hosts('agent'): | ||
os_type = host_manager.get_host_variables(host).get('os') | ||
|
||
if os_type.startswith('macos'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's enhance this by creating a variable that holds a list of operating systems for which npm will be installed. Then, let's incorporate that variable into this fixture. Additionally, let's utilize the 'macos' group instead of 'agent' to streamline the process and avoid unnecessary checks."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified here: abb850f and check in this comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include this change in the changelog
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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Link should be included in a separate variable, defined at start of the module
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified here: 220e419 and checked in this comment: #5128 (comment)
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}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fixture does not warn in case of failure installing npm on macOS hosts
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 /" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
node_install_command = "sudo installer -pkg /tmp/node-v21.7.1.pkg -target /" | |
node_install_command = "installer -pkg /tmp/node-v21.7.1.pkg -target /" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified here: 220e419 and checked in this comment: #5128 (comment)
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}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any option to use install_package method instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified here: 220e419 and checked in this comment: #5128 (comment)
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=/usr/local/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. |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description
This PR implements a fix to the vulnerability test to have the option to pre-install npm on macOS machines.