Skip to content

Commit

Permalink
e2e: add readiness check in tests
Browse files Browse the repository at this point in the history
Fixes #224
  • Loading branch information
mmatczuk committed Mar 7, 2023
1 parent e350f66 commit d879cf1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/forwarder/httpbin/httpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Command() (cmd *cobra.Command) {
apiServerConfig: forwarder.DefaultHTTPServerConfig(),
logConfig: log.DefaultConfig(),
}
c.apiServerConfig.Addr = ":10000"
c.apiServerConfig.Addr = "localhost:10000"

defer func() {
fs := cmd.Flags()
Expand Down
2 changes: 2 additions & 0 deletions e2e/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ version: "3.8"
services:
proxy:
image: saucelabs/forwarder:${FORWARDER_VERSION}
environment:
FORWARDER_API_ADDRESS: ":10000"

upstream-proxy:
extends: proxy
Expand Down
22 changes: 22 additions & 0 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package e2e

import (
"context"
"flag"
"fmt"
"math/rand"
"net"
Expand All @@ -15,8 +16,29 @@ import (
"testing"

"github.com/gavv/httpexpect/v2"
"golang.org/x/sync/errgroup"
)

func TestMain(m *testing.M) {
if !flag.Parsed() {
flag.Parse()
}

var eg errgroup.Group
eg.Go(func() error {
return waitForServerReady(*proxy)
})
eg.Go(func() error {
return waitForServerReady(*httpbin)
})
if err := eg.Wait(); err != nil {
fmt.Fprintf(os.Stderr, err.Error()+"\n")
os.Exit(1)
}

os.Exit(m.Run())
}

func TestStatusCodes(t *testing.T) {
// List of all valid status codes plus some non-standard ones.
// See https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
Expand Down
42 changes: 42 additions & 0 deletions e2e/framework.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package e2e

import (
"context"
"crypto/tls"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"testing"
"time"

"github.com/gavv/httpexpect/v2"
"github.com/gorilla/websocket"
Expand All @@ -16,6 +19,7 @@ import (
var (
proxy = flag.String("proxy", "", "URL of the proxy to test against")
httpbin = flag.String("httpbin", "", "URL of the httpbin server to test against")
maxWait = flag.Duration("max-wait", 5*time.Second, "Maximum time to wait for the containers to become ready")
insecureSkipVerify = flag.Bool("insecure-skip-verify", false, "Skip TLS certificate verification")
)

Expand All @@ -27,6 +31,44 @@ func init() {
}
}

// waitForServerReady checks the API server /readyz endpoint until it returns 200.
// It assumes that the server is running on port 10000.
func waitForServerReady(baseURL string) error {
var client http.Client

u, err := url.Parse(baseURL)
if err != nil {
return err
}
readyz := fmt.Sprintf("http://%s:10000/readyz", u.Hostname())

req, err := http.NewRequest(http.MethodGet, readyz, http.NoBody)
if err != nil {
return err
}

const backoff = 200 * time.Millisecond

var (
resp *http.Response
rerr error
)
for i := 0; i < int(*maxWait/backoff); i++ {
resp, rerr = client.Do(req.Clone(context.Background()))

if resp != nil && resp.StatusCode == http.StatusOK {
return nil
}

time.Sleep(backoff)
}
if rerr != nil {
return fmt.Errorf("%s not ready: %w", u.Hostname(), rerr)
}

return fmt.Errorf("%s not ready", u.Hostname())
}

func newTransport(t testing.TB) *http.Transport {
t.Helper()

Expand Down
1 change: 1 addition & 0 deletions e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/gavv/httpexpect/v2 v2.6.1
github.com/gorilla/websocket v1.4.2
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
)

require (
Expand Down
1 change: 1 addition & 0 deletions e2e/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down

0 comments on commit d879cf1

Please sign in to comment.