Skip to content
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

Merged

Conversation

santipadilla
Copy link
Member

Related issue
#5128

Description

This PR implements a fix to the vulnerability test to have the option to pre-install npm on macOS machines.

@santipadilla santipadilla marked this pull request as ready for review March 27, 2024 14:34
Copy link
Member

@Rebits Rebits left a 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')
Copy link
Member

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

Copy link
Member Author

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."""
Copy link
Member

@Rebits Rebits Apr 1, 2024

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed here: 998c839

Comment on lines 120 to 175
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.")
Copy link
Member

@Rebits Rebits Apr 1, 2024

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

Copy link
Member Author

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.

Comment on lines 115 to 118
for host in host_manager.get_group_hosts('agent'):
os_type = host_manager.get_host_variables(host).get('os')

if os_type.startswith('macos'):
Copy link
Member

@Rebits Rebits Apr 1, 2024

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."

Copy link
Member Author

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.

Copy link
Member

@Rebits Rebits left a 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

@santipadilla santipadilla marked this pull request as draft April 2, 2024 10:25
@santipadilla santipadilla marked this pull request as ready for review April 2, 2024 12:31
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"
Copy link
Member

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

Copy link
Member Author

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)

Comment on lines 144 to 149
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}")
Copy link
Member

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 /"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 /"

Copy link
Member Author

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)

Comment on lines 136 to 149
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}")
Copy link
Member

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?

Copy link
Member Author

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)

Comment on lines +118 to +131
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.

@santipadilla santipadilla marked this pull request as draft April 4, 2024 10:37
@santipadilla santipadilla marked this pull request as ready for review April 4, 2024 12:03
Copy link
Member

@Rebits Rebits left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@davidjiglesias davidjiglesias merged commit 8831b4b into enhancement/vd-e2e-tests Apr 5, 2024
0 of 2 checks passed
@davidjiglesias davidjiglesias deleted the 5128-provision-macos-with-npm branch April 5, 2024 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create fixtures to provision macOS endpoints with npm dependencies.
4 participants