refactor(cli): split decnet/cli.py monolith into decnet/cli/ package
The 1,878-line cli.py held every Typer command plus process/HTTP helpers and mode-gating logic. Split into one module per command using a register(app) pattern so submodules never import app at module scope, eliminating circular-import risk. - utils.py: process helpers, _http_request, _kill_all_services, console, log - gating.py: MASTER_ONLY_* sets, _require_master_mode, _gate_commands_by_mode - deploy.py: deploy + _deploy_swarm (tightly coupled) - lifecycle.py: status, teardown, redeploy - workers.py: probe, collect, mutate, correlate - inventory.py, swarm.py, db.py, and one file per remaining command __init__.py calls register(app) on each module then runs the mode gate last, and re-exports the private symbols tests patch against (_db_reset_mysql_async, _kill_all_services, _require_master_mode, etc.). Test patches retargeted to the submodule where each name now resolves. Enroll-bundle tarball test updated to assert decnet/cli/__init__.py. No behavioral change.
This commit is contained in:
@@ -293,7 +293,7 @@ async def test_get_tgz_contents(client, auth_token, tmp_path):
|
||||
assert "home/.decnet/agent/worker.crt" in names
|
||||
assert "home/.decnet/agent/worker.key" in names
|
||||
assert "services.ini" in names
|
||||
assert "decnet/cli.py" in names # source shipped
|
||||
assert "decnet/cli/__init__.py" in names # source shipped
|
||||
assert "pyproject.toml" in names
|
||||
|
||||
# Excluded paths must NOT be shipped
|
||||
|
||||
@@ -15,7 +15,7 @@ import pytest
|
||||
from typer.testing import CliRunner
|
||||
|
||||
from decnet import cli as cli_mod
|
||||
from decnet.cli import app
|
||||
from decnet.cli import app, deploy as cli_deploy, utils as cli_utils
|
||||
|
||||
|
||||
runner = CliRunner()
|
||||
@@ -49,7 +49,7 @@ def http_stub(monkeypatch: pytest.MonkeyPatch) -> _HttpStub:
|
||||
return resp
|
||||
raise AssertionError(f"Unscripted HTTP call: {method} {url}")
|
||||
|
||||
monkeypatch.setattr(cli_mod, "_http_request", _fake)
|
||||
monkeypatch.setattr(cli_utils, "_http_request", _fake)
|
||||
return calls
|
||||
|
||||
|
||||
@@ -259,9 +259,9 @@ def test_deploy_swarm_round_robins_and_posts(http_stub, monkeypatch: pytest.Monk
|
||||
})
|
||||
|
||||
# Stub network detection so we don't need root / real NICs.
|
||||
monkeypatch.setattr(cli_mod, "detect_interface", lambda: "eth0")
|
||||
monkeypatch.setattr(cli_mod, "detect_subnet", lambda _iface: ("10.0.0.0/24", "10.0.0.254"))
|
||||
monkeypatch.setattr(cli_mod, "get_host_ip", lambda _iface: "10.0.0.100")
|
||||
monkeypatch.setattr(cli_deploy, "detect_interface", lambda: "eth0")
|
||||
monkeypatch.setattr(cli_deploy, "detect_subnet", lambda _iface: ("10.0.0.0/24", "10.0.0.254"))
|
||||
monkeypatch.setattr(cli_deploy, "get_host_ip", lambda _iface: "10.0.0.100")
|
||||
|
||||
result = runner.invoke(app, [
|
||||
"deploy", "--mode", "swarm", "--deckies", "3",
|
||||
@@ -280,9 +280,9 @@ def test_deploy_swarm_round_robins_and_posts(http_stub, monkeypatch: pytest.Monk
|
||||
def test_deploy_swarm_fails_if_no_workers(http_stub, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
http_stub.script[("GET", "/swarm/hosts?host_status=enrolled")] = _FakeResp([])
|
||||
http_stub.script[("GET", "/swarm/hosts?host_status=active")] = _FakeResp([])
|
||||
monkeypatch.setattr(cli_mod, "detect_interface", lambda: "eth0")
|
||||
monkeypatch.setattr(cli_mod, "detect_subnet", lambda _iface: ("10.0.0.0/24", "10.0.0.254"))
|
||||
monkeypatch.setattr(cli_mod, "get_host_ip", lambda _iface: "10.0.0.100")
|
||||
monkeypatch.setattr(cli_deploy, "detect_interface", lambda: "eth0")
|
||||
monkeypatch.setattr(cli_deploy, "detect_subnet", lambda _iface: ("10.0.0.0/24", "10.0.0.254"))
|
||||
monkeypatch.setattr(cli_deploy, "get_host_ip", lambda _iface: "10.0.0.100")
|
||||
|
||||
result = runner.invoke(app, [
|
||||
"deploy", "--mode", "swarm", "--deckies", "2",
|
||||
|
||||
@@ -14,7 +14,7 @@ import pytest
|
||||
from typer.testing import CliRunner
|
||||
|
||||
from decnet import cli as cli_mod
|
||||
from decnet.cli import app
|
||||
from decnet.cli import app, utils as cli_utils
|
||||
|
||||
|
||||
runner = CliRunner()
|
||||
@@ -40,7 +40,7 @@ def http_stub(monkeypatch: pytest.MonkeyPatch) -> dict:
|
||||
return _FakeResp(state["hosts"])
|
||||
raise AssertionError(f"Unscripted HTTP call: {method} {url}")
|
||||
|
||||
monkeypatch.setattr(cli_mod, "_http_request", _fake)
|
||||
monkeypatch.setattr(cli_utils, "_http_request", _fake)
|
||||
return state
|
||||
|
||||
|
||||
|
||||
@@ -60,10 +60,10 @@ class TestArchetypesCommand:
|
||||
|
||||
class TestDeployCommand:
|
||||
@patch("decnet.engine.deploy")
|
||||
@patch("decnet.cli.allocate_ips", return_value=["192.168.1.10"])
|
||||
@patch("decnet.cli.get_host_ip", return_value="192.168.1.2")
|
||||
@patch("decnet.cli.detect_subnet", return_value=("192.168.1.0/24", "192.168.1.1"))
|
||||
@patch("decnet.cli.detect_interface", return_value="eth0")
|
||||
@patch("decnet.cli.deploy.allocate_ips", return_value=["192.168.1.10"])
|
||||
@patch("decnet.cli.deploy.get_host_ip", return_value="192.168.1.2")
|
||||
@patch("decnet.cli.deploy.detect_subnet", return_value=("192.168.1.0/24", "192.168.1.1"))
|
||||
@patch("decnet.cli.deploy.detect_interface", return_value="eth0")
|
||||
def test_deploy_dry_run(self, mock_iface, mock_subnet, mock_hip,
|
||||
mock_ips, mock_deploy):
|
||||
result = runner.invoke(app, [
|
||||
@@ -73,13 +73,13 @@ class TestDeployCommand:
|
||||
mock_deploy.assert_called_once()
|
||||
|
||||
def test_deploy_no_interface_found(self):
|
||||
with patch("decnet.cli.detect_interface", side_effect=ValueError("No interface")):
|
||||
with patch("decnet.cli.deploy.detect_interface", side_effect=ValueError("No interface")):
|
||||
result = runner.invoke(app, ["deploy", "--deckies", "1"])
|
||||
assert result.exit_code == 1
|
||||
|
||||
def test_deploy_no_subnet_found(self):
|
||||
with patch("decnet.cli.detect_interface", return_value="eth0"), \
|
||||
patch("decnet.cli.detect_subnet", side_effect=ValueError("No subnet")):
|
||||
with patch("decnet.cli.deploy.detect_interface", return_value="eth0"), \
|
||||
patch("decnet.cli.deploy.detect_subnet", side_effect=ValueError("No subnet")):
|
||||
result = runner.invoke(app, ["deploy", "--deckies", "1", "--services", "ssh"])
|
||||
assert result.exit_code == 1
|
||||
|
||||
@@ -87,21 +87,21 @@ class TestDeployCommand:
|
||||
result = runner.invoke(app, ["deploy", "--mode", "invalid", "--deckies", "1"])
|
||||
assert result.exit_code == 1
|
||||
|
||||
@patch("decnet.cli.detect_interface", return_value="eth0")
|
||||
@patch("decnet.cli.deploy.detect_interface", return_value="eth0")
|
||||
def test_deploy_no_deckies_no_config(self, mock_iface):
|
||||
result = runner.invoke(app, ["deploy", "--services", "ssh"])
|
||||
assert result.exit_code == 1
|
||||
|
||||
@patch("decnet.cli.detect_interface", return_value="eth0")
|
||||
@patch("decnet.cli.deploy.detect_interface", return_value="eth0")
|
||||
def test_deploy_no_services_no_randomize(self, mock_iface):
|
||||
result = runner.invoke(app, ["deploy", "--deckies", "1"])
|
||||
assert result.exit_code == 1
|
||||
|
||||
@patch("decnet.engine.deploy")
|
||||
@patch("decnet.cli.allocate_ips", return_value=["192.168.1.10"])
|
||||
@patch("decnet.cli.get_host_ip", return_value="192.168.1.2")
|
||||
@patch("decnet.cli.detect_subnet", return_value=("192.168.1.0/24", "192.168.1.1"))
|
||||
@patch("decnet.cli.detect_interface", return_value="eth0")
|
||||
@patch("decnet.cli.deploy.allocate_ips", return_value=["192.168.1.10"])
|
||||
@patch("decnet.cli.deploy.get_host_ip", return_value="192.168.1.2")
|
||||
@patch("decnet.cli.deploy.detect_subnet", return_value=("192.168.1.0/24", "192.168.1.1"))
|
||||
@patch("decnet.cli.deploy.detect_interface", return_value="eth0")
|
||||
def test_deploy_with_archetype(self, mock_iface, mock_subnet, mock_hip,
|
||||
mock_ips, mock_deploy):
|
||||
result = runner.invoke(app, [
|
||||
@@ -117,10 +117,10 @@ class TestDeployCommand:
|
||||
|
||||
@patch("decnet.engine.deploy")
|
||||
@patch("subprocess.Popen")
|
||||
@patch("decnet.cli.allocate_ips", return_value=["192.168.1.10"])
|
||||
@patch("decnet.cli.get_host_ip", return_value="192.168.1.2")
|
||||
@patch("decnet.cli.detect_subnet", return_value=("192.168.1.0/24", "192.168.1.1"))
|
||||
@patch("decnet.cli.detect_interface", return_value="eth0")
|
||||
@patch("decnet.cli.deploy.allocate_ips", return_value=["192.168.1.10"])
|
||||
@patch("decnet.cli.deploy.get_host_ip", return_value="192.168.1.2")
|
||||
@patch("decnet.cli.deploy.detect_subnet", return_value=("192.168.1.0/24", "192.168.1.1"))
|
||||
@patch("decnet.cli.deploy.detect_interface", return_value="eth0")
|
||||
def test_deploy_full_with_api(self, mock_iface, mock_subnet, mock_hip,
|
||||
mock_ips, mock_popen, mock_deploy):
|
||||
# Test non-dry-run with API and collector starts
|
||||
@@ -131,10 +131,10 @@ class TestDeployCommand:
|
||||
assert mock_popen.call_count >= 1 # API
|
||||
|
||||
@patch("decnet.engine.deploy")
|
||||
@patch("decnet.cli.allocate_ips", return_value=["192.168.1.10"])
|
||||
@patch("decnet.cli.get_host_ip", return_value="192.168.1.2")
|
||||
@patch("decnet.cli.detect_subnet", return_value=("192.168.1.0/24", "192.168.1.1"))
|
||||
@patch("decnet.cli.detect_interface", return_value="eth0")
|
||||
@patch("decnet.cli.deploy.allocate_ips", return_value=["192.168.1.10"])
|
||||
@patch("decnet.cli.deploy.get_host_ip", return_value="192.168.1.2")
|
||||
@patch("decnet.cli.deploy.detect_subnet", return_value=("192.168.1.0/24", "192.168.1.1"))
|
||||
@patch("decnet.cli.deploy.detect_interface", return_value="eth0")
|
||||
def test_deploy_with_distro(self, mock_iface, mock_subnet, mock_hip,
|
||||
mock_ips, mock_deploy):
|
||||
result = runner.invoke(app, [
|
||||
@@ -149,10 +149,10 @@ class TestDeployCommand:
|
||||
assert result.exit_code == 1
|
||||
|
||||
@patch("decnet.engine.deploy")
|
||||
@patch("decnet.cli.load_ini")
|
||||
@patch("decnet.cli.get_host_ip", return_value="192.168.1.2")
|
||||
@patch("decnet.cli.detect_subnet", return_value=("192.168.1.0/24", "192.168.1.1"))
|
||||
@patch("decnet.cli.detect_interface", return_value="eth0")
|
||||
@patch("decnet.cli.deploy.load_ini")
|
||||
@patch("decnet.cli.deploy.get_host_ip", return_value="192.168.1.2")
|
||||
@patch("decnet.cli.deploy.detect_subnet", return_value=("192.168.1.0/24", "192.168.1.1"))
|
||||
@patch("decnet.cli.deploy.detect_interface", return_value="eth0")
|
||||
def test_deploy_with_config_file(self, mock_iface, mock_subnet, mock_hip,
|
||||
mock_load_ini, mock_deploy, tmp_path):
|
||||
from decnet.ini_loader import IniConfig, DeckySpec
|
||||
@@ -181,7 +181,7 @@ class TestTeardownCommand:
|
||||
result = runner.invoke(app, ["teardown"])
|
||||
assert result.exit_code == 1
|
||||
|
||||
@patch("decnet.cli._kill_all_services")
|
||||
@patch("decnet.cli.utils._kill_all_services")
|
||||
@patch("decnet.engine.teardown")
|
||||
def test_teardown_all(self, mock_teardown, mock_kill):
|
||||
result = runner.invoke(app, ["teardown", "--all"])
|
||||
|
||||
@@ -56,7 +56,7 @@ class TestDbResetDispatch:
|
||||
monkeypatch.setenv("DECNET_DB_URL", "mysql+aiomysql://u:p@h/d")
|
||||
|
||||
mock = AsyncMock()
|
||||
with patch("decnet.cli._db_reset_mysql_async", new=mock):
|
||||
with patch("decnet.cli.db._db_reset_mysql_async", new=mock):
|
||||
result = runner.invoke(app, ["db-reset"])
|
||||
|
||||
assert result.exit_code == 0, result.stdout
|
||||
@@ -70,7 +70,7 @@ class TestDbResetDispatch:
|
||||
monkeypatch.setenv("DECNET_DB_URL", "mysql+aiomysql://u:p@h/d")
|
||||
|
||||
mock = AsyncMock()
|
||||
with patch("decnet.cli._db_reset_mysql_async", new=mock):
|
||||
with patch("decnet.cli.db._db_reset_mysql_async", new=mock):
|
||||
result = runner.invoke(app, ["db-reset", "--i-know-what-im-doing"])
|
||||
|
||||
assert result.exit_code == 0, result.stdout
|
||||
@@ -81,7 +81,7 @@ class TestDbResetDispatch:
|
||||
monkeypatch.setenv("DECNET_DB_URL", "mysql+aiomysql://u:p@h/d")
|
||||
|
||||
mock = AsyncMock()
|
||||
with patch("decnet.cli._db_reset_mysql_async", new=mock):
|
||||
with patch("decnet.cli.db._db_reset_mysql_async", new=mock):
|
||||
result = runner.invoke(
|
||||
app, ["db-reset", "--mode", "drop-tables", "--i-know-what-im-doing"]
|
||||
)
|
||||
@@ -94,7 +94,7 @@ class TestDbResetDispatch:
|
||||
monkeypatch.setenv("DECNET_DB_URL", "mysql+aiomysql://from-env/db")
|
||||
|
||||
mock = AsyncMock()
|
||||
with patch("decnet.cli._db_reset_mysql_async", new=mock):
|
||||
with patch("decnet.cli.db._db_reset_mysql_async", new=mock):
|
||||
result = runner.invoke(app, [
|
||||
"db-reset", "--url", "mysql+aiomysql://override/db2",
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user