import json import stat from pathlib import Path import pytest from ops import autodl_pro_controller as controller MODULE_PATH = Path(controller.__file__) def test_create_spec_excludes_empty_data_centers(): config = { "autodl": { "req_gpu_amount": 1, "expand_system_disk_by_gb": 120, "gpu_spec_uuid": "pro6000-p", "image_uuid": "image-test", "cuda_v_from": 128, "data_center_list": [], } } spec = controller.create_spec(config) assert spec["gpu_spec_uuid"] == "pro6000-p" assert "data_center_list" not in spec def test_snapshot_to_ssh_target(tmp_path): target = controller.SshTarget.from_snapshot( {"proxy_host": "connect.example", "ssh_port": 32001}, { "private_key": str(tmp_path / "key"), "known_hosts": str(tmp_path / "known_hosts"), }, ) assert target.host == "connect.example" assert target.port == 32001 assert "IdentitiesOnly=yes" in target.options() def test_runner_config_disables_docker_and_keeps_host_labels(): rendered = controller.runner_config(["ces-gpu-formal:host"], "24h") assert 'docker_host: "-"' in rendered assert '"ces-gpu-formal:host"' in rendered assert "capacity: 1" in rendered def test_secret_permissions_are_enforced(tmp_path): secret = tmp_path / "token" secret.write_text("secret\n", encoding="utf-8") secret.chmod(0o644) with pytest.raises(controller.ControllerError): controller.read_secret(str(secret)) secret.chmod(0o600) assert controller.read_secret(str(secret)) == "secret" def test_example_config_is_valid_json(): path = Path(__file__).parents[1] / "ops" / "autodl-pro.example.json" config = json.loads(path.read_text(encoding="utf-8")) assert config["runner"]["labels"] == ["ces-gpu-formal:host"] assert stat.S_IMODE(MODULE_PATH.stat().st_mode) in {0o644, 0o755}