@@ -928,33 +928,8 @@ func TestReconcilePod(t *testing.T) {
928928 },
929929 },
930930 },
931- {
932- name : "error when annotated pod does not exist" ,
933- initialObjs : []runtime.Object {},
934- sandbox : & sandboxv1alpha1.Sandbox {
935- ObjectMeta : metav1.ObjectMeta {
936- Name : sandboxName ,
937- Namespace : sandboxNs ,
938- Annotations : map [string ]string {
939- sandboxv1alpha1 .SandboxPodNameAnnotation : "non-existent-pod" ,
940- },
941- },
942- Spec : sandboxv1alpha1.SandboxSpec {
943- Replicas : ptr .To (int32 (1 )),
944- PodTemplate : sandboxv1alpha1.PodTemplate {
945- Spec : corev1.PodSpec {
946- Containers : []corev1.Container {
947- {
948- Name : "test-container" ,
949- },
950- },
951- },
952- },
953- },
954- },
955- wantPod : nil ,
956- expectErr : true ,
957- },
931+ // "clears stale annotation" test is a standalone test below (TestReconcilePodClearsStaleAnnotation)
932+ // because the returned pod has an initialized annotations map that differs from the stored pod.
958933 {
959934 name : "remove pod name annotation when replicas is 0" ,
960935 initialObjs : []runtime.Object {
@@ -1087,3 +1062,46 @@ func TestSandboxExpiry(t *testing.T) {
10871062 })
10881063 }
10891064}
1065+
1066+ func TestReconcilePodClearsStaleAnnotation (t * testing.T ) {
1067+ sandboxName := "sandbox-name"
1068+ sandboxNs := "sandbox-ns"
1069+ nameHash := NameHash (sandboxName )
1070+
1071+ sandbox := & sandboxv1alpha1.Sandbox {
1072+ ObjectMeta : metav1.ObjectMeta {
1073+ Name : sandboxName ,
1074+ Namespace : sandboxNs ,
1075+ Annotations : map [string ]string {
1076+ sandboxv1alpha1 .SandboxPodNameAnnotation : "non-existent-pod" ,
1077+ },
1078+ },
1079+ Spec : sandboxv1alpha1.SandboxSpec {
1080+ Replicas : ptr .To (int32 (1 )),
1081+ PodTemplate : sandboxv1alpha1.PodTemplate {
1082+ Spec : corev1.PodSpec {
1083+ Containers : []corev1.Container {{Name : "test-container" }},
1084+ },
1085+ },
1086+ },
1087+ }
1088+
1089+ r := SandboxReconciler {
1090+ Client : newFakeClient (sandbox ),
1091+ Scheme : Scheme ,
1092+ Tracer : asmetrics .NewNoOp (),
1093+ }
1094+
1095+ pod , err := r .reconcilePod (t .Context (), sandbox , nameHash )
1096+ require .NoError (t , err )
1097+ require .NotNil (t , pod , "should have created a new pod after clearing stale annotation" )
1098+ require .Equal (t , sandboxName , pod .Name )
1099+ require .Equal (t , nameHash , pod .Labels [sandboxLabel ])
1100+
1101+ // Verify the stale annotation was replaced with the new pod name
1102+ liveSandbox := & sandboxv1alpha1.Sandbox {}
1103+ err = r .Get (t .Context (), types.NamespacedName {Name : sandboxName , Namespace : sandboxNs }, liveSandbox )
1104+ require .NoError (t , err )
1105+ require .Equal (t , sandboxName , liveSandbox .Annotations [sandboxv1alpha1 .SandboxPodNameAnnotation ],
1106+ "annotation should track the newly created pod, not the stale one" )
1107+ }
0 commit comments