@@ -826,6 +826,83 @@ def test_list_containers_filter_illegal_filter(self, docker_client: ContainerCli
826
826
with pytest .raises (ContainerException ):
827
827
docker_client .list_containers (filter = "illegalfilter=foobar" )
828
828
829
+ def test_get_running_container_names_should_ignore_stopped_containers (
830
+ self , docker_client : ContainerClient , create_container
831
+ ):
832
+ cn1 = _random_container_name ()
833
+ cn2 = _random_container_name ()
834
+ cn3 = _random_container_name ()
835
+
836
+ create_container ("alpine" , name = cn1 , command = ["sleep" , "30" ])
837
+ create_container ("alpine" , name = cn2 , command = ["sleep" , "30" ])
838
+ create_container ("alpine" , name = cn3 , command = ["sleep" , "30" ])
839
+
840
+ docker_client .start_container (cn1 )
841
+ docker_client .start_container (cn2 )
842
+
843
+ container_names = docker_client .get_running_container_names ()
844
+ # We dont do equal comparison here to avoid brittle tests since there could be other containers.
845
+ assert 2 <= len (container_names )
846
+ assert all (x in container_names for x in [cn1 , cn2 ])
847
+ assert cn3 not in container_names
848
+
849
+ def test_get_all_container_names_should_include_even_stopped_containers (
850
+ self , docker_client : ContainerClient , create_container
851
+ ):
852
+ cn1 = _random_container_name ()
853
+ cn2 = _random_container_name ()
854
+ cn3 = _random_container_name ()
855
+
856
+ create_container ("alpine" , name = cn1 , command = ["sleep" , "30" ])
857
+ create_container ("alpine" , name = cn2 , command = ["sleep" , "30" ])
858
+ create_container ("alpine" , name = cn3 , command = ["sleep" , "30" ])
859
+
860
+ docker_client .start_container (cn1 )
861
+ docker_client .start_container (cn2 )
862
+
863
+ container_names = docker_client .get_all_container_names ()
864
+ # We dont do equal comparison here to avoid brittle tests since there could be other containers.
865
+ assert 3 <= len (container_names )
866
+ assert all (x in container_names for x in [cn1 , cn2 , cn3 ])
867
+
868
+ def test_remove_container_should_work_when_container_is_running_and_checking_container_existence (
869
+ self , docker_client : ContainerClient , create_container
870
+ ):
871
+ cn1 = _random_container_name ()
872
+ create_container ("alpine" , name = cn1 , command = ["sleep" , "30" ])
873
+
874
+ docker_client .start_container (cn1 )
875
+
876
+ docker_client .remove_container (cn1 , check_existence = True )
877
+
878
+ container_names = docker_client .get_all_container_names ()
879
+ assert cn1 not in container_names
880
+
881
+ def test_remove_container_should_work_when_container_is_stopped_and_checking_container_existence (
882
+ self , docker_client : ContainerClient , create_container
883
+ ):
884
+ cn1 = _random_container_name ()
885
+ create_container ("alpine" , name = cn1 , command = ["sleep" , "30" ])
886
+ docker_client .remove_container (cn1 , check_existence = True )
887
+
888
+ container_names = docker_client .get_all_container_names ()
889
+ assert cn1 not in container_names
890
+
891
+ def test_remove_container_should_throw_exception_when_container_doesnt_exist_and_not_forcing (
892
+ self , docker_client : ContainerClient
893
+ ):
894
+ with pytest .raises (NoSuchContainer ):
895
+ docker_client .remove_container (
896
+ "mygiganonexistingcontainer" , check_existence = False , force = False
897
+ )
898
+
899
+ def test_remove_container_should_work_even_when_container_doesnt_exist_because_of_forcing (
900
+ self , docker_client : ContainerClient
901
+ ):
902
+ docker_client .remove_container (
903
+ "mygiganonexistingcontainer" , check_existence = False , force = True
904
+ )
905
+
829
906
def test_list_containers_filter (self , docker_client : ContainerClient , create_container ):
830
907
name_prefix = "filter_tests_"
831
908
cn1 = name_prefix + _random_container_name ()
0 commit comments