Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xuriwuyun committed Jan 24, 2025
1 parent e7b2be5 commit e011386
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pkg/controller/instanceset/pod_role_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ func updatePodRoleLabel(cli client.Client, reqCtx intctrlutil.RequestCtx,
delete(newPod.Labels, AccessModeLabelKey)
}

if pod.Annotations == nil {
pod.Annotations = map[string]string{}
if newPod.Annotations == nil {
newPod.Annotations = map[string]string{}
}
newPod.Annotations[constant.LastRoleSnapshotVersionAnnotationKey] = version
return cli.Update(ctx, newPod, inDataContext())
Expand Down
42 changes: 40 additions & 2 deletions pkg/controller/instanceset/pod_role_event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ var _ = Describe("pod role label event handler test", func() {
return nil
}).Times(1)
k8sMock.EXPECT().
Patch(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, pd *corev1.Pod, patch client.Patch, _ ...client.PatchOption) error {
Update(gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, pd *corev1.Pod, _ ...client.UpdateOption) error {
Expect(pd).ShouldNot(BeNil())
Expect(pd.Labels).ShouldNot(BeNil())
Expect(pd.Labels[RoleLabelKey]).Should(Equal(role.Name))
Expand Down Expand Up @@ -126,6 +126,44 @@ var _ = Describe("pod role label event handler test", func() {
return nil
}).Times(1)
Expect(handler.Handle(cli, reqCtx, nil, event)).Should(Succeed())

By("read a stale pod")
message = fmt.Sprintf("Readiness probe failed: error: health rpc failed: rpc error: code = Unknown desc = {\"event\":\"Success\",\"originalRole\":\"\",\"role\":\"%s\"}", role.Name)
event = builder.NewEventBuilder(namespace, "foo").
SetInvolvedObject(objectRef).
SetReason(string(util.CheckRoleOperation)).
SetMessage(message).
GetObject()
k8sMock.EXPECT().
Get(gomock.Any(), gomock.Any(), &corev1.Pod{}, gomock.Any()).
DoAndReturn(func(_ context.Context, objKey client.ObjectKey, p *corev1.Pod, _ ...client.GetOption) error {
p.Namespace = objKey.Namespace
p.Name = objKey.Name
p.UID = pod.UID
p.Labels = map[string]string{
constant.AppInstanceLabelKey: name,
WorkloadsInstanceLabelKey: name,
}
return nil
}).Times(1)
k8sMock.EXPECT().
Get(gomock.Any(), gomock.Any(), &workloads.InstanceSet{}, gomock.Any()).
DoAndReturn(func(_ context.Context, objKey client.ObjectKey, its *workloads.InstanceSet, _ ...client.GetOption) error {
its.Namespace = objKey.Namespace
its.Name = objKey.Name
its.Spec.Roles = []workloads.ReplicaRole{role}
return nil
}).Times(1)
updateErr := fmt.Errorf("the object has been modified; please apply your changes to the latest version and try again")
k8sMock.EXPECT().
Update(gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, pd *corev1.Pod, _ ...client.UpdateOption) error {
Expect(pd).ShouldNot(BeNil())
Expect(pd.Labels).ShouldNot(BeNil())
Expect(pd.Labels[RoleLabelKey]).Should(Equal(role.Name))
return updateErr
}).Times(1)
Expect(handler.Handle(cli, reqCtx, nil, event)).Should(Equal(updateErr))
})
})

Expand Down

0 comments on commit e011386

Please sign in to comment.