clan-cli/ssh: add tests + fixes

This commit is contained in:
DavHau
2023-07-25 15:16:05 +02:00
parent 202e07d5fe
commit 310bdacb9d
2 changed files with 35 additions and 29 deletions

View File

@@ -1,6 +1,8 @@
import argparse
import json
import tempfile
import pytest
import sys
from typing import Union
import pytest_subprocess.fake_process
@@ -8,6 +10,15 @@ from pytest_subprocess import utils
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
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",
"StrictHostKeyChecking=no",
f"{user}@{host}",
fp.any(),
]
fp.register(cmd)
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
# using fp fixture from pytest-subprocess
def test_ssh_json(fp: pytest_subprocess.fake_process.FakeProcess) -> None:
with tempfile.NamedTemporaryFile(mode="w+") as file:
json.dump({"password": "XXX", "address": "somehost"}, file)
cmd: list[Union[str, utils.Any]] = [
"nix",
"shell",
"nixpkgs#sshpass",
"-c",
"torify",
"sshpass",
"-p",
"XXX",
"ssh",
"-o",
"UserKnownHostsFile=/dev/null",
"-o",
"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
def test_ssh_with_pass(fp: pytest_subprocess.fake_process.FakeProcess) -> None:
host = "somehost"
user = "user"
cmd: list[Union[str, utils.Any]] = [
"nix",
"shell",
"nixpkgs#sshpass",
"-c",
fp.any(),
]
fp.register(cmd)
clan_cli.ssh.ssh(
host=host,
user=user,
password="XXX",
)
assert fp.call_count(cmd) == 1