...
1
16
17 package http
18
19 import (
20 "fmt"
21 "regexp"
22 "sync"
23 "testing"
24 "time"
25
26 "github.com/stretchr/testify/require"
27 clientset "k8s.io/client-go/kubernetes"
28 "sigs.k8s.io/controller-runtime/pkg/client"
29
30 "sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
31 )
32
33 func ExpectMirroredRequest(t *testing.T, client client.Client, clientset clientset.Interface, mirrorPods []BackendRef, path string) {
34 for i, mirrorPod := range mirrorPods {
35 if mirrorPod.Name == "" {
36 t.Fatalf("Mirrored BackendRef[%d].Name wasn't provided in the testcase, this test should only check http request mirror.", i)
37 }
38 }
39
40 var wg sync.WaitGroup
41 wg.Add(len(mirrorPods))
42
43 for _, mirrorPod := range mirrorPods {
44 go func(mirrorPod BackendRef) {
45 defer wg.Done()
46
47 require.Eventually(t, func() bool {
48 mirrorLogRegexp := regexp.MustCompile(fmt.Sprintf("Echoing back request made to \\%s to client", path))
49
50 t.Log("Searching for the mirrored request log")
51 t.Logf(`Reading "%s/%s" logs`, mirrorPod.Namespace, mirrorPod.Name)
52 logs, err := kubernetes.DumpEchoLogs(mirrorPod.Namespace, mirrorPod.Name, client, clientset)
53 if err != nil {
54 t.Logf(`Couldn't read "%s/%s" logs: %v`, mirrorPod.Namespace, mirrorPod.Name, err)
55 return false
56 }
57
58 for _, log := range logs {
59 if mirrorLogRegexp.MatchString(string(log)) {
60 return true
61 }
62 }
63 return false
64 }, 60*time.Second, time.Millisecond*100, fmt.Sprintf(`Couldn't find mirrored request in "%s/%s" logs`, mirrorPod.Namespace, mirrorPod.Name))
65 }(mirrorPod)
66 }
67
68 wg.Wait()
69
70 t.Log("Found mirrored request log in all desired backends")
71 }
72
View as plain text