Skip to content

Commit

Permalink
add ut Test_TrySuspendPod
Browse files Browse the repository at this point in the history
  • Loading branch information
KubeKyrie committed Aug 30, 2023
1 parent 56ed901 commit b80cd25
Showing 1 changed file with 204 additions and 0 deletions.
204 changes: 204 additions & 0 deletions pkg/controllers/clusterops/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,210 @@ func Test_GetKuBeanCluster(t *testing.T) {
}
}

func Test_TrySuspendPod(t *testing.T) {
controller := Controller{
Client: newFakeClient(),
ClientSet: clientsetfake.NewSimpleClientset(),
KubeanClusterSet: clusterv1alpha1fake.NewSimpleClientset(),
KubeanClusterOpsSet: clusteroperationv1alpha1fake.NewSimpleClientset(),
}
tests := []struct {
name string
args func() bool
want bool
}{
{
name: "job finished",
args: func() bool {
targetJob := &batchv1.Job{
TypeMeta: metav1.TypeMeta{
APIVersion: "batch/v1",
Kind: "Job",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "kubean-system",
Name: "job-finished",
},
Status: batchv1.JobStatus{
Conditions: []batchv1.JobCondition{
{
Type: batchv1.JobComplete,
Status: corev1.ConditionTrue,
},
},
},
}
controller.ClientSet.BatchV1().Jobs("kubean-system").Create(context.Background(), targetJob, metav1.CreateOptions{})
result, err := controller.TrySuspendPod(&clusteroperationv1alpha1.ClusterOperation{
Spec: clusteroperationv1alpha1.Spec{
Cluster: "cluster",
},
Status: clusteroperationv1alpha1.Status{
JobRef: &apis.JobRef{
NameSpace: "kubean-system",
Name: "job-finished",
},
},
})
return result == false && err == nil
},
want: true,
},
{
name: "job not found",
args: func() bool {
targetJob := &batchv1.Job{
TypeMeta: metav1.TypeMeta{
APIVersion: "batch/v1",
Kind: "Job",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "kubean-system",
Name: "job-finished",
},
Status: batchv1.JobStatus{
Conditions: []batchv1.JobCondition{
{
Type: batchv1.JobComplete,
Status: corev1.ConditionTrue,
},
},
},
}
controller.ClientSet.BatchV1().Jobs("kubean-system").Create(context.Background(), targetJob, metav1.CreateOptions{})
result, err := controller.TrySuspendPod(&clusteroperationv1alpha1.ClusterOperation{
Spec: clusteroperationv1alpha1.Spec{
Cluster: "cluster",
},
Status: clusteroperationv1alpha1.Status{
JobRef: &apis.JobRef{
NameSpace: "kubean-system",
Name: "job-not-found",
},
},
})
return result == false && err == nil
},
want: true,
},
{
name: "cluster ops no annotations",
args: func() bool {
controller.ClientSet.CoreV1().Pods("kubean-system").Create(context.Background(), &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "kubean-system", Labels: map[string]string{"a": "b"}},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
},
}, metav1.CreateOptions{})
targetJob := &batchv1.Job{
TypeMeta: metav1.TypeMeta{
APIVersion: "batch/v1",
Kind: "Job",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "kubean-system",
Name: "job-running",
},
Spec: batchv1.JobSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"a": "b"},
},
},
Status: batchv1.JobStatus{
Conditions: []batchv1.JobCondition{
{
Type: batchv1.JobComplete,
Status: corev1.ConditionFalse,
},
},
},
}
controller.ClientSet.BatchV1().Jobs("kubean-system").Create(context.Background(), targetJob, metav1.CreateOptions{})
clusterOps := &clusteroperationv1alpha1.ClusterOperation{
ObjectMeta: metav1.ObjectMeta{
Name: "my_kubean_ops_cluster",
},
Spec: clusteroperationv1alpha1.Spec{
Cluster: "cluster",
},
Status: clusteroperationv1alpha1.Status{
JobRef: &apis.JobRef{
NameSpace: "kubean-system",
Name: "job-running",
},
},
}
controller.Client.Create(context.Background(), clusterOps)
result, err := controller.TrySuspendPod(clusterOps)
return result == true && err == nil && clusterOps.Annotations[JobActorPodAnnoKey] == "pod1"
},
want: true,
},
{
name: "suspend job success",
args: func() bool {
controller.ClientSet.CoreV1().Pods("kubean-system").Create(context.Background(), &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "kubean-system", Labels: map[string]string{"a": "b"}},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
},
}, metav1.CreateOptions{})
targetJob := &batchv1.Job{
TypeMeta: metav1.TypeMeta{
APIVersion: "batch/v1",
Kind: "Job",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "kubean-system",
Name: "job-running",
},
Spec: batchv1.JobSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"a": "b"},
},
},
Status: batchv1.JobStatus{
Conditions: []batchv1.JobCondition{
{
Type: batchv1.JobComplete,
Status: corev1.ConditionFalse,
},
},
},
}
controller.ClientSet.BatchV1().Jobs("kubean-system").Create(context.Background(), targetJob, metav1.CreateOptions{})

clusterOps := &clusteroperationv1alpha1.ClusterOperation{
ObjectMeta: metav1.ObjectMeta{
Name: "my_kubean_ops_cluster",
Annotations: map[string]string{JobActorPodAnnoKey: "key"},
},
Spec: clusteroperationv1alpha1.Spec{
Cluster: "cluster",
},
Status: clusteroperationv1alpha1.Status{
JobRef: &apis.JobRef{
NameSpace: "kubean-system",
Name: "job-running",
},
},
}
controller.Client.Create(context.Background(), clusterOps)
result, err := controller.TrySuspendPod(clusterOps)
return result == true && err == nil
},
want: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.args() != test.want {
t.Fatal()
}
})
}
}

func Test_GetRunningPodFromJob(t *testing.T) {
genController := func() Controller {
return Controller{
Expand Down

0 comments on commit b80cd25

Please sign in to comment.