tstest/integration: use t.Cleanup

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
pull/3479/head
Josh Bleecher Snyder 3 years ago committed by Josh Bleecher Snyder
parent 43983a4a3b
commit 768baafcb5

@ -67,12 +67,9 @@ func TestOneNodeUp_NoAuth(t *testing.T) {
bins := BuildTestBinaries(t) bins := BuildTestBinaries(t)
env := newTestEnv(t, bins) env := newTestEnv(t, bins)
defer env.Close()
n1 := newTestNode(t, env) n1 := newTestNode(t, env)
d1 := n1.StartDaemon(t) d1 := n1.StartDaemon(t)
defer d1.Kill()
n1.AwaitResponding(t) n1.AwaitResponding(t)
n1.MustUp() n1.MustUp()
@ -89,12 +86,9 @@ func TestOneNodeExpiredKey(t *testing.T) {
bins := BuildTestBinaries(t) bins := BuildTestBinaries(t)
env := newTestEnv(t, bins) env := newTestEnv(t, bins)
defer env.Close()
n1 := newTestNode(t, env) n1 := newTestNode(t, env)
d1 := n1.StartDaemon(t) d1 := n1.StartDaemon(t)
defer d1.Kill()
n1.AwaitResponding(t) n1.AwaitResponding(t)
n1.MustUp() n1.MustUp()
n1.AwaitRunning(t) n1.AwaitRunning(t)
@ -130,8 +124,6 @@ func TestCollectPanic(t *testing.T) {
bins := BuildTestBinaries(t) bins := BuildTestBinaries(t)
env := newTestEnv(t, bins) env := newTestEnv(t, bins)
defer env.Close()
n := newTestNode(t, env) n := newTestNode(t, env)
cmd := exec.Command(n.env.Binaries.Daemon, "--cleanup") cmd := exec.Command(n.env.Binaries.Daemon, "--cleanup")
@ -165,12 +157,9 @@ func TestStateSavedOnStart(t *testing.T) {
bins := BuildTestBinaries(t) bins := BuildTestBinaries(t)
env := newTestEnv(t, bins) env := newTestEnv(t, bins)
defer env.Close()
n1 := newTestNode(t, env) n1 := newTestNode(t, env)
d1 := n1.StartDaemon(t) d1 := n1.StartDaemon(t)
defer d1.Kill()
n1.AwaitResponding(t) n1.AwaitResponding(t)
n1.MustUp() n1.MustUp()
@ -208,11 +197,9 @@ func TestOneNodeUp_Auth(t *testing.T) {
env := newTestEnv(t, bins, configureControl(func(control *testcontrol.Server) { env := newTestEnv(t, bins, configureControl(func(control *testcontrol.Server) {
control.RequireAuth = true control.RequireAuth = true
})) }))
defer env.Close()
n1 := newTestNode(t, env) n1 := newTestNode(t, env)
d1 := n1.StartDaemon(t) d1 := n1.StartDaemon(t)
defer d1.Kill()
n1.AwaitListening(t) n1.AwaitListening(t)
@ -253,18 +240,15 @@ func TestTwoNodes(t *testing.T) {
bins := BuildTestBinaries(t) bins := BuildTestBinaries(t)
env := newTestEnv(t, bins) env := newTestEnv(t, bins)
defer env.Close()
// Create two nodes: // Create two nodes:
n1 := newTestNode(t, env) n1 := newTestNode(t, env)
n1SocksAddrCh := n1.socks5AddrChan() n1SocksAddrCh := n1.socks5AddrChan()
d1 := n1.StartDaemon(t) d1 := n1.StartDaemon(t)
defer d1.Kill()
n2 := newTestNode(t, env) n2 := newTestNode(t, env)
n2SocksAddrCh := n2.socks5AddrChan() n2SocksAddrCh := n2.socks5AddrChan()
d2 := n2.StartDaemon(t) d2 := n2.StartDaemon(t)
defer d2.Kill()
n1Socks := n1.AwaitSocksAddr(t, n1SocksAddrCh) n1Socks := n1.AwaitSocksAddr(t, n1SocksAddrCh)
n2Socks := n1.AwaitSocksAddr(t, n2SocksAddrCh) n2Socks := n1.AwaitSocksAddr(t, n2SocksAddrCh)
@ -304,11 +288,8 @@ func TestNodeAddressIPFields(t *testing.T) {
bins := BuildTestBinaries(t) bins := BuildTestBinaries(t)
env := newTestEnv(t, bins) env := newTestEnv(t, bins)
defer env.Close()
n1 := newTestNode(t, env) n1 := newTestNode(t, env)
d1 := n1.StartDaemon(t) d1 := n1.StartDaemon(t)
defer d1.Kill()
n1.AwaitListening(t) n1.AwaitListening(t)
n1.MustUp() n1.MustUp()
@ -335,11 +316,8 @@ func TestAddPingRequest(t *testing.T) {
bins := BuildTestBinaries(t) bins := BuildTestBinaries(t)
env := newTestEnv(t, bins) env := newTestEnv(t, bins)
defer env.Close()
n1 := newTestNode(t, env) n1 := newTestNode(t, env)
d1 := n1.StartDaemon(t) n1.StartDaemon(t)
defer d1.Kill()
n1.AwaitListening(t) n1.AwaitListening(t)
n1.MustUp() n1.MustUp()
@ -394,12 +372,9 @@ func TestNoControlConnWhenDown(t *testing.T) {
bins := BuildTestBinaries(t) bins := BuildTestBinaries(t)
env := newTestEnv(t, bins) env := newTestEnv(t, bins)
defer env.Close()
n1 := newTestNode(t, env) n1 := newTestNode(t, env)
d1 := n1.StartDaemon(t) d1 := n1.StartDaemon(t)
defer d1.Kill()
n1.AwaitResponding(t) n1.AwaitResponding(t)
// Come up the first time. // Come up the first time.
@ -413,7 +388,6 @@ func TestNoControlConnWhenDown(t *testing.T) {
env.LogCatcher.Reset() env.LogCatcher.Reset()
d2 := n1.StartDaemon(t) d2 := n1.StartDaemon(t)
defer d2.Kill()
n1.AwaitResponding(t) n1.AwaitResponding(t)
st := n1.MustStatus(t) st := n1.MustStatus(t)
@ -441,13 +415,10 @@ func TestOneNodeUpWindowsStyle(t *testing.T) {
bins := BuildTestBinaries(t) bins := BuildTestBinaries(t)
env := newTestEnv(t, bins) env := newTestEnv(t, bins)
defer env.Close()
n1 := newTestNode(t, env) n1 := newTestNode(t, env)
n1.upFlagGOOS = "windows" n1.upFlagGOOS = "windows"
d1 := n1.StartDaemonAsIPNGOOS(t, "windows") d1 := n1.StartDaemonAsIPNGOOS(t, "windows")
defer d1.Kill()
n1.AwaitResponding(t) n1.AwaitResponding(t)
n1.MustUp("--unattended") n1.MustUp("--unattended")
@ -483,10 +454,8 @@ func (f configureControl) modifyTestEnv(te *testEnv) {
f(te.Control) f(te.Control)
} }
// newTestEnv starts a bunch of services and returns a new test // newTestEnv starts a bunch of services and returns a new test environment.
// environment. // newTestEnv arranges for the environment's resources to be cleaned up on exit.
//
// Call Close to shut everything down.
func newTestEnv(t testing.TB, bins *Binaries, opts ...testEnvOpt) *testEnv { func newTestEnv(t testing.TB, bins *Binaries, opts ...testEnvOpt) *testEnv {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
t.Skip("not tested/working on Windows yet") t.Skip("not tested/working on Windows yet")
@ -512,19 +481,17 @@ func newTestEnv(t testing.TB, bins *Binaries, opts ...testEnvOpt) *testEnv {
o.modifyTestEnv(e) o.modifyTestEnv(e)
} }
control.HTTPTestServer.Start() control.HTTPTestServer.Start()
return e t.Cleanup(func() {
} // Shut down e.
func (e *testEnv) Close() error {
if err := e.TrafficTrap.Err(); err != nil { if err := e.TrafficTrap.Err(); err != nil {
e.t.Errorf("traffic trap: %v", err) e.t.Errorf("traffic trap: %v", err)
e.t.Logf("logs: %s", e.LogCatcher.logsString()) e.t.Logf("logs: %s", e.LogCatcher.logsString())
} }
e.LogCatcherServer.Close() e.LogCatcherServer.Close()
e.TrafficTrapServer.Close() e.TrafficTrapServer.Close()
e.ControlServer.Close() e.ControlServer.Close()
return nil })
return e
} }
// testNode is a machine with a tailscale & tailscaled. // testNode is a machine with a tailscale & tailscaled.
@ -681,10 +648,6 @@ type Daemon struct {
Process *os.Process Process *os.Process
} }
func (d *Daemon) Kill() {
d.Process.Kill()
}
func (d *Daemon) MustCleanShutdown(t testing.TB) { func (d *Daemon) MustCleanShutdown(t testing.TB) {
d.Process.Signal(os.Interrupt) d.Process.Signal(os.Interrupt)
ps, err := d.Process.Wait() ps, err := d.Process.Wait()
@ -696,8 +659,8 @@ func (d *Daemon) MustCleanShutdown(t testing.TB) {
} }
} }
// StartDaemon starts the node's tailscaled, failing if it fails to // StartDaemon starts the node's tailscaled, failing if it fails to start.
// start. // StartDaemon ensures that the process will exit when the test completes.
func (n *testNode) StartDaemon(t testing.TB) *Daemon { func (n *testNode) StartDaemon(t testing.TB) *Daemon {
return n.StartDaemonAsIPNGOOS(t, runtime.GOOS) return n.StartDaemonAsIPNGOOS(t, runtime.GOOS)
} }
@ -724,6 +687,7 @@ func (n *testNode) StartDaemonAsIPNGOOS(t testing.TB, ipnGOOS string) *Daemon {
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
t.Fatalf("starting tailscaled: %v", err) t.Fatalf("starting tailscaled: %v", err)
} }
t.Cleanup(func() { cmd.Process.Kill() })
return &Daemon{ return &Daemon{
Process: cmd.Process, Process: cmd.Process,
} }

Loading…
Cancel
Save