Skip to content

Commit

Permalink
Use mock folder for ssh config testing (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
unkcpz committed Oct 4, 2023
1 parent ba23f42 commit 6003129
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
6 changes: 3 additions & 3 deletions aiidalab_widgets_base/computational_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import traitlets
from aiida import common, orm, plugins
from aiida.orm.utils.builders.computer import ComputerBuilder
from aiida.transports.plugins.ssh import parse_sshconfig
from aiida.transports.plugins import ssh as aiida_ssh_plugin
from humanfriendly import InvalidSize, parse_size
from IPython.display import clear_output, display

Expand Down Expand Up @@ -425,7 +425,7 @@ def _is_in_config(self):
fpath = Path.home() / ".ssh" / "config"
if not fpath.exists():
return False
sshcfg = parse_sshconfig(self.hostname.value)
sshcfg = aiida_ssh_plugin.parse_sshconfig(self.hostname.value)
# NOTE: parse_sshconfig returns a dict with a hostname
# even if it is not in the config file.
# We require at least the user to be specified.
Expand Down Expand Up @@ -850,7 +850,7 @@ def _configure_computer(self, computer: orm.Computer, transport: str):

def _configure_computer_ssh(self, computer: orm.Computer, user: orm.User):
"""Configure the computer with SSH transport"""
sshcfg = parse_sshconfig(self.hostname.value)
sshcfg = aiida_ssh_plugin.parse_sshconfig(self.hostname.value)
authparams = {
"port": int(sshcfg.get("port", 22)),
"look_for_keys": True,
Expand Down
11 changes: 9 additions & 2 deletions tests/test_computational_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ def test_computational_resources_widget(aiida_local_code_bash):


@pytest.mark.usefixtures("aiida_profile_clean")
def test_ssh_computer_setup_widget():
def test_ssh_computer_setup_widget(monkeypatch, tmp_path):
"""Test the SshComputerSetup."""
# mock home directory for ssh config file
monkeypatch.setenv("HOME", str(tmp_path))
widget = computational_resources.SshComputerSetup()

ssh_config = {
Expand Down Expand Up @@ -63,8 +65,13 @@ def test_ssh_computer_setup_widget():


@pytest.mark.usefixtures("aiida_profile_clean")
def test_aiida_computer_setup_widget():
def test_aiida_computer_setup_widget(monkeypatch):
"""Test the AiidaComputerSetup."""
# monkeypatch the parse_sshconfig
monkeypatch.setattr(
"aiida.transports.plugins.ssh.parse_sshconfig",
lambda _: {"hostname": "daint.cscs.ch", "user": "aiida"},
)
widget = computational_resources.AiidaComputerSetup()

# At the beginning, the computer_name should be an empty string.
Expand Down

0 comments on commit 6003129

Please sign in to comment.