Skip to content

Commit

Permalink
Use tmp_path for ssh config testing
Browse files Browse the repository at this point in the history
  • Loading branch information
unkcpz committed Oct 4, 2023
1 parent ba23f42 commit 1f818fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
27 changes: 16 additions & 11 deletions aiidalab_widgets_base/computational_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,17 @@ class SshComputerSetup(ipw.VBox):
message = traitlets.Unicode()
password_message = traitlets.Unicode("The passwordless enabling log.")

def __init__(self, **kwargs):
def __init__(self, ssh_folder=None, **kwargs):
"""Setup a passwordless access to a computer."""
# ssh folder init
if ssh_folder is None:
ssh_folder = Path.home() / ".ssh"
if not ssh_folder.exists():
ssh_folder.mkdir()
ssh_folder.chmod(0o700)

self.ssh_folder = ssh_folder

self._ssh_connection_message = None
self._password_message = ipw.HTML()
ipw.dlink((self, "password_message"), (self._password_message, "value"))
Expand Down Expand Up @@ -422,8 +432,8 @@ def _can_login(self):

def _is_in_config(self):
"""Check if the SSH config file contains host information."""
fpath = Path.home() / ".ssh" / "config"
if not fpath.exists():
config_path = self.ssh_folder / "config"
if not config_path.exists():
return False
sshcfg = parse_sshconfig(self.hostname.value)
# NOTE: parse_sshconfig returns a dict with a hostname
Expand All @@ -435,15 +445,10 @@ def _is_in_config(self):

def _write_ssh_config(self, private_key_abs_fname=None):
"""Put host information into the config file."""
fpath = Path.home() / ".ssh"
if not fpath.exists():
fpath.mkdir()
fpath.chmod(0o700)

fpath = fpath / "config"
config_path = self.ssh_folder / "config"

self.message = f"Adding {self.hostname.value} section to {fpath}"
with open(fpath, "a") as file:
self.message = f"Adding {self.hostname.value} section to {config_path}"
with open(config_path, "a") as file:
file.write(f"Host {self.hostname.value}\n")
file.write(f" User {self.username.value}\n")
file.write(f" Port {self.port.value}\n")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_computational_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ 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(tmp_path):
"""Test the SshComputerSetup."""
widget = computational_resources.SshComputerSetup()
widget = computational_resources.SshComputerSetup(ssh_folder=tmp_path)

ssh_config = {
"hostname": "daint.cscs.ch",
Expand Down

0 comments on commit 1f818fe

Please sign in to comment.