@@ -1918,99 +1918,129 @@ func Test_RunningPods_RayContainerTerminated(t *testing.T) {
1918
1918
}
1919
1919
1920
1920
func Test_ShouldDeletePod (t * testing.T ) {
1921
- // [Case 1]: The restart policy is `Always` and the Pod is in a terminate state.
1922
- // The expected behavior is that the controller will not delete the Pod because
1923
- // the restart policy is `Always`.
1924
1921
pod := corev1.Pod {
1925
1922
Spec : corev1.PodSpec {
1926
- RestartPolicy : corev1 .RestartPolicyAlways ,
1927
1923
Containers : []corev1.Container {
1928
1924
{
1929
1925
Name : "ray-head" ,
1930
1926
},
1931
1927
},
1932
1928
},
1933
- Status : corev1.PodStatus {
1934
- Phase : corev1 .PodFailed ,
1935
- },
1936
1929
}
1937
- shouldDelete , _ := shouldDeletePod (pod , rayv1 .HeadNode )
1938
- assert .False (t , shouldDelete )
1939
-
1940
- // [Case 2]: The restart policy is `Always`, the Pod is not in a terminate state,
1941
- // and the Ray container has not terminated. The expected behavior is that the
1942
- // controller will not delete the Pod.
1943
- pod .Spec .RestartPolicy = corev1 .RestartPolicyAlways
1944
- pod .Status .Phase = corev1 .PodRunning
1945
- pod .Status .ContainerStatuses = []corev1.ContainerStatus {
1930
+ tests := []struct {
1931
+ name string
1932
+ restartPolicy corev1.RestartPolicy
1933
+ phase corev1.PodPhase
1934
+ containerStatus []corev1.ContainerStatus
1935
+ shouldDelete bool
1936
+ }{
1946
1937
{
1947
- Name : "ray-head" ,
1948
- State : corev1.ContainerState {
1949
- Running : & corev1.ContainerStateRunning {},
1938
+ // The restart policy is `Always` and the Pod is in a terminate state.
1939
+ // The expected behavior is that the controller will not delete the Pod because
1940
+ // the restart policy is `Always`.
1941
+ name : "restartPolicy=Always, phase=PodFailed, shouldDelete=false" ,
1942
+ restartPolicy : corev1 .RestartPolicyAlways ,
1943
+ phase : corev1 .PodFailed ,
1944
+ shouldDelete : false ,
1945
+ },
1946
+ {
1947
+ // The restart policy is `Always`, the Pod is not in a terminate state,
1948
+ // and the Ray container has not terminated. The expected behavior is that the
1949
+ // controller will not delete the Pod.
1950
+ name : "restartPolicy=Always, phase=PodRunning, ray-head=running, shouldDelete=false" ,
1951
+ restartPolicy : corev1 .RestartPolicyAlways ,
1952
+ phase : corev1 .PodRunning ,
1953
+ containerStatus : []corev1.ContainerStatus {
1954
+ {
1955
+ Name : "ray-head" ,
1956
+ State : corev1.ContainerState {
1957
+ Running : & corev1.ContainerStateRunning {},
1958
+ },
1959
+ },
1950
1960
},
1961
+ shouldDelete : false ,
1951
1962
},
1952
- }
1953
- shouldDelete , _ = shouldDeletePod (pod , rayv1 .HeadNode )
1954
- assert .False (t , shouldDelete )
1955
-
1956
- // [Case 3]: The restart policy is `Always`, the Pod is not in a terminate state,
1957
- // and the Ray container has terminated. The expected behavior is that the controller
1958
- // will not delete the Pod because the restart policy is `Always`.
1959
- pod .Spec .RestartPolicy = corev1 .RestartPolicyAlways
1960
- pod .Status .Phase = corev1 .PodRunning
1961
- pod .Status .ContainerStatuses = []corev1.ContainerStatus {
1962
1963
{
1963
- Name : "ray-head" ,
1964
- State : corev1.ContainerState {
1965
- Terminated : & corev1.ContainerStateTerminated {},
1964
+ // The restart policy is `Always`, the Pod is not in a terminate state,
1965
+ // and the Ray container has terminated. The expected behavior is that the controller
1966
+ // will not delete the Pod because the restart policy is `Always`.
1967
+ name : "restartPolicy=Always, phase=PodRunning, ray-head=terminated, shouldDelete=false" ,
1968
+ restartPolicy : corev1 .RestartPolicyAlways ,
1969
+ phase : corev1 .PodRunning ,
1970
+ containerStatus : []corev1.ContainerStatus {
1971
+ {
1972
+ Name : "ray-head" ,
1973
+ State : corev1.ContainerState {
1974
+ Terminated : & corev1.ContainerStateTerminated {},
1975
+ },
1976
+ },
1966
1977
},
1978
+ shouldDelete : false ,
1967
1979
},
1968
- }
1969
- shouldDelete , _ = shouldDeletePod (pod , rayv1 .HeadNode )
1970
- assert .False (t , shouldDelete )
1971
-
1972
- // [Case 4]: The restart policy is `Never` and the Pod is in a terminate state.
1973
- // The expected behavior is that the controller will delete the Pod.
1974
- pod .Spec .RestartPolicy = corev1 .RestartPolicyNever
1975
- pod .Status .Phase = corev1 .PodFailed
1976
- shouldDelete , _ = shouldDeletePod (pod , rayv1 .HeadNode )
1977
- assert .True (t , shouldDelete )
1978
-
1979
- pod .Status .Phase = corev1 .PodSucceeded
1980
- shouldDelete , _ = shouldDeletePod (pod , rayv1 .HeadNode )
1981
- assert .True (t , shouldDelete )
1982
-
1983
- // [Case 5]: The restart policy is set to `Never`, the Pod is not in a terminated state, and
1984
- // the Ray container has not terminated. The expected behavior is that the controller will not
1985
- // delete the Pod.
1986
- pod .Spec .RestartPolicy = corev1 .RestartPolicyNever
1987
- pod .Status .Phase = corev1 .PodRunning
1988
- pod .Status .ContainerStatuses = []corev1.ContainerStatus {
1989
1980
{
1990
- Name : "ray-head" ,
1991
- State : corev1.ContainerState {
1992
- Running : & corev1.ContainerStateRunning {},
1981
+ // The restart policy is `Never` and the Pod is in a terminate state.
1982
+ // The expected behavior is that the controller will delete the Pod.
1983
+ name : "restartPolicy=Never, phase=PodFailed, shouldDelete=true" ,
1984
+ restartPolicy : corev1 .RestartPolicyNever ,
1985
+ phase : corev1 .PodFailed ,
1986
+ shouldDelete : true ,
1987
+ },
1988
+ {
1989
+ // The restart policy is `Never` and the Pod terminated successfully.
1990
+ // The expected behavior is that the controller will delete the Pod.
1991
+ name : "restartPolicy=Never, phase=PodSucceeded, shouldDelete=true" ,
1992
+ restartPolicy : corev1 .RestartPolicyNever ,
1993
+ phase : corev1 .PodSucceeded ,
1994
+ shouldDelete : true ,
1995
+ },
1996
+ {
1997
+ // The restart policy is set to `Never`, the Pod is not in a terminated state, and
1998
+ // the Ray container has not terminated. The expected behavior is that the controller will not
1999
+ // delete the Pod.
2000
+ name : "restartPolicy=Never, phase=PodRunning, ray-head=running, shouldDelete=false" ,
2001
+ restartPolicy : corev1 .RestartPolicyNever ,
2002
+ phase : corev1 .PodRunning ,
2003
+ containerStatus : []corev1.ContainerStatus {
2004
+ {
2005
+ Name : "ray-head" ,
2006
+ State : corev1.ContainerState {
2007
+ Running : & corev1.ContainerStateRunning {},
2008
+ },
2009
+ },
1993
2010
},
2011
+ shouldDelete : false ,
1994
2012
},
1995
- }
1996
- shouldDelete , _ = shouldDeletePod (pod , rayv1 .HeadNode )
1997
- assert .False (t , shouldDelete )
1998
-
1999
- // [Case 6]: The restart policy is set to `Never`, the Pod is not in a terminated state, and
2000
- // the Ray container has terminated. The expected behavior is that the controller will delete
2001
- // the Pod.
2002
- pod .Spec .RestartPolicy = corev1 .RestartPolicyNever
2003
- pod .Status .Phase = corev1 .PodRunning
2004
- pod .Status .ContainerStatuses = []corev1.ContainerStatus {
2005
2013
{
2006
- Name : "ray-head" ,
2007
- State : corev1.ContainerState {
2008
- Terminated : & corev1.ContainerStateTerminated {},
2014
+ // The restart policy is set to `Never`, the Pod is not in a terminated state, and
2015
+ // the Ray container has terminated. The expected behavior is that the controller will delete
2016
+ // the Pod.
2017
+ name : "restartPolicy=Never, phase=PodRunning, ray-head=terminated, shouldDelete=true" ,
2018
+ restartPolicy : corev1 .RestartPolicyNever ,
2019
+ phase : corev1 .PodRunning ,
2020
+ containerStatus : []corev1.ContainerStatus {
2021
+ {
2022
+ Name : "ray-head" ,
2023
+ State : corev1.ContainerState {
2024
+ Terminated : & corev1.ContainerStateTerminated {},
2025
+ },
2026
+ },
2009
2027
},
2028
+ shouldDelete : true ,
2010
2029
},
2011
2030
}
2012
- shouldDelete , _ = shouldDeletePod (pod , rayv1 .HeadNode )
2013
- assert .True (t , shouldDelete )
2031
+ for _ , testCase := range tests {
2032
+ t .Run (testCase .name , func (t * testing.T ) {
2033
+ pod .Spec .RestartPolicy = testCase .restartPolicy
2034
+ pod .Status .Phase = testCase .phase
2035
+ pod .Status .ContainerStatuses = testCase .containerStatus
2036
+
2037
+ shouldDelete , _ := shouldDeletePod (pod , rayv1 .HeadNode )
2038
+ assert .EqualValues (
2039
+ t , shouldDelete , testCase .shouldDelete ,
2040
+ "unexpected value of shouldDelete" ,
2041
+ )
2042
+ })
2043
+ }
2014
2044
}
2015
2045
2016
2046
func Test_RedisCleanupFeatureFlag (t * testing.T ) {
0 commit comments