sigs.k8s.io/gateway-api@v1.0.0/conformance/utils/http/mirror.go (about) 1 /* 2 Copyright 2023 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 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 }