11import errno
22import docker
3+ import json
34from unittest import mock
45from host_modules .docker_service import DockerService
56
@@ -303,7 +304,7 @@ def test_docker_load_success(self, MockInit, MockBusName, MockSystemBus):
303304 assert rc == 0 , "Return code is wrong"
304305 mock_docker_client .images .load .assert_called_once_with (MockOpen .return_value )
305306 MockOpen .assert_called_once_with ("image.tar" , "rb" )
306-
307+
307308 @mock .patch ("dbus.SystemBus" )
308309 @mock .patch ("dbus.service.BusName" )
309310 @mock .patch ("dbus.service.Object.__init__" )
@@ -317,4 +318,44 @@ def test_docker_load_file_not_found(self, MockInit, MockBusName, MockSystemBus):
317318 rc , _ = docker_service .load ("non_existent_image.tar" )
318319
319320 assert rc == errno .ENOENT , "Return code is wrong"
320- MockOpen .assert_called_once_with ("non_existent_image.tar" , "rb" )
321+ MockOpen .assert_called_once_with ("non_existent_image.tar" , "rb" )
322+
323+ @mock .patch ("dbus.SystemBus" )
324+ @mock .patch ("dbus.service.BusName" )
325+ @mock .patch ("dbus.service.Object.__init__" )
326+ def test_docker_list_success (self , MockInit , MockBusName , MockSystemBus ):
327+ mock_docker_client = mock .Mock ()
328+ mock_container_1 = mock .Mock (id = "1" , status = "running" , image = mock .Mock (tags = ["image1" ], id = "hash1" ), labels = {})
329+ mock_container_2 = mock .Mock (id = "2" , status = "exited" , image = mock .Mock (tags = ["image2" ], id = "hash2" ), labels = {})
330+ # The name attribute needs to be explicitly set for the mock object.
331+ mock_container_1 .name = "container1"
332+ mock_container_2 .name = "container2"
333+ mock_docker_client .containers .list .return_value = [
334+ mock_container_1 , mock_container_2
335+ ]
336+
337+ with mock .patch .object (docker , "from_env" , return_value = mock_docker_client ):
338+ docker_service = DockerService (MOD_NAME )
339+ rc , containers = docker_service .list (True , {})
340+
341+ assert rc == 0 , "Return code is wrong {}" .format (containers )
342+ expected_containers = [
343+ {
344+ "id" : "1" ,
345+ "name" : "container1" ,
346+ "status" : "running" ,
347+ "image" : "image1" ,
348+ "labels" : {},
349+ "hash" : "hash1" ,
350+ },
351+ {
352+ "id" : "2" ,
353+ "name" : "container2" ,
354+ "status" : "exited" ,
355+ "image" : "image2" ,
356+ "labels" : {},
357+ "hash" : "hash2" ,
358+ },
359+ ]
360+ assert json .loads (containers ) == expected_containers , "Containers list is wrong"
361+ mock_docker_client .containers .list .assert_called_once_with (all = True , filters = {})
0 commit comments