"core": Makes Tests pass under agent
This is part 1 out of n of a series of CLs to make all go tests
pass when they run under an agent (for e.g., inside vbash)
This CL fixes all tests under "veyron/runtimes/google/..."
Most tests currenly fail because of the following pattern:
func TextXXX(t *testing.T) {
runtime, err := rt.New() // In the new world this is veyron2.Init()
ctx := runtime.NewContext()
doSomething(ctx)
...
}
The issue here is that rt.New (or veyron2.Init) initializes the
principal via the environment variables (for e.g., when run inside
vbash, the test initializes its principal through the agent). This is
a problem for multiple reasons:
- the test is not self-contained
- the test can pollute the environment provided principal's state
- the test was likely written with the assumption that rt.New() always
generates a self-signed principal which is False.
To fix this, I am changing the code to:
func TextXXX(t *testing.T) {
runtime, err := rt.New() // In the new world this is veyron2.Init()
ctx := runtime.NewContext()
ctx, err = veyron2.SetPrincipal(tsecurity.NewPrincipal(...))
doSomething(ctx)
...
}
Change-Id: I2d427f4284b87e206057011f88f88575c0868af3
diff --git a/runtimes/google/ipc/benchmark/benchmark_test.go b/runtimes/google/ipc/benchmark/benchmark_test.go
index 89b1e4f..941a22d 100644
--- a/runtimes/google/ipc/benchmark/benchmark_test.go
+++ b/runtimes/google/ipc/benchmark/benchmark_test.go
@@ -5,6 +5,7 @@
"testing"
"v.io/core/veyron/lib/testutil/benchmark"
+ tsecurity "v.io/core/veyron/lib/testutil/security"
"v.io/core/veyron/profiles"
"v.io/core/veyron2"
@@ -106,6 +107,9 @@
panic(err)
}
ctx := vrt.NewContext()
+ if ctx, err = veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
+ panic(err)
+ }
var serverStop func()
serverAddr, serverStop = StartServer(ctx, profiles.LocalListenSpec)
diff --git a/runtimes/google/ipc/benchmark/glob/glob_test.go b/runtimes/google/ipc/benchmark/glob/glob_test.go
index db032b3..951b16d 100644
--- a/runtimes/google/ipc/benchmark/glob/glob_test.go
+++ b/runtimes/google/ipc/benchmark/glob/glob_test.go
@@ -11,6 +11,7 @@
"v.io/core/veyron2/rt"
"v.io/core/veyron2/security"
+ tsecurity "v.io/core/veyron/lib/testutil/security"
"v.io/core/veyron/profiles"
)
@@ -161,6 +162,9 @@
}
defer runtime.Cleanup()
ctx := runtime.NewContext()
+ if ctx, err = veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
+ panic(err)
+ }
addr, stop, err := startServer(b, ctx, obj)
if err != nil {
diff --git a/runtimes/google/ipc/client_test.go b/runtimes/google/ipc/client_test.go
index e44b5bb..499c7bc 100644
--- a/runtimes/google/ipc/client_test.go
+++ b/runtimes/google/ipc/client_test.go
@@ -27,17 +27,22 @@
inaming "v.io/core/veyron/runtimes/google/naming"
)
-var r veyron2.Runtime
-var gctx *context.T
-
func init() {
modules.RegisterChild("ping", "<name>", childPing)
- var err error
- if r, err = rt.New(); err != nil {
+}
+
+func newCtx() (*context.T, veyron2.Shutdown) {
+ r, err := rt.New()
+ if err != nil {
panic(err)
}
- gctx = r.NewContext()
- veyron2.GetNamespace(gctx).CacheCtl(naming.DisableCache(true))
+ ctx := r.NewContext()
+ if ctx, err = veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
+ panic(err)
+ }
+
+ veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true))
+ return ctx, r.Cleanup
}
func testArgs(args ...string) []string {
@@ -97,8 +102,8 @@
return nil
}
-func numServers(t *testing.T, name string) int {
- me, err := veyron2.GetNamespace(gctx).Resolve(gctx, name)
+func numServers(t *testing.T, ctx *context.T, name string) int {
+ me, err := veyron2.GetNamespace(ctx).Resolve(ctx, name)
if err != nil {
return 0
}
@@ -108,7 +113,9 @@
// TODO(cnicolaou): figure out how to test and see what the internals
// of tryCall are doing - e.g. using stats counters.
func TestMultipleEndpoints(t *testing.T) {
- sh, fn := runMountTable(t, gctx)
+ ctx, shutdown := newCtx()
+ defer shutdown()
+ sh, fn := runMountTable(t, ctx)
defer fn()
srv, err := sh.Start(core.EchoServerCommand, nil, testArgs("echoServer", "echoServer")...)
if err != nil {
@@ -119,7 +126,7 @@
s.ExpectVar("NAME")
// Verify that there are 1 entries for echoServer in the mount table.
- if got, want := numServers(t, "echoServer"), 1; got != want {
+ if got, want := numServers(t, ctx, "echoServer"), 1; got != want {
t.Fatalf("got: %q, want: %q", got, want)
}
@@ -130,13 +137,13 @@
// 203.0.113.0 is TEST-NET-3 from RFC5737
ep := naming.FormatEndpoint("tcp", fmt.Sprintf("203.0.113.%d:443", i))
n := naming.JoinAddressName(ep, "")
- if err := veyron2.GetNamespace(gctx).Mount(gctx, "echoServer", n, time.Hour); err != nil {
+ if err := veyron2.GetNamespace(ctx).Mount(ctx, "echoServer", n, time.Hour); err != nil {
t.Fatalf("unexpected error: %s", err)
}
}
// Verify that there are 101 entries for echoServer in the mount table.
- if got, want := numServers(t, "echoServer"), 101; got != want {
+ if got, want := numServers(t, ctx, "echoServer"), 101; got != want {
t.Fatalf("got: %q, want: %q", got, want)
}
@@ -151,13 +158,15 @@
srv.Shutdown(nil, nil)
// Verify that there are 100 entries for echoServer in the mount table.
- if got, want := numServers(t, "echoServer"), 100; got != want {
+ if got, want := numServers(t, ctx, "echoServer"), 100; got != want {
t.Fatalf("got: %d, want: %d", got, want)
}
}
func TestTimeoutCall(t *testing.T) {
- ctx, _ := context.WithTimeout(gctx, 100*time.Millisecond)
+ ctx, shutdown := newCtx()
+ defer shutdown()
+ ctx, _ = context.WithTimeout(ctx, 100*time.Millisecond)
name := naming.JoinAddressName(naming.FormatEndpoint("tcp", "203.0.113.10:443"), "")
client := veyron2.GetClient(ctx)
_, err := client.StartCall(ctx, name, "echo", []interface{}{"args don't matter"})
@@ -167,8 +176,15 @@
}
func childPing(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
+ r, err := rt.New()
+ if err != nil {
+ panic(err)
+ }
+ ctx := r.NewContext()
+ veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true))
+
name := args[1]
- call, err := veyron2.GetClient(gctx).StartCall(gctx, name, "Ping", nil)
+ call, err := veyron2.GetClient(ctx).StartCall(ctx, name, "Ping", nil)
if err != nil {
fmt.Errorf("unexpected error: %s", err)
}
@@ -223,9 +239,11 @@
}
func TestTimeoutResponse(t *testing.T) {
- name, fn := initServer(t, gctx)
+ ctx, shutdown := newCtx()
+ defer shutdown()
+ name, fn := initServer(t, ctx)
defer fn()
- ctx, _ := context.WithTimeout(gctx, 100*time.Millisecond)
+ ctx, _ = context.WithTimeout(ctx, 100*time.Millisecond)
call, err := veyron2.GetClient(ctx).StartCall(ctx, name, "Sleep", nil)
if err != nil {
testForVerror(t, err, verror.Timeout, verror.BadProtocol)
@@ -237,17 +255,19 @@
}
func TestArgsAndResponses(t *testing.T) {
- name, fn := initServer(t, gctx)
+ ctx, shutdown := newCtx()
+ defer shutdown()
+ name, fn := initServer(t, ctx)
defer fn()
- call, err := veyron2.GetClient(gctx).StartCall(gctx, name, "Sleep", []interface{}{"too many args"})
+ call, err := veyron2.GetClient(ctx).StartCall(ctx, name, "Sleep", []interface{}{"too many args"})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
verr := call.Finish(&err)
testForVerror(t, verr, verror.BadProtocol)
- call, err = veyron2.GetClient(gctx).StartCall(gctx, name, "Ping", nil)
+ call, err = veyron2.GetClient(ctx).StartCall(ctx, name, "Ping", nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
@@ -264,10 +284,19 @@
// The server and client use different runtimes and hence different
// principals and without any shared blessings the server will deny
// access to the client
- name, fn := initServer(t, r1.NewContext())
+ ctx1 := r1.NewContext()
+ var err error
+ if ctx1, err = veyron2.SetPrincipal(ctx1, tsecurity.NewPrincipal("test-blessing")); err != nil {
+ t.Fatal(err)
+ }
+
+ name, fn := initServer(t, ctx1)
defer fn()
ctx2 := r2.NewContext()
+ if ctx2, err = veyron2.SetPrincipal(ctx2, tsecurity.NewPrincipal("test-blessing")); err != nil {
+ t.Fatal(err)
+ }
client2 := veyron2.GetClient(ctx2)
call, err := client2.StartCall(ctx2, name, "Sleep", nil)
if err != nil {
@@ -278,10 +307,12 @@
}
func TestCancelledBeforeFinish(t *testing.T) {
- name, fn := initServer(t, gctx)
+ ctx, shutdown := newCtx()
+ defer shutdown()
+ name, fn := initServer(t, ctx)
defer fn()
- ctx, cancel := context.WithCancel(gctx)
+ ctx, cancel := context.WithCancel(ctx)
call, err := veyron2.GetClient(ctx).StartCall(ctx, name, "Sleep", nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
@@ -294,10 +325,12 @@
}
func TestCancelledDuringFinish(t *testing.T) {
- name, fn := initServer(t, gctx)
+ ctx, shutdown := newCtx()
+ defer shutdown()
+ name, fn := initServer(t, ctx)
defer fn()
- ctx, cancel := context.WithCancel(gctx)
+ ctx, cancel := context.WithCancel(ctx)
call, err := veyron2.GetClient(ctx).StartCall(ctx, name, "Sleep", nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
@@ -313,7 +346,9 @@
}
func TestRendezvous(t *testing.T) {
- sh, fn := runMountTable(t, gctx)
+ ctx, shutdown := newCtx()
+ defer shutdown()
+ sh, fn := runMountTable(t, ctx)
defer fn()
name := "echoServer"
@@ -330,7 +365,7 @@
}
go startServer()
- call, err := veyron2.GetClient(gctx).StartCall(gctx, name, "Echo", []interface{}{"hello"})
+ call, err := veyron2.GetClient(ctx).StartCall(ctx, name, "Echo", []interface{}{"hello"})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
@@ -347,10 +382,12 @@
}
func TestCallback(t *testing.T) {
- sh, fn := runMountTable(t, gctx)
+ ctx, shutdown := newCtx()
+ defer shutdown()
+ sh, fn := runMountTable(t, ctx)
defer fn()
- name, fn := initServer(t, gctx)
+ name, fn := initServer(t, ctx)
defer fn()
srv, err := sh.Start("ping", nil, name)
@@ -364,11 +401,13 @@
}
func TestStreamTimeout(t *testing.T) {
- name, fn := initServer(t, gctx)
+ ctx, shutdown := newCtx()
+ defer shutdown()
+ name, fn := initServer(t, ctx)
defer fn()
want := 10
- ctx, _ := context.WithTimeout(gctx, 300*time.Millisecond)
+ ctx, _ = context.WithTimeout(ctx, 300*time.Millisecond)
call, err := veyron2.GetClient(ctx).StartCall(ctx, name, "Source", []interface{}{want})
if err != nil {
if !verror.Is(err, verror.Timeout.ID) && !verror.Is(err, verror.BadProtocol.ID) {
@@ -397,10 +436,12 @@
}
func TestStreamAbort(t *testing.T) {
- name, fn := initServer(t, gctx)
+ ctx, shutdown := newCtx()
+ defer shutdown()
+ name, fn := initServer(t, ctx)
defer fn()
- call, err := veyron2.GetClient(gctx).StartCall(gctx, name, "Sink", nil)
+ call, err := veyron2.GetClient(ctx).StartCall(ctx, name, "Sink", nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
@@ -434,10 +475,12 @@
}
func TestNoServersAvailable(t *testing.T) {
- _, fn := runMountTable(t, gctx)
+ ctx, shutdown := newCtx()
+ defer shutdown()
+ _, fn := runMountTable(t, ctx)
defer fn()
name := "noservers"
- ctx, _ := context.WithTimeout(gctx, 300*time.Millisecond)
+ ctx, _ = context.WithTimeout(ctx, 300*time.Millisecond)
call, verr := veyron2.GetClient(ctx).StartCall(ctx, name, "Sleep", nil)
if verr != nil {
testForVerror(t, verr, verror.Timeout, verror.BadProtocol, verror.NoExist)
@@ -448,11 +491,13 @@
}
func TestNoMountTable(t *testing.T) {
- veyron2.GetNamespace(gctx).SetRoots()
+ ctx, shutdown := newCtx()
+ defer shutdown()
+ veyron2.GetNamespace(ctx).SetRoots()
name := "a_mount_table_entry"
// If there is no mount table, then we'll get a NoServers error message.
- ctx, _ := context.WithTimeout(gctx, 300*time.Millisecond)
+ ctx, _ = context.WithTimeout(ctx, 300*time.Millisecond)
_, verr := veyron2.GetClient(ctx).StartCall(ctx, name, "Sleep", nil)
testForVerror(t, verr, verror.NoServers)
}
diff --git a/runtimes/google/ipc/glob_test.go b/runtimes/google/ipc/glob_test.go
index 20b60af..799a67e 100644
--- a/runtimes/google/ipc/glob_test.go
+++ b/runtimes/google/ipc/glob_test.go
@@ -15,6 +15,7 @@
"v.io/core/veyron/lib/glob"
"v.io/core/veyron/lib/testutil"
+ tsecurity "v.io/core/veyron/lib/testutil/security"
"v.io/core/veyron/profiles"
)
@@ -41,6 +42,9 @@
}
defer runtime.Cleanup()
ctx := runtime.NewContext()
+ if ctx, err = veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
+ t.Fatal(err)
+ }
namespace := []string{
"a/b/c1/d1",
diff --git a/runtimes/google/ipc/resolve_test.go b/runtimes/google/ipc/resolve_test.go
index ab64566..53dfdbc 100644
--- a/runtimes/google/ipc/resolve_test.go
+++ b/runtimes/google/ipc/resolve_test.go
@@ -12,6 +12,7 @@
"v.io/core/veyron/lib/expect"
"v.io/core/veyron/lib/modules"
"v.io/core/veyron/lib/modules/core"
+ tsecurity "v.io/core/veyron/lib/testutil/security"
iipc "v.io/core/veyron/runtimes/google/ipc"
inaming "v.io/core/veyron/runtimes/google/naming"
)
@@ -40,6 +41,9 @@
}
defer runtime.Cleanup()
ctx := runtime.NewContext()
+ if ctx, err = veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
+ t.Fatal(err)
+ }
ns := veyron2.GetNamespace(ctx)
ns.SetRoots(root)
diff --git a/runtimes/google/ipc/signature_test.go b/runtimes/google/ipc/signature_test.go
index 26a5ec5..a0604c8 100644
--- a/runtimes/google/ipc/signature_test.go
+++ b/runtimes/google/ipc/signature_test.go
@@ -15,6 +15,7 @@
"v.io/core/veyron2/vdl/vdlroot/src/signature"
"v.io/core/veyron/lib/testutil"
+ tsecurity "v.io/core/veyron/lib/testutil/security"
"v.io/core/veyron/profiles"
)
@@ -65,7 +66,9 @@
}
defer runtime.Cleanup()
ctx := runtime.NewContext()
-
+ if ctx, err = veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
+ t.Fatal(err)
+ }
ep, stop, err := startSigServer(ctx, sigImpl{})
if err != nil {
t.Fatalf("startSigServer: %v", err)
@@ -116,7 +119,9 @@
}
defer runtime.Cleanup()
ctx := runtime.NewContext()
-
+ if ctx, err = veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
+ t.Fatal(err)
+ }
ep, stop, err := startSigServer(ctx, sigImpl{})
if err != nil {
t.Fatalf("startSigServer: %v", err)
diff --git a/runtimes/google/ipc/simple_test.go b/runtimes/google/ipc/simple_test.go
index 39b1147..15b2a3b 100644
--- a/runtimes/google/ipc/simple_test.go
+++ b/runtimes/google/ipc/simple_test.go
@@ -67,11 +67,13 @@
}
func TestSimpleRPC(t *testing.T) {
- name, fn := initServer(t, gctx)
+ ctx, shutdown := newCtx()
+ defer shutdown()
+ name, fn := initServer(t, ctx)
defer fn()
- client := veyron2.GetClient(r.NewContext())
- call, err := client.StartCall(r.NewContext(), name, "Ping", nil)
+ client := veyron2.GetClient(ctx)
+ call, err := client.StartCall(ctx, name, "Ping", nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
@@ -87,12 +89,13 @@
}
func TestSimpleStreaming(t *testing.T) {
- name, fn := initServer(t, gctx)
+ ctx, shutdown := newCtx()
+ defer shutdown()
+ name, fn := initServer(t, ctx)
defer fn()
- ctx := r.NewContext()
inc := 1
- call, err := veyron2.GetClient(r.NewContext()).StartCall(ctx, name, "Inc", []interface{}{inc})
+ call, err := veyron2.GetClient(ctx).StartCall(ctx, name, "Inc", []interface{}{inc})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}