From 0fa9f075baa3380f5b3058d06a14e3e046c20588 Mon Sep 17 00:00:00 2001 From: Victor Skvortsov Date: Tue, 27 Oct 2026 15:42:48 +0500 Subject: [PATCH 1/8] Type mocked API clients in backend tests --- .../core/backends/cloudrift/test_compute.py | 57 ++++++----- .../core/backends/hotaisle/test_compute.py | 71 +++++++------ .../core/backends/jarvislabs/test_compute.py | 93 ++++++++--------- .../core/backends/nebius/test_compute.py | 24 ++--- .../core/backends/runpod/test_compute.py | 99 ++++++++++--------- .../core/backends/seeweb/test_compute.py | 41 ++++---- 6 files changed, 194 insertions(+), 191 deletions(-) diff --git a/src/tests/_internal/core/backends/cloudrift/test_compute.py b/src/tests/_internal/core/backends/cloudrift/test_compute.py index 9daf155d53..9e0015eac0 100644 --- a/src/tests/_internal/core/backends/cloudrift/test_compute.py +++ b/src/tests/_internal/core/backends/cloudrift/test_compute.py @@ -15,10 +15,11 @@ from dstack._internal.core.models.runs import JobProvisioningData -def _compute() -> CloudRiftCompute: +def _compute_with_mocked_api_client() -> tuple[CloudRiftCompute, MagicMock]: compute = CloudRiftCompute(CloudRiftConfig(creds=CloudRiftCreds(api_key="test"))) - compute.client = MagicMock() - return compute + client = MagicMock() + compute.client = client + return compute, client def _provisioning_data(ssh_port: int | None = None) -> JobProvisioningData: @@ -75,8 +76,8 @@ def _offer() -> InstanceOfferWithAvailability: class TestCloudRiftComputeCreateInstance: def test_waits_for_ssh_endpoint_from_cloudrift(self): - compute = _compute() - compute.client.deploy_instance.return_value = ["instance-id"] + compute, client = _compute_with_mocked_api_client() + client.deploy_instance.return_value = ["instance-id"] instance_config = InstanceConfiguration( project_name="main", instance_name="test-instance", @@ -97,9 +98,9 @@ def test_waits_for_ssh_endpoint_from_cloudrift(self): class TestCloudRiftComputeUpdateProvisioningData: def test_sets_hostname_and_mapped_ssh_port(self): - compute = _compute() + compute, client = _compute_with_mocked_api_client() provisioning_data = _provisioning_data() - compute.client.get_instance_by_id.return_value = _vm_instance_info() + client.get_instance_by_id.return_value = _vm_instance_info() compute.update_provisioning_data( provisioning_data=provisioning_data, @@ -111,11 +112,9 @@ def test_sets_hostname_and_mapped_ssh_port(self): assert provisioning_data.ssh_port == 57001 def test_accepts_string_port_mapping_values(self): - compute = _compute() + compute, client = _compute_with_mocked_api_client() provisioning_data = _provisioning_data() - compute.client.get_instance_by_id.return_value = _vm_instance_info( - port_mappings=[["22", "57001"]] - ) + client.get_instance_by_id.return_value = _vm_instance_info(port_mappings=[["22", "57001"]]) compute.update_provisioning_data( provisioning_data=provisioning_data, @@ -127,9 +126,9 @@ def test_accepts_string_port_mapping_values(self): assert provisioning_data.ssh_port == 57001 def test_uses_ssh_port_from_instructions_when_port_mappings_are_missing(self): - compute = _compute() + compute, client = _compute_with_mocked_api_client() provisioning_data = _provisioning_data() - compute.client.get_instance_by_id.return_value = _vm_instance_info( + client.get_instance_by_id.return_value = _vm_instance_info( port_mappings=None, instructions={"placeholder_values": [["SSH_PORT", "-p 57001"]]}, ) @@ -144,9 +143,9 @@ def test_uses_ssh_port_from_instructions_when_port_mappings_are_missing(self): assert provisioning_data.ssh_port == 57001 def test_uses_ssh_port_from_instructions_when_port_mappings_are_empty(self): - compute = _compute() + compute, client = _compute_with_mocked_api_client() provisioning_data = _provisioning_data() - compute.client.get_instance_by_id.return_value = _vm_instance_info( + client.get_instance_by_id.return_value = _vm_instance_info( port_mappings=[], instructions={"placeholder_values": [["SSH_PORT", "-p 57001"]]}, ) @@ -161,9 +160,9 @@ def test_uses_ssh_port_from_instructions_when_port_mappings_are_empty(self): assert provisioning_data.ssh_port == 57001 def test_uses_default_ssh_port_from_empty_ssh_port_instruction(self): - compute = _compute() + compute, client = _compute_with_mocked_api_client() provisioning_data = _provisioning_data() - compute.client.get_instance_by_id.return_value = _vm_instance_info( + client.get_instance_by_id.return_value = _vm_instance_info( port_mappings=None, instructions={"placeholder_values": [["SSH_PORT", ""]]}, ) @@ -178,11 +177,11 @@ def test_uses_default_ssh_port_from_empty_ssh_port_instruction(self): assert provisioning_data.ssh_port == 22 def test_uses_default_ssh_port_when_cloudrift_does_not_return_port_mapping_data(self): - compute = _compute() + compute, client = _compute_with_mocked_api_client() provisioning_data = _provisioning_data() instance_info = _vm_instance_info() instance_info.pop("port_mappings") - compute.client.get_instance_by_id.return_value = instance_info + client.get_instance_by_id.return_value = instance_info compute.update_provisioning_data( provisioning_data=provisioning_data, @@ -194,13 +193,13 @@ def test_uses_default_ssh_port_when_cloudrift_does_not_return_port_mapping_data( assert provisioning_data.ssh_port == 22 def test_uses_default_ssh_port_when_instructions_do_not_include_ssh_port(self): - compute = _compute() + compute, client = _compute_with_mocked_api_client() provisioning_data = _provisioning_data() instance_info = _vm_instance_info( instructions={"placeholder_values": [["USERNAME", "riftuser"]]} ) instance_info.pop("port_mappings") - compute.client.get_instance_by_id.return_value = instance_info + client.get_instance_by_id.return_value = instance_info compute.update_provisioning_data( provisioning_data=provisioning_data, @@ -212,9 +211,9 @@ def test_uses_default_ssh_port_when_instructions_do_not_include_ssh_port(self): assert provisioning_data.ssh_port == 22 def test_waits_for_ssh_port_when_port_mappings_are_temporarily_missing(self): - compute = _compute() + compute, client = _compute_with_mocked_api_client() provisioning_data = _provisioning_data() - compute.client.get_instance_by_id.return_value = _vm_instance_info(port_mappings=[]) + client.get_instance_by_id.return_value = _vm_instance_info(port_mappings=[]) compute.update_provisioning_data( provisioning_data=provisioning_data, @@ -226,9 +225,9 @@ def test_waits_for_ssh_port_when_port_mappings_are_temporarily_missing(self): assert provisioning_data.ssh_port is None def test_waits_for_ssh_port_when_port_mappings_are_invalid(self): - compute = _compute() + compute, client = _compute_with_mocked_api_client() provisioning_data = _provisioning_data() - compute.client.get_instance_by_id.return_value = _vm_instance_info( + client.get_instance_by_id.return_value = _vm_instance_info( port_mappings=[ [22], [22, "not-a-port"], @@ -248,13 +247,13 @@ def test_waits_for_ssh_port_when_port_mappings_are_invalid(self): assert provisioning_data.ssh_port is None def test_waits_for_ssh_port_when_ssh_port_instruction_is_invalid(self): - compute = _compute() + compute, client = _compute_with_mocked_api_client() provisioning_data = _provisioning_data() instance_info = _vm_instance_info( instructions={"placeholder_values": [["SSH_PORT", "-p not-a-port"]]} ) instance_info.pop("port_mappings") - compute.client.get_instance_by_id.return_value = instance_info + client.get_instance_by_id.return_value = instance_info compute.update_provisioning_data( provisioning_data=provisioning_data, @@ -266,9 +265,9 @@ def test_waits_for_ssh_port_when_ssh_port_instruction_is_invalid(self): assert provisioning_data.ssh_port is None def test_does_not_update_before_vm_is_ready(self): - compute = _compute() + compute, client = _compute_with_mocked_api_client() provisioning_data = _provisioning_data() - compute.client.get_instance_by_id.return_value = _vm_instance_info( + client.get_instance_by_id.return_value = _vm_instance_info( virtual_machines=[{"ready": False}] ) diff --git a/src/tests/_internal/core/backends/hotaisle/test_compute.py b/src/tests/_internal/core/backends/hotaisle/test_compute.py index 562014770a..1e75e5628e 100644 --- a/src/tests/_internal/core/backends/hotaisle/test_compute.py +++ b/src/tests/_internal/core/backends/hotaisle/test_compute.py @@ -56,10 +56,11 @@ def _compute(bare_metal: Optional[bool] = None) -> HotAisleCompute: ) -def _compute_with_mocked_api_client() -> HotAisleCompute: +def _compute_with_mocked_api_client() -> tuple[HotAisleCompute, MagicMock]: compute = _compute() - compute.api_client = MagicMock() - return compute + api_client = MagicMock() + compute.api_client = api_client + return compute, api_client def _instance_type(name: str) -> InstanceType: @@ -164,8 +165,8 @@ def test_keeps_specs_in_backend_data(self, requests_mock): class TestCreateInstance: def test_creates_vm(self): - compute = _compute_with_mocked_api_client() - compute.api_client.create_virtual_machine.return_value = { + compute, api_client = _compute_with_mocked_api_client() + api_client.create_virtual_machine.return_value = { "name": "vm-name", "ip_address": "10.0.0.1", } @@ -174,17 +175,17 @@ def test_creates_vm(self): _offer("vm-mi300x-1", {"vm_specs": VM_SPECS}), _instance_config(), None ) - compute.api_client.upload_ssh_key.assert_called_once_with("ssh-rsa AAAA test") - compute.api_client.create_virtual_machine.assert_called_once_with(VM_SPECS) - compute.api_client.reserve_bare_metal_server.assert_not_called() + api_client.upload_ssh_key.assert_called_once_with("ssh-rsa AAAA test") + api_client.create_virtual_machine.assert_called_once_with(VM_SPECS) + api_client.reserve_bare_metal_server.assert_not_called() assert provisioning_data.instance_id == "vm-name" assert HotAisleInstanceBackendData.load( provisioning_data.backend_data ) == HotAisleInstanceBackendData(ip_address="10.0.0.1", bare_metal=False) def test_reserves_bare_metal_server(self): - compute = _compute_with_mocked_api_client() - compute.api_client.reserve_bare_metal_server.return_value = { + compute, api_client = _compute_with_mocked_api_client() + api_client.reserve_bare_metal_server.return_value = { "deployment_id": "deployment-id", "name": "server-01", "ip_address": "10.0.0.2", @@ -197,7 +198,7 @@ def test_reserves_bare_metal_server(self): ) # The key must be uploaded before reserving, so the server accepts it. - assert compute.api_client.mock_calls == [ + assert api_client.mock_calls == [ call.upload_ssh_key("ssh-rsa AAAA test"), call.reserve_bare_metal_server(specs=BARE_METAL_SPECS, description="test-instance"), ] @@ -211,13 +212,13 @@ def test_reserves_bare_metal_server(self): @patch("dstack._internal.core.backends.hotaisle.compute._run_ssh_command", return_value=True) class TestUpdateProvisioningData: def test_starts_shim_on_running_vm(self, ssh_mock): - compute = _compute_with_mocked_api_client() - compute.api_client.get_vm_state.return_value = "running" + compute, api_client = _compute_with_mocked_api_client() + api_client.get_vm_state.return_value = "running" provisioning_data = _provisioning_data(HotAisleInstanceBackendData(ip_address="10.0.0.1")) compute.update_provisioning_data(provisioning_data, "public-key", "private-key") - compute.api_client.get_vm_state.assert_called_once_with("instance-id") + api_client.get_vm_state.assert_called_once_with("instance-id") assert provisioning_data.hostname == "10.0.0.1" assert provisioning_data.ssh_port == 22 ssh_mock.assert_called_once() @@ -225,8 +226,8 @@ def test_starts_shim_on_running_vm(self, ssh_mock): def test_retries_if_shim_fails_to_start(self, ssh_mock): ssh_mock.return_value = False - compute = _compute_with_mocked_api_client() - compute.api_client.get_vm_state.return_value = "running" + compute, api_client = _compute_with_mocked_api_client() + api_client.get_vm_state.return_value = "running" provisioning_data = _provisioning_data(HotAisleInstanceBackendData(ip_address="10.0.0.1")) compute.update_provisioning_data(provisioning_data, "public-key", "private-key") @@ -235,8 +236,8 @@ def test_retries_if_shim_fails_to_start(self, ssh_mock): assert provisioning_data.hostname is None def test_waits_for_vm_to_run(self, ssh_mock): - compute = _compute_with_mocked_api_client() - compute.api_client.get_vm_state.return_value = "shut off" + compute, api_client = _compute_with_mocked_api_client() + api_client.get_vm_state.return_value = "shut off" provisioning_data = _provisioning_data(HotAisleInstanceBackendData(ip_address="10.0.0.1")) compute.update_provisioning_data(provisioning_data, "public-key", "private-key") @@ -255,8 +256,8 @@ def test_waits_for_vm_to_run(self, ssh_mock): def test_starts_shim_on_installed_bare_metal_server( self, ssh_mock, ssh_access, hostname, port ): - compute = _compute_with_mocked_api_client() - compute.api_client.get_bare_metal_server.return_value = { + compute, api_client = _compute_with_mocked_api_client() + api_client.get_bare_metal_server.return_value = { "ip_address": "10.0.0.2", "ssh_access": ssh_access, "os_status": {"os_install_status": "installed"}, @@ -267,8 +268,8 @@ def test_starts_shim_on_installed_bare_metal_server( compute.update_provisioning_data(provisioning_data, "public-key", "private-key") - compute.api_client.get_bare_metal_server.assert_called_once_with("instance-id") - compute.api_client.get_vm_state.assert_not_called() + api_client.get_bare_metal_server.assert_called_once_with("instance-id") + api_client.get_vm_state.assert_not_called() assert provisioning_data.hostname == hostname assert provisioning_data.ssh_port == port ssh_mock.assert_called_once() @@ -285,8 +286,8 @@ def test_starts_shim_on_installed_bare_metal_server( ], ) def test_waits_for_bare_metal_os_installation(self, ssh_mock, server_data): - compute = _compute_with_mocked_api_client() - compute.api_client.get_bare_metal_server.return_value = server_data + compute, api_client = _compute_with_mocked_api_client() + api_client.get_bare_metal_server.return_value = server_data provisioning_data = _provisioning_data( HotAisleInstanceBackendData(ip_address="10.0.0.2", bare_metal=True) ) @@ -297,8 +298,8 @@ def test_waits_for_bare_metal_os_installation(self, ssh_mock, server_data): ssh_mock.assert_not_called() def test_raises_on_failed_bare_metal_os_installation(self, ssh_mock): - compute = _compute_with_mocked_api_client() - compute.api_client.get_bare_metal_server.return_value = { + compute, api_client = _compute_with_mocked_api_client() + api_client.get_bare_metal_server.return_value = { "os_status": {"os_install_status": "failed"} } provisioning_data = _provisioning_data( @@ -312,7 +313,7 @@ def test_raises_on_failed_bare_metal_os_installation(self, ssh_mock): class TestTerminateInstance: def test_terminates_vm(self): - compute = _compute_with_mocked_api_client() + compute, api_client = _compute_with_mocked_api_client() compute.terminate_instance( "vm-name", @@ -320,15 +321,15 @@ def test_terminates_vm(self): HotAisleInstanceBackendData(ip_address="10.0.0.1").model_dump_json(), ) - compute.api_client.terminate_virtual_machine.assert_called_once_with("vm-name") - compute.api_client.release_bare_metal_server.assert_not_called() + api_client.terminate_virtual_machine.assert_called_once_with("vm-name") + api_client.release_bare_metal_server.assert_not_called() def test_terminates_vm_without_backend_data(self): - compute = _compute_with_mocked_api_client() + compute, api_client = _compute_with_mocked_api_client() compute.terminate_instance("vm-name", "us-michigan-1", None) - compute.api_client.terminate_virtual_machine.assert_called_once_with("vm-name") + api_client.terminate_virtual_machine.assert_called_once_with("vm-name") @pytest.mark.parametrize( ("no_force_release", "force"), @@ -336,7 +337,7 @@ def test_terminates_vm_without_backend_data(self): ids=["force", "no-force-flag"], ) def test_releases_bare_metal_server(self, no_force_release, force): - compute = _compute_with_mocked_api_client() + compute, api_client = _compute_with_mocked_api_client() with patch.object(FeatureFlags, "HOTAISLE_BARE_METAL_NO_FORCE_RELEASE", no_force_release): compute.terminate_instance( @@ -347,10 +348,8 @@ def test_releases_bare_metal_server(self, no_force_release, force): ).model_dump_json(), ) - compute.api_client.release_bare_metal_server.assert_called_once_with( - "deployment-id", force=force - ) - compute.api_client.terminate_virtual_machine.assert_not_called() + api_client.release_bare_metal_server.assert_called_once_with("deployment-id", force=force) + api_client.terminate_virtual_machine.assert_not_called() class TestLaunchRunner: diff --git a/src/tests/_internal/core/backends/jarvislabs/test_compute.py b/src/tests/_internal/core/backends/jarvislabs/test_compute.py index 10ffae269f..e3b9075679 100644 --- a/src/tests/_internal/core/backends/jarvislabs/test_compute.py +++ b/src/tests/_internal/core/backends/jarvislabs/test_compute.py @@ -28,14 +28,15 @@ from dstack._internal.core.models.runs import JobProvisioningData, Requirements -def _compute() -> JarvisLabsCompute: +def _compute_with_mocked_api_client() -> tuple[JarvisLabsCompute, MagicMock]: compute = JarvisLabsCompute( JarvisLabsConfig(creds=JarvisLabsCreds(api_key="test"), regions=["india-noida-01"]) ) - compute.api_client = MagicMock() - compute.api_client.create_ssh_key.return_value = "ssh-key-id" - compute.api_client.get_instance_status.return_value = {"status": "Running"} - return compute + api_client = MagicMock() + compute.api_client = api_client + api_client.create_ssh_key.return_value = "ssh-key-id" + api_client.get_instance_status.return_value = {"status": "Running"} + return compute, api_client def _instance_config(ssh_keys: list[SSHKey] | None = None) -> InstanceConfiguration: @@ -137,7 +138,7 @@ def test_get_disk_size_gb_clamps_to_jarvislabs_vm_minimum(): def test_get_all_offers_uses_configurable_disk_size(): - compute = _compute() + compute, _ = _compute_with_mocked_api_client() with patch( "dstack._internal.core.backends.jarvislabs.compute.get_catalog_offers", @@ -156,7 +157,7 @@ def test_get_all_offers_uses_configurable_disk_size(): def test_get_offers_reuses_all_offers_cache_and_modifies_disk_size(): - compute = _compute() + compute, _ = _compute_with_mocked_api_client() compute.get_all_offers_with_availability = MagicMock( return_value=[_cpu_offer(disk_size_mib=100 * 1024)] ) @@ -184,8 +185,8 @@ def test_get_offers_reuses_all_offers_cache_and_modifies_disk_size(): def test_create_gpu_instance_creates_ssh_key_and_gpu_vm(): - compute = _compute() - compute.api_client.create_gpu_vm.return_value = "123" + compute, api_client = _compute_with_mocked_api_client() + api_client.create_gpu_vm.return_value = "123" with patch( "dstack._internal.core.backends.jarvislabs.compute.generate_unique_instance_name", @@ -195,11 +196,11 @@ def test_create_gpu_instance_creates_ssh_key_and_gpu_vm(): _gpu_offer(backend_data={"gpu_type": "A100-80GB"}), _instance_config(), None ) - compute.api_client.create_ssh_key.assert_called_once_with( + api_client.create_ssh_key.assert_called_once_with( public_key="ssh-rsa AAAA test", key_name="dstack-test-0.key", ) - compute.api_client.create_gpu_vm.assert_called_once_with( + api_client.create_gpu_vm.assert_called_once_with( gpu_type="A100-80GB", num_gpus=1, is_spot=False, @@ -212,12 +213,12 @@ def test_create_gpu_instance_creates_ssh_key_and_gpu_vm(): assert provisioning_data.dockerized is True backend_data = JarvisLabsInstanceBackendData.load(provisioning_data.backend_data) assert backend_data.ssh_key_ids == ["ssh-key-id"] - compute.api_client.get_instance_status.assert_not_called() + api_client.get_instance_status.assert_not_called() def test_create_gpu_instance_passes_spot_flag(): - compute = _compute() - compute.api_client.create_gpu_vm.return_value = "123" + compute, api_client = _compute_with_mocked_api_client() + api_client.create_gpu_vm.return_value = "123" with patch( "dstack._internal.core.backends.jarvislabs.compute.generate_unique_instance_name", @@ -229,7 +230,7 @@ def test_create_gpu_instance_passes_spot_flag(): None, ) - compute.api_client.create_gpu_vm.assert_called_once_with( + api_client.create_gpu_vm.assert_called_once_with( gpu_type="A100-80GB", num_gpus=1, is_spot=True, @@ -240,8 +241,8 @@ def test_create_gpu_instance_passes_spot_flag(): def test_create_rtx_pro_6000_instance_uses_jarvislabs_gpu_type_from_backend_data(): - compute = _compute() - compute.api_client.create_gpu_vm.return_value = "123" + compute, api_client = _compute_with_mocked_api_client() + api_client.create_gpu_vm.return_value = "123" offer = _gpu_offer( gpu_name="RTXPRO6000", gpu_memory_mib=96 * 1024, @@ -254,7 +255,7 @@ def test_create_rtx_pro_6000_instance_uses_jarvislabs_gpu_type_from_backend_data ): compute.create_instance(offer, _instance_config(), None) - compute.api_client.create_gpu_vm.assert_called_once_with( + api_client.create_gpu_vm.assert_called_once_with( gpu_type="RTX-PRO6000", num_gpus=1, is_spot=False, @@ -265,8 +266,8 @@ def test_create_rtx_pro_6000_instance_uses_jarvislabs_gpu_type_from_backend_data def test_create_cpu_instance_creates_ssh_key_and_cpu_vm(): - compute = _compute() - compute.api_client.create_cpu_vm.return_value = "456" + compute, api_client = _compute_with_mocked_api_client() + api_client.create_cpu_vm.return_value = "456" with patch( "dstack._internal.core.backends.jarvislabs.compute.generate_unique_instance_name", @@ -274,11 +275,11 @@ def test_create_cpu_instance_creates_ssh_key_and_cpu_vm(): ): provisioning_data = compute.create_instance(_cpu_offer(), _instance_config(), None) - compute.api_client.create_ssh_key.assert_called_once_with( + api_client.create_ssh_key.assert_called_once_with( public_key="ssh-rsa AAAA test", key_name="dstack-cpu-0.key", ) - compute.api_client.create_cpu_vm.assert_called_once_with( + api_client.create_cpu_vm.assert_called_once_with( vcpus=4, ram_gb=16, storage=100, @@ -291,8 +292,8 @@ def test_create_cpu_instance_creates_ssh_key_and_cpu_vm(): def test_update_provisioning_data_sets_hostname_and_starts_runner(): - compute = _compute() - compute.api_client.get_instance.return_value = { + compute, api_client = _compute_with_mocked_api_client() + api_client.get_instance.return_value = { "machine_id": 123, "status": "Running", "public_ip": "203.0.113.10", @@ -329,8 +330,8 @@ def test_update_provisioning_data_sets_hostname_and_starts_runner(): def test_update_provisioning_data_does_not_set_hostname_until_runner_starts(): - compute = _compute() - compute.api_client.get_instance.return_value = { + compute, api_client = _compute_with_mocked_api_client() + api_client.get_instance.return_value = { "machine_id": 123, "status": "Running", "public_ip": "203.0.113.10", @@ -369,38 +370,38 @@ def test_get_ssh_username_parses_jarvislabs_ssh_command(): def test_terminate_instance_delegates_to_api_client_without_backend_data(): - compute = _compute() + compute, api_client = _compute_with_mocked_api_client() compute.terminate_instance("123", "india-noida-01") - compute.api_client.destroy_instance.assert_called_once_with( + api_client.destroy_instance.assert_called_once_with( machine_id="123", region="india-noida-01", ) - compute.api_client.delete_ssh_key.assert_not_called() + api_client.delete_ssh_key.assert_not_called() def test_terminate_instance_deletes_created_ssh_keys(): - compute = _compute() + compute, api_client = _compute_with_mocked_api_client() backend_data = JarvisLabsInstanceBackendData( ssh_key_ids=["ssh-key-id-1", "ssh-key-id-2"] ).model_dump_json() compute.terminate_instance("123", "india-noida-01", backend_data) - compute.api_client.destroy_instance.assert_called_once_with( + api_client.destroy_instance.assert_called_once_with( machine_id="123", region="india-noida-01", ) - assert compute.api_client.delete_ssh_key.call_args_list == [ + assert api_client.delete_ssh_key.call_args_list == [ call("ssh-key-id-1"), call("ssh-key-id-2"), ] def test_create_instance_cleans_up_ssh_key_on_create_failure(): - compute = _compute() - compute.api_client.create_gpu_vm.side_effect = NoCapacityError( + compute, api_client = _compute_with_mocked_api_client() + api_client.create_gpu_vm.side_effect = NoCapacityError( "L4 not available at this moment, please try again later" ) @@ -411,13 +412,13 @@ def test_create_instance_cleans_up_ssh_key_on_create_failure(): with pytest.raises(NoCapacityError): compute.create_instance(_gpu_offer(spot=True), _instance_config(), None) - compute.api_client.destroy_instance.assert_not_called() - compute.api_client.delete_ssh_key.assert_called_once_with("ssh-key-id") + api_client.destroy_instance.assert_not_called() + api_client.delete_ssh_key.assert_called_once_with("ssh-key-id") def test_create_instance_cleans_up_created_ssh_key_if_later_ssh_key_create_fails(): - compute = _compute() - compute.api_client.create_ssh_key.side_effect = [ + compute, api_client = _compute_with_mocked_api_client() + api_client.create_ssh_key.side_effect = [ "ssh-key-id-1", BackendError("ssh create failed"), ] @@ -435,14 +436,14 @@ def test_create_instance_cleans_up_created_ssh_key_if_later_ssh_key_create_fails with pytest.raises(BackendError, match="ssh create failed"): compute.create_instance(_gpu_offer(), instance_config, None) - compute.api_client.create_gpu_vm.assert_not_called() - compute.api_client.delete_ssh_key.assert_called_once_with("ssh-key-id-1") + api_client.create_gpu_vm.assert_not_called() + api_client.delete_ssh_key.assert_called_once_with("ssh-key-id-1") def test_update_provisioning_data_raises_provisioning_error_from_failed_capacity_status(): - compute = _compute() - compute.api_client.get_instance.return_value = None - compute.api_client.get_instance_status.return_value = { + compute, api_client = _compute_with_mocked_api_client() + api_client.get_instance.return_value = None + api_client.get_instance_status.return_value = { "status": "Failed", "error": "L4 not available at this moment, please try again later", "code": 404, @@ -467,9 +468,9 @@ def test_update_provisioning_data_raises_provisioning_error_from_failed_capacity def test_update_provisioning_data_raises_provisioning_error_from_failed_status(): - compute = _compute() - compute.api_client.get_instance.return_value = None - compute.api_client.get_instance_status.return_value = { + compute, api_client = _compute_with_mocked_api_client() + api_client.get_instance.return_value = None + api_client.get_instance_status.return_value = { "status": "Failed", "error": "image setup failed", "code": 500, diff --git a/src/tests/_internal/core/backends/nebius/test_compute.py b/src/tests/_internal/core/backends/nebius/test_compute.py index 4e70b3e30f..7abaffe435 100644 --- a/src/tests/_internal/core/backends/nebius/test_compute.py +++ b/src/tests/_internal/core/backends/nebius/test_compute.py @@ -398,7 +398,7 @@ def list_instances(sdk, project_id: str) -> list[Instance]: raise make_request_error(StatusCode.UNAVAILABLE) return [] - compute_module.resources.list_instances.side_effect = list_instances + mocker.patch.object(compute_module.resources, "list_instances", side_effect=list_instances) listing = make_compute().list_resource_groups() @@ -414,16 +414,18 @@ def _mock_sdk(self, mocker): @pytest.fixture def resources(self, mocker): resources = compute_module.resources - for name in [ - "get_instance", - "delete_instance", - "delete_disk", - "get_pricing_policy", - "delete_pricing_policy", - ]: - mocker.patch.object(resources, name) - resources.get_instance.side_effect = make_request_error(StatusCode.NOT_FOUND) - resources.get_pricing_policy.return_value = make_pricing_policy() + mocks = { + name: mocker.patch.object(resources, name) + for name in [ + "get_instance", + "delete_instance", + "delete_disk", + "get_pricing_policy", + "delete_pricing_policy", + ] + } + mocks["get_instance"].side_effect = make_request_error(StatusCode.NOT_FOUND) + mocks["get_pricing_policy"].return_value = make_pricing_policy() return resources def test_deletes_instance_first(self, resources): diff --git a/src/tests/_internal/core/backends/runpod/test_compute.py b/src/tests/_internal/core/backends/runpod/test_compute.py index 964b6128df..b573005891 100644 --- a/src/tests/_internal/core/backends/runpod/test_compute.py +++ b/src/tests/_internal/core/backends/runpod/test_compute.py @@ -25,10 +25,11 @@ ) -def _compute() -> RunpodCompute: +def _compute_with_mocked_api_client() -> tuple[RunpodCompute, MagicMock]: compute = RunpodCompute(RunpodConfig(creds=RunpodAPIKeyCreds(api_key="test"))) - compute.api_client = MagicMock() - return compute + api_client = MagicMock() + compute.api_client = api_client + return compute, api_client def _gpu_offer( @@ -139,13 +140,13 @@ def _pod( class TestRunpodComputeRunJob: def test_requests_per_gpu_minimums_rounded_down(self): - compute = _compute() - compute.api_client.create_pod.return_value = {"id": "pod-1"} + compute, api_client = _compute_with_mocked_api_client() + api_client.create_pod.return_value = {"id": "pod-1"} jpd = _run_job(compute, _gpu_offer(gpu_count=2, cpu_count=15, memory_gib=251)) assert jpd.instance_id == "pod-1" - kwargs = compute.api_client.create_pod.call_args.kwargs + kwargs = api_client.create_pod.call_args.kwargs assert kwargs["cloud"] == "SECURE" assert kwargs["data_center_ids"] == ["EU-RO-1"] assert kwargs["gpu_type_id"] == "NVIDIA H100 80GB HBM3" @@ -154,34 +155,34 @@ def test_requests_per_gpu_minimums_rounded_down(self): assert kwargs["min_memory_in_gb_per_gpu"] == 125 def test_failed_registry_credentials_cleanup_does_not_fail_provisioning(self): - compute = _compute() - compute.api_client.create_pod.return_value = {"id": "pod-1"} - compute.api_client.list_container_registry_credentials.side_effect = RunpodV2ClientError( + compute, api_client = _compute_with_mocked_api_client() + api_client.create_pod.return_value = {"id": "pod-1"} + api_client.list_container_registry_credentials.side_effect = RunpodV2ClientError( status=429, title="Too Many Requests", detail="Rate limit exceeded" ) jpd = _run_job(compute, _gpu_offer()) assert jpd.instance_id == "pod-1" - compute.api_client.terminate_pod.assert_not_called() + api_client.terminate_pod.assert_not_called() def test_encodes_provider_ref_in_pod_name(self): - compute = _compute() - compute.api_client.create_pod.return_value = {"id": "pod-1"} + compute, api_client = _compute_with_mocked_api_client() + api_client.create_pod.return_value = {"id": "pod-1"} job = _job() job.job_submissions = [MagicMock(provider_ref="abcdef123456")] _run_job(compute, _gpu_offer(), job=job) - pod_name = compute.api_client.create_pod.call_args.kwargs["name"] + pod_name = api_client.create_pod.call_args.kwargs["name"] assert parse_provider_ref_from_name(pod_name) == "abcdef123456" class TestRunpodComputeRunJobs: def test_sets_internal_ip_from_cluster_ip(self): - compute = _compute() - compute.api_client.create_cluster.return_value = {"id": "cluster-1", "name": "test"} - compute.api_client.list_cluster_pods.return_value = [ + compute, api_client = _compute_with_mocked_api_client() + api_client.create_cluster.return_value = {"id": "cluster-1", "name": "test"} + api_client.list_cluster_pods.return_value = [ _pod("pod-1", cluster_ip="10.65.0.2"), _pod("pod-2", is_cluster_pod=True), ] @@ -193,31 +194,31 @@ def test_sets_internal_ip_from_cluster_ip(self): assert [jpd.internal_ip for jpd in cgpd.job_provisioning_datas] == ["10.65.0.2", None] def test_deletes_cluster_if_pod_count_mismatch(self): - compute = _compute() - compute.api_client.create_cluster.return_value = {"id": "cluster-1", "name": "test"} - compute.api_client.list_cluster_pods.return_value = [_pod("pod-1", is_cluster_pod=True)] + compute, api_client = _compute_with_mocked_api_client() + api_client.create_cluster.return_value = {"id": "cluster-1", "name": "test"} + api_client.list_cluster_pods.return_value = [_pod("pod-1", is_cluster_pod=True)] with pytest.raises(ComputeError, match="Expected 2 pods"): _run_jobs(compute, _cluster_offer([2, 4]), job_count=2) - compute.api_client.delete_cluster.assert_called_once_with("cluster-1") + api_client.delete_cluster.assert_called_once_with("cluster-1") def test_deletes_cluster_if_listing_pods_fails(self): - compute = _compute() - compute.api_client.create_cluster.return_value = {"id": "cluster-1", "name": "test"} + compute, api_client = _compute_with_mocked_api_client() + api_client.create_cluster.return_value = {"id": "cluster-1", "name": "test"} error = RunpodV2ClientError(status=429, title="Too Many Requests", detail="-") - compute.api_client.list_cluster_pods.side_effect = error + api_client.list_cluster_pods.side_effect = error with pytest.raises(RunpodV2ClientError): _run_jobs(compute, _cluster_offer([2, 4]), job_count=2) - compute.api_client.delete_cluster.assert_called_once_with("cluster-1") + api_client.delete_cluster.assert_called_once_with("cluster-1") def test_raises_original_error_if_cluster_deletion_fails(self): - compute = _compute() - compute.api_client.create_cluster.return_value = {"id": "cluster-1", "name": "test"} - compute.api_client.list_cluster_pods.return_value = [] - compute.api_client.delete_cluster.side_effect = RuntimeError("delete failed") + compute, api_client = _compute_with_mocked_api_client() + api_client.create_cluster.return_value = {"id": "cluster-1", "name": "test"} + api_client.list_cluster_pods.return_value = [] + api_client.delete_cluster.side_effect = RuntimeError("delete failed") with pytest.raises(ComputeError, match="Expected 2 pods"): _run_jobs(compute, _cluster_offer([2, 4]), job_count=2) @@ -232,8 +233,8 @@ def _update(self, compute: RunpodCompute, jpd: JobProvisioningData) -> None: ) def test_sets_hostname_and_ssh_port(self): - compute = _compute() - compute.api_client.get_pod.return_value = _pod(ports=SSH_PORTS) + compute, api_client = _compute_with_mocked_api_client() + api_client.get_pod.return_value = _pod(ports=SSH_PORTS) jpd = get_job_provisioning_data(backend=BackendType.RUNPOD, hostname=None) self._update(compute, jpd) @@ -242,8 +243,8 @@ def test_sets_hostname_and_ssh_port(self): assert jpd.ssh_port == 40022 def test_waits_for_runtime(self): - compute = _compute() - compute.api_client.get_pod.return_value = _pod(ports=None) + compute, api_client = _compute_with_mocked_api_client() + api_client.get_pod.return_value = _pod(ports=None) jpd = get_job_provisioning_data(backend=BackendType.RUNPOD, hostname=None) self._update(compute, jpd) @@ -251,8 +252,8 @@ def test_waits_for_runtime(self): assert jpd.hostname is None def test_cluster_pod_waits_for_cluster_ip(self): - compute = _compute() - compute.api_client.get_pod.return_value = _pod(ports=SSH_PORTS, is_cluster_pod=True) + compute, api_client = _compute_with_mocked_api_client() + api_client.get_pod.return_value = _pod(ports=SSH_PORTS, is_cluster_pod=True) jpd = get_job_provisioning_data( backend=BackendType.RUNPOD, hostname=None, internal_ip=None ) @@ -263,8 +264,8 @@ def test_cluster_pod_waits_for_cluster_ip(self): assert jpd.internal_ip is None def test_cluster_pod_sets_internal_ip(self): - compute = _compute() - compute.api_client.get_pod.return_value = _pod(ports=SSH_PORTS, cluster_ip="10.65.0.3") + compute, api_client = _compute_with_mocked_api_client() + api_client.get_pod.return_value = _pod(ports=SSH_PORTS, cluster_ip="10.65.0.3") jpd = get_job_provisioning_data( backend=BackendType.RUNPOD, hostname=None, internal_ip=None ) @@ -294,8 +295,8 @@ def _listed_pod( class TestRunpodComputeListResourceGroups: def test_lists_pods_with_provider_ref(self): - compute = _compute() - compute.api_client.list_pods.return_value = [ + compute, api_client = _compute_with_mocked_api_client() + api_client.list_pods.return_value = [ _listed_pod("pod-1", name="dstack-main-test-job-0-abcdef123456wxyz"), ] @@ -316,8 +317,8 @@ def test_lists_pods_with_provider_ref(self): ] def test_skips_pods_without_provider_ref(self): - compute = _compute() - compute.api_client.list_pods.return_value = [ + compute, api_client = _compute_with_mocked_api_client() + api_client.list_pods.return_value = [ _listed_pod("pod-1", name="dstack-main-test-job-0-abcdefgh"), _listed_pod("pod-2", name="my-pod"), ] @@ -325,8 +326,8 @@ def test_skips_pods_without_provider_ref(self): assert compute.list_resource_groups().resource_groups == [] def test_skips_terminated_pods(self): - compute = _compute() - compute.api_client.list_pods.return_value = [ + compute, api_client = _compute_with_mocked_api_client() + api_client.list_pods.return_value = [ _listed_pod( "pod-1", name="dstack-main-test-job-0-abcdef123456wxyz", status="TERMINATED" ), @@ -335,8 +336,8 @@ def test_skips_terminated_pods(self): assert compute.list_resource_groups().resource_groups == [] def test_unparsable_created_at(self): - compute = _compute() - compute.api_client.list_pods.return_value = [ + compute, api_client = _compute_with_mocked_api_client() + api_client.list_pods.return_value = [ _listed_pod( "pod-1", name="dstack-main-test-job-0-abcdef123456wxyz", created_at="invalid" ), @@ -361,16 +362,16 @@ def _group(self, primary_id: Optional[str]) -> ListedResourceGroup: ) def test_terminates_pod(self): - compute = _compute() + compute, api_client = _compute_with_mocked_api_client() compute.delete_resource_group(self._group("pod-1")) - compute.api_client.terminate_pod.assert_called_once_with("pod-1") + api_client.terminate_pod.assert_called_once_with("pod-1") def test_pod_already_deleted(self): - compute = _compute() - compute.api_client.terminate_pod.return_value = False + compute, api_client = _compute_with_mocked_api_client() + api_client.terminate_pod.return_value = False compute.delete_resource_group(self._group("pod-1")) - compute.api_client.terminate_pod.assert_called_once_with("pod-1") + api_client.terminate_pod.assert_called_once_with("pod-1") diff --git a/src/tests/_internal/core/backends/seeweb/test_compute.py b/src/tests/_internal/core/backends/seeweb/test_compute.py index 301dd69292..df572c62d4 100644 --- a/src/tests/_internal/core/backends/seeweb/test_compute.py +++ b/src/tests/_internal/core/backends/seeweb/test_compute.py @@ -35,10 +35,11 @@ def _config(regions=None) -> SeewebConfig: return SeewebConfig(regions=regions, creds=SeewebAPITokenCreds(api_token="tok")) -def _compute(regions=None) -> SeewebCompute: +def _compute_with_mocked_api_client(regions=None) -> tuple[SeewebCompute, MagicMock]: compute = SeewebCompute(_config(regions)) - compute.api_client = MagicMock() - return compute + api_client = MagicMock() + compute.api_client = api_client + return compute, api_client def _raw(name: str, gpu: str, location: str = "it-fr2") -> CatalogItem: @@ -201,16 +202,16 @@ def test_select_image_rejects_unsupported_images(): def test_create_instance_uses_allowed_image_and_quotes_ssh_key(): - compute = _compute() + compute, api_client = _compute_with_mocked_api_client() public_key = "ssh-ed25519 AAAATEST comment with ' quote" - compute.api_client.get_available_plans.return_value = { + api_client.get_available_plans.return_value = { "ECS1GPU7": SeewebPlanAvailability( regions=frozenset({"it-fr2"}), images=("ubuntu-2204-uefi-nvidia-driver",), ) } - compute.api_client.get_or_create_ssh_key.return_value = "dstack-key" - compute.api_client.create_server.return_value = ({"name": "ec-test"}, 35) + api_client.get_or_create_ssh_key.return_value = "dstack-key" + api_client.create_server.return_value = ({"name": "ec-test"}, 35) with ( patch( @@ -230,7 +231,7 @@ def test_create_instance_uses_allowed_image_and_quotes_ssh_key(): _gpu_offer(), _instance_config(public_key), placement_group=None ) - body = compute.api_client.create_server.call_args.args[0] + body = api_client.create_server.call_args.args[0] assert body["plan"] == "ECS1GPU7" assert body["location"] == "it-fr2" assert body["image"] == "ubuntu-2204-uefi-nvidia-driver" @@ -251,20 +252,20 @@ def test_create_instance_uses_allowed_image_and_quotes_ssh_key(): def test_update_provisioning_data_waits_for_action(): - compute = _compute() - compute.api_client.get_action.return_value = {"status": "in-progress"} + compute, api_client = _compute_with_mocked_api_client() + api_client.get_action.return_value = {"status": "in-progress"} provisioning_data = _provisioning_data() compute.update_provisioning_data(provisioning_data, "public", "private") assert provisioning_data.hostname is None - compute.api_client.get_server.assert_not_called() + api_client.get_server.assert_not_called() def test_update_provisioning_data_sets_hostname_after_completed_action(): - compute = _compute() - compute.api_client.get_action.return_value = {"status": "completed"} - compute.api_client.get_server.return_value = { + compute, api_client = _compute_with_mocked_api_client() + api_client.get_action.return_value = {"status": "completed"} + api_client.get_server.return_value = { "name": "ec-test", "status": "Booted", "ipv4": "192.0.2.1", @@ -277,16 +278,16 @@ def test_update_provisioning_data_sets_hostname_after_completed_action(): def test_update_provisioning_data_raises_for_failed_action(): - compute = _compute() - compute.api_client.get_action.return_value = {"status": "failed"} + compute, api_client = _compute_with_mocked_api_client() + api_client.get_action.return_value = {"status": "failed"} with pytest.raises(ProvisioningError, match="entered status 'failed'"): compute.update_provisioning_data(_provisioning_data(), "public", "private") def test_update_provisioning_data_raises_for_failed_server(): - compute = _compute() - compute.api_client.get_server.return_value = { + compute, api_client = _compute_with_mocked_api_client() + api_client.get_server.return_value = { "name": "ec-test", "status": "Fail", } @@ -296,8 +297,8 @@ def test_update_provisioning_data_raises_for_failed_server(): def test_terminate_instance_delegates_to_idempotent_api(): - compute = _compute() + compute, api_client = _compute_with_mocked_api_client() compute.terminate_instance("ec-test", "it-fr2") - compute.api_client.delete_server.assert_called_once_with("ec-test") + api_client.delete_server.assert_called_once_with("ec-test") From b35f0c2d07124390fd759f2da4341cbba1fd999a Mon Sep 17 00:00:00 2001 From: Victor Skvortsov Date: Tue, 27 Oct 2026 15:47:59 +0500 Subject: [PATCH 2/8] Use model_validate for shorthand in core tests --- .../core/backends/base/test_compute.py | 6 +- .../core/backends/base/test_offers.py | 93 +++++++++++++------ .../core/backends/jarvislabs/test_compute.py | 4 +- .../core/backends/kubernetes/test_compute.py | 12 ++- .../core/backends/seeweb/test_compute.py | 4 +- .../core/backends/slurm/test_compute.py | 12 ++- .../core/backends/slurm/test_configurator.py | 22 +++-- .../core/models/test_configurations.py | 64 +++++++------ .../_internal/core/models/test_profiles.py | 8 +- src/tests/_internal/core/models/test_runs.py | 4 +- 10 files changed, 149 insertions(+), 80 deletions(-) diff --git a/src/tests/_internal/core/backends/base/test_compute.py b/src/tests/_internal/core/backends/base/test_compute.py index f6c52f44bd..59d963cf46 100644 --- a/src/tests/_internal/core/backends/base/test_compute.py +++ b/src/tests/_internal/core/backends/base/test_compute.py @@ -60,7 +60,9 @@ def test_fits_disk_only_for_offers_matching_other_requirements(self): compute = _ComputeWithCachedOffers( [gpu_offer, cpu_offer], {"gpu": configurable, "cpu": configurable} ) - requirements = Requirements(resources=ResourcesSpec(gpu=1, disk="200GB..")) + requirements = Requirements( + resources=ResourcesSpec.model_validate({"gpu": 1, "disk": "200GB.."}) + ) offers = list(compute.get_offers(requirements, False, False)) @@ -77,7 +79,7 @@ def test_excludes_offers_whose_disk_does_not_fit(self): [configurable_offer, fixed_offer], {"configurable": Range[Memory](min=Memory.parse("50GB"), max=None), "fixed": None}, ) - requirements = Requirements(resources=ResourcesSpec(disk="200GB..")) + requirements = Requirements(resources=ResourcesSpec.model_validate({"disk": "200GB.."})) offers = list(compute.get_offers(requirements, False, False)) diff --git a/src/tests/_internal/core/backends/base/test_offers.py b/src/tests/_internal/core/backends/base/test_offers.py index cfd928431a..922e709394 100644 --- a/src/tests/_internal/core/backends/base/test_offers.py +++ b/src/tests/_internal/core/backends/base/test_offers.py @@ -33,79 +33,108 @@ def test_empty_spec_matches_any_gpu(self): assert gpu_matches_gpu_spec(make_gpu(vendor=AMD), GPUSpec()) def test_vendor_matches(self): - assert gpu_matches_gpu_spec(make_gpu(vendor=NVIDIA), GPUSpec(vendor="nvidia")) + assert gpu_matches_gpu_spec( + make_gpu(vendor=NVIDIA), GPUSpec.model_validate({"vendor": "nvidia"}) + ) def test_vendor_mismatch(self): - assert not gpu_matches_gpu_spec(make_gpu(vendor=AMD), GPUSpec(vendor="nvidia")) + assert not gpu_matches_gpu_spec( + make_gpu(vendor=AMD), GPUSpec.model_validate({"vendor": "nvidia"}) + ) def test_name_matches(self): - assert gpu_matches_gpu_spec(make_gpu(name="A100"), GPUSpec(name="A100")) + assert gpu_matches_gpu_spec( + make_gpu(name="A100"), GPUSpec.model_validate({"name": "A100"}) + ) @pytest.mark.parametrize( ("gpu_name", "spec_name"), [("A100", "a100"), ("a100", "A100"), ("H100", "h100")], ) def test_name_match_is_case_insensitive(self, gpu_name: str, spec_name: str): - assert gpu_matches_gpu_spec(make_gpu(name=gpu_name), GPUSpec(name=spec_name)) + assert gpu_matches_gpu_spec( + make_gpu(name=gpu_name), GPUSpec.model_validate({"name": spec_name}) + ) def test_name_matches_any_in_list(self): assert gpu_matches_gpu_spec(make_gpu(name="A100"), GPUSpec(name=["V100", "A100"])) def test_name_mismatch(self): - assert not gpu_matches_gpu_spec(make_gpu(name="A100"), GPUSpec(name="V100")) + assert not gpu_matches_gpu_spec( + make_gpu(name="A100"), GPUSpec.model_validate({"name": "V100"}) + ) def test_name_not_in_list(self): assert not gpu_matches_gpu_spec(make_gpu(name="A100"), GPUSpec(name=["V100", "H100"])) def test_memory_within_range(self): - assert gpu_matches_gpu_spec(make_gpu(memory_gib=40), GPUSpec(memory="16GB..80GB")) + assert gpu_matches_gpu_spec( + make_gpu(memory_gib=40), GPUSpec.model_validate({"memory": "16GB..80GB"}) + ) def test_memory_equal_to_min(self): - assert gpu_matches_gpu_spec(make_gpu(memory_gib=16), GPUSpec(memory="16GB..")) + assert gpu_matches_gpu_spec( + make_gpu(memory_gib=16), GPUSpec.model_validate({"memory": "16GB.."}) + ) def test_memory_equal_to_max(self): - assert gpu_matches_gpu_spec(make_gpu(memory_gib=80), GPUSpec(memory="..80GB")) + assert gpu_matches_gpu_spec( + make_gpu(memory_gib=80), GPUSpec.model_validate({"memory": "..80GB"}) + ) def test_memory_below_min(self): - assert not gpu_matches_gpu_spec(make_gpu(memory_gib=15), GPUSpec(memory="16GB..")) + assert not gpu_matches_gpu_spec( + make_gpu(memory_gib=15), GPUSpec.model_validate({"memory": "16GB.."}) + ) def test_memory_above_max(self): - assert not gpu_matches_gpu_spec(make_gpu(memory_gib=100), GPUSpec(memory="..80GB")) + assert not gpu_matches_gpu_spec( + make_gpu(memory_gib=100), GPUSpec.model_validate({"memory": "..80GB"}) + ) def test_compute_capability_equal(self): # A100 has compute capability 8.0 - assert gpu_matches_gpu_spec(make_gpu(name="A100"), GPUSpec(compute_capability="8.0")) + assert gpu_matches_gpu_spec( + make_gpu(name="A100"), GPUSpec.model_validate({"compute_capability": "8.0"}) + ) def test_compute_capability_gpu_higher_than_required(self): # A100 (8.0) satisfies the 7.0 minimum - assert gpu_matches_gpu_spec(make_gpu(name="A100"), GPUSpec(compute_capability="7.0")) + assert gpu_matches_gpu_spec( + make_gpu(name="A100"), GPUSpec.model_validate({"compute_capability": "7.0"}) + ) def test_compute_capability_gpu_lower_than_required(self): # A100 (8.0) does not satisfy the 9.0 minimum - assert not gpu_matches_gpu_spec(make_gpu(name="A100"), GPUSpec(compute_capability="9.0")) + assert not gpu_matches_gpu_spec( + make_gpu(name="A100"), GPUSpec.model_validate({"compute_capability": "9.0"}) + ) def test_compute_capability_non_nvidia_never_matches(self): assert not gpu_matches_gpu_spec( - make_gpu(name="MI300X", vendor=AMD), GPUSpec(compute_capability="8.0") + make_gpu(name="MI300X", vendor=AMD), + GPUSpec.model_validate({"compute_capability": "8.0"}), ) def test_compute_capability_unknown_gpu_name_never_matches(self): assert not gpu_matches_gpu_spec( - make_gpu(name="UnknownGPU"), GPUSpec(compute_capability="8.0") + make_gpu(name="UnknownGPU"), GPUSpec.model_validate({"compute_capability": "8.0"}) ) def test_all_constraints_match(self): - spec = GPUSpec( - vendor="nvidia", - name="A100", - memory="40GB..80GB", - compute_capability="8.0", + spec = GPUSpec.model_validate( + { + "vendor": "nvidia", + "name": "A100", + "memory": "40GB..80GB", + "compute_capability": "8.0", + } ) assert gpu_matches_gpu_spec(make_gpu(name="A100", memory_gib=80), spec) def test_single_failing_constraint_rejects_gpu(self): # Everything matches except memory, which is below the minimum. - spec = GPUSpec(vendor="nvidia", name="A100", memory="40GB..80GB") + spec = GPUSpec.model_validate({"vendor": "nvidia", "name": "A100", "memory": "40GB..80GB"}) assert not gpu_matches_gpu_spec(make_gpu(name="A100", memory_gib=24), spec) @@ -140,26 +169,30 @@ def test_unknown_disk_size_ignores_disk_requirement(self): # An offer with a real 0 GB disk would satisfy neither the min nor the max # bound, so a match here proves the disk requirement is ignored entirely. offer = self.make_offer(disk_size_mib=0) - requirements = Requirements(resources=ResourcesSpec(disk="100GB..200GB")) + requirements = Requirements( + resources=ResourcesSpec.model_validate({"disk": "100GB..200GB"}) + ) assert list(filter_offers_by_requirements([offer], requirements)) == [offer] def test_known_disk_size_below_min_is_filtered_out(self): # A non-zero disk size is still checked, so the requirement stays effective. offer = self.make_offer(disk_size_mib=50 * 1024) - requirements = Requirements(resources=ResourcesSpec(disk="100GB..")) + requirements = Requirements(resources=ResourcesSpec.model_validate({"disk": "100GB.."})) assert list(filter_offers_by_requirements([offer], requirements)) == [] def test_known_disk_size_within_range_matches(self): offer = self.make_offer(disk_size_mib=150 * 1024) - requirements = Requirements(resources=ResourcesSpec(disk="100GB..200GB")) + requirements = Requirements( + resources=ResourcesSpec.model_validate({"disk": "100GB..200GB"}) + ) assert list(filter_offers_by_requirements([offer], requirements)) == [offer] def test_ignore_disk_skips_disk_requirement(self): offer = self.make_offer(disk_size_mib=50 * 1024) - requirements = Requirements(resources=ResourcesSpec(disk="100GB..")) + requirements = Requirements(resources=ResourcesSpec.model_validate({"disk": "100GB.."})) assert list(filter_offers_by_requirements([offer], requirements, ignore_disk=True)) == [ offer @@ -169,7 +202,7 @@ def test_ignore_disk_skips_disk_requirement(self): class TestOfferDiskFitter: def test_sets_min_disk_size_within_configurable_and_required_ranges(self): offer = get_instance_offer_with_availability(gpu_count=1, disk_gib=100) - requirements = Requirements(resources=ResourcesSpec(disk="200GB..")) + requirements = Requirements(resources=ResourcesSpec.model_validate({"disk": "200GB.."})) fitter = OfferDiskFitter(requirements) fitted_a = fitter.fit(offer, Range[Memory](min=Memory.parse("10GB"), max=None)) @@ -195,14 +228,14 @@ def test_uses_default_disk_if_requirements_have_no_disk(self): def test_returns_none_if_configurable_range_does_not_fit(self): offer = get_instance_offer_with_availability() - requirements = Requirements(resources=ResourcesSpec(disk="..50GB")) + requirements = Requirements(resources=ResourcesSpec.model_validate({"disk": "..50GB"})) configurable = Range[Memory](min=Memory.parse("100GB"), max=None) assert OfferDiskFitter(requirements).fit(offer, configurable) is None def test_returns_same_offer_if_disk_size_unchanged(self): offer = get_instance_offer_with_availability(disk_gib=100) - requirements = Requirements(resources=ResourcesSpec(disk="100GB..")) + requirements = Requirements(resources=ResourcesSpec.model_validate({"disk": "100GB.."})) configurable = Range[Memory](min=Memory.parse("10GB"), max=None) assert OfferDiskFitter(requirements).fit(offer, configurable) is offer @@ -219,7 +252,9 @@ def test_returns_same_offer_if_disk_size_unchanged(self): ) def test_checks_fixed_disk(self, disk_gib: int, required_disk: Optional[str], fits: bool): offer = get_instance_offer_with_availability(disk_gib=disk_gib) - requirements = Requirements(resources=ResourcesSpec(disk=required_disk)) + requirements = Requirements( + resources=ResourcesSpec.model_validate({"disk": required_disk}) + ) fitted = OfferDiskFitter(requirements).fit(offer, None) diff --git a/src/tests/_internal/core/backends/jarvislabs/test_compute.py b/src/tests/_internal/core/backends/jarvislabs/test_compute.py index e3b9075679..47d94e2361 100644 --- a/src/tests/_internal/core/backends/jarvislabs/test_compute.py +++ b/src/tests/_internal/core/backends/jarvislabs/test_compute.py @@ -164,14 +164,14 @@ def test_get_offers_reuses_all_offers_cache_and_modifies_disk_size(): offers_250gb = list( compute.get_offers( - Requirements(resources=ResourcesSpec(disk="250GB")), + Requirements(resources=ResourcesSpec.model_validate({"disk": "250GB"})), full_offers=False, unallocated_resources=False, ) ) offers_300gb = list( compute.get_offers( - Requirements(resources=ResourcesSpec(disk="300GB")), + Requirements(resources=ResourcesSpec.model_validate({"disk": "300GB"})), full_offers=False, unallocated_resources=False, ) diff --git a/src/tests/_internal/core/backends/kubernetes/test_compute.py b/src/tests/_internal/core/backends/kubernetes/test_compute.py index 39ef170850..5e7ba550d5 100644 --- a/src/tests/_internal/core/backends/kubernetes/test_compute.py +++ b/src/tests/_internal/core/backends/kubernetes/test_compute.py @@ -49,7 +49,9 @@ def _node_offer() -> InstanceOfferWithAvailability: def test_get_offers_modifiers_are_skipped_with_full_offers(): compute = _compute() - requirements = Requirements(resources=ResourcesSpec(cpu="2", memory="8GB", gpu="1")) + requirements = Requirements( + resources=ResourcesSpec.model_validate({"cpu": "2", "memory": "8GB", "gpu": "1"}) + ) assert compute.get_offers_modifiers(requirements, full_offers=True) == [] assert compute.get_offers_modifiers(requirements, full_offers=False) != [] @@ -60,7 +62,9 @@ def test_get_offers_with_full_offers_keeps_full_node_resources(): compute.get_all_offers_with_availability = MagicMock(return_value=[_node_offer()]) # Open-ended requirements so the full node satisfies them without an upper bound. requirements = Requirements( - resources=ResourcesSpec(cpu="2..", memory="8GB..", gpu="1..", disk="100GB..") + resources=ResourcesSpec.model_validate( + {"cpu": "2..", "memory": "8GB..", "gpu": "1..", "disk": "100GB.."} + ) ) full_offers = list( @@ -79,7 +83,9 @@ def test_get_offers_without_full_offers_adjusts_to_requested_slice(): compute = _compute() compute.get_all_offers_with_availability = MagicMock(return_value=[_node_offer()]) requirements = Requirements( - resources=ResourcesSpec(cpu="2..", memory="8GB..", gpu="1..", disk="100GB..") + resources=ResourcesSpec.model_validate( + {"cpu": "2..", "memory": "8GB..", "gpu": "1..", "disk": "100GB.."} + ) ) adjusted_offers = list( diff --git a/src/tests/_internal/core/backends/seeweb/test_compute.py b/src/tests/_internal/core/backends/seeweb/test_compute.py index df572c62d4..f675c65985 100644 --- a/src/tests/_internal/core/backends/seeweb/test_compute.py +++ b/src/tests/_internal/core/backends/seeweb/test_compute.py @@ -2,7 +2,7 @@ from unittest.mock import MagicMock, patch import pytest -from gpuhunt import CatalogItem +from gpuhunt import AcceleratorVendor, CatalogItem from dstack._internal.core.backends.base.compute import ( ComputeWithInstanceVolumesSupport, @@ -53,7 +53,7 @@ def _raw(name: str, gpu: str, location: str = "it-fr2") -> CatalogItem: gpu_count=1, gpu_name=gpu, gpu_memory=24.0, - gpu_vendor="nvidia", + gpu_vendor=AcceleratorVendor.NVIDIA, spot=False, disk_size=100.0, ) diff --git a/src/tests/_internal/core/backends/slurm/test_compute.py b/src/tests/_internal/core/backends/slurm/test_compute.py index 427ac9a628..efa9e7553b 100644 --- a/src/tests/_internal/core/backends/slurm/test_compute.py +++ b/src/tests/_internal/core/backends/slurm/test_compute.py @@ -45,7 +45,9 @@ def _node_offer() -> InstanceOfferWithAvailability: def test_get_offers_modifiers_are_skipped_with_full_offers(): compute = _compute() - requirements = Requirements(resources=ResourcesSpec(cpu="2", memory="8GB", gpu="1")) + requirements = Requirements( + resources=ResourcesSpec.model_validate({"cpu": "2", "memory": "8GB", "gpu": "1"}) + ) assert compute.get_offers_modifiers(requirements, full_offers=True) == [] assert compute.get_offers_modifiers(requirements, full_offers=False) != [] @@ -56,7 +58,9 @@ def test_get_offers_with_full_offers_keeps_full_node_resources(): compute.get_all_offers_with_availability = MagicMock(return_value=[_node_offer()]) # Open-ended requirements so the full node satisfies them without an upper bound. requirements = Requirements( - resources=ResourcesSpec(cpu="2..", memory="8GB..", gpu="1..", disk="100GB..") + resources=ResourcesSpec.model_validate( + {"cpu": "2..", "memory": "8GB..", "gpu": "1..", "disk": "100GB.."} + ) ) full_offers = list( @@ -76,7 +80,9 @@ def test_get_offers_without_full_offers_adjusts_to_requested_slice(): compute.get_all_offers_with_availability = MagicMock(return_value=[_node_offer()]) compute._get_cluster = MagicMock() requirements = Requirements( - resources=ResourcesSpec(cpu="2..", memory="8GB..", gpu="1..", disk="100GB..") + resources=ResourcesSpec.model_validate( + {"cpu": "2..", "memory": "8GB..", "gpu": "1..", "disk": "100GB.."} + ) ) # Slicing needs cluster/partition lookups, which are otherwise backed by live cluster state. diff --git a/src/tests/_internal/core/backends/slurm/test_configurator.py b/src/tests/_internal/core/backends/slurm/test_configurator.py index c3b2895f01..ad98670201 100644 --- a/src/tests/_internal/core/backends/slurm/test_configurator.py +++ b/src/tests/_internal/core/backends/slurm/test_configurator.py @@ -15,16 +15,18 @@ def _make_backend_record() -> BackendRecord: - config = SlurmBackendConfigWithCreds( - clusters=[ - { - "name": "c1", - "hostname": HOSTNAME, - "port": PORT, - "user": USER, - "private_key": {"content": PRIVATE_KEY}, - } - ] + config = SlurmBackendConfigWithCreds.model_validate( + { + "clusters": [ + { + "name": "c1", + "hostname": HOSTNAME, + "port": PORT, + "user": USER, + "private_key": {"content": PRIVATE_KEY}, + } + ] + } ) return SlurmConfigurator().create_backend("proj", config) diff --git a/src/tests/_internal/core/models/test_configurations.py b/src/tests/_internal/core/models/test_configurations.py index 19188bdc85..fdb31a9090 100644 --- a/src/tests/_internal/core/models/test_configurations.py +++ b/src/tests/_internal/core/models/test_configurations.py @@ -1215,7 +1215,7 @@ def test_schema_documents_supported_input(self): ]["anyOf"] def test_parses_string_as_exact_repo(self): - configuration = PresetConfiguration(model="Qwen/Qwen3.5-27B") + configuration = PresetConfiguration.model_validate({"model": "Qwen/Qwen3.5-27B"}) assert isinstance(configuration.model, PresetModelRepo) assert configuration.model.exact_repo == "Qwen/Qwen3.5-27B" @@ -1223,7 +1223,7 @@ def test_parses_string_as_exact_repo(self): assert not configuration.model.allows_variant_selection def test_parses_base_model(self): - configuration = PresetConfiguration(base="Qwen/Qwen3.5-27B") + configuration = PresetConfiguration.model_validate({"base": "Qwen/Qwen3.5-27B"}) assert isinstance(configuration.model, PresetModelBase) assert configuration.model.exact_repo is None @@ -1231,24 +1231,26 @@ def test_parses_base_model(self): assert configuration.model.allows_variant_selection def test_agent_is_optional_and_carries_provider_model_and_effort(self): - assert PresetConfiguration(model="Qwen/Qwen3.5-27B").agent is None + assert PresetConfiguration.model_validate({"model": "Qwen/Qwen3.5-27B"}).agent is None - configuration = PresetConfiguration( - model="Qwen/Qwen3.5-27B", - agent={"provider": "codex", "model": "gpt-6-astra", "effort": "xhigh"}, + configuration = PresetConfiguration.model_validate( + { + "model": "Qwen/Qwen3.5-27B", + "agent": {"provider": "codex", "model": "gpt-6-astra", "effort": "xhigh"}, + } ) assert configuration.agent == PresetAgentConfig( provider="codex", model="gpt-6-astra", effort="xhigh" ) - assert PresetConfiguration( - model="Qwen/Qwen3.5-27B", agent={"provider": "claude"} + assert PresetConfiguration.model_validate( + {"model": "Qwen/Qwen3.5-27B", "agent": {"provider": "claude"}} ).agent == (PresetAgentConfig(provider="claude", model=None, effort=None)) @pytest.mark.parametrize("agent", [{}, {"model": "gpt-6-astra"}, {"provider": "gemini"}]) def test_agent_requires_a_known_provider(self, agent): with pytest.raises(ValidationError): - PresetConfiguration(model="Qwen/Qwen3.5-27B", agent=agent) + PresetConfiguration.model_validate({"model": "Qwen/Qwen3.5-27B", "agent": agent}) @pytest.mark.parametrize( "agent", @@ -1256,13 +1258,15 @@ def test_agent_requires_a_known_provider(self, agent): ) def test_agent_effort_must_exist_for_the_provider(self, agent): with pytest.raises(ValidationError): - PresetConfiguration(model="Qwen/Qwen3.5-27B", agent=agent) + PresetConfiguration.model_validate({"model": "Qwen/Qwen3.5-27B", "agent": agent}) def test_parses_exact_repo_with_client_facing_name(self): - configuration = PresetConfiguration( - model={ - "repo": "community/Qwen3.5-27B-GPTQ-Int4", - "name": "Qwen/Qwen3.5-27B", + configuration = PresetConfiguration.model_validate( + { + "model": { + "repo": "community/Qwen3.5-27B-GPTQ-Int4", + "name": "Qwen/Qwen3.5-27B", + } } ) @@ -1271,24 +1275,28 @@ def test_parses_exact_repo_with_client_facing_name(self): def test_rejects_ambiguous_model_object(self): with pytest.raises(ValidationError): - PresetConfiguration(model={"base": "Qwen/base", "repo": "Qwen/repo"}) + PresetConfiguration.model_validate( + {"model": {"base": "Qwen/base", "repo": "Qwen/repo"}} + ) def test_parses_top_level_base_shorthand(self): - configuration = PresetConfiguration(base="Qwen/Qwen3.5-27B") + configuration = PresetConfiguration.model_validate({"base": "Qwen/Qwen3.5-27B"}) assert isinstance(configuration.model, PresetModelBase) assert configuration.model.api_model_name == "Qwen/Qwen3.5-27B" assert configuration.base is None def test_parses_top_level_repo_shorthand(self): - configuration = PresetConfiguration(repo="community/Qwen3.5-27B-GPTQ-Int4") + configuration = PresetConfiguration.model_validate( + {"repo": "community/Qwen3.5-27B-GPTQ-Int4"} + ) assert isinstance(configuration.model, PresetModelRepo) assert configuration.model.exact_repo == "community/Qwen3.5-27B-GPTQ-Int4" assert configuration.repo is None def test_shorthand_round_trips_through_dict(self): - configuration = PresetConfiguration(base="Qwen/Qwen3.5-27B") + configuration = PresetConfiguration.model_validate({"base": "Qwen/Qwen3.5-27B"}) round_tripped = PresetConfiguration.model_validate(configuration.model_dump()) @@ -1296,24 +1304,28 @@ def test_shorthand_round_trips_through_dict(self): def test_rejects_combined_base_and_repo_shorthand(self): with pytest.raises(ValidationError): - PresetConfiguration(base="Qwen/base", repo="Qwen/repo") + PresetConfiguration.model_validate({"base": "Qwen/base", "repo": "Qwen/repo"}) def test_rejects_shorthand_combined_with_model(self): with pytest.raises(ValidationError, match="cannot be combined"): - PresetConfiguration(base="Qwen/base", model={"repo": "Qwen/repo"}) + PresetConfiguration.model_validate( + {"base": "Qwen/base", "model": {"repo": "Qwen/repo"}} + ) def test_requires_model(self): with pytest.raises(ValidationError): - PresetConfiguration() + PresetConfiguration.model_validate({}) @pytest.mark.parametrize("field", ["input_tokens", "output_tokens", "shared_prefix_tokens"]) def test_rejects_request_shape_fields_with_a_custom_dataset(self, field): with pytest.raises(ValidationError, match="cannot be set together with `dataset`"): - PresetConfiguration(base="Qwen/Qwen3.5-27B", dataset="spec_bench", **{field: 512}) + PresetConfiguration.model_validate( + {"base": "Qwen/Qwen3.5-27B", "dataset": "spec_bench", field: 512} + ) def test_allows_request_shape_fields_without_a_dataset(self): - configuration = PresetConfiguration( - base="Qwen/Qwen3.5-27B", input_tokens=1024, output_tokens=256 + configuration = PresetConfiguration.model_validate( + {"base": "Qwen/Qwen3.5-27B", "input_tokens": 1024, "output_tokens": 256} ) assert configuration.input_tokens == 1024 @@ -1323,10 +1335,10 @@ def test_rejects_the_retired_random_alias(self): # `random` used to be the explicit way to ask for synthetic prompts; # now a set dataset always means a real one. with pytest.raises(ValidationError, match="omit `dataset` for synthetic prompts"): - PresetConfiguration(base="Qwen/Qwen3.5-27B", dataset="random") + PresetConfiguration.model_validate({"base": "Qwen/Qwen3.5-27B", "dataset": "random"}) def test_defaults_to_a_synthetic_workload(self): - configuration = PresetConfiguration(base="Qwen/Qwen3.5-27B") + configuration = PresetConfiguration.model_validate({"base": "Qwen/Qwen3.5-27B"}) assert configuration.dataset is None diff --git a/src/tests/_internal/core/models/test_profiles.py b/src/tests/_internal/core/models/test_profiles.py index eee809d670..59fc7f8f93 100644 --- a/src/tests/_internal/core/models/test_profiles.py +++ b/src/tests/_internal/core/models/test_profiles.py @@ -48,11 +48,15 @@ def test_string_is_parsed_as_instance_name_selector(self): ({"hostname": "worker-1"}, InstanceHostnameSelector(hostname="worker-1")), ( {"fleet": "my-fleet", "instance": 3}, - FleetInstanceSelector(fleet="my-fleet", instance=3), + FleetInstanceSelector( + fleet=EntityReference(project=None, name="my-fleet"), instance=3 + ), ), ( {"fleet": "other-project/my-fleet", "instance": 3}, - FleetInstanceSelector(fleet="other-project/my-fleet", instance=3), + FleetInstanceSelector( + fleet=EntityReference(project="other-project", name="my-fleet"), instance=3 + ), ), ], ) diff --git a/src/tests/_internal/core/models/test_runs.py b/src/tests/_internal/core/models/test_runs.py index e911a07125..20b5558b2c 100644 --- a/src/tests/_internal/core/models/test_runs.py +++ b/src/tests/_internal/core/models/test_runs.py @@ -57,7 +57,9 @@ def test_unset_service_groups_are_excluded_for_compatibility(): def test_set_service_groups_are_not_excluded(): - configuration = ServiceConfiguration(port=8000, groups=[{"replicas": 1, "commands": ["true"]}]) + configuration = ServiceConfiguration.model_validate( + {"port": 8000, "groups": [{"replicas": 1, "commands": ["true"]}]} + ) configuration_excludes = get_run_spec_excludes(RunSpec(configuration=configuration)).get( "configuration" From d1cd70b1924f8149b1bf51d5bbd0db51a009b419 Mon Sep 17 00:00:00 2001 From: Victor Skvortsov Date: Tue, 27 Oct 2026 15:56:42 +0500 Subject: [PATCH 3/8] Narrow configuration types and drop fakes in core tests --- .../core/backends/lium/test_volumes.py | 24 +++-- .../core/backends/verda/test_compute.py | 101 ++++++------------ .../core/models/test_configurations.py | 16 ++- 3 files changed, 58 insertions(+), 83 deletions(-) diff --git a/src/tests/_internal/core/backends/lium/test_volumes.py b/src/tests/_internal/core/backends/lium/test_volumes.py index ced76db31a..922c896bfc 100644 --- a/src/tests/_internal/core/backends/lium/test_volumes.py +++ b/src/tests/_internal/core/backends/lium/test_volumes.py @@ -1,4 +1,5 @@ from types import SimpleNamespace +from typing import Union, cast from unittest.mock import Mock, patch import pytest @@ -7,8 +8,10 @@ from dstack._internal.core.backends.lium.compute import LiumCompute, _get_volume_commands from dstack._internal.core.backends.lium.models import LiumConfig, LiumCreds from dstack._internal.core.errors import ComputeError +from dstack._internal.core.models.runs import Job from dstack._internal.core.models.unix import UnixUser from dstack._internal.core.models.volumes import LiumVolumeConfiguration, VolumeMountPoint +from dstack._internal.server.testing.common import get_volume COMPUTE_MODULE = "dstack._internal.core.backends.lium.compute" @@ -94,19 +97,18 @@ def test_two_volumes_rejected(self): _get_volume_commands(Mock(), Mock(), [Mock(), Mock()]) def test_selects_volume_from_alternatives(self): - job = SimpleNamespace( - job_spec=SimpleNamespace( - user=None, - volumes=[VolumeMountPoint(name=["elsewhere", "cache"], path="/data")], - ) - ) - assert _get_volume_commands(Mock(), job, [SimpleNamespace(name="cache")])[-1] == ( + assert _volume_commands(name=["elsewhere", "cache"], path="/data")[-1] == ( "mount --bind /mnt /data" ) -def _volume_commands(user=None, path="/checkpoints"): - job = SimpleNamespace( - job_spec=SimpleNamespace(user=user, volumes=[VolumeMountPoint(name="cache", path=path)]) +def _volume_commands(user=None, path="/checkpoints", name: Union[str, list[str]] = "cache"): + # A real `Job` needs a full `JobSpec`, while `_get_volume_commands()` only reads the user and + # the mount points + job = cast( + Job, + SimpleNamespace( + job_spec=SimpleNamespace(user=user, volumes=[VolumeMountPoint(name=name, path=path)]) + ), ) - return _get_volume_commands(Mock(), job, [SimpleNamespace(name="cache")]) + return _get_volume_commands(Mock(), job, [get_volume(name="cache")]) diff --git a/src/tests/_internal/core/backends/verda/test_compute.py b/src/tests/_internal/core/backends/verda/test_compute.py index d7ddad065c..8edabaf0c7 100644 --- a/src/tests/_internal/core/backends/verda/test_compute.py +++ b/src/tests/_internal/core/backends/verda/test_compute.py @@ -14,9 +14,12 @@ from dstack._internal.core.models.backends.base import BackendType from dstack._internal.core.models.instances import ( InstanceAvailability, + InstanceConfiguration, InstanceOffer, + InstanceOfferWithAvailability, InstanceType, Resources, + SSHKey, ) @@ -32,6 +35,28 @@ def _offer(spot: bool, name: str = "SOME.INSTANCE", region: str = "FIN-01") -> I ) +def _instance_offer() -> InstanceOfferWithAvailability: + return InstanceOfferWithAvailability( + backend=BackendType.VERDA, + instance=InstanceType( + name="CPU.4V.16G", + resources=Resources(cpus=4, memory_mib=16384, gpus=[], spot=False), + ), + region="FIN-01", + price=0.0279, + availability=InstanceAvailability.AVAILABLE, + ) + + +def _instance_config(public_keys: list[str]) -> InstanceConfiguration: + return InstanceConfiguration( + project_name="main", + instance_name="verda-one-node-0", + user="admin", + ssh_keys=[SSHKey(public=key) for key in public_keys], + ) + + def _assert_terminate_call(action_mock: MagicMock): action_mock.assert_called_once() kwargs = action_mock.call_args.kwargs @@ -101,23 +126,8 @@ def test_cleans_up_created_ssh_keys_if_later_ssh_key_create_fails(self): compute = VerdaCompute.__new__(VerdaCompute) compute.client = MagicMock() - instance_offer = SimpleNamespace( - backend="verda", - instance=SimpleNamespace( - name="CPU.4V.16G", - resources=SimpleNamespace( - disk=SimpleNamespace(size_mib=102400), - gpus=[], - spot=False, - ), - ), - region="FIN-01", - price=0.0279, - ) - instance_config = SimpleNamespace( - instance_name="verda-one-node-0", - get_public_keys=lambda: ["ssh-rsa test-1", "ssh-rsa test-2"], - ) + instance_offer = _instance_offer() + instance_config = _instance_config(["ssh-rsa test-1", "ssh-rsa test-2"]) with ( patch( @@ -149,23 +159,8 @@ def test_cleans_up_ssh_keys_if_startup_script_create_fails(self): compute = VerdaCompute.__new__(VerdaCompute) compute.client = MagicMock() - instance_offer = SimpleNamespace( - backend="verda", - instance=SimpleNamespace( - name="CPU.4V.16G", - resources=SimpleNamespace( - disk=SimpleNamespace(size_mib=102400), - gpus=[], - spot=False, - ), - ), - region="FIN-01", - price=0.0279, - ) - instance_config = SimpleNamespace( - instance_name="verda-one-node-0", - get_public_keys=lambda: ["ssh-rsa test-1", "ssh-rsa test-2"], - ) + instance_offer = _instance_offer() + instance_config = _instance_config(["ssh-rsa test-1", "ssh-rsa test-2"]) with ( patch( @@ -197,23 +192,8 @@ def test_cleans_up_startup_script_if_deploy_fails(self): compute = VerdaCompute.__new__(VerdaCompute) compute.client = MagicMock() - instance_offer = SimpleNamespace( - backend="verda", - instance=SimpleNamespace( - name="CPU.4V.16G", - resources=SimpleNamespace( - disk=SimpleNamespace(size_mib=102400), - gpus=[], - spot=False, - ), - ), - region="FIN-01", - price=0.0279, - ) - instance_config = SimpleNamespace( - instance_name="verda-one-node-0", - get_public_keys=lambda: ["ssh-rsa test"], - ) + instance_offer = _instance_offer() + instance_config = _instance_config(["ssh-rsa test"]) with ( patch( @@ -253,23 +233,8 @@ def test_stores_ssh_key_ids_in_backend_data(self): compute = VerdaCompute.__new__(VerdaCompute) compute.client = MagicMock() - instance_offer = SimpleNamespace( - backend="verda", - instance=SimpleNamespace( - name="CPU.4V.16G", - resources=SimpleNamespace( - disk=SimpleNamespace(size_mib=102400), - gpus=[], - spot=False, - ), - ), - region="FIN-01", - price=0.0279, - ) - instance_config = SimpleNamespace( - instance_name="verda-one-node-0", - get_public_keys=lambda: ["ssh-rsa test-1", "ssh-rsa test-2"], - ) + instance_offer = _instance_offer() + instance_config = _instance_config(["ssh-rsa test-1", "ssh-rsa test-2"]) provider_instance = SimpleNamespace(id="provider-instance-id", location="FIN-01") with ( diff --git a/src/tests/_internal/core/models/test_configurations.py b/src/tests/_internal/core/models/test_configurations.py index fdb31a9090..9677861326 100644 --- a/src/tests/_internal/core/models/test_configurations.py +++ b/src/tests/_internal/core/models/test_configurations.py @@ -15,6 +15,7 @@ PythonVersion, RepoSpec, ServiceConfiguration, + TaskConfiguration, parse_run_configuration, ) from dstack._internal.core.models.resources import Range, ResourcesSpec @@ -113,15 +114,20 @@ def test_conf(replicas: Any, scaling: Optional[Any] = None): conf["scaling"] = scaling return conf - assert parse_run_configuration(test_conf(1)).replicas == Range(min=1, max=1) - assert parse_run_configuration(test_conf("2")).replicas == Range(min=2, max=2) - assert parse_run_configuration(test_conf("3..3")).replicas == Range(min=3, max=3) + def parse_service(conf: dict) -> ServiceConfiguration: + parsed = parse_run_configuration(conf) + assert isinstance(parsed, ServiceConfiguration) + return parsed + + assert parse_service(test_conf(1)).replicas == Range(min=1, max=1) + assert parse_service(test_conf("2")).replicas == Range(min=2, max=2) + assert parse_service(test_conf("3..3")).replicas == Range(min=3, max=3) with pytest.raises( ConfigurationError, match="When you set `replicas` to a range, ensure to specify `scaling`", ): parse_run_configuration(test_conf("0..10")) - assert parse_run_configuration( + assert parse_service( test_conf( "0..10", { @@ -957,6 +963,7 @@ def test_rejects_nodes_with_groups(self): def test_omitted_nodes_defaults_to_one_node_group(self): parsed = parse_run_configuration({"type": "task", "commands": ["true"]}) + assert isinstance(parsed, TaskConfiguration) assert parsed.nodes is None assert parsed.nodes_num == 1 assert parsed.node_groups[0].nodes == 1 @@ -1090,6 +1097,7 @@ def test_accepts_top_level_resources_with_groups(self): "groups": [{"nodes": 1, "commands": ["true"]}], } ) + assert isinstance(parsed, TaskConfiguration) assert parsed.groups is not None assert parsed.groups[0].resources == ResourcesSpec() assert parsed.resources.gpu is not None From 23e87a0bfbc7095d6e63ac21473715bf38f79e47 Mon Sep 17 00:00:00 2001 From: Victor Skvortsov Date: Tue, 27 Oct 2026 16:15:56 +0500 Subject: [PATCH 4/8] Type compatibility excludes as keyed by field names --- .../_internal/core/compatibility/backends.py | 6 ++--- .../_internal/core/compatibility/events.py | 6 ++--- .../_internal/core/compatibility/exports.py | 10 +++---- .../_internal/core/compatibility/fleets.py | 16 ++++++------ .../_internal/core/compatibility/gateways.py | 26 ++++++++++--------- .../_internal/core/compatibility/gpus.py | 6 ++--- .../_internal/core/compatibility/logs.py | 6 ++--- .../_internal/core/compatibility/runs.py | 26 +++++++++---------- .../_internal/core/compatibility/volumes.py | 14 +++++----- src/dstack/_internal/core/models/common.py | 7 +++-- src/tests/_internal/core/models/test_runs.py | 8 +++--- 11 files changed, 69 insertions(+), 62 deletions(-) diff --git a/src/dstack/_internal/core/compatibility/backends.py b/src/dstack/_internal/core/compatibility/backends.py index b742e3c155..97ef46c386 100644 --- a/src/dstack/_internal/core/compatibility/backends.py +++ b/src/dstack/_internal/core/compatibility/backends.py @@ -1,16 +1,16 @@ from dstack._internal.core.backends.gcp.models import GCPBackendConfigWithCreds from dstack._internal.core.backends.hotaisle.models import HotAisleBackendConfigWithCreds from dstack._internal.core.backends.models import AnyBackendConfigWithCreds -from dstack._internal.core.models.common import IncludeExcludeDictType +from dstack._internal.core.models.common import IncludeExcludeStrDictType -def get_backend_config_excludes(config: AnyBackendConfigWithCreds) -> IncludeExcludeDictType: +def get_backend_config_excludes(config: AnyBackendConfigWithCreds) -> IncludeExcludeStrDictType: """ Returns `config` exclude mapping to exclude certain fields from the create/update backend request. Use this method to exclude new fields when they are not set to keep clients backward-compatibility with older servers. """ - excludes: IncludeExcludeDictType = {} + excludes: IncludeExcludeStrDictType = {} if isinstance(config, GCPBackendConfigWithCreds) and config.tpu is None: excludes["tpu"] = True if isinstance(config, HotAisleBackendConfigWithCreds) and config.bare_metal is None: diff --git a/src/dstack/_internal/core/compatibility/events.py b/src/dstack/_internal/core/compatibility/events.py index 5c0be978ee..22d72026bd 100644 --- a/src/dstack/_internal/core/compatibility/events.py +++ b/src/dstack/_internal/core/compatibility/events.py @@ -1,9 +1,9 @@ -from dstack._internal.core.models.common import IncludeExcludeDictType +from dstack._internal.core.models.common import IncludeExcludeStrDictType from dstack._internal.server.schemas.events import ListEventsRequest -def get_list_events_excludes(request: ListEventsRequest) -> IncludeExcludeDictType: - list_events_excludes: IncludeExcludeDictType = {} +def get_list_events_excludes(request: ListEventsRequest) -> IncludeExcludeStrDictType: + list_events_excludes: IncludeExcludeStrDictType = {} if request.target_presets is None: list_events_excludes["target_presets"] = True if request.target_gateway_replicas is None: diff --git a/src/dstack/_internal/core/compatibility/exports.py b/src/dstack/_internal/core/compatibility/exports.py index cf6e665947..945eb2d9d9 100644 --- a/src/dstack/_internal/core/compatibility/exports.py +++ b/src/dstack/_internal/core/compatibility/exports.py @@ -1,12 +1,12 @@ -from dstack._internal.core.models.common import IncludeExcludeDictType +from dstack._internal.core.models.common import IncludeExcludeStrDictType from dstack._internal.server.schemas.exports import CreateExportRequest, UpdateExportRequest -def get_create_export_excludes(request: CreateExportRequest) -> IncludeExcludeDictType: - excludes: IncludeExcludeDictType = {} +def get_create_export_excludes(request: CreateExportRequest) -> IncludeExcludeStrDictType: + excludes: IncludeExcludeStrDictType = {} return excludes -def get_update_export_excludes(request: UpdateExportRequest) -> IncludeExcludeDictType: - excludes: IncludeExcludeDictType = {} +def get_update_export_excludes(request: UpdateExportRequest) -> IncludeExcludeStrDictType: + excludes: IncludeExcludeStrDictType = {} return excludes diff --git a/src/dstack/_internal/core/compatibility/fleets.py b/src/dstack/_internal/core/compatibility/fleets.py index ffa6592b92..3c7e61792a 100644 --- a/src/dstack/_internal/core/compatibility/fleets.py +++ b/src/dstack/_internal/core/compatibility/fleets.py @@ -1,26 +1,26 @@ from typing import Optional from dstack._internal.core.compatibility.common import get_profile_excludes -from dstack._internal.core.models.common import IncludeExcludeDictType +from dstack._internal.core.models.common import IncludeExcludeStrDictType from dstack._internal.core.models.fleets import ApplyFleetPlanInput, FleetSpec -def get_get_plan_excludes(fleet_spec: FleetSpec) -> IncludeExcludeDictType: - get_plan_excludes: IncludeExcludeDictType = {} +def get_get_plan_excludes(fleet_spec: FleetSpec) -> IncludeExcludeStrDictType: + get_plan_excludes: IncludeExcludeStrDictType = {} spec_excludes = get_fleet_spec_excludes(fleet_spec) if spec_excludes: get_plan_excludes["spec"] = spec_excludes return get_plan_excludes -def get_apply_plan_excludes(plan_input: ApplyFleetPlanInput) -> IncludeExcludeDictType: - apply_plan_excludes: IncludeExcludeDictType = {} +def get_apply_plan_excludes(plan_input: ApplyFleetPlanInput) -> IncludeExcludeStrDictType: + apply_plan_excludes: IncludeExcludeStrDictType = {} spec_excludes = get_fleet_spec_excludes(plan_input.spec) if spec_excludes: apply_plan_excludes["spec"] = spec_excludes current_resource = plan_input.current_resource if current_resource is not None: - current_resource_excludes: IncludeExcludeDictType = {} + current_resource_excludes: IncludeExcludeStrDictType = {} current_resource_spec_excludes = get_fleet_spec_excludes(current_resource.spec) if current_resource_spec_excludes: current_resource_excludes["spec"] = current_resource_spec_excludes @@ -33,13 +33,13 @@ def get_apply_plan_excludes(plan_input: ApplyFleetPlanInput) -> IncludeExcludeDi return {"plan": apply_plan_excludes} -def get_fleet_spec_excludes(fleet_spec: FleetSpec) -> Optional[IncludeExcludeDictType]: +def get_fleet_spec_excludes(fleet_spec: FleetSpec) -> Optional[IncludeExcludeStrDictType]: """ Returns `fleet_spec` exclude mapping to exclude certain fields from the request. Use this method to exclude new fields when they are not set to keep clients backward-compatibility with older servers. """ - spec_excludes: IncludeExcludeDictType = {} + spec_excludes: IncludeExcludeStrDictType = {} profile_excludes = get_profile_excludes(fleet_spec.profile) spec_excludes["autocreated"] = True diff --git a/src/dstack/_internal/core/compatibility/gateways.py b/src/dstack/_internal/core/compatibility/gateways.py index 6df9d16331..63798b4f79 100644 --- a/src/dstack/_internal/core/compatibility/gateways.py +++ b/src/dstack/_internal/core/compatibility/gateways.py @@ -1,4 +1,4 @@ -from dstack._internal.core.models.common import IncludeExcludeDictType +from dstack._internal.core.models.common import IncludeExcludeStrDictType from dstack._internal.core.models.gateways import ( ApplyGatewayPlanInput, GatewayConfiguration, @@ -10,12 +10,12 @@ ) -def get_get_plan_excludes(body: GetGatewayPlanRequest) -> IncludeExcludeDictType: +def get_get_plan_excludes(body: GetGatewayPlanRequest) -> IncludeExcludeStrDictType: return {"spec": get_gateway_spec_excludes(body.spec)} -def get_apply_plan_excludes(plan_input: ApplyGatewayPlanInput) -> IncludeExcludeDictType: - apply_plan_excludes: IncludeExcludeDictType = { +def get_apply_plan_excludes(plan_input: ApplyGatewayPlanInput) -> IncludeExcludeStrDictType: + apply_plan_excludes: IncludeExcludeStrDictType = { "spec": get_gateway_spec_excludes(plan_input.spec) } if plan_input.current_resource is not None: @@ -31,39 +31,41 @@ def get_apply_plan_excludes(plan_input: ApplyGatewayPlanInput) -> IncludeExclude return {"plan": apply_plan_excludes} -def get_gateway_spec_excludes(gateway_spec: GatewaySpec) -> IncludeExcludeDictType: +def get_gateway_spec_excludes(gateway_spec: GatewaySpec) -> IncludeExcludeStrDictType: """ Returns `gateway_spec` exclude mapping to exclude certain fields from the request. Use this method to exclude new fields when they are not set to keep clients backward-compatibility with older servers. """ - spec_excludes: IncludeExcludeDictType = {} + spec_excludes: IncludeExcludeStrDictType = {} spec_excludes["configuration"] = _get_gateway_configuration_excludes( gateway_spec.configuration ) return spec_excludes -def get_create_gateway_excludes(configuration: GatewayConfiguration) -> IncludeExcludeDictType: +def get_create_gateway_excludes(configuration: GatewayConfiguration) -> IncludeExcludeStrDictType: """ Returns an exclude mapping to exclude certain fields from the create gateway request. Use this method to exclude new fields when they are not set to keep clients backward-compatibility with older servers. """ - create_gateway_excludes: IncludeExcludeDictType = {} + create_gateway_excludes: IncludeExcludeStrDictType = {} create_gateway_excludes["configuration"] = _get_gateway_configuration_excludes(configuration) return create_gateway_excludes -def get_set_default_gateway_excludes(request: SetDefaultGatewayRequest) -> IncludeExcludeDictType: - excludes: IncludeExcludeDictType = {} +def get_set_default_gateway_excludes( + request: SetDefaultGatewayRequest, +) -> IncludeExcludeStrDictType: + excludes: IncludeExcludeStrDictType = {} return excludes def _get_gateway_configuration_excludes( configuration: GatewayConfiguration, -) -> IncludeExcludeDictType: - configuration_excludes: IncludeExcludeDictType = {} +) -> IncludeExcludeStrDictType: + configuration_excludes: IncludeExcludeStrDictType = {} if configuration.default is None: configuration_excludes["default"] = True diff --git a/src/dstack/_internal/core/compatibility/gpus.py b/src/dstack/_internal/core/compatibility/gpus.py index 998259e62a..9f21567e19 100644 --- a/src/dstack/_internal/core/compatibility/gpus.py +++ b/src/dstack/_internal/core/compatibility/gpus.py @@ -1,12 +1,12 @@ from typing import Optional from dstack._internal.core.compatibility.runs import get_run_spec_excludes -from dstack._internal.core.models.common import IncludeExcludeDictType +from dstack._internal.core.models.common import IncludeExcludeStrDictType from dstack._internal.server.schemas.gpus import ListGpusRequest -def get_list_gpus_excludes(request: ListGpusRequest) -> Optional[IncludeExcludeDictType]: - list_gpus_excludes: IncludeExcludeDictType = {} if request.reverse else {"reverse": True} +def get_list_gpus_excludes(request: ListGpusRequest) -> Optional[IncludeExcludeStrDictType]: + list_gpus_excludes: IncludeExcludeStrDictType = {} if request.reverse else {"reverse": True} run_spec_excludes = get_run_spec_excludes(request.run_spec) if run_spec_excludes is not None: list_gpus_excludes["run_spec"] = run_spec_excludes diff --git a/src/dstack/_internal/core/compatibility/logs.py b/src/dstack/_internal/core/compatibility/logs.py index 078ce62218..0576a2c5f5 100644 --- a/src/dstack/_internal/core/compatibility/logs.py +++ b/src/dstack/_internal/core/compatibility/logs.py @@ -1,14 +1,14 @@ from typing import Optional -from dstack._internal.core.models.common import IncludeExcludeDictType +from dstack._internal.core.models.common import IncludeExcludeStrDictType from dstack._internal.server.schemas.logs import PollLogsRequest -def get_poll_logs_excludes(request: PollLogsRequest) -> Optional[IncludeExcludeDictType]: +def get_poll_logs_excludes(request: PollLogsRequest) -> Optional[IncludeExcludeStrDictType]: """ Returns exclude mapping to exclude certain fields from the request. Use this method to exclude new fields when they are not set to keep clients backward-compatibility with older servers. """ - excludes: IncludeExcludeDictType = {} + excludes: IncludeExcludeStrDictType = {} return excludes if excludes else None diff --git a/src/dstack/_internal/core/compatibility/runs.py b/src/dstack/_internal/core/compatibility/runs.py index 671522b658..a6d1dfdcbe 100644 --- a/src/dstack/_internal/core/compatibility/runs.py +++ b/src/dstack/_internal/core/compatibility/runs.py @@ -2,8 +2,8 @@ from dstack._internal.core.compatibility.common import get_profile_excludes from dstack._internal.core.models.common import ( - IncludeExcludeDictType, IncludeExcludeSetType, + IncludeExcludeStrDictType, ) from dstack._internal.core.models.configurations import ( ServiceConfiguration, @@ -24,19 +24,19 @@ def get_list_runs_excludes(list_runs_request: ListRunsRequest) -> IncludeExclude return excludes -def get_apply_plan_excludes(plan: ApplyRunPlanInput) -> Optional[IncludeExcludeDictType]: +def get_apply_plan_excludes(plan: ApplyRunPlanInput) -> Optional[IncludeExcludeStrDictType]: """ Returns `plan` exclude mapping to exclude certain fields from the request. Use this method to exclude new fields when they are not set to keep clients backward-compatibility with older servers. """ - apply_plan_excludes: IncludeExcludeDictType = {} + apply_plan_excludes: IncludeExcludeStrDictType = {} run_spec_excludes = get_run_spec_excludes(plan.run_spec) if run_spec_excludes is not None: apply_plan_excludes["run_spec"] = run_spec_excludes current_resource = plan.current_resource if current_resource is not None: - current_resource_excludes: IncludeExcludeDictType = {} + current_resource_excludes: IncludeExcludeStrDictType = {} apply_plan_excludes["current_resource"] = current_resource_excludes current_resource_excludes["run_spec"] = get_run_spec_excludes(current_resource.run_spec) current_resource_excludes["jobs"] = { @@ -62,12 +62,12 @@ def get_apply_plan_excludes(plan: ApplyRunPlanInput) -> Optional[IncludeExcludeD return {"plan": apply_plan_excludes} -def get_get_plan_excludes(request: GetRunPlanRequest) -> Optional[IncludeExcludeDictType]: +def get_get_plan_excludes(request: GetRunPlanRequest) -> Optional[IncludeExcludeStrDictType]: """ Excludes new fields when they are not set to keep clients backward-compatibility with older servers. """ - get_plan_excludes: IncludeExcludeDictType = ( + get_plan_excludes: IncludeExcludeStrDictType = ( {} if request.reverse_offers else {"reverse_offers": True} ) run_spec_excludes = get_run_spec_excludes(request.run_spec) @@ -76,14 +76,14 @@ def get_get_plan_excludes(request: GetRunPlanRequest) -> Optional[IncludeExclude return get_plan_excludes -def get_run_spec_excludes(run_spec: RunSpec) -> IncludeExcludeDictType: +def get_run_spec_excludes(run_spec: RunSpec) -> IncludeExcludeStrDictType: """ Returns `run_spec` exclude mapping to exclude certain fields from the request. Use this method to exclude new fields when they are not set to keep clients backward-compatibility with older servers. """ - spec_excludes: IncludeExcludeDictType = {} - configuration_excludes: IncludeExcludeDictType = {} + spec_excludes: IncludeExcludeStrDictType = {} + configuration_excludes: IncludeExcludeStrDictType = {} profile_excludes = get_profile_excludes(run_spec.profile) for field in get_profile_excludes(run_spec.configuration): configuration_excludes[field] = True @@ -106,13 +106,13 @@ def get_run_spec_excludes(run_spec: RunSpec) -> IncludeExcludeDictType: return spec_excludes -def get_job_spec_excludes(job_specs: list[JobSpec]) -> IncludeExcludeDictType: +def get_job_spec_excludes(job_specs: list[JobSpec]) -> IncludeExcludeStrDictType: """ Returns `job_spec` exclude mapping to exclude certain fields from the request. Use this method to exclude new fields when they are not set to keep clients backward-compatibility with older servers. """ - spec_excludes: IncludeExcludeDictType = {} + spec_excludes: IncludeExcludeStrDictType = {} if all(s.node_group_index == 0 for s in job_specs): spec_excludes["node_group_index"] = True if all(s.node_group_name == DEFAULT_REPLICA_GROUP_NAME for s in job_specs): @@ -122,8 +122,8 @@ def get_job_spec_excludes(job_specs: list[JobSpec]) -> IncludeExcludeDictType: return spec_excludes -def get_job_submission_excludes(job_submissions: list[JobSubmission]) -> IncludeExcludeDictType: - submission_excludes: IncludeExcludeDictType = {} +def get_job_submission_excludes(job_submissions: list[JobSubmission]) -> IncludeExcludeStrDictType: + submission_excludes: IncludeExcludeStrDictType = {} # `Resources.description` is deprecated and never set since 0.21. Not sending it lets 0.22 # drop the field without breaking 0.21 clients. submission_excludes["job_provisioning_data"] = { diff --git a/src/dstack/_internal/core/compatibility/volumes.py b/src/dstack/_internal/core/compatibility/volumes.py index 63d530c4ad..1f5e1e3ab4 100644 --- a/src/dstack/_internal/core/compatibility/volumes.py +++ b/src/dstack/_internal/core/compatibility/volumes.py @@ -1,34 +1,34 @@ -from dstack._internal.core.models.common import IncludeExcludeDictType +from dstack._internal.core.models.common import IncludeExcludeStrDictType from dstack._internal.core.models.volumes import ( AnyVolumeConfiguration, VolumeSpec, ) -def get_volume_spec_excludes(volume_spec: VolumeSpec) -> IncludeExcludeDictType: +def get_volume_spec_excludes(volume_spec: VolumeSpec) -> IncludeExcludeStrDictType: """ Returns `volume_spec` exclude mapping to exclude certain fields from the request. Use this method to exclude new fields when they are not set to keep clients backward-compatibility with older servers. """ - spec_excludes: IncludeExcludeDictType = {} + spec_excludes: IncludeExcludeStrDictType = {} spec_excludes["configuration"] = _get_volume_configuration_excludes(volume_spec.configuration) return spec_excludes -def get_create_volume_excludes(configuration: AnyVolumeConfiguration) -> IncludeExcludeDictType: +def get_create_volume_excludes(configuration: AnyVolumeConfiguration) -> IncludeExcludeStrDictType: """ Returns an exclude mapping to exclude certain fields from the create volume request. Use this method to exclude new fields when they are not set to keep clients backward-compatibility with older servers. """ - create_volume_excludes: IncludeExcludeDictType = {} + create_volume_excludes: IncludeExcludeStrDictType = {} create_volume_excludes["configuration"] = _get_volume_configuration_excludes(configuration) return create_volume_excludes def _get_volume_configuration_excludes( configuration: AnyVolumeConfiguration, -) -> IncludeExcludeDictType: - configuration_excludes: IncludeExcludeDictType = {} +) -> IncludeExcludeStrDictType: + configuration_excludes: IncludeExcludeStrDictType = {} return configuration_excludes diff --git a/src/dstack/_internal/core/models/common.py b/src/dstack/_internal/core/models/common.py index a649420edb..d0142a1b54 100644 --- a/src/dstack/_internal/core/models/common.py +++ b/src/dstack/_internal/core/models/common.py @@ -29,10 +29,13 @@ def drop_merged_profile(schema: dict[str, Any]) -> None: IncludeExcludeFieldType = Union[int, str] IncludeExcludeSetType = Union[set[int], set[str]] # `dict` rather than `Mapping`, so these stay assignable both *to* pydantic's `IncEx` and to the -# plain `Dict` parameters the plugin API declares. Keyed by int or str but not both, like `IncEx`. +# plain `Dict` parameters the plugin API declares. +# Keyed by field names, as include/exclude mappings for models are. +IncludeExcludeStrDictType = dict[str, Union["IncludeExcludeType", bool]] +# Keyed by int or str but not both, like `IncEx`. IncludeExcludeDictType = Union[ dict[int, Union["IncludeExcludeType", bool]], - dict[str, Union["IncludeExcludeType", bool]], + IncludeExcludeStrDictType, ] IncludeExcludeType = Union[IncludeExcludeSetType, IncludeExcludeDictType] diff --git a/src/tests/_internal/core/models/test_runs.py b/src/tests/_internal/core/models/test_runs.py index 20b5558b2c..a617ff1bd2 100644 --- a/src/tests/_internal/core/models/test_runs.py +++ b/src/tests/_internal/core/models/test_runs.py @@ -1,8 +1,10 @@ +from typing import cast + import pytest from pydantic import ValidationError from dstack._internal.core.compatibility.runs import get_run_spec_excludes -from dstack._internal.core.models.common import validate_extra_ignore +from dstack._internal.core.models.common import IncludeExcludeStrDictType, validate_extra_ignore from dstack._internal.core.models.configurations import ( ServiceConfiguration, TaskConfiguration, @@ -42,7 +44,7 @@ def test_unset_task_nodes_are_excluded_for_compatibility(): ) assert isinstance(configuration_excludes, dict) - assert configuration_excludes["nodes"] is True + assert cast(IncludeExcludeStrDictType, configuration_excludes)["nodes"] is True def test_unset_service_groups_are_excluded_for_compatibility(): @@ -53,7 +55,7 @@ def test_unset_service_groups_are_excluded_for_compatibility(): ) assert isinstance(configuration_excludes, dict) - assert configuration_excludes["groups"] is True + assert cast(IncludeExcludeStrDictType, configuration_excludes)["groups"] is True def test_set_service_groups_are_not_excluded(): From ef3b67497a42c32a2b919a7866faa3283bea3bc6 Mon Sep 17 00:00:00 2001 From: Victor Skvortsov Date: Tue, 27 Oct 2026 16:17:55 +0500 Subject: [PATCH 5/8] Fix remaining pyright errors in core tests --- .../core/backends/cloudrift/test_api_client.py | 1 + .../_internal/core/backends/daytona/test_compute.py | 1 + src/tests/_internal/core/backends/gcp/test_compute.py | 2 +- .../_internal/core/backends/nebius/test_compute.py | 3 ++- .../_internal/core/backends/seeweb/test_api_client.py | 8 ++++++-- .../_internal/core/backends/seeweb/test_compute.py | 11 ++++++++--- src/tests/_internal/core/models/test_duration.py | 4 +++- 7 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/tests/_internal/core/backends/cloudrift/test_api_client.py b/src/tests/_internal/core/backends/cloudrift/test_api_client.py index 0452bd98d7..0914b475c5 100644 --- a/src/tests/_internal/core/backends/cloudrift/test_api_client.py +++ b/src/tests/_internal/core/backends/cloudrift/test_api_client.py @@ -75,6 +75,7 @@ def test_preserves_http_errors(self, requests_mock): with pytest.raises(requests.HTTPError) as exc_info: RiftClient("test-key").list_recipes() + assert exc_info.value.response is not None assert exc_info.value.response.text == "unsupported version" diff --git a/src/tests/_internal/core/backends/daytona/test_compute.py b/src/tests/_internal/core/backends/daytona/test_compute.py index b4d6566878..f68989ae0b 100644 --- a/src/tests/_internal/core/backends/daytona/test_compute.py +++ b/src/tests/_internal/core/backends/daytona/test_compute.py @@ -368,6 +368,7 @@ def test_registry_cleanup_survives_provisioning_and_server_restart(self, compute data = _run_job(compute, registry_auth=RegistryAuth(username="user", password="password")) _update(compute, data) data = JobProvisioningData.model_validate_json(data.model_dump_json()) + assert data.backend_data is not None assert json.loads(data.backend_data) == { "startup_command": None, "registry_id": "registry-id", diff --git a/src/tests/_internal/core/backends/gcp/test_compute.py b/src/tests/_internal/core/backends/gcp/test_compute.py index 041b368cfc..341df09cc6 100644 --- a/src/tests/_internal/core/backends/gcp/test_compute.py +++ b/src/tests/_internal/core/backends/gcp/test_compute.py @@ -116,8 +116,8 @@ def _get_tpu_node( accelerator_type="v5litepod-8", state=state, labels={"owner": "dstack", "dstack_provider_ref": PROVIDER_REF}, + create_time=CREATED_AT, ) - node.create_time = CREATED_AT return node diff --git a/src/tests/_internal/core/backends/nebius/test_compute.py b/src/tests/_internal/core/backends/nebius/test_compute.py index 7abaffe435..d111917c31 100644 --- a/src/tests/_internal/core/backends/nebius/test_compute.py +++ b/src/tests/_internal/core/backends/nebius/test_compute.py @@ -4,7 +4,8 @@ from unittest.mock import Mock import pytest -from nebius.aio.service_error import RequestError, RequestStatusExtended, StatusCode +from grpc import StatusCode +from nebius.aio.service_error import RequestError, RequestStatusExtended from nebius.api.nebius.billing.v1 import PricingPolicy, PricingPolicyStatus from nebius.api.nebius.common.v1 import ResourceMetadata from nebius.api.nebius.compute.v1 import ( diff --git a/src/tests/_internal/core/backends/seeweb/test_api_client.py b/src/tests/_internal/core/backends/seeweb/test_api_client.py index 80cf38ab2c..4ada113dcc 100644 --- a/src/tests/_internal/core/backends/seeweb/test_api_client.py +++ b/src/tests/_internal/core/backends/seeweb/test_api_client.py @@ -167,8 +167,12 @@ def test_get_action_and_server(requests_mock): ) client = SeewebApiClient("token") - assert client.get_action(35)["status"] == "completed" - assert client.get_server("ec-test")["ipv4"] == "192.0.2.1" + action = client.get_action(35) + assert action is not None + assert action["status"] == "completed" + server = client.get_server("ec-test") + assert server is not None + assert server["ipv4"] == "192.0.2.1" def test_delete_server_is_idempotent(requests_mock): diff --git a/src/tests/_internal/core/backends/seeweb/test_compute.py b/src/tests/_internal/core/backends/seeweb/test_compute.py index f675c65985..99a0e523ed 100644 --- a/src/tests/_internal/core/backends/seeweb/test_compute.py +++ b/src/tests/_internal/core/backends/seeweb/test_compute.py @@ -28,7 +28,8 @@ Resources, SSHKey, ) -from dstack._internal.core.models.runs import JobProvisioningData +from dstack._internal.core.models.resources import ResourcesSpec +from dstack._internal.core.models.runs import JobProvisioningData, Requirements def _config(regions=None) -> SeewebConfig: @@ -124,7 +125,9 @@ def test_get_offers_marks_only_creatable_as_available(): ), ): offers = SeewebCompute(_config()).get_offers_by_requirements( - requirements=None, full_offers=False, unallocated_resources=False + requirements=Requirements(resources=ResourcesSpec()), + full_offers=False, + unallocated_resources=False, ) by_name = {offer.instance.name: offer for offer in offers} @@ -150,7 +153,9 @@ def test_get_offers_filters_configured_regions(): ), ): offers = SeewebCompute(_config(["it-mi2"])).get_offers_by_requirements( - requirements=None, full_offers=False, unallocated_resources=False + requirements=Requirements(resources=ResourcesSpec()), + full_offers=False, + unallocated_resources=False, ) assert [offer.region for offer in offers] == ["it-mi2"] diff --git a/src/tests/_internal/core/models/test_duration.py b/src/tests/_internal/core/models/test_duration.py index dc74d4255f..0d88b8fb39 100644 --- a/src/tests/_internal/core/models/test_duration.py +++ b/src/tests/_internal/core/models/test_duration.py @@ -101,4 +101,6 @@ def test_accepts_negative_for_backward_compatibility(self, raw: Any): `-1` is how older clients and existing stored rows spell "off", so negatives have to keep parsing rather than being rejected the way `OptionalOffableDuration` rejects them. """ - assert TypeAdapter(OptionalIdleDuration).validate_python(raw) < 0 + duration = TypeAdapter(OptionalIdleDuration).validate_python(raw) + assert duration is not None + assert duration < 0 From fbc0aa7fc7e5104a6127ef7bd4a6d17ae1597199 Mon Sep 17 00:00:00 2001 From: Victor Skvortsov Date: Tue, 27 Oct 2026 16:20:13 +0500 Subject: [PATCH 6/8] Check core tests with pyright --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 0b72fa2323..fc7ced77cc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -112,6 +112,7 @@ include = [ "src/dstack/_internal/core/backends", "src/dstack/_internal/cli/services/configurators", "src/dstack/_internal/cli/commands", + "src/tests/_internal/core", "src/tests/_internal/server/background/pipeline_tasks", ] exclude = [ From d9d3c29d363f6a88619e46f570af4ec15a60b9be Mon Sep 17 00:00:00 2001 From: Victor Skvortsov Date: Wed, 28 Oct 2026 11:10:09 +0500 Subject: [PATCH 7/8] Address review comments on core test typing --- .../core/backends/nebius/test_compute.py | 46 +++++++---- .../core/models/test_configurations.py | 82 ++++++++++--------- 2 files changed, 71 insertions(+), 57 deletions(-) diff --git a/src/tests/_internal/core/backends/nebius/test_compute.py b/src/tests/_internal/core/backends/nebius/test_compute.py index d111917c31..ab1441434a 100644 --- a/src/tests/_internal/core/backends/nebius/test_compute.py +++ b/src/tests/_internal/core/backends/nebius/test_compute.py @@ -1,6 +1,7 @@ import json from datetime import datetime, timezone -from typing import Optional +from types import SimpleNamespace +from typing import Callable, Optional from unittest.mock import Mock import pytest @@ -243,13 +244,19 @@ def mock( disks: Optional[list[Disk]] = None, pricing_policies: Optional[list[PricingPolicy]] = None, instances: Optional[list[Instance]] = None, + instances_side_effect: Optional[Callable[..., list[Instance]]] = None, ) -> None: resources = compute_module.resources mocker.patch.object(resources, "list_disks", return_value=disks or []) mocker.patch.object( resources, "list_pricing_policies", return_value=pricing_policies or [] ) - mocker.patch.object(resources, "list_instances", return_value=instances or []) + mocker.patch.object( + resources, + "list_instances", + return_value=instances or [], + side_effect=instances_side_effect, + ) return mock @@ -391,15 +398,17 @@ def test_reports_failed_region(self, mocker, mock_listings): "_region_to_project_id", {"eu-north1": "project-1", "us-central1": "project-2"}, ) - name = make_name() - mock_listings(disks=[make_disk(name=name, labels=make_labels(name))]) def list_instances(sdk, project_id: str) -> list[Instance]: if project_id == "project-2": raise make_request_error(StatusCode.UNAVAILABLE) return [] - mocker.patch.object(compute_module.resources, "list_instances", side_effect=list_instances) + name = make_name() + mock_listings( + disks=[make_disk(name=name, labels=make_labels(name))], + instances_side_effect=list_instances, + ) listing = make_compute().list_resource_groups() @@ -414,19 +423,20 @@ def _mock_sdk(self, mocker): @pytest.fixture def resources(self, mocker): - resources = compute_module.resources - mocks = { - name: mocker.patch.object(resources, name) - for name in [ - "get_instance", - "delete_instance", - "delete_disk", - "get_pricing_policy", - "delete_pricing_policy", - ] - } - mocks["get_instance"].side_effect = make_request_error(StatusCode.NOT_FOUND) - mocks["get_pricing_policy"].return_value = make_pricing_policy() + resources = SimpleNamespace( + **{ + name: mocker.patch.object(compute_module.resources, name) + for name in [ + "get_instance", + "delete_instance", + "delete_disk", + "get_pricing_policy", + "delete_pricing_policy", + ] + } + ) + resources.get_instance.side_effect = make_request_error(StatusCode.NOT_FOUND) + resources.get_pricing_policy.return_value = make_pricing_policy() return resources def test_deletes_instance_first(self, resources): diff --git a/src/tests/_internal/core/models/test_configurations.py b/src/tests/_internal/core/models/test_configurations.py index 9677861326..a77656c43d 100644 --- a/src/tests/_internal/core/models/test_configurations.py +++ b/src/tests/_internal/core/models/test_configurations.py @@ -1,5 +1,5 @@ from copy import deepcopy -from typing import Any, Optional +from typing import Any, Optional, TypeVar import pytest from pydantic import ValidationError @@ -71,8 +71,7 @@ def test_service_model_probes_none_when_omitted(self): "port": 8000, "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", } - parsed = parse_run_configuration(conf) - assert isinstance(parsed, ServiceConfiguration) + parsed = _parse_run_configuration_as(conf, ServiceConfiguration) assert parsed.probes is None def test_service_model_does_not_override_explicit_probes(self): @@ -83,8 +82,7 @@ def test_service_model_does_not_override_explicit_probes(self): "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "probes": [{"type": "http", "url": "/health"}], } - parsed = parse_run_configuration(conf) - assert isinstance(parsed, ServiceConfiguration) + parsed = _parse_run_configuration_as(conf, ServiceConfiguration) assert parsed.probes is not None assert len(parsed.probes) == 1 assert parsed.probes[0].url == "/health" @@ -97,8 +95,7 @@ def test_service_model_explicit_empty_probes_no_default(self): "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "probes": [], } - parsed = parse_run_configuration(conf) - assert isinstance(parsed, ServiceConfiguration) + parsed = _parse_run_configuration_as(conf, ServiceConfiguration) assert parsed.probes is not None assert len(parsed.probes) == 0 @@ -114,28 +111,28 @@ def test_conf(replicas: Any, scaling: Optional[Any] = None): conf["scaling"] = scaling return conf - def parse_service(conf: dict) -> ServiceConfiguration: - parsed = parse_run_configuration(conf) - assert isinstance(parsed, ServiceConfiguration) - return parsed - - assert parse_service(test_conf(1)).replicas == Range(min=1, max=1) - assert parse_service(test_conf("2")).replicas == Range(min=2, max=2) - assert parse_service(test_conf("3..3")).replicas == Range(min=3, max=3) + parsed = _parse_run_configuration_as(test_conf(1), ServiceConfiguration) + assert parsed.replicas == Range(min=1, max=1) + parsed = _parse_run_configuration_as(test_conf("2"), ServiceConfiguration) + assert parsed.replicas == Range(min=2, max=2) + parsed = _parse_run_configuration_as(test_conf("3..3"), ServiceConfiguration) + assert parsed.replicas == Range(min=3, max=3) with pytest.raises( ConfigurationError, match="When you set `replicas` to a range, ensure to specify `scaling`", ): parse_run_configuration(test_conf("0..10")) - assert parse_service( + parsed = _parse_run_configuration_as( test_conf( "0..10", { "metric": "rps", "target": 10, }, - ) - ).replicas == Range(min=0, max=10) + ), + ServiceConfiguration, + ) + assert parsed.replicas == Range(min=0, max=10) # `metric: rpc` is a typo, so `scaling` itself fails to validate. The config stays # rejected, but the message is no longer the `scaling`-is-missing one: pydantic v1 ran a # bare `root_validator` even after a field had failed, handing it a partial `values` dict @@ -166,8 +163,7 @@ def test_replica_group_router(self): {"name": "worker", "count": 2, "commands": ["worker"]}, ], } - parsed = parse_run_configuration(conf) - assert isinstance(parsed, ServiceConfiguration) + parsed = _parse_run_configuration_as(conf, ServiceConfiguration) assert parsed.groups is not None router_g = next(g for g in parsed.groups if g.name == "router") assert isinstance(router_g.router, ReplicaGroupRouterConfig) @@ -251,8 +247,7 @@ def test_replica_group_accepts_image_python_nvcc_docker(self): {"name": "d", "count": 1, "docker": True, "commands": ["x"]}, ], } - parsed = parse_run_configuration(conf) - assert isinstance(parsed, ServiceConfiguration) + parsed = _parse_run_configuration_as(conf, ServiceConfiguration) assert parsed.groups is not None groups = {g.name: g for g in parsed.groups} assert groups["a"].image == "nginx:latest" @@ -274,8 +269,7 @@ def test_replica_group_accepts_privileged(self): }, ], } - parsed = parse_run_configuration(conf) - assert isinstance(parsed, ServiceConfiguration) + parsed = _parse_run_configuration_as(conf, ServiceConfiguration) assert parsed.groups is not None assert parsed.groups[0].privileged is True @@ -295,8 +289,7 @@ def test_replica_group_python_yaml_coercion(self, yaml_value, expected): "port": 8000, "replicas": [{"count": 1, "python": yaml_value, "commands": ["x"]}], } - parsed = parse_run_configuration(conf) - assert isinstance(parsed, ServiceConfiguration) + parsed = _parse_run_configuration_as(conf, ServiceConfiguration) assert parsed.groups is not None assert parsed.groups[0].python == expected @@ -369,8 +362,7 @@ def test_replica_group_python_nvcc_allowed_together(self): {"count": 1, "python": "3.12", "nvcc": True, "commands": ["x"]}, ], } - parsed = parse_run_configuration(conf) - assert isinstance(parsed, ServiceConfiguration) + parsed = _parse_run_configuration_as(conf, ServiceConfiguration) assert parsed.groups is not None assert parsed.groups[0].python == PythonVersion.PY312 assert parsed.groups[0].nvcc is True @@ -962,8 +954,9 @@ def test_rejects_nodes_with_groups(self): ) def test_omitted_nodes_defaults_to_one_node_group(self): - parsed = parse_run_configuration({"type": "task", "commands": ["true"]}) - assert isinstance(parsed, TaskConfiguration) + parsed = _parse_run_configuration_as( + {"type": "task", "commands": ["true"]}, TaskConfiguration + ) assert parsed.nodes is None assert parsed.nodes_num == 1 assert parsed.node_groups[0].nodes == 1 @@ -1089,15 +1082,15 @@ def test_rejects_group_without_commands_or_image(self): def test_accepts_top_level_resources_with_groups(self): """Top-level resources is allowed (not rejected) but not used for group jobs.""" - parsed = parse_run_configuration( + parsed = _parse_run_configuration_as( { "type": "task", "image": "debian", "resources": {"gpu": "H100"}, "groups": [{"nodes": 1, "commands": ["true"]}], - } + }, + TaskConfiguration, ) - assert isinstance(parsed, TaskConfiguration) assert parsed.groups is not None assert parsed.groups[0].resources == ResourcesSpec() assert parsed.resources.gpu is not None @@ -1106,14 +1099,14 @@ def test_accepts_top_level_resources_with_groups(self): class TestServiceGroupsPhase1: def test_legacy_replicas_list_parses_to_groups(self): - parsed = parse_run_configuration( + parsed = _parse_run_configuration_as( { "type": "service", "port": 8000, "replicas": [{"count": 1, "commands": ["x"]}], - } + }, + ServiceConfiguration, ) - assert isinstance(parsed, ServiceConfiguration) assert parsed.replicas is None assert parsed.groups is not None assert parsed.groups[0].replicas == Range(min=1, max=1) @@ -1159,14 +1152,14 @@ def test_dump_is_groups_canonical(self): assert "replicas" in dumped_json["groups"][0] def test_dump_validate_is_fixed_point(self): - parsed = parse_run_configuration( + parsed = _parse_run_configuration_as( { "type": "service", "port": 8000, "groups": [{"replicas": 1, "commands": ["x"]}], - } + }, + ServiceConfiguration, ) - assert isinstance(parsed, ServiceConfiguration) once = ServiceConfiguration.model_validate(parsed.model_dump()) twice = ServiceConfiguration.model_validate(once.model_dump()) assert once.model_dump() == twice.model_dump() == parsed.model_dump() @@ -1359,3 +1352,14 @@ def test_schema_does_not_require_model(self): assert "model" not in schema.get("required", []) for field in ("model", "base", "repo"): assert field in schema["properties"] + + +RunConfigurationT = TypeVar("RunConfigurationT") + + +def _parse_run_configuration_as( + data: dict, configuration_type: type[RunConfigurationT] +) -> RunConfigurationT: + configuration = parse_run_configuration(data) + assert isinstance(configuration, configuration_type) + return configuration From feba8a66a0910f44e56af672a69d53cfb3987079 Mon Sep 17 00:00:00 2001 From: Victor Skvortsov Date: Wed, 28 Oct 2026 11:10:10 +0500 Subject: [PATCH 8/8] Ignore POSIX-only SIGKILL in tunnel tests on Windows --- src/tests/_internal/core/services/ssh/test_tunnel.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/tests/_internal/core/services/ssh/test_tunnel.py b/src/tests/_internal/core/services/ssh/test_tunnel.py index 0a3a1f8ce7..4e3099ab6b 100644 --- a/src/tests/_internal/core/services/ssh/test_tunnel.py +++ b/src/tests/_internal/core/services/ssh/test_tunnel.py @@ -370,7 +370,7 @@ async def wait(): ssh_process.kill.assert_called_once_with() kill_process_group.assert_not_called() else: - kill_process_group.assert_called_once_with(ssh_process.pid, signal.SIGKILL) + kill_process_group.assert_called_once_with(ssh_process.pid, signal.SIGKILL) # pyright: ignore[reportAttributeAccessIssue] ssh_process.kill.assert_not_called() assert ssh_process.wait.await_count == 2 assert not Path(foreground_tunnel.control_sock_path).exists() @@ -436,7 +436,7 @@ async def check(): if IS_WINDOWS: ssh_process.kill.assert_called_once_with() else: - kill_process_group.assert_called_once_with(ssh_process.pid, signal.SIGKILL) + kill_process_group.assert_called_once_with(ssh_process.pid, signal.SIGKILL) # pyright: ignore[reportAttributeAccessIssue] ssh_process.wait.assert_awaited_once_with() assert not Path(foreground_tunnel.control_sock_path).exists() with pytest.raises(SSHError, match="No foreground SSH process"): @@ -484,7 +484,7 @@ def kill(): if IS_WINDOWS: ssh_process.kill.assert_called_once_with() else: - kill_process_group.assert_called_once_with(ssh_process.pid, signal.SIGKILL) + kill_process_group.assert_called_once_with(ssh_process.pid, signal.SIGKILL) # pyright: ignore[reportAttributeAccessIssue] ssh_process.wait.assert_awaited_once_with() assert foreground_tunnel._process is None @@ -506,7 +506,7 @@ async def test_timed_out_aopen_cleans_up_exited_process( await foreground_tunnel.aopen() if not IS_WINDOWS: - kill_process_group.assert_called_once_with(ssh_process.pid, signal.SIGKILL) + kill_process_group.assert_called_once_with(ssh_process.pid, signal.SIGKILL) # pyright: ignore[reportAttributeAccessIssue] ssh_process.kill.assert_not_called() else: ssh_process.kill.assert_called_once_with() @@ -546,7 +546,7 @@ async def create_process(*args, **kwargs): ssh_process.kill.assert_called_once_with() kill_process_group.assert_not_called() else: - kill_process_group.assert_called_once_with(ssh_process.pid, signal.SIGKILL) + kill_process_group.assert_called_once_with(ssh_process.pid, signal.SIGKILL) # pyright: ignore[reportAttributeAccessIssue] ssh_process.kill.assert_not_called() ssh_process.wait.assert_awaited_once_with() ssh_process.communicate.assert_not_called() @@ -616,7 +616,7 @@ async def wait(): if IS_WINDOWS: ssh_process.kill.assert_called_once_with() else: - kill_process_group.assert_called_once_with(ssh_process.pid, signal.SIGKILL) + kill_process_group.assert_called_once_with(ssh_process.pid, signal.SIGKILL) # pyright: ignore[reportAttributeAccessIssue] class TestSSHTunnelControlTimeouts: