-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmodules.go
153 lines (143 loc) · 4.5 KB
/
modules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// 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 servicetest
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"v.io/v23"
"v.io/v23/context"
"v.io/v23/options"
"v.io/x/lib/gosh"
"v.io/x/ref"
"v.io/x/ref/internal/logger"
"v.io/x/ref/lib/signals"
"v.io/x/ref/services/internal/dirprinter"
"v.io/x/ref/services/mounttable/mounttablelib"
"v.io/x/ref/test"
"v.io/x/ref/test/testutil"
"v.io/x/ref/test/v23test"
)
const (
// Setting this environment variable to any non-empty value avoids
// removing the generated workspace for successful test runs (for
// failed test runs, this is already the case). This is useful when
// developing test cases.
preserveWorkspaceEnv = "V23_TEST_PRESERVE_WORKSPACE"
)
var rootMT = gosh.RegisterFunc("rootMT", func() error {
ctx, shutdown := test.V23Init()
defer shutdown()
mt, err := mounttablelib.NewMountTableDispatcher(ctx, "", "", "mounttable")
if err != nil {
return fmt.Errorf("mounttablelib.NewMountTableDispatcher failed: %s", err)
}
ctx, server, err := v23.WithNewDispatchingServer(ctx, "", mt, options.ServesMountTable(true))
if err != nil {
return fmt.Errorf("rootMT failed: %v", err)
}
gosh.SendVars(map[string]string{"NAME": server.Status().Endpoints[0].Name()})
<-signals.ShutdownOnSignals(ctx)
return nil
})
// startRootMT sets up a root mount table for tests.
func startRootMT(t *testing.T, sh *v23test.Shell) string {
c := sh.FuncCmd(rootMT)
c.Args = append(c.Args, "--v23.tcp.address=127.0.0.1:0")
c.PropagateOutput = false
c.Start()
return c.AwaitVars("NAME")["NAME"]
}
// setNSRoots sets the roots for the local runtime's namespace.
func setNSRoots(t *testing.T, ctx *context.T, roots ...string) {
ns := v23.GetNamespace(ctx)
if err := ns.SetRoots(roots...); err != nil {
t.Fatal(testutil.FormatLogLine(3, "SetRoots(%v) failed: %v", roots, err))
}
}
// CreateShellAndMountTable builds a new shell and starts a root mount table.
// Returns the shell and a cleanup function.
// TODO(sadovsky): Use v23test.StartRootMountTable.
func CreateShellAndMountTable(t *testing.T, ctx *context.T) (*v23test.Shell, func()) {
sh := v23test.NewShell(t, ctx)
sh.PropagateChildOutput = true
ok := false
defer func() {
if !ok {
sh.Cleanup()
}
}()
mtName := startRootMT(t, sh)
ctx.VI(1).Infof("Started root mount table with name %s", mtName)
oldNamespaceRoots := v23.GetNamespace(ctx).Roots()
fn := func() {
ctx.VI(1).Info("------------ CLEANUP ------------")
ctx.VI(1).Info("---------------------------------")
ctx.VI(1).Info("--(cleaning up shell)------------")
sh.Cleanup()
ctx.VI(1).Info("--(done cleaning up shell)-------")
setNSRoots(t, ctx, oldNamespaceRoots...)
}
setNSRoots(t, ctx, mtName)
sh.Vars[ref.EnvNamespacePrefix] = mtName
ok = true
return sh, fn
}
// CreateShell builds a new shell.
// Returns the shell and a cleanup function.
func CreateShell(t *testing.T, ctx *context.T) (*v23test.Shell, func()) {
sh := v23test.NewShell(t, ctx)
sh.PropagateChildOutput = true
ok := false
defer func() {
if !ok {
sh.Cleanup()
}
}()
fn := func() {
ctx.VI(1).Info("------------ CLEANUP ------------")
ctx.VI(1).Info("---------------------------------")
ctx.VI(1).Info("--(cleaning up shell)------------")
sh.Cleanup()
ctx.VI(1).Info("--(done cleaning up shell)-------")
}
nsRoots := v23.GetNamespace(ctx).Roots()
if len(nsRoots) == 0 {
t.Fatalf("no namespace roots")
}
sh.Vars[ref.EnvNamespacePrefix] = nsRoots[0]
ok = true
return sh, fn
}
// SetupRootDir sets up and returns a directory for the root and returns
// a cleanup function.
func SetupRootDir(t *testing.T, prefix string) (string, func()) {
rootDir, err := ioutil.TempDir("", prefix)
if err != nil {
t.Fatalf("Failed to set up temporary dir for test: %v", err)
}
// On some operating systems (e.g. darwin) os.TempDir() can return a
// symlink. To avoid having to account for this eventuality later,
// evaluate the symlink.
rootDir, err = filepath.EvalSymlinks(rootDir)
if err != nil {
logger.Global().Fatalf("EvalSymlinks(%v) failed: %v", rootDir, err)
}
return rootDir, func() {
if t.Failed() || os.Getenv(preserveWorkspaceEnv) != "" {
t.Logf("A dump of the %s workspace at %v:", prefix, rootDir)
var dump bytes.Buffer
if err := dirprinter.DumpDir(&dump, rootDir); err != nil {
t.Logf("Failed to dump %v: %v", rootDir, err)
} else {
t.Log("\n" + dump.String())
}
} else {
os.RemoveAll(rootDir)
}
}
}