clan-cli/ssh: add tests + fixes
This commit is contained in:
@@ -36,7 +36,7 @@ def ssh(
|
|||||||
subprocess.run(cmd)
|
subprocess.run(cmd)
|
||||||
|
|
||||||
|
|
||||||
def qrcode_scan(pictureFile: str) -> dict:
|
def qrcode_scan(pictureFile: str) -> dict: # pragma: no cover
|
||||||
subprocess.Popen(
|
subprocess.Popen(
|
||||||
[
|
[
|
||||||
"nix",
|
"nix",
|
||||||
@@ -52,7 +52,7 @@ def qrcode_scan(pictureFile: str) -> dict:
|
|||||||
).stdout.read()
|
).stdout.read()
|
||||||
|
|
||||||
|
|
||||||
def main(args: argparse.Namespace) -> None:
|
def main(args: argparse.Namespace) -> None: # pragma: no cover
|
||||||
if args.json:
|
if args.json:
|
||||||
with open(args.json) as file:
|
with open(args.json) as file:
|
||||||
ssh_data = json.load(file)
|
ssh_data = json.load(file)
|
||||||
@@ -62,13 +62,15 @@ def main(args: argparse.Namespace) -> None:
|
|||||||
ssh(host=ssh_data["address"], password=ssh_data["password"])
|
ssh(host=ssh_data["address"], password=ssh_data["password"])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def register_parser(parser: argparse.ArgumentParser) -> None:
|
def register_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
parser.add_argument(
|
group = parser.add_mutually_exclusive_group(required=True)
|
||||||
|
group.add_argument(
|
||||||
"-j",
|
"-j",
|
||||||
"--json",
|
"--json",
|
||||||
help="specify the json file for ssh data (generated by starting the clan installer)",
|
help="specify the json file for ssh data (generated by starting the clan installer)",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
group.add_argument(
|
||||||
"-P",
|
"-P",
|
||||||
"--png",
|
"--png",
|
||||||
help="specify the json file for ssh data as the qrcode image (generated by starting the clan installer)",
|
help="specify the json file for ssh data as the qrcode image (generated by starting the clan installer)",
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import pytest
|
||||||
|
import sys
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
import pytest_subprocess.fake_process
|
import pytest_subprocess.fake_process
|
||||||
@@ -8,6 +10,15 @@ from pytest_subprocess import utils
|
|||||||
|
|
||||||
import clan_cli.ssh
|
import clan_cli.ssh
|
||||||
|
|
||||||
|
def test_no_args(
|
||||||
|
capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch
|
||||||
|
) -> None:
|
||||||
|
monkeypatch.setattr(sys, "argv", ["", "ssh"])
|
||||||
|
with pytest.raises(SystemExit) as pytest_wrapped_e:
|
||||||
|
clan_cli.main()
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert captured.err.startswith("usage:")
|
||||||
|
|
||||||
|
|
||||||
# using fp fixture from pytest-subprocess
|
# using fp fixture from pytest-subprocess
|
||||||
def test_ssh_no_pass(fp: pytest_subprocess.fake_process.FakeProcess) -> None:
|
def test_ssh_no_pass(fp: pytest_subprocess.fake_process.FakeProcess) -> None:
|
||||||
@@ -21,6 +32,7 @@ def test_ssh_no_pass(fp: pytest_subprocess.fake_process.FakeProcess) -> None:
|
|||||||
"-o",
|
"-o",
|
||||||
"StrictHostKeyChecking=no",
|
"StrictHostKeyChecking=no",
|
||||||
f"{user}@{host}",
|
f"{user}@{host}",
|
||||||
|
fp.any(),
|
||||||
]
|
]
|
||||||
fp.register(cmd)
|
fp.register(cmd)
|
||||||
clan_cli.ssh.ssh(
|
clan_cli.ssh.ssh(
|
||||||
@@ -30,28 +42,20 @@ def test_ssh_no_pass(fp: pytest_subprocess.fake_process.FakeProcess) -> None:
|
|||||||
assert fp.call_count(cmd) == 1
|
assert fp.call_count(cmd) == 1
|
||||||
|
|
||||||
|
|
||||||
# using fp fixture from pytest-subprocess
|
def test_ssh_with_pass(fp: pytest_subprocess.fake_process.FakeProcess) -> None:
|
||||||
def test_ssh_json(fp: pytest_subprocess.fake_process.FakeProcess) -> None:
|
host = "somehost"
|
||||||
with tempfile.NamedTemporaryFile(mode="w+") as file:
|
user = "user"
|
||||||
json.dump({"password": "XXX", "address": "somehost"}, file)
|
cmd: list[Union[str, utils.Any]] = [
|
||||||
cmd: list[Union[str, utils.Any]] = [
|
"nix",
|
||||||
"nix",
|
"shell",
|
||||||
"shell",
|
"nixpkgs#sshpass",
|
||||||
"nixpkgs#sshpass",
|
"-c",
|
||||||
"-c",
|
fp.any(),
|
||||||
"torify",
|
]
|
||||||
"sshpass",
|
fp.register(cmd)
|
||||||
"-p",
|
clan_cli.ssh.ssh(
|
||||||
"XXX",
|
host=host,
|
||||||
"ssh",
|
user=user,
|
||||||
"-o",
|
password="XXX",
|
||||||
"UserKnownHostsFile=/dev/null",
|
)
|
||||||
"-o",
|
assert fp.call_count(cmd) == 1
|
||||||
"StrictHostKeyChecking=no",
|
|
||||||
"root@somehost",
|
|
||||||
]
|
|
||||||
fp.register(cmd)
|
|
||||||
file.seek(0) # write file and go to the beginning
|
|
||||||
args = argparse.Namespace(json=file.name, ssh_args=[])
|
|
||||||
clan_cli.ssh.main(args)
|
|
||||||
assert fp.call_count(cmd) == 1
|
|
||||||
|
|||||||
Reference in New Issue
Block a user