ref: Add test.V23InitWithNamespace

Many tests use endpoints instead of names out of laziness.  This makes
it nearly zero cost to use names which makes the test code more realistic
and cleaner.

This introduces new dependencies for external tests in other repos, so
several .godepcop files were modified.

MultiPart: 3/3

Change-Id: I0053cf44166d5f970ecb06516b56353fa8de5578
diff --git a/lib/xrpc/xserver_test.go b/lib/xrpc/xserver_test.go
index de0ee78..d14ac6d 100644
--- a/lib/xrpc/xserver_test.go
+++ b/lib/xrpc/xserver_test.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package xrpc
+package xrpc_test
 
 import (
 	"testing"
@@ -11,6 +11,7 @@
 	"v.io/v23/context"
 	"v.io/v23/rpc"
 	"v.io/v23/security"
+	"v.io/x/ref/lib/xrpc"
 	_ "v.io/x/ref/runtime/factories/generic"
 	"v.io/x/ref/test"
 )
@@ -29,7 +30,7 @@
 	ctx, shutdown := test.V23Init()
 	defer shutdown()
 
-	server, err := NewServer(ctx, "", &service{}, nil)
+	server, err := xrpc.NewServer(ctx, "", &service{}, nil)
 	if err != nil {
 		t.Fatalf("Error creating server: %v", err)
 	}
@@ -54,7 +55,7 @@
 	ctx, shutdown := test.V23Init()
 	defer shutdown()
 
-	server, err := NewDispatchingServer(ctx, "", &dispatcher{})
+	server, err := xrpc.NewDispatchingServer(ctx, "", &dispatcher{})
 	if err != nil {
 		t.Fatalf("Error creating server: %v", err)
 	}
diff --git a/runtime/internal/rpc/testutil_test.go b/runtime/internal/rpc/testutil_test.go
index 9a2e8f3..0dca147 100644
--- a/runtime/internal/rpc/testutil_test.go
+++ b/runtime/internal/rpc/testutil_test.go
@@ -79,7 +79,7 @@
 }
 
 func initForTest() (*context.T, v23.Shutdown) {
-	ctx, shutdown := test.V23Init()
+	ctx, shutdown := test.V23InitWithParams(test.InitParams{})
 	ctx, err := ivtrace.Init(ctx, flags.VtraceFlags{})
 	if err != nil {
 		panic(err)
diff --git a/services/mounttable/mounttablelib/collectionserver_test.go b/services/mounttable/mounttablelib/collectionserver_test.go
index 1ae841c..07b2e5a 100644
--- a/services/mounttable/mounttablelib/collectionserver_test.go
+++ b/services/mounttable/mounttablelib/collectionserver_test.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package mounttablelib
+package mounttablelib_test
 
 import (
 	"sync"
diff --git a/services/mounttable/mounttablelib/mounttable_test.go b/services/mounttable/mounttablelib/mounttable_test.go
index 21a2f29..42b5cee 100644
--- a/services/mounttable/mounttablelib/mounttable_test.go
+++ b/services/mounttable/mounttablelib/mounttable_test.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package mounttablelib
+package mounttablelib_test
 
 import (
 	"encoding/json"
@@ -29,10 +29,15 @@
 
 	libstats "v.io/x/ref/lib/stats"
 	"v.io/x/ref/services/debug/debuglib"
+	"v.io/x/ref/services/mounttable/mounttablelib"
 	"v.io/x/ref/test"
 	"v.io/x/ref/test/testutil"
 )
 
+func init() {
+	test.Init()
+}
+
 // Simulate different processes with different runtimes.
 // rootCtx is the one running the mounttable service.
 const ttlSecs = 60 * 60
@@ -188,9 +193,9 @@
 		boom(t, "r.NewServer: %s", err)
 	}
 	// Add mount table service.
-	mt, err := NewMountTableDispatcher(permsFile, persistDir, statsDir)
+	mt, err := mounttablelib.NewMountTableDispatcher(permsFile, persistDir, statsDir)
 	if err != nil {
-		boom(t, "NewMountTableDispatcher: %v", err)
+		boom(t, "mounttablelib.NewMountTableDispatcher: %v", err)
 	}
 	// Start serving on a loopback address.
 	eps, err := server.Listen(v23.GetListenSpec(ctx))
@@ -315,11 +320,11 @@
 
 	// Try timing out a mount.
 	vlog.Info("Try timing out a mount.")
-	ft := NewFakeTimeClock()
-	setServerListClock(ft)
+	ft := mounttablelib.NewFakeTimeClock()
+	mounttablelib.SetServerListClock(ft)
 	doMount(t, rootCtx, mtAddr, "stuffWithTTL", collectionName, true)
 	checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuffWithTTL/the/rain"), "the rain", true)
-	ft.advance(time.Duration(ttlSecs+4) * time.Second)
+	ft.Advance(time.Duration(ttlSecs+4) * time.Second)
 	checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuffWithTTL/the/rain"), "the rain", false)
 
 	// Test unauthorized mount.
@@ -470,7 +475,7 @@
 	doSetPermissions(t, aliceCtx, estr, "users/alice/a/b/c/d", perms, "", true)
 
 	// Do we obey limits?
-	for i := 0; i < defaultMaxNodesPerUser-5; i++ {
+	for i := 0; i < mounttablelib.DefaultMaxNodesPerUser()-5; i++ {
 		node := fmt.Sprintf("users/alice/a/b/c/d/%d", i)
 		doSetPermissions(t, aliceCtx, estr, node, perms, "", true)
 	}
@@ -481,7 +486,7 @@
 		key      string
 		expected interface{}
 	}{
-		{"alice", int64(defaultMaxNodesPerUser)},
+		{"alice", int64(mounttablelib.DefaultMaxNodesPerUser())},
 		{"bob", int64(0)},
 		{"root", int64(0)},
 		{conventions.ServerUser, int64(3)},
@@ -635,31 +640,31 @@
 
 	collectionName := naming.JoinAddressName(collectionAddr, "collection")
 
-	ft := NewFakeTimeClock()
-	setServerListClock(ft)
+	ft := mounttablelib.NewFakeTimeClock()
+	mounttablelib.SetServerListClock(ft)
 	doMount(t, rootCtx, estr, "a1/b1", collectionName, true)
 	doMount(t, rootCtx, estr, "a1/b2", collectionName, true)
 	doMount(t, rootCtx, estr, "a2/b1", collectionName, true)
 	doMount(t, rootCtx, estr, "a2/b2/c", collectionName, true)
 
 	checkMatch(t, []string{"a1/b1", "a2/b1"}, doGlob(t, rootCtx, estr, "", "*/b1/..."))
-	ft.advance(time.Duration(ttlSecs/2) * time.Second)
+	ft.Advance(time.Duration(ttlSecs/2) * time.Second)
 	checkMatch(t, []string{"a1/b1", "a2/b1"}, doGlob(t, rootCtx, estr, "", "*/b1/..."))
 	checkMatch(t, []string{"c"}, doGlob(t, rootCtx, estr, "a2/b2", "*"))
 	// Refresh only a1/b1.  All the other mounts will expire upon the next
 	// ft advance.
 	doMount(t, rootCtx, estr, "a1/b1", collectionName, true)
-	ft.advance(time.Duration(ttlSecs/2+4) * time.Second)
+	ft.Advance(time.Duration(ttlSecs/2+4) * time.Second)
 	checkMatch(t, []string{"a1", "a1/b1"}, doGlob(t, rootCtx, estr, "", "*/..."))
 	checkMatch(t, []string{"a1/b1"}, doGlob(t, rootCtx, estr, "", "*/b1/..."))
 }
 
 func TestBadAccessLists(t *testing.T) {
-	_, err := NewMountTableDispatcher("testdata/invalid.perms", "", "mounttable")
+	_, err := mounttablelib.NewMountTableDispatcher("testdata/invalid.perms", "", "mounttable")
 	if err == nil {
 		boom(t, "Expected json parse error in permissions file")
 	}
-	_, err = NewMountTableDispatcher("testdata/doesntexist.perms", "", "mounttable")
+	_, err = mounttablelib.NewMountTableDispatcher("testdata/doesntexist.perms", "", "mounttable")
 	if err != nil {
 		boom(t, "Missing permissions file should not cause an error")
 	}
@@ -693,8 +698,8 @@
 	rootCtx, shutdown := test.V23Init()
 	defer shutdown()
 
-	ft := NewFakeTimeClock()
-	setServerListClock(ft)
+	ft := mounttablelib.NewFakeTimeClock()
+	mounttablelib.SetServerListClock(ft)
 
 	server, estr := newMT(t, "", "", "mounttable", rootCtx)
 	defer server.Stop()
@@ -793,7 +798,7 @@
 
 	// Test expired mounts
 	// "1/2/3/4/5" is still mounted from earlier.
-	ft.advance(time.Duration(ttlSecs+4) * time.Second)
+	ft.Advance(time.Duration(ttlSecs+4) * time.Second)
 	if _, err := resolve(rootCtx, naming.JoinAddressName(estr, "1/2/3/4/5")); err == nil {
 		t.Errorf("Expected failure. Got success")
 	}
diff --git a/services/mounttable/mounttablelib/neighborhood_test.go b/services/mounttable/mounttablelib/neighborhood_test.go
index 4225985..bf0c918 100644
--- a/services/mounttable/mounttablelib/neighborhood_test.go
+++ b/services/mounttable/mounttablelib/neighborhood_test.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package mounttablelib
+package mounttablelib_test
 
 import (
 	"fmt"
@@ -17,11 +17,10 @@
 	"v.io/x/lib/vlog"
 
 	_ "v.io/x/ref/runtime/factories/generic"
+	"v.io/x/ref/services/mounttable/mounttablelib"
 	"v.io/x/ref/test"
 )
 
-//go:generate v23 test generate
-
 func protocolAndAddress(e naming.Endpoint) (string, string, error) {
 	addr := e.Addr()
 	if addr == nil {
@@ -30,6 +29,10 @@
 	return addr.Network(), addr.String(), nil
 }
 
+type stopper interface {
+	Stop()
+}
+
 func TestNeighborhood(t *testing.T) {
 	rootCtx, shutdown := test.V23Init()
 	defer shutdown()
@@ -57,11 +60,11 @@
 	serverName := fmt.Sprintf("nhtest%d", os.Getpid())
 
 	// Add neighborhood server.
-	nhd, err := NewLoopbackNeighborhoodDispatcher(serverName, addresses...)
+	nhd, err := mounttablelib.NewLoopbackNeighborhoodDispatcher(serverName, addresses...)
 	if err != nil {
 		boom(t, "Failed to create neighborhood server: %s\n", err)
 	}
-	defer nhd.(*neighborhood).Stop()
+	defer nhd.(stopper).Stop()
 	if err := server.ServeDispatcher("", nhd); err != nil {
 		boom(t, "Failed to register neighborhood server: %s", err)
 	}
diff --git a/services/mounttable/mounttablelib/persist_test.go b/services/mounttable/mounttablelib/persist_test.go
index c6b7aca..a1b90b9 100644
--- a/services/mounttable/mounttablelib/persist_test.go
+++ b/services/mounttable/mounttablelib/persist_test.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package mounttablelib
+package mounttablelib_test
 
 import (
 	"fmt"
diff --git a/services/mounttable/mounttablelib/serverlist.go b/services/mounttable/mounttablelib/serverlist.go
index 123dc9d..65f1061 100644
--- a/services/mounttable/mounttablelib/serverlist.go
+++ b/services/mounttable/mounttablelib/serverlist.go
@@ -14,12 +14,12 @@
 )
 
 type serverListClock interface {
-	now() time.Time
+	Now() time.Time
 }
 
 type realTime bool
 
-func (t realTime) now() time.Time {
+func (t realTime) Now() time.Time {
 	return time.Now()
 }
 
@@ -44,11 +44,6 @@
 	return &serverList{l: list.New()}
 }
 
-// set up an alternate clock.
-func setServerListClock(x serverListClock) {
-	slc = x
-}
-
 func (sl *serverList) len() int {
 	sl.Lock()
 	defer sl.Unlock()
@@ -65,7 +60,7 @@
 // update the expiration time and move to the front of the list.  That
 // way the most recently refreshed is always first.
 func (sl *serverList) add(oa string, ttl time.Duration) {
-	expires := slc.now().Add(ttl)
+	expires := slc.Now().Add(ttl)
 	sl.Lock()
 	defer sl.Unlock()
 	for e := sl.l.Front(); e != nil; e = e.Next() {
@@ -102,7 +97,7 @@
 	sl.Lock()
 	defer sl.Unlock()
 
-	now := slc.now()
+	now := slc.Now()
 	var next *list.Element
 	removed := 0
 	for e := sl.l.Front(); e != nil; e = next {
diff --git a/services/mounttable/mounttablelib/serverlist_test.go b/services/mounttable/mounttablelib/serverlist_test.go
index 9b5a484..c061f00 100644
--- a/services/mounttable/mounttablelib/serverlist_test.go
+++ b/services/mounttable/mounttablelib/serverlist_test.go
@@ -13,22 +13,6 @@
 	vdltime "v.io/v23/vdlroot/time"
 )
 
-var now = time.Now()
-
-type fakeTime struct {
-	theTime time.Time
-}
-
-func (ft *fakeTime) now() time.Time {
-	return ft.theTime
-}
-func (ft *fakeTime) advance(d time.Duration) {
-	ft.theTime = ft.theTime.Add(d)
-}
-func NewFakeTimeClock() *fakeTime {
-	return &fakeTime{theTime: now}
-}
-
 func TestServerList(t *testing.T) {
 	eps := []string{
 		"endpoint:adfasdf@@who",
@@ -39,7 +23,7 @@
 
 	// Test adding entries.
 	ft := NewFakeTimeClock()
-	setServerListClock(ft)
+	SetServerListClock(ft)
 	sl := newServerList()
 	for i, ep := range eps {
 		sl.add(ep, time.Duration(5*i)*time.Second)
@@ -49,7 +33,7 @@
 	}
 
 	// Test timing out entries.
-	ft.advance(6 * time.Second)
+	ft.Advance(6 * time.Second)
 	if numLeft, _ := sl.removeExpired(); numLeft != len(eps)-2 {
 		t.Fatalf("got %d, want %d", sl.len(), len(eps)-2)
 	}
diff --git a/services/mounttable/mounttablelib/util_test.go b/services/mounttable/mounttablelib/util_test.go
new file mode 100644
index 0000000..b99129b
--- /dev/null
+++ b/services/mounttable/mounttablelib/util_test.go
@@ -0,0 +1,35 @@
+// Copyright 2015 The Vanadium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package mounttablelib
+
+import "time"
+
+// These routines are used by external tests.
+
+// SetServerListClock sets up an alternate clock.
+func SetServerListClock(x serverListClock) {
+	slc = x
+}
+
+// DefaultMaxNodesPerUser returns the maximum number of nodes per user.
+func DefaultMaxNodesPerUser() int {
+	return defaultMaxNodesPerUser
+}
+
+var now = time.Now()
+
+type fakeTime struct {
+	theTime time.Time
+}
+
+func NewFakeTimeClock() *fakeTime {
+	return &fakeTime{theTime: now}
+}
+func (ft *fakeTime) Now() time.Time {
+	return ft.theTime
+}
+func (ft *fakeTime) Advance(d time.Duration) {
+	ft.theTime = ft.theTime.Add(d)
+}
diff --git a/services/mounttable/mounttablelib/v23_internal_test.go b/services/mounttable/mounttablelib/v23_internal_test.go
deleted file mode 100644
index 82b7beb..0000000
--- a/services/mounttable/mounttablelib/v23_internal_test.go
+++ /dev/null
@@ -1,20 +0,0 @@
-// Copyright 2015 The Vanadium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// This file was auto-generated via go generate.
-// DO NOT UPDATE MANUALLY
-
-package mounttablelib
-
-import (
-	"os"
-	"testing"
-
-	"v.io/x/ref/test"
-)
-
-func TestMain(m *testing.M) {
-	test.Init()
-	os.Exit(m.Run())
-}
diff --git a/services/wspr/internal/lib/signature_manager_test.go b/services/wspr/internal/lib/signature_manager_test.go
index 7e50213..80fb562 100644
--- a/services/wspr/internal/lib/signature_manager_test.go
+++ b/services/wspr/internal/lib/signature_manager_test.go
@@ -22,7 +22,7 @@
 )
 
 func initContext(t *testing.T) (*context.T, clientWithTimesCalled, v23.Shutdown) {
-	ctx, shutdown := test.V23Init()
+	ctx, shutdown := test.V23InitWithParams(test.InitParams{})
 	initialSig := []signature.Interface{
 		{
 			Methods: []signature.Method{
diff --git a/test/init.go b/test/init.go
index 071d9c1..c69aca0 100644
--- a/test/init.go
+++ b/test/init.go
@@ -12,9 +12,13 @@
 
 	"v.io/v23"
 	"v.io/v23/context"
+	"v.io/v23/naming"
+	"v.io/v23/options"
 
 	"v.io/x/ref/internal/logger"
 	"v.io/x/ref/lib/flags"
+	"v.io/x/ref/lib/xrpc"
+	"v.io/x/ref/services/mounttable/mounttablelib"
 	"v.io/x/ref/test/testutil"
 )
 
@@ -61,18 +65,48 @@
 	once.Do(init)
 }
 
-// V23Init initializes a new context.T and sets a freshly created principal
-// (with a single self-signed blessing) on it. The principal setting step is
-// skipped if this function is invoked from a process run using the modules
-// package.
+// V23Init initializes the runtime and sets up some convenient infrastructure for tests:
+// - Sets a freshly created principal (with a single self-signed blessing) on it.
+// - Creates a mounttable and sets the namespace roots appropriately
+// Both steps are skipped if this function is invoked from a process run
+// using the modules package.
 func V23Init() (*context.T, v23.Shutdown) {
+	moduleProcess := len(os.Getenv("V23_SHELL_HELPER_PROCESS_ENTRY_POINT")) != 0
+	return V23InitWithParams(InitParams{
+		CreatePrincipal:  !moduleProcess,
+		CreateMounttable: !moduleProcess,
+	})
+}
+
+// Params contains parameters for tests that need to control what happens during
+// init carefully.
+type InitParams struct {
+	CreateMounttable bool // CreateMounttable creates a new mounttable.
+	CreatePrincipal  bool // CreatePrincipal creates a new principal with self-signed blessing.
+}
+
+// V23InitWithParams initializes the runtime and returns a new context and shutdown function.
+// Specific aspects of initialization can be controlled via the params struct.
+func V23InitWithParams(params InitParams) (*context.T, v23.Shutdown) {
 	ctx, shutdown := v23.Init()
-	if len(os.Getenv("V23_SHELL_HELPER_PROCESS_ENTRY_POINT")) != 0 {
-		return ctx, shutdown
+	if params.CreatePrincipal {
+		var err error
+		if ctx, err = v23.WithPrincipal(ctx, testutil.NewPrincipal(TestBlessing)); err != nil {
+			panic(err)
+		}
 	}
-	var err error
-	if ctx, err = v23.WithPrincipal(ctx, testutil.NewPrincipal(TestBlessing)); err != nil {
-		panic(err)
+	if params.CreateMounttable {
+		disp, err := mounttablelib.NewMountTableDispatcher("", "", "mounttable")
+		if err != nil {
+			panic(err)
+		}
+		s, err := xrpc.NewDispatchingServer(ctx, "", disp, options.ServesMountTable(true))
+		if err != nil {
+			panic(err)
+		}
+		ns := v23.GetNamespace(ctx)
+		ns.SetRoots(s.Status().Endpoints[0].Name())
+		ns.CacheCtl(naming.DisableCache(true))
 	}
 	return ctx, shutdown
 }