2222)
2323
2424
25+ @pytest .mark .asyncio
26+ @pytest .mark .parametrize ("test_db" , ["sqlite" , "postgres" ], indirect = True )
2527class TestProcessIdleVolumes :
26- @pytest .mark .asyncio
27- @pytest .mark .parametrize ("test_db" , ["sqlite" , "postgres" ], indirect = True )
2828 async def test_no_idle_duration_configured (self , test_db , session : AsyncSession ):
29- """Test that volumes without idle_duration configured are not deleted."""
3029 project = await create_project (session = session )
3130 user = await create_user (session = session )
3231
33- # Create volume without idle_duration
3432 volume = await create_volume (
3533 session = session ,
3634 project = project ,
@@ -41,17 +39,13 @@ async def test_no_idle_duration_configured(self, test_db, session: AsyncSession)
4139 volume_provisioning_data = get_volume_provisioning_data (),
4240 )
4341
44- should_delete = await _should_delete_idle_volume (volume )
42+ should_delete = _should_delete_idle_volume (volume )
4543 assert not should_delete
4644
47- @pytest .mark .asyncio
48- @pytest .mark .parametrize ("test_db" , ["sqlite" , "postgres" ], indirect = True )
4945 async def test_idle_duration_disabled (self , test_db , session : AsyncSession ):
50- """Test that volumes with idle_duration set to -1 (disabled) are not deleted."""
5146 project = await create_project (session = session )
5247 user = await create_user (session = session )
5348
54- # Create volume with idle_duration disabled
5549 volume_config = get_volume_configuration (name = "test-volume" )
5650 volume_config .idle_duration = - 1
5751
@@ -65,19 +59,15 @@ async def test_idle_duration_disabled(self, test_db, session: AsyncSession):
6559 volume_provisioning_data = get_volume_provisioning_data (),
6660 )
6761
68- should_delete = await _should_delete_idle_volume (volume )
62+ should_delete = _should_delete_idle_volume (volume )
6963 assert not should_delete
7064
71- @pytest .mark .asyncio
72- @pytest .mark .parametrize ("test_db" , ["sqlite" , "postgres" ], indirect = True )
7365 async def test_volume_still_attached (self , test_db , session : AsyncSession ):
74- """Test that volumes still attached to instances are not deleted."""
7566 project = await create_project (session = session )
7667 user = await create_user (session = session )
7768
78- # Create volume with idle_duration
7969 volume_config = get_volume_configuration (name = "test-volume" )
80- volume_config .idle_duration = "1h" # 1 hour
70+ volume_config .idle_duration = "1h"
8171
8272 volume = await create_volume (
8373 session = session ,
@@ -89,7 +79,6 @@ async def test_volume_still_attached(self, test_db, session: AsyncSession):
8979 volume_provisioning_data = get_volume_provisioning_data (),
9080 )
9181
92- # Create an instance and attach the volume to it
9382 instance = await create_instance (session = session , project = project )
9483 attachment = VolumeAttachmentModel (
9584 volume_id = volume .id ,
@@ -98,19 +87,15 @@ async def test_volume_still_attached(self, test_db, session: AsyncSession):
9887 volume .attachments .append (attachment )
9988 await session .commit ()
10089
101- should_delete = await _should_delete_idle_volume (volume )
90+ should_delete = _should_delete_idle_volume (volume )
10291 assert not should_delete
10392
104- @pytest .mark .asyncio
105- @pytest .mark .parametrize ("test_db" , ["sqlite" , "postgres" ], indirect = True )
10693 async def test_idle_duration_not_exceeded (self , test_db , session : AsyncSession ):
107- """Test that volumes within idle duration threshold are not deleted."""
10894 project = await create_project (session = session )
10995 user = await create_user (session = session )
11096
111- # Create volume with idle_duration
11297 volume_config = get_volume_configuration (name = "test-volume" )
113- volume_config .idle_duration = "1h" # 1 hour
98+ volume_config .idle_duration = "1h"
11499
115100 volume = await create_volume (
116101 session = session ,
@@ -122,24 +107,19 @@ async def test_idle_duration_not_exceeded(self, test_db, session: AsyncSession):
122107 volume_provisioning_data = get_volume_provisioning_data (),
123108 )
124109
125- # Set last_job_processed_at to 30 minutes ago (less than 1 hour)
126110 volume .last_job_processed_at = (
127111 datetime .datetime .now (datetime .timezone .utc ) - datetime .timedelta (minutes = 30 )
128112 ).replace (tzinfo = None )
129113
130- should_delete = await _should_delete_idle_volume (volume )
114+ should_delete = _should_delete_idle_volume (volume )
131115 assert not should_delete
132116
133- @pytest .mark .asyncio
134- @pytest .mark .parametrize ("test_db" , ["sqlite" , "postgres" ], indirect = True )
135117 async def test_idle_duration_exceeded (self , test_db , session : AsyncSession ):
136- """Test that volumes exceeding idle duration threshold are marked for deletion."""
137118 project = await create_project (session = session )
138119 user = await create_user (session = session )
139120
140- # Create volume with idle_duration
141121 volume_config = get_volume_configuration (name = "test-volume" )
142- volume_config .idle_duration = "1h" # 1 hour
122+ volume_config .idle_duration = "1h"
143123
144124 volume = await create_volume (
145125 session = session ,
@@ -151,22 +131,17 @@ async def test_idle_duration_exceeded(self, test_db, session: AsyncSession):
151131 volume_provisioning_data = get_volume_provisioning_data (),
152132 )
153133
154- # Set last_job_processed_at to 2 hours ago (more than 1 hour)
155134 volume .last_job_processed_at = (
156135 datetime .datetime .now (datetime .timezone .utc ) - datetime .timedelta (hours = 2 )
157136 ).replace (tzinfo = None )
158137
159- should_delete = await _should_delete_idle_volume (volume )
138+ should_delete = _should_delete_idle_volume (volume )
160139 assert should_delete
161140
162- @pytest .mark .asyncio
163- @pytest .mark .parametrize ("test_db" , ["sqlite" , "postgres" ], indirect = True )
164141 async def test_volume_never_used_by_job (self , test_db , session : AsyncSession ):
165- """Test idle duration calculation for volumes never used by jobs."""
166142 project = await create_project (session = session )
167143 user = await create_user (session = session )
168144
169- # Create volume with old created_at time
170145 volume = await create_volume (
171146 session = session ,
172147 project = project ,
@@ -178,23 +153,17 @@ async def test_volume_never_used_by_job(self, test_db, session: AsyncSession):
178153 created_at = datetime .datetime .now (datetime .timezone .utc ) - datetime .timedelta (hours = 2 ),
179154 )
180155
181- # last_job_processed_at is None, so it should use created_at
182156 volume .last_job_processed_at = None
183157
184158 idle_duration = _get_volume_idle_duration (volume )
185- # Should be approximately 2 hours
186- assert idle_duration .total_seconds () >= 7000 # ~2 hours in seconds
159+ assert idle_duration .total_seconds () >= 7000
187160
188- @pytest .mark .asyncio
189- @pytest .mark .parametrize ("test_db" , ["sqlite" , "postgres" ], indirect = True )
190161 async def test_process_idle_volumes_integration (self , test_db , session : AsyncSession ):
191- """Integration test for the full process_idle_volumes function."""
192162 project = await create_project (session = session )
193163 user = await create_user (session = session )
194164
195- # Create volume that should be deleted (exceeded idle duration)
196165 volume_config = get_volume_configuration (name = "test-volume" )
197- volume_config .idle_duration = "1h" # 1 hour
166+ volume_config .idle_duration = "1h"
198167
199168 volume = await create_volume (
200169 session = session ,
@@ -206,21 +175,17 @@ async def test_process_idle_volumes_integration(self, test_db, session: AsyncSes
206175 volume_provisioning_data = get_volume_provisioning_data (),
207176 )
208177
209- # Set as idle for more than threshold
210178 volume .last_job_processed_at = (
211179 datetime .datetime .now (datetime .timezone .utc ) - datetime .timedelta (hours = 2 )
212180 ).replace (tzinfo = None )
213181
214182 await session .commit ()
215183
216- # Mock the delete_volumes function to avoid actual deletion
217184 with patch (
218185 "dstack._internal.server.background.tasks.process_idle_volumes.delete_volumes"
219186 ) as mock_delete :
220187 await process_idle_volumes ()
221188
222- # Should have called delete_volumes with the volume
223189 mock_delete .assert_called_once ()
224190 call_args = mock_delete .call_args
225- # The function is called with (session, project, volume_names_list)
226191 assert call_args [0 ][2 ] == ["test-volume" ]
0 commit comments