testing: use httptest with freeport

This commit is contained in:
Daniel Nephin 2021-11-27 15:53:51 -05:00 committed by Daniel Nephin
parent 20a8e11bf2
commit 59a7fb5ca9
4 changed files with 28 additions and 20 deletions

View File

@ -138,7 +138,7 @@ func TestSnapshotSaveCommand_TruncatedStream(t *testing.T) {
var fakeResult atomic.Value var fakeResult atomic.Value
// Run a fake webserver to pretend to be the snapshot API. // Run a fake webserver to pretend to be the snapshot API.
fakeAddr := lib.StartTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { srv := lib.NewHTTPTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/v1/snapshot" { if req.URL.Path != "/v1/snapshot" {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
return return
@ -160,7 +160,7 @@ func TestSnapshotSaveCommand_TruncatedStream(t *testing.T) {
// Wait until the server is actually listening. // Wait until the server is actually listening.
retry.Run(t, func(r *retry.R) { retry.Run(t, func(r *retry.R) {
resp, err := http.Get("http://" + fakeAddr + "/not-real") resp, err := srv.Client().Get(srv.URL + "/not-real")
require.NoError(r, err) require.NoError(r, err)
require.Equal(r, http.StatusNotFound, resp.StatusCode) require.Equal(r, http.StatusNotFound, resp.StatusCode)
}) })
@ -179,7 +179,7 @@ func TestSnapshotSaveCommand_TruncatedStream(t *testing.T) {
file := filepath.Join(dir, "backup.tgz") file := filepath.Join(dir, "backup.tgz")
args := []string{ args := []string{
"-http-addr=" + fakeAddr, // point to the fake "-http-addr=" + srv.Listener.Addr().String(), // point to the fake
file, file,
} }

View File

@ -1,31 +1,33 @@
package lib package lib
import ( import (
"net"
"net/http" "net/http"
"net/http/httptest"
"github.com/hashicorp/consul/ipaddr" "github.com/hashicorp/consul/ipaddr"
"github.com/hashicorp/consul/sdk/freeport" "github.com/hashicorp/consul/sdk/freeport"
"github.com/mitchellh/go-testing-interface"
) )
// StartTestServer fires up a web server on a random unused port to serve the // NewHTTPTestServer starts and returns an httptest.Server that is listening
// given handler body. The address it is listening on is returned. When the // on a random port from freeport.Port. When the test case ends the server
// test case terminates the server will be stopped via cleanup functions. // will be stopped.
// //
// We can't directly use httptest.Server here because that only thinks a port // We can't directly use httptest.Server here because that only thinks a port
// is free if it's not bound. Consul tests frequently reserve ports via // is free if it's not bound. Consul tests frequently reserve ports via
// `sdk/freeport` so you can have one part of the test try to use a port and // `sdk/freeport` so you can have one part of the test try to use a port and
// _know_ nothing is listening. If you simply assumed unbound ports were free // _know_ nothing is listening. If you simply assumed unbound ports were free
// you'd end up with test cross-talk and weirdness. // you'd end up with test cross-talk and weirdness.
func StartTestServer(t testing.T, handler http.Handler) string { func NewHTTPTestServer(t freeport.TestingT, handler http.Handler) *httptest.Server {
srv := httptest.NewUnstartedServer(handler)
addr := ipaddr.FormatAddressPort("127.0.0.1", freeport.Port(t)) addr := ipaddr.FormatAddressPort("127.0.0.1", freeport.Port(t))
listener, err := net.Listen("tcp", addr)
server := &http.Server{Addr: addr, Handler: handler} if err != nil {
t.Cleanup(func() { t.Fatalf("failed to listen on %v", addr)
server.Close() }
}) srv.Listener = listener
t.Cleanup(srv.Close)
go server.ListenAndServe() srv.Start()
return srv
return addr
} }

View File

@ -29,11 +29,12 @@ import (
"testing" "testing"
"time" "time"
"github.com/hashicorp/consul/sdk/freeport"
"github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/hashicorp/go-cleanhttp" "github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-uuid" "github.com/hashicorp/go-uuid"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/hashicorp/consul/sdk/freeport"
"github.com/hashicorp/consul/sdk/testutil/retry"
) )
// TestPerformanceConfig configures the performance parameters. // TestPerformanceConfig configures the performance parameters.
@ -142,7 +143,11 @@ func defaultServerConfig(t TestingTB) *TestServerConfig {
panic(err) panic(err)
} }
ports := freeport.MustTake(6) ports, err := freeport.Take(6)
if err != nil {
t.Fatalf("failed to take ports: %v", err)
}
logBuffer := NewLogBuffer(t) logBuffer := NewLogBuffer(t)
return &TestServerConfig{ return &TestServerConfig{

View File

@ -8,4 +8,5 @@ type TestingTB interface {
Failed() bool Failed() bool
Logf(format string, args ...interface{}) Logf(format string, args ...interface{})
Name() string Name() string
Fatalf(fmt string, args ...interface{})
} }