From e011386fd4bd4dd9c1f48e20af30afcbe439a314 Mon Sep 17 00:00:00 2001 From: xuriwuyun Date: Fri, 24 Jan 2025 14:25:06 +0800 Subject: [PATCH] add tests --- .../instanceset/pod_role_event_handler.go | 4 +- .../pod_role_event_handler_test.go | 42 ++++++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/pkg/controller/instanceset/pod_role_event_handler.go b/pkg/controller/instanceset/pod_role_event_handler.go index 8d24193a6c6..d06941b1f9a 100644 --- a/pkg/controller/instanceset/pod_role_event_handler.go +++ b/pkg/controller/instanceset/pod_role_event_handler.go @@ -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()) diff --git a/pkg/controller/instanceset/pod_role_event_handler_test.go b/pkg/controller/instanceset/pod_role_event_handler_test.go index 42e3539fe05..1c7af012028 100644 --- a/pkg/controller/instanceset/pod_role_event_handler_test.go +++ b/pkg/controller/instanceset/pod_role_event_handler_test.go @@ -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)) @@ -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)) }) })