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