Skip to content

Commit a0c632a

Browse files
committed
tests: first attempt to mock shell() call
1 parent 113683b commit a0c632a

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

cmdeploy/src/cmdeploy/remote/rdns.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
"""
1212

1313
import re
14-
14+
from pprint import pprint
1515
from .rshell import CalledProcessError, shell, log_progress
1616

1717

1818
def perform_initial_checks(mail_domain, pre_command=""):
1919
"""Collecting initial DNS settings."""
2020
assert mail_domain
21+
pprint("rdns.perform_initial_checks: " + shell.__module__)
22+
2123
if not shell("dig", fail_ok=True, print=log_progress):
2224
shell("apt-get update && apt-get install -y dnsutils", print=log_progress)
2325
A = query_dns("A", mail_domain)

cmdeploy/src/cmdeploy/remote/rshell.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
2+
from pprint import pprint
33
from subprocess import DEVNULL, CalledProcessError, check_output
44

55

@@ -9,6 +9,7 @@ def log_progress(data):
99

1010

1111
def shell(command, fail_ok=False, print=print):
12+
pprint("test_cmdeploy: " + shell.__module__)
1213
print(f"$ {command}")
1314
args = dict(shell=True)
1415
if fail_ok:

cmdeploy/src/cmdeploy/tests/test_cmdeploy.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from cmdeploy.cmdeploy import get_parser, main
77
from cmdeploy.www import get_paths
8+
import cmdeploy.remote.rshell
9+
import cmdeploy.dns
810

911

1012
@pytest.fixture(autouse=True)
@@ -59,3 +61,27 @@ def test_www_folder(example_config, tmp_path):
5961
assert www_path == tmp_path
6062
assert src_dir == src_path
6163
assert build_dir == tmp_path.joinpath("build")
64+
65+
66+
def test_dns_when_ssh_docker(monkeypatch):
67+
commands = []
68+
69+
def shell(command, fail_ok=None, print=None):
70+
assert command == False
71+
commands.append(command)
72+
73+
# mock shell function to add called commands to a global list
74+
monkeypatch.setattr(
75+
cmdeploy.remote.rshell, shell.__name__, shell
76+
) # still doesn't get called in get_initial_remote_data :(
77+
print("test_cmdeploy: " + shell.__module__)
78+
# run cmdeploy dns with --ssh-host
79+
# @docker
80+
cmdeploy.dns.get_initial_remote_data("@docker", "chatmail.example.org")
81+
for cmd in commands:
82+
print(cmd)
83+
# localhost
84+
# @local
85+
# without --ssh-host
86+
# check which commands were called
87+
assert False

0 commit comments

Comments
 (0)