|
1 | 1 | import builtins |
2 | 2 | import copy |
| 3 | +import platform |
3 | 4 | import stat |
4 | | - |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +import warnings |
| 8 | +import os |
| 9 | +import subprocess |
| 10 | +from pathlib import Path |
5 | 11 | import paramiko |
6 | 12 |
|
7 | 13 | from datashuttle.utils import rclone, ssh |
@@ -49,33 +55,54 @@ def restore_mock_input(orig_builtin): |
49 | 55 | builtins.input = orig_builtin |
50 | 56 |
|
51 | 57 |
|
52 | | -def setup_hostkeys(project): |
| 58 | +def setup_hostkeys(project, setup_ssh_key_pair=True): # TODO: RENAME FUNCTION |
53 | 59 | """ |
54 | 60 | Convenience function to verify the server hostkey. |
| 61 | +
|
| 62 | + This requires monkeypatching a number of functions involved |
| 63 | + in the SSH setup process. `input()` is patched to always |
| 64 | + return the required hostkey confirmation "y". `getpass()` is |
| 65 | + patched to allways return the password for the container in which |
| 66 | + SSH tests are run. `isatty()` is patched because when running this |
| 67 | + for some reason it appears to be in a TTY - this might be a |
| 68 | + container thing. |
55 | 69 | """ |
| 70 | + # Monkeypatch |
56 | 71 | orig_builtin = setup_mock_input(input_="y") |
57 | | - ssh.verify_ssh_central_host( |
58 | | - project.cfg["central_host_id"], project.cfg.hostkeys_path, log=True |
59 | | - ) |
60 | | - restore_mock_input(orig_builtin) |
61 | 72 |
|
62 | 73 | orig_getpass = copy.deepcopy(ssh.getpass.getpass) |
63 | 74 | ssh.getpass.getpass = lambda _: "password" # type: ignore |
64 | 75 |
|
65 | | - ssh.setup_ssh_key(project.cfg, log=False) |
| 76 | + orig_isatty = copy.deepcopy(sys.stdin.isatty) |
| 77 | + sys.stdin.isatty = lambda: True |
| 78 | + |
| 79 | + # Run setup |
| 80 | + verified = ssh.verify_ssh_central_host( |
| 81 | + project.cfg["central_host_id"], project.cfg.hostkeys_path, log=True |
| 82 | + ) |
| 83 | + |
| 84 | + if setup_ssh_key_pair: |
| 85 | + ssh.setup_ssh_key(project.cfg, log=False) |
| 86 | + |
| 87 | + # Restore functions |
| 88 | + restore_mock_input(orig_builtin) |
66 | 89 | ssh.getpass.getpass = orig_getpass |
| 90 | + sys.stdin.isatty = orig_isatty |
| 91 | + |
| 92 | + return verified |
67 | 93 |
|
68 | 94 |
|
69 | 95 | def build_docker_image(project): |
70 | | - import os |
71 | | - import subprocess |
72 | | - from pathlib import Path |
| 96 | + """""" |
| 97 | + container_software = is_docker_or_singularity_installed() |
| 98 | + assert container_software is not False, ("docker or singularity not installed, " |
| 99 | + "this should be checked at the top of test script") |
73 | 100 |
|
74 | 101 | image_path = Path(__file__).parent / "ssh_test_images" |
75 | 102 | os.chdir(image_path) |
76 | | - subprocess.run("docker build -t ssh_server .", shell=True) |
| 103 | + subprocess.run(f"{container_software} build -t ssh_server .", shell=True) |
77 | 104 | subprocess.run( |
78 | | - "docker run -d -p 22:22 ssh_server", shell=True |
| 105 | + f"{container_software} run -d -p 22:22 ssh_server", shell=True |
79 | 106 | ) # ; docker build -t ssh_server .", shell=True) # ;docker run -p 22:22 ssh_server |
80 | 107 |
|
81 | 108 | setup_project_for_ssh( |
@@ -118,3 +145,32 @@ def recursive_search_central(project): |
118 | 145 | all_filenames, |
119 | 146 | ) |
120 | 147 | return all_filenames |
| 148 | + |
| 149 | + |
| 150 | +def get_test_ssh(): |
| 151 | + """""" |
| 152 | + if is_docker_or_singularity_installed(): |
| 153 | + test_ssh = True |
| 154 | + else: |
| 155 | + warnings.warn("SSH tests are not run as docker (Windows, macOS) " |
| 156 | + "or singularity (Linux) is not installed.") |
| 157 | + test_ssh = False |
| 158 | + |
| 159 | + return test_ssh |
| 160 | + |
| 161 | + |
| 162 | +def is_docker_or_singularity_installed(): # TODO: need to test |
| 163 | + """""" |
| 164 | + check_install = lambda command: subprocess.run( |
| 165 | + command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL |
| 166 | + ).returncode == 0 |
| 167 | + |
| 168 | + installed = False |
| 169 | + if platform.system() == "Linux": |
| 170 | + if check_install("singularity version"): |
| 171 | + installed = "singularity" |
| 172 | + else: |
| 173 | + if check_install("docker -v"): |
| 174 | + installed = "docker" |
| 175 | + |
| 176 | + return installed |
0 commit comments