Skip to content

Commit

Permalink
Merge pull request #51 from cloudmesh/patch
Browse files Browse the repository at this point in the history
fix macOS no error raise in run
  • Loading branch information
jpfleischer committed Nov 26, 2023
2 parents 19e0baa + 15f72e5 commit 11596fa
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 37 deletions.
37 changes: 24 additions & 13 deletions .github/workflows/python-package-conda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
- name: Test with pytest
run: |
conda install pytest
source activate base & pytest tests
source activate base & pytest tests -rsx
build-windows:
Expand All @@ -52,37 +52,48 @@ jobs:
max-parallel: 5

steps:
# - uses: actions/checkout@v3
# - name: Set up Python 3.10
# uses: conda-incubator/setup-miniconda@v2
# with:
# miniconda-version: "latest"
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: conda-incubator/setup-miniconda@v2
uses: actions/setup-python@v3
with:
miniconda-version: "latest"
python-version: '3.10'
# - name: Add conda to system path
# run: |
# # $CONDA is an environment variable pointing to the root of the miniconda directory
# echo $CONDA/bin >> $GITHUB_PATH
- name: Add extra channels
run: |
conda config --add channels conda-forge
conda config --add channels defaults
# - name: Add extra channels
# run: |
# conda config --add channels conda-forge
# conda config --add channels defaults

- name: Install dependencies
# - name: Install dependencies
# run: |
# conda env update --file environment.yml --name base
- name: set up env3
run: |
conda env update --file environment.yml --name base
python -m venv ENV3
- name: Lint with flake8
run: |
conda install flake8
.\ENV3\Scripts\activate.ps1
pip install flake8
# stop the build if there are Python syntax errors or undefined names
flake8 --exclude deprecated . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 --exclude deprecated . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Install current Python library
run: |
conda activate base # Activate the conda environment
.\ENV3\Scripts\activate.ps1
pip install -e . # Install the current Python library in editable mode
pip install cloudmesh-vpn
- name: Test with pytest
run: |
conda install pytest
source activate base & pytest tests
.\ENV3\Scripts\activate.ps1
pip install pytest
pytest tests -rsx
15 changes: 12 additions & 3 deletions cloudmesh/common/Host.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cloudmesh.common.parameter import Parameter
from cloudmesh.common.util import path_expand
from cloudmesh.common.util import readfile
from cloudmesh.common.systeminfo import os_is_windows
from pprint import pprint
from cloudmesh.common.Printer import Printer

Expand Down Expand Up @@ -233,6 +234,7 @@ def ssh(hosts=None,
ssh_command = ['ssh',
'-o', 'StrictHostKeyChecking=no',
'-o', 'UserKnownHostsFile=/dev/null',
'-o', 'PreferredAuthentications=publickey',
'-i', key,
'{host}',
f'{command}']
Expand Down Expand Up @@ -325,10 +327,17 @@ def _ping(args):
"""
ip = args['ip']
count = str(args['count'])
count_flag = '-n' if platform == 'windows' else '-c'
command = ['ping', count_flag, count, ip]

count_flag = '-n' if os_is_windows() else '-c'
if os_is_windows():
# adding ipv4 enforce for windows
# for some reason -4 is required or hosts
# fail. we need ipv4
command = ['ping', '-4', ip, count_flag, count]
else:
command = ['ping', count_flag, count, ip]
# command = ['ping', '-4', ip, count_flag, count]
result = subprocess.run(command, capture_output=True)

try:
timers = result.stdout \
.decode("utf-8", "ignore") \
Expand Down
47 changes: 32 additions & 15 deletions cloudmesh/common/Shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,25 @@ def locale():
@staticmethod
def ssh_enabled():
if os_is_linux():
r = Shell.run("service sshd status | fgrep running").strip()
try:
r = Shell.run("which sshd")
except RuntimeError as e:
raise RuntimeError("You do not have OpenSSH installed. "
"sudo apt-get install openssh-client openssh-server "
"Automatic installation will be implemented in future cloudmesh version.")
# the reason why we do it like this is because WSL
# does not have sshd as a status. this works fine
r = Shell.run("service ssh status | fgrep running").strip()

return len(r) > 0
elif os_is_windows():
r = Shell.run("ps | grep -F ssh")
return "ssh" in r
# r = Shell.run("ps | grep -F ssh")
# return "ssh" in r
processes = psutil.process_iter(attrs=['name'])
# Filter the processes for 'ssh'
ssh_processes = [p.info for p in processes if 'ssh' in p.info['name']]
return len(ssh_processes) > 0

elif os_is_mac():
r = Shell.run("ps -ef")
if "sshd" in r:
Expand Down Expand Up @@ -281,7 +295,7 @@ def run_timed(label, command, encoding=None, service=None):
return str(result)

@staticmethod
def run(command, exit="; exit 0", encoding='utf-8', replace=True, timeout=None):
def run(command, exitcode="", encoding='utf-8', replace=True, timeout=None):
"""
executes the command and returns the output as string
:param command:
Expand All @@ -295,18 +309,21 @@ def run(command, exit="; exit 0", encoding='utf-8', replace=True, timeout=None):
else:
c = ";"
command = f"{command}".replace(";", c)
else:
command = f"{command} {exit}"
elif exitcode:
command = f"{command} {exitcode}"

if timeout is not None:
r = subprocess.check_output(command,
stderr=subprocess.STDOUT,
shell=True,
timeout=timeout)
else:
r = subprocess.check_output(command,
stderr=subprocess.STDOUT,
shell=True)
try:
if timeout is not None:
r = subprocess.check_output(command,
stderr=subprocess.STDOUT,
shell=True,
timeout=timeout)
else:
r = subprocess.check_output(command,
stderr=subprocess.STDOUT,
shell=True)
except subprocess.CalledProcessError as e:
raise RuntimeError(f"{e.returncode} {e.output.decode()}")
if encoding is None or encoding == 'utf-8':
return str(r, 'utf-8')
else:
Expand Down
9 changes: 8 additions & 1 deletion tests/ssh/test_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
# pytest -v --capture=no tests/ssh/test_ssh..py::Test_name::<METHODNAME>
###############################################################

# https://github.com/actions/runner-images/issues/1519 ping does not work in github runner so we skip it.
import os
from distutils.util import strtobool
github_action = strtobool(os.getenv('GITHUB_ACTIONS', 'false'))

import pytest
from cloudmesh.common.Benchmark import Benchmark
Expand Down Expand Up @@ -49,7 +52,8 @@ def craete_location(host):



@pytest.mark.skipif(not Shell.ssh_enabled(), reason="SSH is not aenabled")
@pytest.mark.skipif(not Shell.ssh_enabled(), reason="SSH is not enabled")
@pytest.mark.skipif(github_action, reason='GitHub Runner uses Azure and Azure does not have an ssh key set up!')
@pytest.mark.incremental
class TestSsh:

Expand All @@ -71,6 +75,9 @@ def test_ssh_processors(self):
results = self.ssh(processors=processors)
print(Printer.write(results))
for result in results:
if "denied (publickey)" in result["stderr"].decode():
pytest.skip("ssh test cannot proceed because ssh-copy-id not yet "
"done.")
assert result["success"]

#
Expand Down
4 changes: 2 additions & 2 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def test_custom_path(self):

def test_environment_variable_path(self):
HEADING()
os.environ["CLOUDMESH"] = "./tmp/.cloudmesh"
os.environ["CLOUDMESH_CONFIG_DIR"] = "./tmp/.cloudmesh"
cloudmesh = Base()
assert cloudmesh.path == "./tmp/.cloudmesh"
assert cloudmesh.config == f"{cloudmesh.path}/cloudmesh.yaml"
shutil.rmtree("./tmp")
del os.environ["CLOUDMESH"]
del os.environ["CLOUDMESH_CONFIG_DIR"]

def test_cloudmesh_in_cwd(self):
HEADING()
Expand Down
6 changes: 6 additions & 0 deletions tests/test_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

cloud = "local"

# https://github.com/actions/runner-images/issues/1519 ping does not work in github runner so we skip it.
import os
from distutils.util import strtobool
github_action = strtobool(os.getenv('GITHUB_ACTIONS', 'false'))

# multiping only works if you have root, so we can not use it
# from multiping import MultiPing

Expand All @@ -33,6 +38,7 @@


@pytest.mark.incremental
@pytest.mark.skipif(github_action, reason='GitHub Runner uses Azure and Azure disables ping. :( Too bad!')
class Test_ping:

def ping(self, processors=1):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_pwd(self):
Benchmark.Start()
r = Shell.pwd()
Benchmark.Stop()
assert 'cm/cloudmesh-common' in r or 'cm\\cloudmesh-common' in r
assert 'cloudmesh-common' in r

def test_open(self):
HEADING()
Expand Down
8 changes: 6 additions & 2 deletions tests/test_shell_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
from cloudmesh.common.systeminfo import os_is_windows, os_is_linux, os_is_mac
from pathlib import Path

import time
# https://github.com/actions/runner-images/issues/1519 ping does not work in github runner so we skip it.
import os
from distutils.util import strtobool
github_action = strtobool(os.getenv('GITHUB_ACTIONS', 'false'))


class TestShell:
Expand Down Expand Up @@ -139,7 +142,7 @@ def test_map_filename(self):
assert os.path.exists(path_expand('./tmp')) == False
Benchmark.Stop()


@pytest.mark.skipif(github_action, reason='GitHub Runner is headless, and GUI is not possible, so this is skipped.')
def test_open(self):
HEADING()
Benchmark.Start()
Expand Down Expand Up @@ -188,6 +191,7 @@ def test_shell_cat(self):
assert '#' in r
assert 'tabulate' in r

@pytest.mark.skipif(github_action, reason='GitHub Runner uses Azure and Azure disables ping. :( Too bad!')
def test_shell_ping(self):
HEADING()
Benchmark.Start()
Expand Down

0 comments on commit 11596fa

Please sign in to comment.