|
| 1 | +import subprocess |
| 2 | +import pytest |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +from transformerlab.services import juicefs_gateway |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture(autouse=True) |
| 9 | +def reset_gateway_state(): |
| 10 | + juicefs_gateway._gateway_process = None |
| 11 | + juicefs_gateway._shutdown_event.clear() |
| 12 | + yield |
| 13 | + juicefs_gateway._shutdown_event.set() |
| 14 | + juicefs_gateway._gateway_process = None |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def juicefs_env(monkeypatch): |
| 19 | + monkeypatch.setenv("TFL_STORAGE_PROVIDER", "juicefs") |
| 20 | + monkeypatch.setenv("TFL_JUICEFS_METADATA_URL", "redis://localhost:6379/1") |
| 21 | + monkeypatch.setenv("TFL_JUICEFS_VOLUME_NAME", "myvol") |
| 22 | + monkeypatch.setenv("TFL_JUICEFS_STORAGE_BACKEND", "aws") |
| 23 | + monkeypatch.setenv("TFL_JUICEFS_TOKEN", "hosted-token") |
| 24 | + monkeypatch.setenv("TFL_JUICEFS_GATEWAY_ACCESS_KEY", "gw-access") |
| 25 | + monkeypatch.setenv("TFL_JUICEFS_GATEWAY_SECRET_KEY", "gw-secret-123") |
| 26 | + monkeypatch.delenv("TFL_JUICEFS_GATEWAY_ENDPOINT", raising=False) |
| 27 | + monkeypatch.delenv("TFL_JUICEFS_CONSOLE_URL", raising=False) |
| 28 | + |
| 29 | + |
| 30 | +def test_noop_for_other_providers(monkeypatch): |
| 31 | + monkeypatch.setenv("TFL_STORAGE_PROVIDER", "aws") |
| 32 | + |
| 33 | + with patch.object(juicefs_gateway, "_spawn_gateway") as mock_spawn: |
| 34 | + juicefs_gateway.ensure_juicefs_gateway() |
| 35 | + |
| 36 | + mock_spawn.assert_not_called() |
| 37 | + |
| 38 | + |
| 39 | +def test_reuses_already_running_gateway(juicefs_env): |
| 40 | + with ( |
| 41 | + patch.object(juicefs_gateway, "is_gateway_ready", return_value=True), |
| 42 | + patch.object(juicefs_gateway, "_run_juicefs_auth") as mock_auth, |
| 43 | + patch.object(juicefs_gateway, "_spawn_gateway") as mock_spawn, |
| 44 | + ): |
| 45 | + juicefs_gateway.ensure_juicefs_gateway() |
| 46 | + |
| 47 | + # Auth runs even when the gateway is reused: it is idempotent and writes the on-disk |
| 48 | + # config that later `juicefs quota set` calls depend on (e.g. for a second API worker |
| 49 | + # that reuses a gateway it did not spawn). |
| 50 | + mock_auth.assert_called_once() |
| 51 | + mock_spawn.assert_not_called() |
| 52 | + |
| 53 | + |
| 54 | +def test_starts_gateway_and_waits_for_ready(juicefs_env): |
| 55 | + with ( |
| 56 | + patch.object(juicefs_gateway, "is_gateway_ready", side_effect=[False, True]), |
| 57 | + patch.object(juicefs_gateway, "_run_juicefs_auth") as mock_auth, |
| 58 | + patch.object(juicefs_gateway, "_spawn_gateway", return_value=MagicMock()) as mock_spawn, |
| 59 | + patch.object(juicefs_gateway, "_start_supervisor") as mock_supervisor, |
| 60 | + ): |
| 61 | + juicefs_gateway.ensure_juicefs_gateway() |
| 62 | + |
| 63 | + mock_auth.assert_called_once() |
| 64 | + mock_spawn.assert_called_once() |
| 65 | + mock_supervisor.assert_called_once() |
| 66 | + |
| 67 | + |
| 68 | +def test_exits_when_auth_fails(juicefs_env): |
| 69 | + auth_error = subprocess.CalledProcessError(1, ["juicefs", "auth"], stderr="bad token") |
| 70 | + with ( |
| 71 | + patch.object(juicefs_gateway, "is_gateway_ready", return_value=False), |
| 72 | + patch.object(juicefs_gateway, "_run_juicefs_auth", side_effect=auth_error), |
| 73 | + patch.object(juicefs_gateway, "_spawn_gateway") as mock_spawn, |
| 74 | + ): |
| 75 | + with pytest.raises(SystemExit): |
| 76 | + juicefs_gateway.ensure_juicefs_gateway() |
| 77 | + |
| 78 | + mock_spawn.assert_not_called() |
| 79 | + |
| 80 | + |
| 81 | +def test_exits_when_juicefs_binary_missing(juicefs_env): |
| 82 | + with ( |
| 83 | + patch.object(juicefs_gateway, "is_gateway_ready", return_value=False), |
| 84 | + patch.object(juicefs_gateway, "_run_juicefs_auth", side_effect=FileNotFoundError("juicefs")), |
| 85 | + ): |
| 86 | + with pytest.raises(SystemExit): |
| 87 | + juicefs_gateway.ensure_juicefs_gateway() |
| 88 | + |
| 89 | + |
| 90 | +def test_exits_when_gateway_never_ready(juicefs_env): |
| 91 | + with ( |
| 92 | + patch.object(juicefs_gateway, "is_gateway_ready", return_value=False), |
| 93 | + patch.object(juicefs_gateway, "_run_juicefs_auth"), |
| 94 | + patch.object(juicefs_gateway, "_spawn_gateway", return_value=MagicMock()), |
| 95 | + patch.object(juicefs_gateway, "_wait_until_ready", return_value=False), |
| 96 | + ): |
| 97 | + with pytest.raises(SystemExit): |
| 98 | + juicefs_gateway.ensure_juicefs_gateway() |
| 99 | + |
| 100 | + |
| 101 | +def test_exits_when_required_env_missing(juicefs_env, monkeypatch): |
| 102 | + monkeypatch.delenv("TFL_JUICEFS_TOKEN", raising=False) |
| 103 | + |
| 104 | + with patch.object(juicefs_gateway, "is_gateway_ready", return_value=False): |
| 105 | + with pytest.raises(SystemExit): |
| 106 | + juicefs_gateway.ensure_juicefs_gateway() |
| 107 | + |
| 108 | + |
| 109 | +def test_run_juicefs_auth_builds_command(juicefs_env, monkeypatch): |
| 110 | + monkeypatch.setenv("TFL_JUICEFS_CONSOLE_URL", "http://juicefs-console:8080") |
| 111 | + |
| 112 | + with ( |
| 113 | + patch("transformerlab.services.juicefs_gateway.subprocess.run") as mock_run, |
| 114 | + patch( |
| 115 | + "transformerlab.services.compute_provider.launch_credentials.get_aws_credentials_from_file", |
| 116 | + return_value=("AKID", "SECRET"), |
| 117 | + ), |
| 118 | + patch( |
| 119 | + "transformerlab.shared.remote_workspace.get_default_aws_profile", |
| 120 | + return_value="transformerlab-s3", |
| 121 | + ), |
| 122 | + ): |
| 123 | + juicefs_gateway._run_juicefs_auth() |
| 124 | + |
| 125 | + cmd = mock_run.call_args[0][0] |
| 126 | + assert cmd[:4] == ["juicefs", "auth", "myvol", "--token"] |
| 127 | + assert "hosted-token" in cmd |
| 128 | + assert "--console-url" in cmd and "http://juicefs-console:8080" in cmd |
| 129 | + assert "--access-key" in cmd and "AKID" in cmd |
| 130 | + assert "--secret-key" in cmd and "SECRET" in cmd |
| 131 | + |
| 132 | + |
| 133 | +def test_spawn_gateway_uses_minio_root_creds(juicefs_env, tmp_path, monkeypatch): |
| 134 | + monkeypatch.setenv("TFL_HOME_DIR", str(tmp_path)) |
| 135 | + |
| 136 | + with patch("transformerlab.services.juicefs_gateway.subprocess.Popen") as mock_popen: |
| 137 | + juicefs_gateway._spawn_gateway() |
| 138 | + |
| 139 | + args, kwargs = mock_popen.call_args |
| 140 | + assert args[0] == [ |
| 141 | + "juicefs", |
| 142 | + "gateway", |
| 143 | + "myvol", |
| 144 | + "127.0.0.1:9000", |
| 145 | + "--multi-buckets", |
| 146 | + "--keep-etag", |
| 147 | + "--umask", |
| 148 | + "000", |
| 149 | + ] |
| 150 | + assert kwargs["env"]["MINIO_ROOT_USER"] == "gw-access" |
| 151 | + assert kwargs["env"]["MINIO_ROOT_PASSWORD"] == "gw-secret-123" |
| 152 | + |
| 153 | + |
| 154 | +def test_stop_gateway_terminates_process(juicefs_env): |
| 155 | + mock_proc = MagicMock() |
| 156 | + mock_proc.poll.return_value = None |
| 157 | + juicefs_gateway._gateway_process = mock_proc |
| 158 | + |
| 159 | + juicefs_gateway.stop_juicefs_gateway() |
| 160 | + |
| 161 | + mock_proc.terminate.assert_called_once() |
| 162 | + assert juicefs_gateway._gateway_process is None |
| 163 | + |
| 164 | + |
| 165 | +def test_supervise_respawns_after_crash(juicefs_env): |
| 166 | + crashed_proc = MagicMock() |
| 167 | + crashed_proc.returncode = 1 |
| 168 | + # wait() returns immediately (crash); after respawn the loop must observe |
| 169 | + # the shutdown event and exit. |
| 170 | + crashed_proc.wait.return_value = 1 |
| 171 | + juicefs_gateway._gateway_process = crashed_proc |
| 172 | + |
| 173 | + respawned_proc = MagicMock() |
| 174 | + |
| 175 | + def fake_spawn(): |
| 176 | + # Stop the loop after the first respawn. |
| 177 | + juicefs_gateway._shutdown_event.set() |
| 178 | + return respawned_proc |
| 179 | + |
| 180 | + with ( |
| 181 | + patch.object(juicefs_gateway, "_spawn_gateway", side_effect=fake_spawn) as mock_spawn, |
| 182 | + patch.object(juicefs_gateway.time, "sleep"), |
| 183 | + ): |
| 184 | + juicefs_gateway._supervise() |
| 185 | + |
| 186 | + mock_spawn.assert_called_once() |
| 187 | + assert juicefs_gateway._gateway_process is respawned_proc |
| 188 | + |
| 189 | + |
| 190 | +def test_supervise_does_not_respawn_on_shutdown(juicefs_env): |
| 191 | + proc = MagicMock() |
| 192 | + |
| 193 | + def wait_then_shutdown(): |
| 194 | + # Simulate stop_juicefs_gateway(): the shutdown event is set before the |
| 195 | + # process exits, so the supervisor must NOT respawn. |
| 196 | + juicefs_gateway._shutdown_event.set() |
| 197 | + return 0 |
| 198 | + |
| 199 | + proc.wait.side_effect = wait_then_shutdown |
| 200 | + juicefs_gateway._gateway_process = proc |
| 201 | + |
| 202 | + with patch.object(juicefs_gateway, "_spawn_gateway") as mock_spawn: |
| 203 | + juicefs_gateway._supervise() |
| 204 | + |
| 205 | + mock_spawn.assert_not_called() |
0 commit comments