|
| 1 | +"""compose.py: ENABLE_SSH 時の SSH ポート publish 挙動 (PLAN33 / PR2) |
| 2 | +
|
| 3 | +`_build_dev_instance()` は ENABLE_SSH が有効なとき、各 dev-<index> サービスへ |
| 4 | +`<bind>:<port>:22` の publish を注入する。ポートは `ssh_host_port()` により |
| 5 | +プロジェクト名 + index から決定的に算出され、`down`→`up` を跨いでも一定である。 |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import yaml |
| 11 | +import pytest |
| 12 | + |
| 13 | +from devbase.volume import compose |
| 14 | +from devbase.volume.ports import ssh_host_port, _stable_hash |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def in_tmp_cwd(tmp_path, monkeypatch): |
| 19 | + """生成物 (.docker-compose.scale.yml) が散らからないよう CWD を tmp に移す。""" |
| 20 | + monkeypatch.chdir(tmp_path) |
| 21 | + monkeypatch.delenv("DEV_SERVICE_NAME", raising=False) |
| 22 | + # SSH 系 env を既定で無効化 (外部環境に左右されないよう明示的に消す) |
| 23 | + monkeypatch.delenv("ENABLE_SSH", raising=False) |
| 24 | + monkeypatch.delenv("DEVBASE_SSH_BIND", raising=False) |
| 25 | + monkeypatch.delenv("DEVBASE_SSH_PORT_BASE", raising=False) |
| 26 | + return tmp_path |
| 27 | + |
| 28 | + |
| 29 | +def _write_compose(tmp_path, services: dict) -> None: |
| 30 | + (tmp_path / "compose.yml").write_text( |
| 31 | + yaml.safe_dump({"services": services}, sort_keys=False), |
| 32 | + encoding="utf-8", |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def _load_scaled(tmp_path) -> dict: |
| 37 | + return yaml.safe_load((tmp_path / ".docker-compose.scale.yml").read_text()) |
| 38 | + |
| 39 | + |
| 40 | +def _ssh_ports(service: dict) -> list: |
| 41 | + """service の ports から `:22` を publish するエントリだけ抜き出す。""" |
| 42 | + return [p for p in service.get("ports", []) if str(p).endswith(":22")] |
| 43 | + |
| 44 | + |
| 45 | +# --- ENABLE_SSH 無効時 --- |
| 46 | + |
| 47 | +def test_no_ssh_ports_when_enable_ssh_unset(in_tmp_cwd): |
| 48 | + """ENABLE_SSH 未設定なら :22 の publish は注入されない。""" |
| 49 | + _write_compose(in_tmp_cwd, {"dev": {"image": "dev:latest"}}) |
| 50 | + |
| 51 | + compose.generate_scaled_compose(scale=2, project_name="proj") |
| 52 | + scaled = _load_scaled(in_tmp_cwd)["services"] |
| 53 | + |
| 54 | + for i in (1, 2): |
| 55 | + assert _ssh_ports(scaled[f"dev-{i}"]) == [] |
| 56 | + |
| 57 | + |
| 58 | +def test_no_ssh_ports_when_enable_ssh_false(in_tmp_cwd, monkeypatch): |
| 59 | + """ENABLE_SSH=false なら :22 の publish は注入されない。""" |
| 60 | + monkeypatch.setenv("ENABLE_SSH", "false") |
| 61 | + _write_compose(in_tmp_cwd, {"dev": {"image": "dev:latest"}}) |
| 62 | + |
| 63 | + compose.generate_scaled_compose(scale=1, project_name="proj") |
| 64 | + scaled = _load_scaled(in_tmp_cwd)["services"] |
| 65 | + |
| 66 | + assert _ssh_ports(scaled["dev-1"]) == [] |
| 67 | + |
| 68 | + |
| 69 | +# --- ENABLE_SSH 有効時 --- |
| 70 | + |
| 71 | +def test_ssh_ports_injected_when_enabled(in_tmp_cwd, monkeypatch): |
| 72 | + """ENABLE_SSH=true なら各 dev-<index> に 127.0.0.1:<port>:22 が付く。""" |
| 73 | + monkeypatch.setenv("ENABLE_SSH", "true") |
| 74 | + _write_compose(in_tmp_cwd, {"dev": {"image": "dev:latest"}}) |
| 75 | + |
| 76 | + compose.generate_scaled_compose(scale=2, project_name="proj") |
| 77 | + scaled = _load_scaled(in_tmp_cwd)["services"] |
| 78 | + |
| 79 | + for i in (1, 2): |
| 80 | + port = ssh_host_port("proj", i, 2200) |
| 81 | + assert _ssh_ports(scaled[f"dev-{i}"]) == [f"127.0.0.1:{port}:22"] |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize("truthy", ["true", "True", "TRUE", "1"]) |
| 85 | +def test_enable_ssh_truthy_values(in_tmp_cwd, monkeypatch, truthy): |
| 86 | + """'true'/'True'/'1' などを大文字小文字を問わず有効と解釈する。""" |
| 87 | + monkeypatch.setenv("ENABLE_SSH", truthy) |
| 88 | + _write_compose(in_tmp_cwd, {"dev": {"image": "dev:latest"}}) |
| 89 | + |
| 90 | + compose.generate_scaled_compose(scale=1, project_name="proj") |
| 91 | + scaled = _load_scaled(in_tmp_cwd)["services"] |
| 92 | + |
| 93 | + assert len(_ssh_ports(scaled["dev-1"])) == 1 |
| 94 | + |
| 95 | + |
| 96 | +def test_existing_ports_are_preserved(in_tmp_cwd, monkeypatch): |
| 97 | + """既存の ports は保持され、SSH publish が追記される。""" |
| 98 | + monkeypatch.setenv("ENABLE_SSH", "true") |
| 99 | + _write_compose(in_tmp_cwd, { |
| 100 | + "dev": {"image": "dev:latest", "ports": ["8080:8080"]}, |
| 101 | + }) |
| 102 | + |
| 103 | + compose.generate_scaled_compose(scale=1, project_name="proj") |
| 104 | + scaled = _load_scaled(in_tmp_cwd)["services"] |
| 105 | + |
| 106 | + ports = scaled["dev-1"]["ports"] |
| 107 | + assert "8080:8080" in ports |
| 108 | + assert len(_ssh_ports({"ports": ports})) == 1 |
| 109 | + |
| 110 | + |
| 111 | +# --- bind / base の env 反映 --- |
| 112 | + |
| 113 | +def test_ssh_bind_is_honored(in_tmp_cwd, monkeypatch): |
| 114 | + """DEVBASE_SSH_BIND が publish の bind 先に反映される。""" |
| 115 | + monkeypatch.setenv("ENABLE_SSH", "true") |
| 116 | + monkeypatch.setenv("DEVBASE_SSH_BIND", "0.0.0.0") |
| 117 | + _write_compose(in_tmp_cwd, {"dev": {"image": "dev:latest"}}) |
| 118 | + |
| 119 | + compose.generate_scaled_compose(scale=1, project_name="proj") |
| 120 | + scaled = _load_scaled(in_tmp_cwd)["services"] |
| 121 | + |
| 122 | + port = ssh_host_port("proj", 1, 2200) |
| 123 | + assert _ssh_ports(scaled["dev-1"]) == [f"0.0.0.0:{port}:22"] |
| 124 | + |
| 125 | + |
| 126 | +def test_ssh_port_base_is_honored(in_tmp_cwd, monkeypatch): |
| 127 | + """DEVBASE_SSH_PORT_BASE がポート算出の起点に反映される。""" |
| 128 | + monkeypatch.setenv("ENABLE_SSH", "true") |
| 129 | + monkeypatch.setenv("DEVBASE_SSH_PORT_BASE", "3000") |
| 130 | + _write_compose(in_tmp_cwd, {"dev": {"image": "dev:latest"}}) |
| 131 | + |
| 132 | + compose.generate_scaled_compose(scale=1, project_name="proj") |
| 133 | + scaled = _load_scaled(in_tmp_cwd)["services"] |
| 134 | + |
| 135 | + port = ssh_host_port("proj", 1, 3000) |
| 136 | + assert port >= 3000 |
| 137 | + assert _ssh_ports(scaled["dev-1"]) == [f"127.0.0.1:{port}:22"] |
| 138 | + |
| 139 | + |
| 140 | +# --- ssh_host_port() の決定性・衝突回避 --- |
| 141 | + |
| 142 | +def test_ssh_host_port_is_deterministic(): |
| 143 | + """同じ (project, index) は毎回同じポートに解決する (純粋関数)。""" |
| 144 | + a = ssh_host_port("carmo", 1, 2200) |
| 145 | + b = ssh_host_port("carmo", 1, 2200) |
| 146 | + assert a == b |
| 147 | + |
| 148 | + |
| 149 | +def test_stable_hash_is_not_builtin_hash_salted(): |
| 150 | + """_stable_hash は既知の固定値を返す (プロセス跨ぎで一定)。""" |
| 151 | + # sha1('proj') の整数化を 100 で割った剰余は実装非依存に確定する。 |
| 152 | + assert _stable_hash("proj") == _stable_hash("proj") |
| 153 | + assert isinstance(_stable_hash("proj"), int) |
| 154 | + assert _stable_hash("proj") >= 0 |
| 155 | + |
| 156 | + |
| 157 | +def test_different_projects_get_different_ports(): |
| 158 | + """別プロジェクトは (ほぼ) 別ポートに解決する。""" |
| 159 | + ports = {ssh_host_port(name, 1, 2200) |
| 160 | + for name in ("carmo", "alpha", "bravo", "charlie", "delta")} |
| 161 | + # 5 個中 4 個以上はユニーク (100 バケットなので衝突は稀) |
| 162 | + assert len(ports) >= 4 |
| 163 | + |
| 164 | + |
| 165 | +def test_index_shifts_port_within_project(): |
| 166 | + """同一プロジェクト内では index が +1 ずつポートをずらす。""" |
| 167 | + p1 = ssh_host_port("proj", 1, 2200) |
| 168 | + p2 = ssh_host_port("proj", 2, 2200) |
| 169 | + assert p2 == p1 + 1 |
| 170 | + |
| 171 | + |
| 172 | +def test_base_offsets_port(): |
| 173 | + """base を変えるとポートも同じ差分だけずれる。""" |
| 174 | + assert ssh_host_port("proj", 1, 3000) == ssh_host_port("proj", 1, 2200) + 800 |
0 commit comments