Skip to content

Commit

Permalink
test: use delete count to test DeleteFollowerPods method
Browse files Browse the repository at this point in the history
  • Loading branch information
googs1025 committed Apr 24, 2024
1 parent 7ff9a5a commit 56ae233
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 53 deletions.
57 changes: 10 additions & 47 deletions pkg/controllers/pod_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"context"
"errors"
"fmt"
"sort"
"strconv"
"testing"

Expand Down Expand Up @@ -225,37 +224,21 @@ func TestDeleteFollowerPods(t *testing.T) {
podName: "test-jobset-replicated-job-1-test-job-0-1",
ns: ns,
jobIdx: 0}).AddAnnotation(batchv1.JobCompletionIndexAnnotation, "1")
wantPodWrapper = makePod(&makePodArgs{
jobSetName: jobSetName,
replicatedJobName: "replicated-job-1",
jobName: "test-jobset-replicated-job-1-test-job-0",
podName: "test-jobset-replicated-job-1-test-job-0-1",
ns: ns,
jobIdx: 0}).AddAnnotation(batchv1.JobCompletionIndexAnnotation, "1")
)
tests := []struct {
name string
pods []corev1.Pod
wantPodsDeleted []corev1.Pod
wantErr error
forceClientErr bool
name string
pods []corev1.Pod
wantPodsDeletedCount int
wantErr error
forceClientErr bool
}{
{
name: "delete follower pods",
pods: []corev1.Pod{
leaderPodWrapper.Obj(),
followerPodWrapper.Obj(),
},
wantPodsDeleted: []corev1.Pod{
wantPodWrapper.ResourceVersion("999").SetConditions([]corev1.PodCondition{{
Type: corev1.DisruptionTarget,
Status: corev1.ConditionTrue,
Reason: constants.ExclusivePlacementViolationReason,
LastTransitionTime: metav1.Now(),
Message: constants.ExclusivePlacementViolationMessage,
},
}).Obj(),
},
wantPodsDeletedCount: 1,
},
{
name: "delete follower pods with pod conditions status is false",
Expand All @@ -270,16 +253,7 @@ func TestDeleteFollowerPods(t *testing.T) {
},
}).Obj(),
},
wantPodsDeleted: []corev1.Pod{
wantPodWrapper.ResourceVersion("999").SetConditions([]corev1.PodCondition{{
Type: corev1.DisruptionTarget,
Status: corev1.ConditionTrue,
Reason: constants.ExclusivePlacementViolationReason,
LastTransitionTime: metav1.Now(),
Message: constants.ExclusivePlacementViolationMessage,
},
}).Obj(),
},
wantPodsDeletedCount: 1,
},
{
name: "delete follower pods with update pod status error",
Expand Down Expand Up @@ -314,7 +288,7 @@ func TestDeleteFollowerPods(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var gotPodsDeleted []corev1.Pod
var deletedCount int
fc := makeFakeClient(interceptor.Funcs{
Delete: func(ctx context.Context, client client.WithWatch,
obj client.Object, opts ...client.DeleteOption) error {
Expand All @@ -325,11 +299,7 @@ func TestDeleteFollowerPods(t *testing.T) {
if tc.forceClientErr || pod == nil {
return errors.New("example error")
}
// This is to solve the problem that the timestamps do not match.
// There will be a slight gap.
pod.Status.Conditions[0].LastTransitionTime =
tc.wantPodsDeleted[0].Status.Conditions[0].LastTransitionTime
gotPodsDeleted = append(gotPodsDeleted, *pod)
deletedCount++
return nil
},
SubResourceUpdate: func(ctx context.Context, client client.Client,
Expand All @@ -354,14 +324,7 @@ func TestDeleteFollowerPods(t *testing.T) {
if tc.wantErr != nil && gotErr != nil {
assert.Equal(t, tc.wantErr.Error(), gotErr.Error())
}
sort.Slice(gotPodsDeleted, func(i, j int) bool {
return gotPodsDeleted[i].Name < gotPodsDeleted[j].Name
})
sort.Slice(tc.wantPodsDeleted, func(i, j int) bool {
return tc.wantPodsDeleted[i].Name < tc.wantPodsDeleted[j].Name
})

if !assert.Equal(t, tc.wantPodsDeleted, gotPodsDeleted) {
if tc.wantPodsDeletedCount != deletedCount {
t.Errorf("deleteFollowerPods() did not make the expected pod deletion calls")
}
})
Expand Down
6 changes: 0 additions & 6 deletions pkg/util/testing/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,6 @@ func (p *PodWrapper) Labels(labels map[string]string) *PodWrapper {
return p
}

// ResourceVersion sets the pod.metadata.resourceVersion.
func (p *PodWrapper) ResourceVersion(resourceVersion string) *PodWrapper {
p.ObjectMeta.ResourceVersion = resourceVersion
return p
}

// SetConditions sets the value of the pod.status.conditions.
func (p *PodWrapper) SetConditions(conditions []corev1.PodCondition) *PodWrapper {
p.Status.Conditions = conditions
Expand Down

0 comments on commit 56ae233

Please sign in to comment.