11package e2e
22
33import (
4+ "context"
45 "crypto/tls"
56 "flag"
7+ "fmt"
68 "net/http"
79 "net/url"
810 "os"
911 "strings"
1012 "testing"
13+ "time"
1114
1215 "github.com/gavv/httpexpect/v2"
1316 "github.com/gorilla/websocket"
@@ -16,6 +19,7 @@ import (
1619var (
1720 proxy = flag .String ("proxy" , "" , "URL of the proxy to test against" )
1821 httpbin = flag .String ("httpbin" , "" , "URL of the httpbin server to test against" )
22+ maxWait = flag .Duration ("max-wait" , 5 * time .Second , "Maximum time to wait for the containers to become ready" )
1923 insecureSkipVerify = flag .Bool ("insecure-skip-verify" , false , "Skip TLS certificate verification" )
2024)
2125
@@ -27,6 +31,44 @@ func init() {
2731 }
2832}
2933
34+ // waitForServerReady checks the API server /readyz endpoint until it returns 200.
35+ // It assumes that the server is running on port 10000.
36+ func waitForServerReady (baseURL string ) error {
37+ var client http.Client
38+
39+ u , err := url .Parse (baseURL )
40+ if err != nil {
41+ return err
42+ }
43+ readyz := fmt .Sprintf ("http://%s:10000/readyz" , u .Hostname ())
44+
45+ req , err := http .NewRequest (http .MethodGet , readyz , http .NoBody )
46+ if err != nil {
47+ return err
48+ }
49+
50+ const backoff = 200 * time .Millisecond
51+
52+ var (
53+ resp * http.Response
54+ rerr error
55+ )
56+ for i := 0 ; i < int (* maxWait / backoff ); i ++ {
57+ resp , rerr = client .Do (req .Clone (context .Background ()))
58+
59+ if resp != nil && resp .StatusCode == http .StatusOK {
60+ return nil
61+ }
62+
63+ time .Sleep (backoff )
64+ }
65+ if rerr != nil {
66+ return fmt .Errorf ("%s not ready: %w" , u .Hostname (), rerr )
67+ }
68+
69+ return fmt .Errorf ("%s not ready" , u .Hostname ())
70+ }
71+
3072func newTransport (t testing.TB ) * http.Transport {
3173 t .Helper ()
3274
0 commit comments