1- from datetime import datetime , timezone
21from unittest .mock import Mock , patch
32
43import pytest
98 _benchmark_single_table_compute_gcp ,
109 _get_user_data_script ,
1110 _gpu_wait_block ,
12- _make_instance_name ,
1311 _run_on_gcp ,
1412 _terminate_instance ,
1513 _upload_logs ,
@@ -36,21 +34,6 @@ def base_credentials():
3634 }
3735
3836
39- @patch ('sdgym._benchmark.benchmark.uuid.uuid4' )
40- @patch ('sdgym._benchmark.benchmark.datetime' )
41- def test_make_instance_name (mock_datetime , mock_uuid ):
42- """Test `_make_instance_name` generates a stable, readable name."""
43- # Setup
44- mock_datetime .now .return_value = datetime (2025 , 1 , 15 , tzinfo = timezone .utc )
45- mock_uuid .return_value .hex = 'abcdef123456'
46-
47- # Run
48- result = _make_instance_name ('sdgym-run' )
49-
50- # Assert
51- assert result == 'sdgym-run-20250115-abcdef'
52-
53-
5437def test_terminate_instance_aws ():
5538 """AWS termination script self-terminates via EC2 metadata and AWS CLI."""
5639 # Run
@@ -79,16 +62,16 @@ def test_terminate_instance_gcp():
7962 assert 'Metadata-Flavor: Google' not in script
8063
8164
82- def test_terminate_instance_invalid_service ():
65+ def test__terminate_instance_invalid_service ():
8366 """Invalid compute service raises a clear error."""
8467 # Run and Assert
8568 with pytest .raises (ValueError , match = 'Unsupported compute service' ):
8669 _terminate_instance ('azure' )
8770
8871
89- def test_gpu_wait_block_contents ():
72+ def test__gpu_wait_block_contents ():
9073 """GPU wait block waits for nvidia-smi to become available."""
91- # Setup
74+ # Run
9275 block = _gpu_wait_block ()
9376
9477 # Assert
@@ -98,7 +81,7 @@ def test_gpu_wait_block_contents():
9881 assert 'for i in' in block or 'while' in block
9982
10083
101- def test_upload_logs_fn_no_uri ():
84+ def test__upload_logs_fn_no_uri ():
10285 """No log URI returns a no-op upload_logs function."""
10386 # Run
10487 fn = _upload_logs ('' )
@@ -107,7 +90,7 @@ def test_upload_logs_fn_no_uri():
10790 assert fn .strip () == 'upload_logs() { :; }'
10891
10992
110- def test_upload_logs_fn_with_uri ():
93+ def test__upload_logs_fn_with_uri ():
11194 """Upload logs function uploads user-data.log to S3."""
11295 # Setup
11396 uri = 's3://bucket/prefix/logs/instance-user-data.log'
@@ -121,7 +104,7 @@ def test_upload_logs_fn_with_uri():
121104 assert uri in fn
122105
123106
124- def test_get_user_data_script_gcp_gpu_wait (base_credentials ):
107+ def test__get_user_data_script_gcp_gpu_wait (base_credentials ):
125108 """Test GCP user-data script includes GPU wait and delete logic."""
126109 # Setup
127110 config = {
@@ -141,6 +124,7 @@ def test_get_user_data_script_gcp_gpu_wait(base_credentials):
141124 'upload_logs_to_s3' : True ,
142125 }
143126
127+ # Run
144128 script = _get_user_data_script (
145129 credentials = base_credentials ,
146130 script_content = "print('hello')" ,
@@ -159,7 +143,7 @@ def test_get_user_data_script_gcp_gpu_wait(base_credentials):
159143 assert "print('hello')" in script
160144
161145
162- def test_get_user_data_script_aws_termination (base_credentials ):
146+ def test__get_user_data_script_aws_termination (base_credentials ):
163147 """Test AWS user-data script includes EC2 termination logic."""
164148 # Setup
165149 config = {
@@ -173,6 +157,7 @@ def test_get_user_data_script_aws_termination(base_credentials):
173157 'upload_logs_to_s3' : True ,
174158 }
175159
160+ # Run
176161 script = _get_user_data_script (
177162 credentials = base_credentials ,
178163 script_content = "print('aws')" ,
@@ -284,11 +269,44 @@ def test_run_on_gcp(
284269 )
285270 ],
286271 )
287- mock_instances_client .insert .assert_called_once ()
288- mock_compute_v1 .ZoneOperationsClient .assert_called_once ()
289- mock_zone_ops_client .wait .assert_called_once ()
290- mock_compute_v1 .Metadata .assert_called_once ()
291- mock_compute_v1 .Instance .assert_called_once ()
272+ mock_instances_client .insert .assert_called_once_with (
273+ project = 'test-project' ,
274+ zone = 'us-central1-a' ,
275+ instance_resource = mock_compute_v1 .Instance .return_value ,
276+ )
277+ mock_compute_v1 .ZoneOperationsClient .assert_called_once_with (credentials = gcp_cred )
278+ mock_zone_ops_client .wait .assert_called_once_with (
279+ project = 'test-project' ,
280+ zone = 'us-central1-a' ,
281+ operation = mock_instances_client .insert .return_value .name ,
282+ )
283+ mock_compute_v1 .Metadata .assert_called_once_with (
284+ items = [
285+ mock_compute_v1 .Items (
286+ key = 'startup-script' ,
287+ value = 'STARTUP_SCRIPT' ,
288+ ),
289+ mock_compute_v1 .Items (
290+ key = 'enable-oslogin' ,
291+ value = 'TRUE' ,
292+ ),
293+ ]
294+ )
295+ mock_compute_v1 .Instance .assert_called_once_with (
296+ name = 'instance-123' ,
297+ machine_type = 'zones/us-central1-a/machineTypes/n1-standard-4' ,
298+ disks = [boot_disk ],
299+ network_interfaces = [nic ],
300+ metadata = metadata ,
301+ guest_accelerators = [gpu ],
302+ scheduling = scheduling ,
303+ service_accounts = [
304+ mock_compute_v1 .ServiceAccount (
305+ email = 'default' ,
306+ scopes = ['https://www.googleapis.com/auth/cloud-platform' ],
307+ )
308+ ],
309+ )
292310
293311
294312@patch ('sdgym._benchmark.benchmark._run_on_gcp' )
0 commit comments