🌐 AI搜索 & 代理 主页
Skip to content

Commit f2a3066

Browse files
committed
REFACTOR probe worker test file
1 parent 1e44895 commit f2a3066

File tree

1 file changed

+71
-53
lines changed

1 file changed

+71
-53
lines changed

pkg/kubelet/prober/worker_test.go

Lines changed: 71 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ limitations under the License.
1717
package prober
1818

1919
import (
20-
"context"
2120
"fmt"
21+
"net/http"
2222
"testing"
2323
"time"
2424

@@ -578,9 +578,9 @@ func TestStartupProbeDisabledByStarted(t *testing.T) {
578578
}
579579

580580
func TestWorkerHTTPProbeRequestCaching(t *testing.T) {
581-
logger, _ := ktesting.NewTestContext(t)
581+
logger, ctx := ktesting.NewTestContext(t)
582582
m := newTestManager()
583-
pod := getTestPod()
583+
584584
httpGet := &v1.HTTPGetAction{
585585
Host: "",
586586
Path: "",
@@ -595,60 +595,78 @@ func TestWorkerHTTPProbeRequestCaching(t *testing.T) {
595595
SuccessThreshold: 1,
596596
FailureThreshold: 1,
597597
}
598+
599+
tests := []struct {
600+
name string
601+
podIP string
602+
containerID string
603+
expectNewReq bool
604+
}{
605+
{
606+
name: "initial probe",
607+
podIP: "10.0.0.1",
608+
containerID: "docker://abc123",
609+
expectNewReq: true,
610+
},
611+
{
612+
name: "same pod IP and container ID",
613+
podIP: "10.0.0.1",
614+
containerID: "docker://abc123",
615+
expectNewReq: false,
616+
},
617+
{
618+
name: "pod IP change",
619+
podIP: "10.0.0.2",
620+
containerID: "docker://abc123",
621+
expectNewReq: true,
622+
},
623+
{
624+
name: "container restart",
625+
podIP: "10.0.0.2",
626+
containerID: "docker://def456",
627+
expectNewReq: true,
628+
},
629+
}
630+
631+
// Create test pod with HTTP probe
632+
pod := getTestPod()
598633
pod.Spec.Containers[0].LivenessProbe = &probeSpec
599-
// the test worker is always initialized with an exec probe , so we we call the newWorker function explicitly
600634
w := newWorker(m, liveness, pod, pod.Spec.Containers[0])
601-
podIP1 := "10.0.0.1"
602-
containerID1 := "docker://abc123"
603-
status := getTestRunningStatusWithStarted(true)
604-
status.PodIP = podIP1
605-
status.ContainerStatuses[0].ContainerID = containerID1
606-
m.statusManager.SetPodStatus(logger, w.pod, status)
607-
608-
// First probe: should create a new request
609-
req1, err := w.httpProbeRequest.getRequest(&w.container, status.PodIP)
610-
if err != nil {
611-
t.Fatalf("Expected no error when creating first request, got: %v", err)
612-
}
613-
if w.httpProbeRequest == nil || w.httpProbeRequest.request == nil {
614-
t.Fatal("Expected httpProbeRequest and request to be initialized after first probe")
615-
}
616635

617-
// Second probe with same podIP and containerID: should reuse request
618-
req2, err := w.httpProbeRequest.getRequest(&w.container, status.PodIP)
619-
if err != nil {
620-
t.Fatalf("Expected no error when reusing request, got: %v", err)
621-
}
622-
if req2 != req1 {
623-
t.Error("Expected cached request to be reused when podIP and containerID do not change")
624-
}
636+
var prevReq *http.Request
637+
for i, test := range tests {
638+
t.Run(test.name, func(t *testing.T) {
639+
// Update pod status
640+
status := getTestRunningStatusWithStarted(true)
641+
status.PodIP = test.podIP
642+
status.ContainerStatuses[0].ContainerID = test.containerID
643+
m.statusManager.SetPodStatus(logger, w.pod, status)
644+
645+
// For container restart case, need to trigger restart logic
646+
if i > 0 && test.containerID != tests[i-1].containerID {
647+
w.doProbe(ctx)
648+
}
625649

626-
// Simulate pod IP change
627-
podIP2 := "10.0.0.2"
628-
status.PodIP = podIP2
629-
m.statusManager.SetPodStatus(logger, w.pod, status)
630-
req3, err := w.httpProbeRequest.getRequest(&w.container, status.PodIP)
631-
if err != nil {
632-
t.Fatalf("Expected no error when creating request after pod IP change, got: %v", err)
633-
}
634-
if req3 == req1 {
635-
t.Error("Expected new request after pod IP change")
636-
}
637-
if req3 == nil {
638-
t.Fatal("Expected request to be initialized after pod IP change")
639-
}
650+
// Get HTTP probe request
651+
req, err := w.httpProbeRequest.getRequest(&w.container, status.PodIP)
652+
if err != nil {
653+
t.Fatalf("Expected no error creating request, got: %v", err)
654+
}
655+
if req == nil {
656+
t.Fatal("Expected request to be initialized")
657+
}
640658

641-
// Simulate container restart by setting a new container ID
642-
containerID2 := "docker://def456"
643-
status.ContainerStatuses[0].ContainerID = containerID2
644-
m.statusManager.SetPodStatus(logger, w.pod, status)
645-
// Trigger container restart logic by calling doProbe
646-
w.doProbe(context.Background()) // maybe refactor the getRequest logic to avoid needing to call doProbe here (include the container ID in the request creation logic)
647-
// After container restart, the request should be reset
648-
if w.httpProbeRequest.request == req3 {
649-
t.Error("Expected request to be reset after container ID change (container restart)")
650-
}
651-
if w.httpProbeRequest.request == nil {
652-
t.Fatal("Expected request to be recreated after container restart")
659+
if test.expectNewReq {
660+
if req == prevReq {
661+
t.Error("Expected new request to be created")
662+
}
663+
} else {
664+
if req != prevReq {
665+
t.Error("Expected cached request to be reused")
666+
}
667+
}
668+
669+
prevReq = req
670+
})
653671
}
654672
}

0 commit comments

Comments
 (0)