Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Cosmos Nicolaou | 4a77c19 | 2015-02-08 15:29:18 -0800 | [diff] [blame] | 5 | package v23tests |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 6 | |
| 7 | import ( |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 8 | "errors" |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 9 | "fmt" |
| 10 | "io/ioutil" |
Cosmos Nicolaou | 47d55d9 | 2015-04-08 14:49:12 -0700 | [diff] [blame] | 11 | "math/rand" |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 12 | "os" |
| 13 | "os/exec" |
| 14 | "path" |
| 15 | "path/filepath" |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 16 | "runtime" |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 17 | "strings" |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 18 | "syscall" |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 19 | "testing" |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 20 | "time" |
| 21 | |
Cosmos Nicolaou | 47d55d9 | 2015-04-08 14:49:12 -0700 | [diff] [blame] | 22 | "v.io/x/lib/vlog" |
| 23 | |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 24 | "v.io/v23" |
| 25 | "v.io/v23/security" |
Cosmos Nicolaou | 47d55d9 | 2015-04-08 14:49:12 -0700 | [diff] [blame] | 26 | |
Asim Shankar | 59b8b69 | 2015-03-30 01:23:36 -0700 | [diff] [blame] | 27 | "v.io/x/ref/envvar" |
Todd Wang | 8850968 | 2015-04-10 10:28:24 -0700 | [diff] [blame] | 28 | "v.io/x/ref/services/agent/agentlib" |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 29 | "v.io/x/ref/test" |
| 30 | "v.io/x/ref/test/modules" |
Asim Shankar | 4a69828 | 2015-03-21 21:59:18 -0700 | [diff] [blame] | 31 | "v.io/x/ref/test/testutil" |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 32 | ) |
| 33 | |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 34 | // TB is an exact mirror of testing.TB. It is provided to allow for testing |
| 35 | // of this package using a mock implementation. As per testing.TB, it is not |
| 36 | // intended to be implemented outside of this package. |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 37 | type TB interface { |
| 38 | Error(args ...interface{}) |
| 39 | Errorf(format string, args ...interface{}) |
| 40 | Fail() |
| 41 | FailNow() |
| 42 | Failed() bool |
| 43 | Fatal(args ...interface{}) |
| 44 | Fatalf(format string, args ...interface{}) |
| 45 | Log(args ...interface{}) |
| 46 | Logf(format string, args ...interface{}) |
| 47 | Skip(args ...interface{}) |
| 48 | SkipNow() |
| 49 | Skipf(format string, args ...interface{}) |
| 50 | Skipped() bool |
| 51 | } |
James Ring | 855dfd4 | 2015-01-23 12:30:58 -0800 | [diff] [blame] | 52 | |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 53 | // T represents an integration test environment. |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 54 | type T struct { |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 55 | // The embedded TB |
| 56 | TB |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 57 | |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 58 | // The function to shutdown the context used to create the environment. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 59 | shutdown v23.Shutdown |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 60 | |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 61 | // The shell to use to start commands. |
| 62 | shell *modules.Shell |
| 63 | |
| 64 | // The environment's root security principal. |
| 65 | principal security.Principal |
| 66 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 67 | // Maps path to Binary. |
| 68 | builtBinaries map[string]*Binary |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 69 | |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 70 | tempFiles []*os.File |
| 71 | tempDirs []string |
| 72 | binDir, cachedBinDir string |
| 73 | dirStack []string |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 74 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 75 | invocations []*Invocation |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 78 | var errNotShutdown = errors.New("has not been shutdown") |
| 79 | |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 80 | // Caller returns a string of the form <filename>:<lineno> for the |
| 81 | // caller specified by skip, where skip is as per runtime.Caller. |
| 82 | func Caller(skip int) string { |
| 83 | _, file, line, _ := runtime.Caller(skip + 1) |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 84 | return fmt.Sprintf("%s:%d", filepath.Base(file), line) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Matt Rosencrantz | 8df1ac3 | 2015-04-27 20:21:28 -0700 | [diff] [blame^] | 87 | // SkipInRegressionBefore skips the test if this is being run in a regression |
| 88 | // test and some of the binaries are older than the specified date. |
| 89 | // This is useful when we're breaking compatibility and we know it. |
| 90 | // The parameter is a string formatted date YYYY-MM-DD. |
| 91 | func (t *T) SkipInRegressionBefore(dateStr string) { |
| 92 | testStr := os.Getenv("V23_REGTEST_DATE") |
| 93 | if testStr == "" { |
| 94 | return |
| 95 | } |
| 96 | testDate, err := time.Parse("2006-01-02", testStr) |
| 97 | if err != nil { |
| 98 | t.Fatalf("%s: could not parse V23_REGTEST_DATE=%q: %v", Caller(1), testStr, err) |
| 99 | } |
| 100 | date, err := time.Parse("2006-01-02", dateStr) |
| 101 | if err != nil { |
| 102 | t.Fatalf("%s: could not parse date %q: %v", Caller(1), dateStr, err) |
| 103 | } |
| 104 | if testDate.Before(date) { |
| 105 | t.Skipf("Skipping regression test with binary from %s", testStr) |
| 106 | } |
| 107 | } |
| 108 | |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 109 | // Run constructs a Binary for path and invokes Run on it. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 110 | func (t *T) Run(path string, args ...string) string { |
| 111 | return t.BinaryFromPath(path).run(args...) |
| 112 | } |
| 113 | |
| 114 | // Run constructs a Binary for path and invokes Run on it using |
| 115 | // the specified StartOpts |
| 116 | func (t *T) RunWithOpts(opts modules.StartOpts, path string, args ...string) string { |
| 117 | b := t.BinaryFromPath(path) |
| 118 | return b.WithStartOpts(opts).run(args...) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // WaitFunc is the type of the functions to be used in conjunction |
| 122 | // with WaitFor and WaitForAsync. It should return a value or an error |
| 123 | // when it wants those functions to terminate, returning a nil value |
| 124 | // and nil error will result in it being called again after the specified |
| 125 | // delay time specified in the calls to WaitFor and WaitForAsync. |
| 126 | type WaitFunc func() (interface{}, error) |
| 127 | |
| 128 | // WaitFor calls fn at least once with the specified delay value |
| 129 | // between iterations until the first of the following is encountered: |
| 130 | // 1. fn returns a non-nil value. |
| 131 | // 2. fn returns an error value |
| 132 | // 3. fn is executed at least once and the specified timeout is exceeded. |
| 133 | // |
| 134 | // WaitFor returns the non-nil value for the first case and calls e.Fatalf for |
| 135 | // the other two cases. |
| 136 | // WaitFor will always run fn at least once to completion and hence it will |
| 137 | // hang if that first iteration of fn hangs. If this behaviour is not |
| 138 | // appropriate, then WaitForAsync should be used. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 139 | func (t *T) WaitFor(fn WaitFunc, delay, timeout time.Duration) interface{} { |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 140 | deadline := time.Now().Add(timeout) |
| 141 | for { |
| 142 | val, err := fn() |
| 143 | if val != nil { |
| 144 | return val |
| 145 | } |
| 146 | if err != nil { |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 147 | t.Fatalf("%s: the WaitFunc returned an error: %v", Caller(1), err) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 148 | } |
| 149 | if time.Now().After(deadline) { |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 150 | t.Fatalf("%s: timed out after %s", Caller(1), timeout) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 151 | } |
| 152 | time.Sleep(delay) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // WaitForAsync is like WaitFor except that it calls fn in a goroutine |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 157 | // and can timeout during the execution of fn. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 158 | func (t *T) WaitForAsync(fn WaitFunc, delay, timeout time.Duration) interface{} { |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 159 | resultCh := make(chan interface{}) |
| 160 | errCh := make(chan interface{}) |
| 161 | go func() { |
| 162 | for { |
| 163 | val, err := fn() |
| 164 | if val != nil { |
| 165 | resultCh <- val |
| 166 | return |
| 167 | } |
| 168 | if err != nil { |
| 169 | errCh <- err |
| 170 | return |
| 171 | } |
| 172 | time.Sleep(delay) |
| 173 | } |
| 174 | }() |
| 175 | select { |
| 176 | case err := <-errCh: |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 177 | t.Fatalf("%s: the WaitFunc returned error: %v", Caller(1), err) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 178 | case result := <-resultCh: |
| 179 | return result |
| 180 | case <-time.After(timeout): |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 181 | t.Fatalf("%s: timed out after %s", Caller(1), timeout) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 182 | } |
| 183 | return nil |
| 184 | } |
| 185 | |
| 186 | // Pushd pushes the current working directory to the stack of |
| 187 | // directories, returning it as its result, and changes the working |
| 188 | // directory to dir. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 189 | func (t *T) Pushd(dir string) string { |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 190 | cwd, err := os.Getwd() |
| 191 | if err != nil { |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 192 | t.Fatalf("%s: Getwd failed: %s", Caller(1), err) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 193 | } |
| 194 | if err := os.Chdir(dir); err != nil { |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 195 | t.Fatalf("%s: Chdir failed: %s", Caller(1), err) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 196 | } |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 197 | vlog.VI(1).Infof("Pushd: %s -> %s", cwd, dir) |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 198 | t.dirStack = append(t.dirStack, cwd) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 199 | return cwd |
| 200 | } |
| 201 | |
| 202 | // Popd pops the most recent entry from the directory stack and changes |
| 203 | // the working directory to that directory. It returns the new working |
| 204 | // directory as its result. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 205 | func (t *T) Popd() string { |
| 206 | if len(t.dirStack) == 0 { |
| 207 | t.Fatalf("%s: directory stack empty", Caller(1)) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 208 | } |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 209 | dir := t.dirStack[len(t.dirStack)-1] |
| 210 | t.dirStack = t.dirStack[:len(t.dirStack)-1] |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 211 | if err := os.Chdir(dir); err != nil { |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 212 | t.Fatalf("%s: Chdir failed: %s", Caller(1), err) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 213 | } |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 214 | vlog.VI(1).Infof("Popd: -> %s", dir) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 215 | return dir |
| 216 | } |
| 217 | |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 218 | // Caller returns a string of the form <filename>:<lineno> for the |
| 219 | // caller specified by skip, where skip is as per runtime.Caller. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 220 | func (t *T) Caller(skip int) string { |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 221 | return Caller(skip + 1) |
| 222 | } |
| 223 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 224 | // Principal returns the security principal of this environment. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 225 | func (t *T) Principal() security.Principal { |
| 226 | return t.principal |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 229 | // Cleanup cleans up the environment, deletes all its artifacts and |
| 230 | // kills all subprocesses. It will kill subprocesses in LIFO order. |
| 231 | // Cleanup checks to see if the test has failed and logs information |
| 232 | // as to the state of the processes it was asked to invoke up to that |
| 233 | // point and optionally, if the --v23.tests.shell-on-fail flag is set |
| 234 | // then it will run a debug shell before cleaning up its state. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 235 | func (t *T) Cleanup() { |
| 236 | if t.Failed() { |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 237 | if test.IntegrationTestsDebugShellOnError { |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 238 | t.DebugSystemShell() |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 239 | } |
| 240 | // Print out a summary of the invocations and their status. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 241 | for i, inv := range t.invocations { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 242 | if inv.hasShutdown && inv.Exists() { |
| 243 | m := fmt.Sprintf("%d: %s has been shutdown but still exists: %v", i, inv.path, inv.shutdownErr) |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 244 | t.Log(m) |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 245 | vlog.VI(1).Info(m) |
| 246 | vlog.VI(2).Infof("%d: %s %v", i, inv.path, inv.args) |
| 247 | continue |
| 248 | } |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 249 | if inv.shutdownErr != nil { |
| 250 | m := fmt.Sprintf("%d: %s: shutdown status: %v", i, inv.path, inv.shutdownErr) |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 251 | t.Log(m) |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 252 | vlog.VI(1).Info(m) |
| 253 | vlog.VI(2).Infof("%d: %s %v", i, inv.path, inv.args) |
| 254 | } |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 258 | vlog.VI(1).Infof("V23Test.Cleanup") |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 259 | // Shut down all processes in LIFO order before attempting to delete any |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 260 | // files/directories to avoid potential 'file system busy' problems |
| 261 | // on non-unix systems. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 262 | for i := len(t.invocations); i > 0; i-- { |
| 263 | inv := t.invocations[i-1] |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 264 | if inv.hasShutdown { |
| 265 | vlog.VI(1).Infof("V23Test.Cleanup: %q has been shutdown", inv.Path()) |
| 266 | continue |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 267 | } |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 268 | vlog.VI(1).Infof("V23Test.Cleanup: Kill: %q", inv.Path()) |
| 269 | err := inv.Kill(syscall.SIGTERM) |
| 270 | inv.Wait(os.Stdout, os.Stderr) |
| 271 | vlog.VI(1).Infof("V23Test.Cleanup: Killed: %q: %v", inv.Path(), err) |
| 272 | } |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 273 | vlog.VI(1).Infof("V23Test.Cleanup: all invocations taken care of.") |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 274 | |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 275 | if err := t.shell.Cleanup(os.Stdout, os.Stderr); err != nil { |
| 276 | t.Fatalf("WARNING: could not clean up shell (%v)", err) |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 279 | vlog.VI(1).Infof("V23Test.Cleanup: cleaning up binaries & files") |
| 280 | |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 281 | for _, tempFile := range t.tempFiles { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 282 | vlog.VI(1).Infof("V23Test.Cleanup: cleaning up %s", tempFile.Name()) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 283 | if err := tempFile.Close(); err != nil { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 284 | vlog.Errorf("WARNING: Close(%q) failed: %v", tempFile.Name(), err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 285 | } |
James Ring | 23b5486 | 2015-01-07 11:54:48 -0800 | [diff] [blame] | 286 | if err := os.RemoveAll(tempFile.Name()); err != nil { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 287 | vlog.Errorf("WARNING: RemoveAll(%q) failed: %v", tempFile.Name(), err) |
James Ring | 23b5486 | 2015-01-07 11:54:48 -0800 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 291 | for _, tempDir := range t.tempDirs { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 292 | vlog.VI(1).Infof("V23Test.Cleanup: cleaning up %s", tempDir) |
Cosmos Nicolaou | 185c0c6 | 2015-04-13 21:22:43 -0700 | [diff] [blame] | 293 | vlog.Infof("V23Test.Cleanup: cleaning up %s", tempDir) |
James Ring | 23b5486 | 2015-01-07 11:54:48 -0800 | [diff] [blame] | 294 | if err := os.RemoveAll(tempDir); err != nil { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 295 | vlog.Errorf("WARNING: RemoveAll(%q) failed: %v", tempDir, err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 299 | // shutdown the runtime |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 300 | t.shutdown() |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 303 | // GetVar returns the variable associated with the specified key |
| 304 | // and an indication of whether it is defined or not. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 305 | func (t *T) GetVar(key string) (string, bool) { |
| 306 | return t.shell.GetVar(key) |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 309 | // SetVar sets the value to be associated with key. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 310 | func (t *T) SetVar(key, value string) { |
| 311 | t.shell.SetVar(key, value) |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 314 | // ClearVar removes the speficied variable from the Shell's environment |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 315 | func (t *T) ClearVar(key string) { |
| 316 | t.shell.ClearVar(key) |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 319 | func writeStringOrDie(t *T, f *os.File, s string) { |
James Ring | 0bad567 | 2015-01-05 09:51:36 -0800 | [diff] [blame] | 320 | if _, err := f.WriteString(s); err != nil { |
James Ring | 0d525c2 | 2014-12-30 12:03:31 -0800 | [diff] [blame] | 321 | t.Fatalf("Write() failed: %v", err) |
| 322 | } |
| 323 | } |
| 324 | |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 325 | // DebugSystemShell drops the user into a debug system shell (e.g. bash) |
| 326 | // with any environment variables specified in env... (in VAR=VAL format) |
| 327 | // available to it. |
| 328 | // If there is no controlling TTY, DebugSystemShell will emit a warning message |
| 329 | // and take no futher action. The DebugSystemShell also sets some environment |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 330 | // variables that relate to the running test: |
| 331 | // - V23_TMP_DIR<#> contains the name of each temp directory created. |
| 332 | // - V23_BIN_DIR contains the name of the directory containing binaries. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 333 | func (t *T) DebugSystemShell(env ...string) { |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 334 | // Get the current working directory. |
| 335 | cwd, err := os.Getwd() |
| 336 | if err != nil { |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 337 | t.Fatalf("Getwd() failed: %v", err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | // Transfer stdin, stdout, and stderr to the new process |
| 341 | // and also set target directory for the shell to start in. |
| 342 | dev := "/dev/tty" |
James Ring | 0d525c2 | 2014-12-30 12:03:31 -0800 | [diff] [blame] | 343 | fd, err := syscall.Open(dev, syscall.O_RDWR, 0) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 344 | if err != nil { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 345 | vlog.Errorf("WARNING: Open(%v) failed, was asked to create a debug shell but cannot: %v", dev, err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 346 | return |
| 347 | } |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 348 | |
Ryan Brown | 61d6938 | 2015-02-25 11:13:39 -0800 | [diff] [blame] | 349 | var agentFile *os.File |
Asim Shankar | 34fb249 | 2015-03-12 10:25:46 -0700 | [diff] [blame] | 350 | if creds, err := t.shell.NewChildCredentials("debug"); err == nil { |
Ryan Brown | 61d6938 | 2015-02-25 11:13:39 -0800 | [diff] [blame] | 351 | if agentFile, err = creds.File(); err != nil { |
| 352 | vlog.Errorf("WARNING: failed to obtain credentials for the debug shell: %v", err) |
| 353 | } |
| 354 | } else { |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 355 | vlog.Errorf("WARNING: failed to obtain credentials for the debug shell: %v", err) |
| 356 | } |
| 357 | |
James Ring | 0d525c2 | 2014-12-30 12:03:31 -0800 | [diff] [blame] | 358 | file := os.NewFile(uintptr(fd), dev) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 359 | attr := os.ProcAttr{ |
James Ring | 0d525c2 | 2014-12-30 12:03:31 -0800 | [diff] [blame] | 360 | Files: []*os.File{file, file, file}, |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 361 | Dir: cwd, |
| 362 | } |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 363 | // Set up agent for Child. |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 364 | attr.Files = append(attr.Files, agentFile) |
Ryan Brown | 7f950a8 | 2015-04-20 18:08:39 -0700 | [diff] [blame] | 365 | attr.Env = append(attr.Env, fmt.Sprintf("%s=%d", envvar.AgentEndpoint, agentlib.AgentEndpoint(len(attr.Files)-1))) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 366 | |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 367 | // Set up environment for Child. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 368 | for _, v := range t.shell.Env() { |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 369 | attr.Env = append(attr.Env, v) |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 370 | } |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 371 | |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 372 | for i, td := range t.tempDirs { |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 373 | attr.Env = append(attr.Env, fmt.Sprintf("V23_TMP_DIR%d=%s", i, td)) |
| 374 | } |
| 375 | |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 376 | if len(t.cachedBinDir) > 0 { |
| 377 | attr.Env = append(attr.Env, "V23_BIN_DIR="+t.BinDir()) |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 378 | } |
| 379 | attr.Env = append(attr.Env, env...) |
| 380 | |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 381 | // Start up a new shell. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 382 | writeStringOrDie(t, file, ">> Starting a new interactive shell\n") |
| 383 | writeStringOrDie(t, file, "Hit CTRL-D to resume the test\n") |
| 384 | if len(t.builtBinaries) > 0 { |
| 385 | writeStringOrDie(t, file, "Built binaries:\n") |
| 386 | for _, value := range t.builtBinaries { |
| 387 | writeStringOrDie(t, file, "\t"+value.Path()+"\n") |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 388 | } |
| 389 | } |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 390 | if len(t.cachedBinDir) > 0 { |
| 391 | writeStringOrDie(t, file, fmt.Sprintf("Binaries are cached in %q\n", t.cachedBinDir)) |
Cosmos Nicolaou | a866f26 | 2015-02-10 14:56:06 -0800 | [diff] [blame] | 392 | } else { |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 393 | writeStringOrDie(t, file, fmt.Sprintf("Caching of binaries was not enabled, being written to %q\n", t.binDir)) |
Cosmos Nicolaou | a866f26 | 2015-02-10 14:56:06 -0800 | [diff] [blame] | 394 | } |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 395 | |
| 396 | shellPath := "/bin/sh" |
James Ring | 2fdeb45 | 2015-02-02 21:18:22 -0800 | [diff] [blame] | 397 | if shellPathFromEnv := os.Getenv("SHELL"); shellPathFromEnv != "" { |
James Ring | aa70149 | 2015-02-03 11:43:51 -0800 | [diff] [blame] | 398 | shellPath = shellPathFromEnv |
James Ring | 2fdeb45 | 2015-02-02 21:18:22 -0800 | [diff] [blame] | 399 | } |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 400 | proc, err := os.StartProcess(shellPath, []string{}, &attr) |
| 401 | if err != nil { |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 402 | t.Fatalf("StartProcess(%q) failed: %v", shellPath, err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | // Wait until user exits the shell |
| 406 | state, err := proc.Wait() |
| 407 | if err != nil { |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 408 | t.Fatalf("Wait(%v) failed: %v", shellPath, err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 411 | writeStringOrDie(t, file, fmt.Sprintf("<< Exited shell: %s\n", state.String())) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 414 | // BinaryFromPath returns a new Binary that, when started, will execute the |
| 415 | // executable or script at the given path. The binary is assumed to not |
| 416 | // implement the exec protocol defined in v.io/x/ref/lib/exec. |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 417 | // |
| 418 | // E.g. env.BinaryFromPath("/bin/bash").Start("-c", "echo hello world").Output() -> "hello world" |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 419 | func (t *T) BinaryFromPath(path string) *Binary { |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 420 | return &Binary{ |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 421 | env: t, |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 422 | envVars: nil, |
| 423 | path: path, |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 424 | opts: t.shell.DefaultStartOpts().NoExecCommand(), |
James Ring | 90d21dc | 2015-02-02 18:10:38 -0800 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 428 | // BuildGoPkg expects a Go package path that identifies a "main" package, and |
| 429 | // any build flags to pass to "go build", and returns a Binary representing the |
| 430 | // newly built binary. |
| 431 | // |
| 432 | // The resulting binary is assumed to not use the exec protocol defined in |
| 433 | // v.io/x/ref/lib/exec and in particular will not have access to the security |
| 434 | // agent or any other shared file descriptors. Environment variables and |
| 435 | // command line arguments are the only means of communicating with the |
| 436 | // invocations of this binary. |
| 437 | // |
Cosmos Nicolaou | 52e9a22 | 2015-03-16 21:57:16 -0700 | [diff] [blame] | 438 | // Use this for non-Vanadium command-line tools and servers. |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 439 | func (t *T) BuildGoPkg(pkg string, flags ...string) *Binary { |
| 440 | return t.buildPkg(pkg, flags...) |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 443 | // BuildV23 is like BuildGoPkg, but instead assumes that the resulting binary is |
| 444 | // a Vanadium application and does implement the exec protocol defined in |
| 445 | // v.io/x/ref/lib/exec. The invocations of this binary will have access to the |
| 446 | // security agent configured for the parent process, to shared file descriptors, |
| 447 | // the config shared by the exec package. |
| 448 | // |
Cosmos Nicolaou | 52e9a22 | 2015-03-16 21:57:16 -0700 | [diff] [blame] | 449 | // Use this for Vanadium servers. Note that some vanadium client only binaries, |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 450 | // that do not call v23.Init and hence do not implement the exec protocol cannot |
| 451 | // be used via BuildV23Pkg. |
| 452 | func (t *T) BuildV23Pkg(pkg string, flags ...string) *Binary { |
| 453 | b := t.buildPkg(pkg, flags...) |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 454 | b.opts = t.shell.DefaultStartOpts().ExternalCommand() |
| 455 | return b |
| 456 | } |
| 457 | |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 458 | func (t *T) buildPkg(pkg string, flags ...string) *Binary { |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 459 | then := time.Now() |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 460 | loc := Caller(1) |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 461 | cached, built_path, err := buildPkg(t, t.BinDir(), pkg, flags) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 462 | if err != nil { |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 463 | t.Fatalf("%s: buildPkg(%s, %v) failed: %v", loc, pkg, flags, err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 464 | return nil |
| 465 | } |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 466 | if _, err := os.Stat(built_path); err != nil { |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 467 | t.Fatalf("%s: buildPkg(%s, %v) failed to stat %q", loc, pkg, flags, built_path) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 468 | } |
| 469 | taken := time.Now().Sub(then) |
| 470 | if cached { |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 471 | vlog.Infof("%s: using %s, from %s in %s.", loc, pkg, built_path, taken) |
Cosmos Nicolaou | a866f26 | 2015-02-10 14:56:06 -0800 | [diff] [blame] | 472 | } else { |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 473 | vlog.Infof("%s: built %s, written to %s in %s.", loc, pkg, built_path, taken) |
Cosmos Nicolaou | a866f26 | 2015-02-10 14:56:06 -0800 | [diff] [blame] | 474 | } |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 475 | binary := &Binary{ |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 476 | env: t, |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 477 | envVars: nil, |
| 478 | path: built_path, |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 479 | opts: t.shell.DefaultStartOpts().NoExecCommand(), |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 480 | } |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 481 | t.builtBinaries[pkg] = binary |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 482 | return binary |
| 483 | } |
| 484 | |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 485 | // NewTempFile creates a temporary file. Temporary files will be deleted |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 486 | // by Cleanup. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 487 | func (t *T) NewTempFile() *os.File { |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 488 | loc := Caller(1) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 489 | f, err := ioutil.TempFile("", "") |
| 490 | if err != nil { |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 491 | t.Fatalf("%s: TempFile() failed: %v", loc, err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 492 | } |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 493 | vlog.Infof("%s: created temporary file at %s", loc, f.Name()) |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 494 | t.tempFiles = append(t.tempFiles, f) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 495 | return f |
| 496 | } |
| 497 | |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 498 | // NewTempDir creates a temporary directory. Temporary directories and |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 499 | // their contents will be deleted by Cleanup. |
Robert Kroeger | 02714b7 | 2015-04-14 18:02:38 -0700 | [diff] [blame] | 500 | func (t *T) NewTempDir(dir string) string { |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 501 | loc := Caller(1) |
Robert Kroeger | 02714b7 | 2015-04-14 18:02:38 -0700 | [diff] [blame] | 502 | f, err := ioutil.TempDir(dir, "") |
James Ring | 23b5486 | 2015-01-07 11:54:48 -0800 | [diff] [blame] | 503 | if err != nil { |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 504 | t.Fatalf("%s: TempDir() failed: %v", loc, err) |
James Ring | 23b5486 | 2015-01-07 11:54:48 -0800 | [diff] [blame] | 505 | } |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 506 | vlog.Infof("%s: created temporary directory at %s", loc, f) |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 507 | t.tempDirs = append(t.tempDirs, f) |
James Ring | 23b5486 | 2015-01-07 11:54:48 -0800 | [diff] [blame] | 508 | return f |
| 509 | } |
| 510 | |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 511 | func (t *T) appendInvocation(inv *Invocation) { |
| 512 | t.invocations = append(t.invocations, inv) |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 513 | } |
| 514 | |
James Ring | a71e49a | 2015-01-16 14:08:02 -0800 | [diff] [blame] | 515 | // Creates a new local testing environment. A local testing environment has a |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 516 | // a security principle available via Principal(). |
James Ring | a71e49a | 2015-01-16 14:08:02 -0800 | [diff] [blame] | 517 | // |
| 518 | // You should clean up the returned environment using the env.Cleanup() method. |
| 519 | // A typical end-to-end test will begin like: |
| 520 | // |
| 521 | // func TestFoo(t *testing.T) { |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 522 | // env := integration.NewT(t) |
James Ring | a71e49a | 2015-01-16 14:08:02 -0800 | [diff] [blame] | 523 | // defer env.Cleanup() |
| 524 | // |
| 525 | // ... |
| 526 | // } |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 527 | func New(t TB) *T { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 528 | ctx, shutdown := v23.Init() |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 529 | |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 530 | vlog.Infof("creating root principal") |
Asim Shankar | 4a69828 | 2015-03-21 21:59:18 -0700 | [diff] [blame] | 531 | principal := testutil.NewPrincipal("root") |
Todd Wang | ad49204 | 2015-04-17 15:58:40 -0700 | [diff] [blame] | 532 | ctx, err := v23.WithPrincipal(ctx, principal) |
Ryan Brown | a08a221 | 2015-01-15 15:40:10 -0800 | [diff] [blame] | 533 | if err != nil { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 534 | t.Fatalf("failed to set principal: %v", err) |
Ryan Brown | a08a221 | 2015-01-15 15:40:10 -0800 | [diff] [blame] | 535 | } |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 536 | |
Cosmos Nicolaou | 9e90984 | 2015-03-17 11:58:59 -0700 | [diff] [blame] | 537 | shell, err := modules.NewShell(ctx, principal, testing.Verbose(), t) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 538 | if err != nil { |
| 539 | t.Fatalf("NewShell() failed: %v", err) |
| 540 | } |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 541 | opts := modules.DefaultStartOpts() |
| 542 | opts.StartTimeout = time.Minute |
| 543 | opts.ShutdownTimeout = 5 * time.Minute |
| 544 | shell.SetDefaultStartOpts(opts) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 545 | |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 546 | // The V23_BIN_DIR environment variable can be |
| 547 | // used to identify a directory that multiple integration |
| 548 | // tests can use to share binaries. Whoever sets this |
| 549 | // environment variable is responsible for cleaning up the |
| 550 | // directory it points to. |
Cosmos Nicolaou | a866f26 | 2015-02-10 14:56:06 -0800 | [diff] [blame] | 551 | cachedBinDir := os.Getenv("V23_BIN_DIR") |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 552 | e := &T{ |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 553 | TB: t, |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 554 | principal: principal, |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 555 | builtBinaries: make(map[string]*Binary), |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 556 | shell: shell, |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 557 | tempFiles: []*os.File{}, |
James Ring | 23b5486 | 2015-01-07 11:54:48 -0800 | [diff] [blame] | 558 | tempDirs: []string{}, |
Cosmos Nicolaou | a866f26 | 2015-02-10 14:56:06 -0800 | [diff] [blame] | 559 | cachedBinDir: cachedBinDir, |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 560 | shutdown: shutdown, |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 561 | } |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 562 | if len(e.cachedBinDir) == 0 { |
Robert Kroeger | 02714b7 | 2015-04-14 18:02:38 -0700 | [diff] [blame] | 563 | e.binDir = e.NewTempDir("") |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 564 | } |
| 565 | return e |
| 566 | } |
| 567 | |
Cosmos Nicolaou | 52e9a22 | 2015-03-16 21:57:16 -0700 | [diff] [blame] | 568 | // Shell returns the underlying modules.Shell used by v23tests. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 569 | func (t *T) Shell() *modules.Shell { |
| 570 | return t.shell |
| 571 | } |
| 572 | |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 573 | // BinDir returns the directory that binarie files are stored in. |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 574 | func (t *T) BinDir() string { |
| 575 | if len(t.cachedBinDir) > 0 { |
| 576 | return t.cachedBinDir |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 577 | } |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 578 | return t.binDir |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 581 | // buildPkg returns a path to a directory that contains the built binary for |
James Ring | bd2216f | 2015-01-15 20:06:18 -0800 | [diff] [blame] | 582 | // the given packages and a function that should be invoked to clean up the |
| 583 | // build artifacts. Note that the clients of this function should not modify |
| 584 | // the contents of this directory directly and instead defer to the cleanup |
| 585 | // function. |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 586 | func buildPkg(t *T, binDir, pkg string, flags []string) (bool, string, error) { |
James Ring | bd2216f | 2015-01-15 20:06:18 -0800 | [diff] [blame] | 587 | binFile := filepath.Join(binDir, path.Base(pkg)) |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 588 | vlog.Infof("buildPkg: %v .. %v %v", binDir, pkg, flags) |
James Ring | bd2216f | 2015-01-15 20:06:18 -0800 | [diff] [blame] | 589 | if _, err := os.Stat(binFile); err != nil { |
| 590 | if !os.IsNotExist(err) { |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 591 | return false, "", err |
James Ring | bd2216f | 2015-01-15 20:06:18 -0800 | [diff] [blame] | 592 | } |
Cosmos Nicolaou | 47d55d9 | 2015-04-08 14:49:12 -0700 | [diff] [blame] | 593 | baseName := path.Base(binFile) |
| 594 | tmpdir, err := ioutil.TempDir(binDir, baseName+"-") |
| 595 | if err != nil { |
| 596 | return false, "", err |
| 597 | } |
| 598 | defer os.RemoveAll(tmpdir) |
| 599 | uniqueBinFile := filepath.Join(tmpdir, baseName) |
| 600 | |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 601 | buildArgs := []string{"go", "build", "-x", "-o", uniqueBinFile} |
| 602 | buildArgs = append(buildArgs, flags...) |
| 603 | buildArgs = append(buildArgs, pkg) |
| 604 | cmd := exec.Command("v23", buildArgs...) |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 605 | if output, err := cmd.CombinedOutput(); err != nil { |
| 606 | vlog.VI(1).Infof("\n%v:\n%v\n", strings.Join(cmd.Args, " "), string(output)) |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 607 | return false, "", err |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 608 | } |
Cosmos Nicolaou | 47d55d9 | 2015-04-08 14:49:12 -0700 | [diff] [blame] | 609 | if err := os.Rename(uniqueBinFile, binFile); err != nil { |
| 610 | // It seems that on some systems a rename may fail if another rename |
| 611 | // is in progress in the same directory. We back a random amount of time |
| 612 | // in the hope that a second attempt will succeed. |
| 613 | time.Sleep(time.Duration(rand.Int63n(1000)) * time.Millisecond) |
| 614 | if err := os.Rename(uniqueBinFile, binFile); err != nil { |
| 615 | return false, "", err |
| 616 | } |
| 617 | } |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 618 | return false, binFile, nil |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 619 | } |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 620 | return true, binFile, nil |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Cosmos Nicolaou | 4a77c19 | 2015-02-08 15:29:18 -0800 | [diff] [blame] | 623 | // RunTest runs a single Vanadium 'v23 style' integration test. |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 624 | func RunTest(t *testing.T, fn func(i *T)) { |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 625 | if !test.IntegrationTestsEnabled { |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 626 | t.Skip() |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 627 | } |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 628 | i := New(t) |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 629 | // defer the Cleanup method so that it will be called even if |
| 630 | // t.Fatalf/FailNow etc are called and can print out useful information. |
| 631 | defer i.Cleanup() |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 632 | fn(i) |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 633 | } |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 634 | |
Asim Shankar | 59b8b69 | 2015-03-30 01:23:36 -0700 | [diff] [blame] | 635 | // RunRootMT builds and runs a root mount table instance. It populates the |
| 636 | // envvar.NamespacePrefix variable in the test environment so that all |
| 637 | // subsequent invocations will access this root mount table. |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 638 | func RunRootMT(i *T, args ...string) (*Binary, *Invocation) { |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 639 | b := i.BuildV23Pkg("v.io/x/ref/services/mounttable/mounttabled") |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 640 | inv := b.start(1, args...) |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 641 | name := inv.ExpectVar("NAME") |
Asim Shankar | 59b8b69 | 2015-03-30 01:23:36 -0700 | [diff] [blame] | 642 | inv.Environment().SetVar(envvar.NamespacePrefix, name) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 643 | vlog.Infof("Running root mount table: %q", name) |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 644 | return b, inv |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 647 | // UseSharedBinDir ensures that a shared directory is used for binaries |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 648 | // across multiple instances of the test environment. This is achieved |
| 649 | // by setting the V23_BIN_DIR environment variable if it is not already |
| 650 | // set in the test processes environment (as will typically be the case when |
| 651 | // these tests are run from the v23 tool). It is intended to be called |
| 652 | // from TestMain. |
| 653 | func UseSharedBinDir() func() { |
| 654 | if v23BinDir := os.Getenv("V23_BIN_DIR"); len(v23BinDir) == 0 { |
| 655 | v23BinDir, err := ioutil.TempDir("", "bin-") |
| 656 | if err == nil { |
| 657 | vlog.Infof("Setting V23_BIN_DIR to %q", v23BinDir) |
| 658 | os.Setenv("V23_BIN_DIR", v23BinDir) |
| 659 | return func() { os.RemoveAll(v23BinDir) } |
| 660 | } |
| 661 | } else { |
| 662 | vlog.Infof("Using V23_BIN_DIR %q", v23BinDir) |
| 663 | } |
| 664 | return func() {} |
| 665 | } |