tstest/integration: fix race flake

Fixes #2172

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2021-06-18 10:09:16 -07:00
parent 1ae35b6c59
commit 2f4817fe20
2 changed files with 23 additions and 4 deletions

View File

@ -317,6 +317,8 @@ func newTestEnv(t testing.TB, bins *Binaries) *testEnv {
control := &testcontrol.Server{ control := &testcontrol.Server{
DERPMap: derpMap, DERPMap: derpMap,
} }
control.HTTPTestServer = httptest.NewUnstartedServer(control)
control.HTTPTestServer.Start()
trafficTrap := new(trafficTrap) trafficTrap := new(trafficTrap)
e := &testEnv{ e := &testEnv{
t: t, t: t,
@ -324,12 +326,11 @@ func newTestEnv(t testing.TB, bins *Binaries) *testEnv {
LogCatcher: logc, LogCatcher: logc,
LogCatcherServer: httptest.NewServer(logc), LogCatcherServer: httptest.NewServer(logc),
Control: control, Control: control,
ControlServer: httptest.NewServer(control), ControlServer: control.HTTPTestServer,
TrafficTrap: trafficTrap, TrafficTrap: trafficTrap,
TrafficTrapServer: httptest.NewServer(trafficTrap), TrafficTrapServer: httptest.NewServer(trafficTrap),
derpShutdown: derpShutdown, derpShutdown: derpShutdown,
} }
e.Control.BaseURL = e.ControlServer.URL
return e return e
} }

View File

@ -18,6 +18,7 @@
"log" "log"
"math/rand" "math/rand"
"net/http" "net/http"
"net/http/httptest"
"net/url" "net/url"
"sort" "sort"
"strings" "strings"
@ -40,9 +41,12 @@ type Server struct {
Logf logger.Logf // nil means to use the log package Logf logger.Logf // nil means to use the log package
DERPMap *tailcfg.DERPMap // nil means to use prod DERP map DERPMap *tailcfg.DERPMap // nil means to use prod DERP map
RequireAuth bool RequireAuth bool
BaseURL string // must be set to e.g. "http://127.0.0.1:1234" with no trailing URL
Verbose bool Verbose bool
// ExplicitBaseURL or HTTPTestServer must be set.
ExplicitBaseURL string // e.g. "http://127.0.0.1:1234" with no trailing URL
HTTPTestServer *httptest.Server // if non-nil, used to get BaseURL
initMuxOnce sync.Once initMuxOnce sync.Once
mux *http.ServeMux mux *http.ServeMux
@ -59,6 +63,20 @@ type Server struct {
pingReqsToAdd map[tailcfg.NodeKey]*tailcfg.PingRequest pingReqsToAdd map[tailcfg.NodeKey]*tailcfg.PingRequest
} }
// BaseURL returns the server's base URL, without trailing slash.
func (s *Server) BaseURL() string {
if e := s.ExplicitBaseURL; e != "" {
return e
}
if hs := s.HTTPTestServer; hs != nil {
if hs.URL != "" {
return hs.URL
}
panic("Server.HTTPTestServer not started")
}
panic("Server ExplicitBaseURL and HTTPTestServer both unset")
}
// NumNodes returns the number of nodes in the testcontrol server. // NumNodes returns the number of nodes in the testcontrol server.
// //
// This is useful when connecting a bunch of virtual machines to a testcontrol // This is useful when connecting a bunch of virtual machines to a testcontrol
@ -415,7 +433,7 @@ func (s *Server) serveRegister(w http.ResponseWriter, r *http.Request, mkey tail
crand.Read(randHex) crand.Read(randHex)
authPath := fmt.Sprintf("/auth/%x", randHex) authPath := fmt.Sprintf("/auth/%x", randHex)
s.addAuthPath(authPath, req.NodeKey) s.addAuthPath(authPath, req.NodeKey)
authURL = s.BaseURL + authPath authURL = s.BaseURL() + authPath
} }
res, err := s.encode(mkey, false, tailcfg.RegisterResponse{ res, err := s.encode(mkey, false, tailcfg.RegisterResponse{