@@ -162,6 +162,134 @@ def test_list_no_filters(self, mock):
162162 "6dc84cc0a46747da94e4c1571efcc01a756b4017261440b4b8985d37203c3c03" ,
163163 )
164164
165+ @requests_mock .Mocker ()
166+ def test_list_sparse_libpod_default (self , mock ):
167+ mock .get (
168+ tests .LIBPOD_URL + "/containers/json" ,
169+ json = [FIRST_CONTAINER , SECOND_CONTAINER ],
170+ )
171+ actual = self .client .containers .list ()
172+ self .assertIsInstance (actual , list )
173+
174+ self .assertEqual (
175+ actual [0 ].id , "87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd"
176+ )
177+ self .assertEqual (
178+ actual [1 ].id , "6dc84cc0a46747da94e4c1571efcc01a756b4017261440b4b8985d37203c3c03"
179+ )
180+
181+ # Verify that no individual reload() calls were made for sparse=True (default)
182+ # Should be only 1 request for the list endpoint
183+ self .assertEqual (len (mock .request_history ), 1 )
184+ # lower() needs to be enforced since the mocked url is transformed as lowercase and
185+ # this avoids %2f != %2F errors. Same applies for other instances of assertEqual
186+ self .assertEqual (mock .request_history [0 ].url , tests .LIBPOD_URL .lower () + "/containers/json" )
187+
188+ @requests_mock .Mocker ()
189+ def test_list_sparse_libpod_false (self , mock ):
190+ mock .get (
191+ tests .LIBPOD_URL + "/containers/json" ,
192+ json = [FIRST_CONTAINER , SECOND_CONTAINER ],
193+ )
194+ # Mock individual container detail endpoints for reload() calls
195+ # that are done for sparse=False
196+ mock .get (
197+ tests .LIBPOD_URL + f"/containers/{ FIRST_CONTAINER ['Id' ]} /json" ,
198+ json = FIRST_CONTAINER ,
199+ )
200+ mock .get (
201+ tests .LIBPOD_URL + f"/containers/{ SECOND_CONTAINER ['Id' ]} /json" ,
202+ json = SECOND_CONTAINER ,
203+ )
204+ actual = self .client .containers .list (sparse = False )
205+ self .assertIsInstance (actual , list )
206+
207+ self .assertEqual (
208+ actual [0 ].id , "87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd"
209+ )
210+ self .assertEqual (
211+ actual [1 ].id , "6dc84cc0a46747da94e4c1571efcc01a756b4017261440b4b8985d37203c3c03"
212+ )
213+
214+ # Verify that individual reload() calls were made for sparse=False
215+ # Should be 3 requests total: 1 for list + 2 for individual container details
216+ self .assertEqual (len (mock .request_history ), 3 )
217+
218+ # Verify the list endpoint was called first
219+ self .assertEqual (mock .request_history [0 ].url , tests .LIBPOD_URL .lower () + "/containers/json" )
220+
221+ # Verify the individual container detail endpoints were called
222+ individual_urls = {req .url for req in mock .request_history [1 :]}
223+ expected_urls = {
224+ tests .LIBPOD_URL .lower () + f"/containers/{ FIRST_CONTAINER ['Id' ]} /json" ,
225+ tests .LIBPOD_URL .lower () + f"/containers/{ SECOND_CONTAINER ['Id' ]} /json" ,
226+ }
227+ self .assertEqual (individual_urls , expected_urls )
228+
229+ @requests_mock .Mocker ()
230+ def test_list_sparse_compat_default (self , mock ):
231+ mock .get (
232+ tests .COMPATIBLE_URL + "/containers/json" ,
233+ json = [FIRST_CONTAINER , SECOND_CONTAINER ],
234+ )
235+ # Mock individual container detail endpoints for reload() calls
236+ # that are done for sparse=False
237+ mock .get (
238+ tests .COMPATIBLE_URL + f"/containers/{ FIRST_CONTAINER ['Id' ]} /json" ,
239+ json = FIRST_CONTAINER ,
240+ )
241+ mock .get (
242+ tests .COMPATIBLE_URL + f"/containers/{ SECOND_CONTAINER ['Id' ]} /json" ,
243+ json = SECOND_CONTAINER ,
244+ )
245+ actual = self .client .containers .list (compatible = True )
246+ self .assertIsInstance (actual , list )
247+
248+ self .assertEqual (
249+ actual [0 ].id , "87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd"
250+ )
251+ self .assertEqual (
252+ actual [1 ].id , "6dc84cc0a46747da94e4c1571efcc01a756b4017261440b4b8985d37203c3c03"
253+ )
254+
255+ # Verify that individual reload() calls were made for compat default (sparse=True)
256+ # Should be 3 requests total: 1 for list + 2 for individual container details
257+ self .assertEqual (len (mock .request_history ), 3 )
258+ self .assertEqual (
259+ mock .request_history [0 ].url , tests .COMPATIBLE_URL .lower () + "/containers/json"
260+ )
261+
262+ # Verify the individual container detail endpoints were called
263+ individual_urls = {req .url for req in mock .request_history [1 :]}
264+ expected_urls = {
265+ tests .COMPATIBLE_URL .lower () + f"/containers/{ FIRST_CONTAINER ['Id' ]} /json" ,
266+ tests .COMPATIBLE_URL .lower () + f"/containers/{ SECOND_CONTAINER ['Id' ]} /json" ,
267+ }
268+ self .assertEqual (individual_urls , expected_urls )
269+
270+ @requests_mock .Mocker ()
271+ def test_list_sparse_compat_true (self , mock ):
272+ mock .get (
273+ tests .COMPATIBLE_URL + "/containers/json" ,
274+ json = [FIRST_CONTAINER , SECOND_CONTAINER ],
275+ )
276+ actual = self .client .containers .list (sparse = True , compatible = True )
277+ self .assertIsInstance (actual , list )
278+
279+ self .assertEqual (
280+ actual [0 ].id , "87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd"
281+ )
282+ self .assertEqual (
283+ actual [1 ].id , "6dc84cc0a46747da94e4c1571efcc01a756b4017261440b4b8985d37203c3c03"
284+ )
285+
286+ # Verify that no individual reload() calls were made for sparse=True
287+ # Should be only 1 request for the list endpoint
288+ self .assertEqual (len (mock .request_history ), 1 )
289+ self .assertEqual (
290+ mock .request_history [0 ].url , tests .COMPATIBLE_URL .lower () + "/containers/json"
291+ )
292+
165293 @requests_mock .Mocker ()
166294 def test_prune (self , mock ):
167295 mock .post (
0 commit comments