Skip to content

Commit 780c354

Browse files
authored
ray-operator: parameterize Test_ShouldDeletePod (#2000) (#2030)
1 parent d2a65c0 commit 780c354

File tree

1 file changed

+101
-71
lines changed

1 file changed

+101
-71
lines changed

ray-operator/controllers/ray/raycluster_controller_fake_test.go

Lines changed: 101 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,99 +1918,129 @@ func Test_RunningPods_RayContainerTerminated(t *testing.T) {
19181918
}
19191919

19201920
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`.
19241921
pod := corev1.Pod{
19251922
Spec: corev1.PodSpec{
1926-
RestartPolicy: corev1.RestartPolicyAlways,
19271923
Containers: []corev1.Container{
19281924
{
19291925
Name: "ray-head",
19301926
},
19311927
},
19321928
},
1933-
Status: corev1.PodStatus{
1934-
Phase: corev1.PodFailed,
1935-
},
19361929
}
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+
}{
19461937
{
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+
},
19501960
},
1961+
shouldDelete: false,
19511962
},
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{
19621963
{
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+
},
19661977
},
1978+
shouldDelete: false,
19671979
},
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{
19891980
{
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+
},
19932010
},
2011+
shouldDelete: false,
19942012
},
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{
20052013
{
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+
},
20092027
},
2028+
shouldDelete: true,
20102029
},
20112030
}
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+
}
20142044
}
20152045

20162046
func Test_RedisCleanupFeatureFlag(t *testing.T) {

0 commit comments

Comments
 (0)