Skip to content

Commit d879cf1

Browse files
committed
e2e: add readiness check in tests
Fixes #224
1 parent e350f66 commit d879cf1

File tree

6 files changed

+69
-1
lines changed

6 files changed

+69
-1
lines changed

cmd/forwarder/httpbin/httpbin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func Command() (cmd *cobra.Command) {
4646
apiServerConfig: forwarder.DefaultHTTPServerConfig(),
4747
logConfig: log.DefaultConfig(),
4848
}
49-
c.apiServerConfig.Addr = ":10000"
49+
c.apiServerConfig.Addr = "localhost:10000"
5050

5151
defer func() {
5252
fs := cmd.Flags()

e2e/docker-compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ version: "3.8"
33
services:
44
proxy:
55
image: saucelabs/forwarder:${FORWARDER_VERSION}
6+
environment:
7+
FORWARDER_API_ADDRESS: ":10000"
68

79
upstream-proxy:
810
extends: proxy

e2e/e2e_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package e2e
44

55
import (
66
"context"
7+
"flag"
78
"fmt"
89
"math/rand"
910
"net"
@@ -15,8 +16,29 @@ import (
1516
"testing"
1617

1718
"github.com/gavv/httpexpect/v2"
19+
"golang.org/x/sync/errgroup"
1820
)
1921

22+
func TestMain(m *testing.M) {
23+
if !flag.Parsed() {
24+
flag.Parse()
25+
}
26+
27+
var eg errgroup.Group
28+
eg.Go(func() error {
29+
return waitForServerReady(*proxy)
30+
})
31+
eg.Go(func() error {
32+
return waitForServerReady(*httpbin)
33+
})
34+
if err := eg.Wait(); err != nil {
35+
fmt.Fprintf(os.Stderr, err.Error()+"\n")
36+
os.Exit(1)
37+
}
38+
39+
os.Exit(m.Run())
40+
}
41+
2042
func TestStatusCodes(t *testing.T) {
2143
// List of all valid status codes plus some non-standard ones.
2244
// See https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml

e2e/framework.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package e2e
22

33
import (
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 (
1619
var (
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+
3072
func newTransport(t testing.TB) *http.Transport {
3173
t.Helper()
3274

e2e/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.20
55
require (
66
github.com/gavv/httpexpect/v2 v2.6.1
77
github.com/gorilla/websocket v1.4.2
8+
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
89
)
910

1011
require (

e2e/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3
117117
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
118118
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
119119
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
120+
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck=
120121
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
121122
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
122123
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

0 commit comments

Comments
 (0)