From 248b2256658189f356ba430fc0ffbd7742498649 Mon Sep 17 00:00:00 2001 From: missytake Date: Tue, 14 Oct 2025 15:32:18 +0200 Subject: [PATCH] tests: first attempt to mock shell() call --- cmdeploy/src/cmdeploy/remote/rdns.py | 4 ++- cmdeploy/src/cmdeploy/remote/rshell.py | 3 ++- cmdeploy/src/cmdeploy/tests/test_cmdeploy.py | 26 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/cmdeploy/src/cmdeploy/remote/rdns.py b/cmdeploy/src/cmdeploy/remote/rdns.py index 7340a777..ce66c46d 100644 --- a/cmdeploy/src/cmdeploy/remote/rdns.py +++ b/cmdeploy/src/cmdeploy/remote/rdns.py @@ -11,13 +11,15 @@ All functions of this module """ import re - +from pprint import pprint from .rshell import CalledProcessError, shell, log_progress def perform_initial_checks(mail_domain, pre_command=""): """Collecting initial DNS settings.""" assert mail_domain + pprint("rdns.perform_initial_checks: " + shell.__module__) + if not shell("dig", fail_ok=True, print=log_progress): shell("apt-get update && apt-get install -y dnsutils", print=log_progress) A = query_dns("A", mail_domain) diff --git a/cmdeploy/src/cmdeploy/remote/rshell.py b/cmdeploy/src/cmdeploy/remote/rshell.py index f8166816..84475816 100644 --- a/cmdeploy/src/cmdeploy/remote/rshell.py +++ b/cmdeploy/src/cmdeploy/remote/rshell.py @@ -1,5 +1,5 @@ import sys - +from pprint import pprint from subprocess import DEVNULL, CalledProcessError, check_output @@ -9,6 +9,7 @@ def log_progress(data): def shell(command, fail_ok=False, print=print): + pprint("test_cmdeploy: " + shell.__module__) print(f"$ {command}") args = dict(shell=True) if fail_ok: diff --git a/cmdeploy/src/cmdeploy/tests/test_cmdeploy.py b/cmdeploy/src/cmdeploy/tests/test_cmdeploy.py index 6e87b4ea..6f9a2cc7 100644 --- a/cmdeploy/src/cmdeploy/tests/test_cmdeploy.py +++ b/cmdeploy/src/cmdeploy/tests/test_cmdeploy.py @@ -5,6 +5,8 @@ import pytest from cmdeploy.cmdeploy import get_parser, main from cmdeploy.www import get_paths +import cmdeploy.remote.rshell +import cmdeploy.dns @pytest.fixture(autouse=True) @@ -59,3 +61,27 @@ def test_www_folder(example_config, tmp_path): assert www_path == tmp_path assert src_dir == src_path assert build_dir == tmp_path.joinpath("build") + + +def test_dns_when_ssh_docker(monkeypatch): + commands = [] + + def shell(command, fail_ok=None, print=None): + assert command == False + commands.append(command) + + # mock shell function to add called commands to a global list + monkeypatch.setattr( + cmdeploy.remote.rshell, shell.__name__, shell + ) # still doesn't get called in get_initial_remote_data :( + print("test_cmdeploy: " + shell.__module__) + # run cmdeploy dns with --ssh-host + # @docker + cmdeploy.dns.get_initial_remote_data("@docker", "chatmail.example.org") + for cmd in commands: + print(cmd) + # localhost + # @local + # without --ssh-host + # check which commands were called + assert False