Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 1 | // Package v23tests provides support for writing end-to-end style integration |
| 2 | // tests. In particular, support is provided for building binaries, running |
| 3 | // processes, making assertions about their output/state and ensuring that |
| 4 | // no processes or files are left behind on exit. Since such tests are often |
| 5 | // difficult to debug facilities are provided to help do so. |
| 6 | // |
| 7 | // The preferred usage of this integration test framework is via the v23 |
| 8 | // tool which generates supporting code. The primary reason for doing so is |
| 9 | // to cleanly separate integration tests, which can be very expensive to run, |
| 10 | // from normal unit tests which are intended to be fast and used constantly. |
| 11 | // However, it still beneficial to be able to always compile the integration |
| 12 | // test code with the normal test code, just not to run it. Similarly, it |
| 13 | // is beneficial to share as much of the existing go test infrastructure as |
| 14 | // possible, so the generated code uses a flag and a naming convention to |
| 15 | // separate the tests. Integration tests may be run in addition to unit tests |
| 16 | // by supplying the --v23.tests flag; the -run flag can be used |
| 17 | // to avoid running unit tests by specifying a prefix of TestV23 since |
| 18 | // the generate test functions always. Thus: |
| 19 | // |
| 20 | // v23 go test -v <pkgs> --v23.test // runs both unit and integration tests |
| 21 | // v23 go test -v -run=TestV23 <pkgs> --v23.test // runs just integration tests |
| 22 | // |
| 23 | // The go generate mechanism is used to generate the test code, thus the |
| 24 | // comment: |
| 25 | // |
| 26 | // //go:generate v23 integration generate |
| 27 | // |
| 28 | // will generate the files v23_test.go and internal_v23_test.go for the |
| 29 | // package in which it occurs. Run v23 integration generate help for full |
| 30 | // details and options. In short, any function in an external |
| 31 | // (i.e. <pgk>_test) test package of the following form: |
| 32 | // |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 33 | // V23Test<x>(t *v23tests.T) |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 34 | // |
| 35 | // will be invoked as integration test if the --v23.tests flag is used. |
| 36 | // |
| 37 | // The generated code makes use of the RunTest function, documented below. |
| 38 | // |
| 39 | // The test environment is implemented by an instance of the interface T. |
| 40 | // It is constructed with an instance of another interface Test, which is |
| 41 | // generally implemented by testing.T. Thus, the integration test environment |
| 42 | // directly as follows: |
James Ring | a71e49a | 2015-01-16 14:08:02 -0800 | [diff] [blame] | 43 | // |
| 44 | // func TestFoo(t *testing.T) { |
Cosmos Nicolaou | 4a77c19 | 2015-02-08 15:29:18 -0800 | [diff] [blame] | 45 | // env := v23tests.New(t) |
James Ring | a71e49a | 2015-01-16 14:08:02 -0800 | [diff] [blame] | 46 | // defer env.Cleanup() |
| 47 | // |
| 48 | // ... |
| 49 | // } |
| 50 | // |
| 51 | // The methods in this API typically do not return error in the case of |
| 52 | // failure. Instead, the current test will fail with an appropriate error |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 53 | // message. This avoids the need to handle errors inline the test itself. |
James Ring | a71e49a | 2015-01-16 14:08:02 -0800 | [diff] [blame] | 54 | // |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 55 | // The test environment manages all built packages, subprocesses and a |
| 56 | // set of environment variables that are passed to subprocesses. |
| 57 | // |
| 58 | // Debugging is supported as follows: |
| 59 | // 1. The DebugShell method creates an interative shell at that point in |
| 60 | // the tests execution that has access to all of the running processes |
| 61 | // and environment of those processes. The developer can interact with |
| 62 | // those processes to determine the state of the test. |
| 63 | // 2. Calls to methods on Test (e.g. FailNow, Fatalf) that fail the test |
| 64 | // cause the Cleanup method to print out the status of all invocations. |
| 65 | // 3. Similarly, if the --v23.tests.shell-on-error flag is set then the |
| 66 | // cleanup method will invoke a DebugShell on a test failure allowing |
| 67 | // the developer to inspect the state of the test. |
| 68 | // 4. The implementation of this package uses filenames that start with v23test |
| 69 | // to allow for easy tracing with --vmodule=v23test*=2 for example. |
| 70 | // |
Cosmos Nicolaou | 4a77c19 | 2015-02-08 15:29:18 -0800 | [diff] [blame] | 71 | package v23tests |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 72 | |
| 73 | import ( |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 74 | "bytes" |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 75 | "container/list" |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 76 | "errors" |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 77 | "fmt" |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 78 | "io" |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 79 | "io/ioutil" |
| 80 | "os" |
| 81 | "os/exec" |
| 82 | "path" |
| 83 | "path/filepath" |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 84 | "runtime" |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 85 | "strings" |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 86 | "syscall" |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 87 | "testing" |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 88 | "time" |
| 89 | |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 90 | "v.io/core/veyron2" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 91 | "v.io/core/veyron2/security" |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 92 | "v.io/core/veyron2/vlog" |
| 93 | |
| 94 | "v.io/core/veyron/lib/expect" |
| 95 | "v.io/core/veyron/lib/modules" |
| 96 | "v.io/core/veyron/lib/testutil" |
| 97 | tsecurity "v.io/core/veyron/lib/testutil/security" |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 98 | "v.io/core/veyron/security/agent" |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 99 | ) |
| 100 | |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 101 | // TB is an exact mirror of testing.TB. It is provided to allow for testing |
| 102 | // of this package using a mock implementation. As per testing.TB, it is not |
| 103 | // intended to be implemented outside of this package. |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 104 | type TB interface { |
| 105 | Error(args ...interface{}) |
| 106 | Errorf(format string, args ...interface{}) |
| 107 | Fail() |
| 108 | FailNow() |
| 109 | Failed() bool |
| 110 | Fatal(args ...interface{}) |
| 111 | Fatalf(format string, args ...interface{}) |
| 112 | Log(args ...interface{}) |
| 113 | Logf(format string, args ...interface{}) |
| 114 | Skip(args ...interface{}) |
| 115 | SkipNow() |
| 116 | Skipf(format string, args ...interface{}) |
| 117 | Skipped() bool |
| 118 | } |
James Ring | 855dfd4 | 2015-01-23 12:30:58 -0800 | [diff] [blame] | 119 | |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 120 | // T represents an integration test environment. |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 121 | type T struct { |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 122 | // The embedded TB |
| 123 | TB |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 124 | |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 125 | // The function to shutdown the context used to create the environment. |
| 126 | shutdown veyron2.Shutdown |
| 127 | |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 128 | // The shell to use to start commands. |
| 129 | shell *modules.Shell |
| 130 | |
| 131 | // The environment's root security principal. |
| 132 | principal security.Principal |
| 133 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 134 | // Maps path to Binary. |
| 135 | builtBinaries map[string]*Binary |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 136 | |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 137 | tempFiles []*os.File |
| 138 | tempDirs []string |
| 139 | binDir, cachedBinDir string |
| 140 | dirStack []string |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 141 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 142 | invocations []*Invocation |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 145 | // Binary represents an executable program that will be executed during a |
| 146 | // test. A binary may be invoked multiple times by calling Start, each call |
| 147 | // will return a new Invocation. |
| 148 | // |
| 149 | // Binary instances are typically obtained from a T by calling BuildGoPkg |
| 150 | // (for Vanadium and other Go binaries) or BinaryFromPath (to start binaries |
| 151 | // that are already present on the system). |
| 152 | type Binary struct { |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 153 | // The environment to which this binary belongs. |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 154 | env *T |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 155 | |
| 156 | // The path to the binary. |
| 157 | path string |
| 158 | |
James Ring | 7edf1ca | 2015-01-08 12:32:29 -0800 | [diff] [blame] | 159 | // Environment variables that will be used when creating invocations |
| 160 | // via Start. |
| 161 | envVars []string |
| 162 | |
James Ring | 79a9ceb | 2015-02-09 13:25:54 -0800 | [diff] [blame] | 163 | // The reader who is supplying the bytes we're going to send to our stdin. |
| 164 | inputReader io.Reader |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 167 | // Invocation represents a single invocation of a Binary. |
| 168 | // |
| 169 | // Any bytes written by the invocation to its standard error may be recovered |
| 170 | // using the Wait or WaitOrDie functions. |
| 171 | // |
| 172 | // For example: |
| 173 | // bin := env.BinaryFromPath("/bin/bash") |
| 174 | // inv := bin.Start("-c", "echo hello world 1>&2") |
| 175 | // var stderr bytes.Buffer |
| 176 | // inv.WaitOrDie(nil, &stderr) |
| 177 | // // stderr.Bytes() now contains 'hello world\n' |
| 178 | type Invocation struct { |
| 179 | *expect.Session |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 180 | |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 181 | // The environment to which this invocation belongs. |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 182 | env *T |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 183 | |
| 184 | // The handle to the process that was run when this invocation was started. |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 185 | handle modules.Handle |
| 186 | |
| 187 | // The element representing this invocation in the list of |
| 188 | // invocations stored in the environment |
| 189 | el *list.Element |
| 190 | |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 191 | // The path of the binary used for this invocation. |
| 192 | path string |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 193 | |
| 194 | // The args the binary was started with |
| 195 | args []string |
| 196 | |
| 197 | // True if the process has been shutdown |
| 198 | hasShutdown bool |
| 199 | |
| 200 | // The error, if any, as determined when the invocation was |
| 201 | // shutdown. It must be set to a default initial value of |
| 202 | // errNotShutdown rather than nil to allow us to distinguish between |
| 203 | // a successful shutdown or an error. |
| 204 | shutdownErr error |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 207 | var errNotShutdown = errors.New("has not been shutdown") |
| 208 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 209 | // Stdin returns this invocations Stdin stream. |
| 210 | func (i *Invocation) Stdin() io.Writer { |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 211 | return i.handle.Stdin() |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 214 | // CloseStdin closes the write-side of the pipe to the invocation's |
| 215 | // standard input. |
| 216 | func (i *Invocation) CloseStdin() { |
James Ring | 79a9ceb | 2015-02-09 13:25:54 -0800 | [diff] [blame] | 217 | i.handle.CloseStdin() |
| 218 | } |
| 219 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 220 | // Stdout returns this invocations Stdout stream. |
| 221 | func (i *Invocation) Stdout() io.Reader { |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 222 | return i.handle.Stdout() |
| 223 | } |
| 224 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 225 | // Path returns the path to the binary that was used for this invocation. |
| 226 | func (i *Invocation) Path() string { |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 227 | return i.path |
| 228 | } |
| 229 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 230 | // Exists returns true if the invocation still exists. |
| 231 | func (i *Invocation) Exists() bool { |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 232 | return syscall.Kill(i.handle.Pid(), 0) == nil |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 235 | // Sends the given signal to this invocation. It is up to the test |
| 236 | // author to decide whether failure to deliver the signal is fatal to |
| 237 | // the test. |
| 238 | func (i *Invocation) Kill(sig syscall.Signal) error { |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 239 | pid := i.handle.Pid() |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 240 | vlog.VI(1).Infof("sending signal %v to PID %d", sig, pid) |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 241 | return syscall.Kill(pid, sig) |
James Ring | 72b14bd | 2014-12-29 14:26:54 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 244 | // Caller returns a string of the form <filename>:<lineno> for the |
| 245 | // caller specified by skip, where skip is as per runtime.Caller. |
| 246 | func Caller(skip int) string { |
| 247 | _, file, line, _ := runtime.Caller(skip + 1) |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 248 | return fmt.Sprintf("%s:%d", filepath.Base(file), line) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 251 | // Output reads the invocation's stdout until EOF and then returns what |
| 252 | // was read as a string. |
| 253 | func (i *Invocation) Output() string { |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 254 | buf := bytes.Buffer{} |
| 255 | _, err := buf.ReadFrom(i.Stdout()) |
| 256 | if err != nil { |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 257 | i.env.Fatalf("%s: ReadFrom() failed: %v", Caller(1), err) |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 258 | } |
| 259 | return buf.String() |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 262 | // Wait waits for this invocation to finish. If either stdout or stderr |
| 263 | // is non-nil, any remaining unread output from those sources will be |
| 264 | // written to the corresponding writer. The returned error represents |
| 265 | // the exit status of the underlying command. |
| 266 | func (i *Invocation) Wait(stdout, stderr io.Writer) error { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 267 | err := i.handle.Shutdown(stdout, stderr) |
| 268 | i.hasShutdown = true |
| 269 | i.shutdownErr = err |
| 270 | return err |
James Ring | e78c7da | 2015-01-06 15:59:37 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 273 | // Wait waits for this invocation to finish. If either stdout or stderr |
| 274 | // is non-nil, any remaining unread output from those sources will be |
| 275 | // written to the corresponding writer. If the underlying command |
| 276 | // exited with anything but success (exit status 0), this function will |
| 277 | // cause the current test to fail. |
| 278 | func (i *Invocation) WaitOrDie(stdout, stderr io.Writer) { |
James Ring | e78c7da | 2015-01-06 15:59:37 -0800 | [diff] [blame] | 279 | if err := i.Wait(stdout, stderr); err != nil { |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 280 | i.env.Fatalf("%s: FATAL: Wait() for pid %d failed: %v", Caller(1), i.handle.Pid(), err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 284 | // Environment returns the instance of the test environment that this |
| 285 | // invocation was from. |
| 286 | func (i *Invocation) Environment() *T { |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 287 | return i.env |
| 288 | } |
| 289 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 290 | func (b *Binary) cleanup() { |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 291 | binaryDir := path.Dir(b.path) |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 292 | vlog.Infof("cleaning up %s", binaryDir) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 293 | if err := os.RemoveAll(binaryDir); err != nil { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 294 | vlog.Infof("WARNING: RemoveAll(%s) failed (%v)", binaryDir, err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 298 | // Path returns the path to the binary. |
| 299 | func (b *Binary) Path() string { |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 300 | return b.path |
| 301 | } |
| 302 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 303 | // Start starts the given binary with the given arguments. |
| 304 | func (b *Binary) Start(args ...string) *Invocation { |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 305 | return b.start(1, args...) |
| 306 | } |
| 307 | |
| 308 | func (b *Binary) start(skip int, args ...string) *Invocation { |
| 309 | vlog.Infof("%s: starting %s %s", Caller(skip+1), b.Path(), strings.Join(args, " ")) |
Cosmos Nicolaou | d61e5a8 | 2015-02-22 16:32:48 -0800 | [diff] [blame] | 310 | handle, err := b.env.shell.StartExternalCommand(b.inputReader, b.envVars, append([]string{b.Path()}, args...)...) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 311 | if err != nil { |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 312 | // TODO(cnicolaou): calling Fatalf etc from a goroutine often leads |
| 313 | // to deadlock. Need to make sure that we handle this here. Maybe |
| 314 | // it's best to just return an error? Or provide a StartWithError |
| 315 | // call for use from goroutines. |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 316 | b.env.Fatalf("%s: StartExternalCommand(%v, %v) failed: %v", Caller(skip+1), b.Path(), strings.Join(args, ", "), err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 317 | } |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 318 | vlog.Infof("started PID %d\n", handle.Pid()) |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 319 | inv := &Invocation{ |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 320 | env: b.env, |
| 321 | handle: handle, |
| 322 | path: b.path, |
| 323 | args: args, |
| 324 | shutdownErr: errNotShutdown, |
| 325 | Session: expect.NewSession(b.env, handle.Stdout(), 5*time.Minute), |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 326 | } |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 327 | b.env.appendInvocation(inv) |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 328 | return inv |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 331 | func (b *Binary) run(args ...string) string { |
| 332 | inv := b.start(2, args...) |
| 333 | var stdout, stderr bytes.Buffer |
| 334 | err := inv.Wait(&stdout, &stderr) |
| 335 | if err != nil { |
| 336 | a := strings.Join(args, ", ") |
| 337 | b.env.Fatalf("%s: Run(%s): failed: %v: \n%s\n", Caller(2), a, err, stderr.String()) |
| 338 | } |
| 339 | return strings.TrimRight(stdout.String(), "\n") |
| 340 | } |
| 341 | |
| 342 | // Run runs the binary with the specified arguments to completion. On |
| 343 | // success it returns the contents of stdout, on failure it terminates the |
| 344 | // test with an error message containing the error and the contents of |
| 345 | // stderr. |
| 346 | func (b *Binary) Run(args ...string) string { |
| 347 | return b.run(args...) |
| 348 | } |
| 349 | |
| 350 | // Run constructs a Binary for path and invokes Run on it. |
| 351 | func (e *T) Run(path string, args ...string) string { |
| 352 | return e.BinaryFromPath(path).run(args...) |
| 353 | } |
| 354 | |
| 355 | // WaitFunc is the type of the functions to be used in conjunction |
| 356 | // with WaitFor and WaitForAsync. It should return a value or an error |
| 357 | // when it wants those functions to terminate, returning a nil value |
| 358 | // and nil error will result in it being called again after the specified |
| 359 | // delay time specified in the calls to WaitFor and WaitForAsync. |
| 360 | type WaitFunc func() (interface{}, error) |
| 361 | |
| 362 | // WaitFor calls fn at least once with the specified delay value |
| 363 | // between iterations until the first of the following is encountered: |
| 364 | // 1. fn returns a non-nil value. |
| 365 | // 2. fn returns an error value |
| 366 | // 3. fn is executed at least once and the specified timeout is exceeded. |
| 367 | // |
| 368 | // WaitFor returns the non-nil value for the first case and calls e.Fatalf for |
| 369 | // the other two cases. |
| 370 | // WaitFor will always run fn at least once to completion and hence it will |
| 371 | // hang if that first iteration of fn hangs. If this behaviour is not |
| 372 | // appropriate, then WaitForAsync should be used. |
| 373 | func (e *T) WaitFor(fn WaitFunc, delay, timeout time.Duration) interface{} { |
| 374 | deadline := time.Now().Add(timeout) |
| 375 | for { |
| 376 | val, err := fn() |
| 377 | if val != nil { |
| 378 | return val |
| 379 | } |
| 380 | if err != nil { |
| 381 | e.Fatalf("%s: the WaitFunc returned an error: %v", Caller(1), err) |
| 382 | } |
| 383 | if time.Now().After(deadline) { |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 384 | e.Fatalf("%s: timed out after %s", Caller(1), timeout) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 385 | } |
| 386 | time.Sleep(delay) |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | // WaitForAsync is like WaitFor except that it calls fn in a goroutine |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 391 | // and can timeout during the execution of fn. |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 392 | func (e *T) WaitForAsync(fn WaitFunc, delay, timeout time.Duration) interface{} { |
| 393 | resultCh := make(chan interface{}) |
| 394 | errCh := make(chan interface{}) |
| 395 | go func() { |
| 396 | for { |
| 397 | val, err := fn() |
| 398 | if val != nil { |
| 399 | resultCh <- val |
| 400 | return |
| 401 | } |
| 402 | if err != nil { |
| 403 | errCh <- err |
| 404 | return |
| 405 | } |
| 406 | time.Sleep(delay) |
| 407 | } |
| 408 | }() |
| 409 | select { |
| 410 | case err := <-errCh: |
| 411 | e.Fatalf("%s: the WaitFunc returned error: %v", Caller(1), err) |
| 412 | case result := <-resultCh: |
| 413 | return result |
| 414 | case <-time.After(timeout): |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 415 | e.Fatalf("%s: timed out after %s", Caller(1), timeout) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 416 | } |
| 417 | return nil |
| 418 | } |
| 419 | |
| 420 | // Pushd pushes the current working directory to the stack of |
| 421 | // directories, returning it as its result, and changes the working |
| 422 | // directory to dir. |
| 423 | func (e *T) Pushd(dir string) string { |
| 424 | cwd, err := os.Getwd() |
| 425 | if err != nil { |
| 426 | e.Fatalf("%s: Getwd failed: %s", Caller(1), err) |
| 427 | } |
| 428 | if err := os.Chdir(dir); err != nil { |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 429 | e.Fatalf("%s: Chdir failed: %s", Caller(1), err) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 430 | } |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 431 | vlog.VI(1).Infof("Pushd: %s -> %s", cwd, dir) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 432 | e.dirStack = append(e.dirStack, cwd) |
| 433 | return cwd |
| 434 | } |
| 435 | |
| 436 | // Popd pops the most recent entry from the directory stack and changes |
| 437 | // the working directory to that directory. It returns the new working |
| 438 | // directory as its result. |
| 439 | func (e *T) Popd() string { |
| 440 | if len(e.dirStack) == 0 { |
| 441 | e.Fatalf("%s: directory stack empty", Caller(1)) |
| 442 | } |
| 443 | dir := e.dirStack[len(e.dirStack)-1] |
| 444 | e.dirStack = e.dirStack[:len(e.dirStack)-1] |
| 445 | if err := os.Chdir(dir); err != nil { |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 446 | e.Fatalf("%s: Chdir failed: %s", Caller(1), err) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 447 | } |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 448 | vlog.VI(1).Infof("Popd: -> %s", dir) |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 449 | return dir |
| 450 | } |
| 451 | |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 452 | // Caller returns a string of the form <filename>:<lineno> for the |
| 453 | // caller specified by skip, where skip is as per runtime.Caller. |
| 454 | func (e *T) Caller(skip int) string { |
| 455 | return Caller(skip + 1) |
| 456 | } |
| 457 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 458 | // WithStdin returns a copy of this binary that, when Start is called, |
| 459 | // will read its input from the given reader. Once the reader returns |
| 460 | // EOF, the returned invocation's standard input will be closed (see |
| 461 | // Invocation.CloseStdin). |
| 462 | func (b *Binary) WithStdin(r io.Reader) *Binary { |
James Ring | 79a9ceb | 2015-02-09 13:25:54 -0800 | [diff] [blame] | 463 | newBin := *b |
| 464 | newBin.inputReader = r |
| 465 | return &newBin |
| 466 | } |
| 467 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 468 | // Returns a copy of this binary that, when Start is called, will use |
| 469 | // the given environment variables. Each environment variable should be |
| 470 | // in "key=value" form. For example: |
| 471 | // |
| 472 | // bin.WithEnv("EXAMPLE_ENV=/tmp/something").Start(...) |
| 473 | func (b *Binary) WithEnv(env ...string) *Binary { |
James Ring | 7edf1ca | 2015-01-08 12:32:29 -0800 | [diff] [blame] | 474 | newBin := *b |
| 475 | newBin.envVars = env |
| 476 | return &newBin |
| 477 | } |
| 478 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 479 | // Principal returns the security principal of this environment. |
| 480 | func (e *T) Principal() security.Principal { |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 481 | return e.principal |
| 482 | } |
| 483 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 484 | // Cleanup cleans up the environment, deletes all its artifacts and |
| 485 | // kills all subprocesses. It will kill subprocesses in LIFO order. |
| 486 | // Cleanup checks to see if the test has failed and logs information |
| 487 | // as to the state of the processes it was asked to invoke up to that |
| 488 | // point and optionally, if the --v23.tests.shell-on-fail flag is set |
| 489 | // then it will run a debug shell before cleaning up its state. |
| 490 | func (e *T) Cleanup() { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 491 | if e.Failed() { |
| 492 | if testutil.IntegrationTestsDebugShellOnError { |
| 493 | e.DebugShell() |
| 494 | } |
| 495 | // Print out a summary of the invocations and their status. |
| 496 | for i, inv := range e.invocations { |
| 497 | if inv.hasShutdown && inv.Exists() { |
| 498 | m := fmt.Sprintf("%d: %s has been shutdown but still exists: %v", i, inv.path, inv.shutdownErr) |
| 499 | e.Log(m) |
| 500 | vlog.VI(1).Info(m) |
| 501 | vlog.VI(2).Infof("%d: %s %v", i, inv.path, inv.args) |
| 502 | continue |
| 503 | } |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 504 | if inv.shutdownErr != nil { |
| 505 | m := fmt.Sprintf("%d: %s: shutdown status: %v", i, inv.path, inv.shutdownErr) |
| 506 | e.Log(m) |
| 507 | vlog.VI(1).Info(m) |
| 508 | vlog.VI(2).Infof("%d: %s %v", i, inv.path, inv.args) |
| 509 | } |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 513 | vlog.VI(1).Infof("V23Test.Cleanup") |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 514 | // Shut down all processes in LIFO order before attempting to delete any |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 515 | // files/directories to avoid potential 'file system busy' problems |
| 516 | // on non-unix systems. |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 517 | for i := len(e.invocations); i > 0; i-- { |
| 518 | inv := e.invocations[i-1] |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 519 | if inv.hasShutdown { |
| 520 | vlog.VI(1).Infof("V23Test.Cleanup: %q has been shutdown", inv.Path()) |
| 521 | continue |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 522 | } |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 523 | vlog.VI(1).Infof("V23Test.Cleanup: Kill: %q", inv.Path()) |
| 524 | err := inv.Kill(syscall.SIGTERM) |
| 525 | inv.Wait(os.Stdout, os.Stderr) |
| 526 | vlog.VI(1).Infof("V23Test.Cleanup: Killed: %q: %v", inv.Path(), err) |
| 527 | } |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 528 | vlog.VI(1).Infof("V23Test.Cleanup: all invocations taken care of.") |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 529 | |
| 530 | if err := e.shell.Cleanup(os.Stdout, os.Stderr); err != nil { |
| 531 | e.Fatalf("WARNING: could not clean up shell (%v)", err) |
| 532 | } |
| 533 | |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 534 | vlog.VI(1).Infof("V23Test.Cleanup: cleaning up binaries & files") |
| 535 | |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 536 | for _, tempFile := range e.tempFiles { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 537 | vlog.VI(1).Infof("V23Test.Cleanup: cleaning up %s", tempFile.Name()) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 538 | if err := tempFile.Close(); err != nil { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 539 | vlog.Errorf("WARNING: Close(%q) failed: %v", tempFile.Name(), err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 540 | } |
James Ring | 23b5486 | 2015-01-07 11:54:48 -0800 | [diff] [blame] | 541 | if err := os.RemoveAll(tempFile.Name()); err != nil { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 542 | vlog.Errorf("WARNING: RemoveAll(%q) failed: %v", tempFile.Name(), err) |
James Ring | 23b5486 | 2015-01-07 11:54:48 -0800 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
| 546 | for _, tempDir := range e.tempDirs { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 547 | vlog.VI(1).Infof("V23Test.Cleanup: cleaning up %s", tempDir) |
James Ring | 23b5486 | 2015-01-07 11:54:48 -0800 | [diff] [blame] | 548 | if err := os.RemoveAll(tempDir); err != nil { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 549 | vlog.Errorf("WARNING: RemoveAll(%q) failed: %v", tempDir, err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 553 | // shutdown the runtime |
Cosmos Nicolaou | 731c692 | 2015-02-05 17:05:20 -0800 | [diff] [blame] | 554 | e.shutdown() |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 555 | } |
| 556 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 557 | // GetVar returns the variable associated with the specified key |
| 558 | // and an indication of whether it is defined or not. |
| 559 | func (e *T) GetVar(key string) (string, bool) { |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 560 | return e.shell.GetVar(key) |
| 561 | } |
| 562 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 563 | // SetVar sets the value to be associated with key. |
| 564 | func (e *T) SetVar(key, value string) { |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 565 | e.shell.SetVar(key, value) |
| 566 | } |
| 567 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 568 | // ClearVar removes the speficied variable from the Shell's environment |
| 569 | func (e *T) ClearVar(key string) { |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 570 | e.shell.ClearVar(key) |
| 571 | } |
| 572 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 573 | func writeStringOrDie(t *T, f *os.File, s string) { |
James Ring | 0bad567 | 2015-01-05 09:51:36 -0800 | [diff] [blame] | 574 | if _, err := f.WriteString(s); err != nil { |
James Ring | 0d525c2 | 2014-12-30 12:03:31 -0800 | [diff] [blame] | 575 | t.Fatalf("Write() failed: %v", err) |
| 576 | } |
| 577 | } |
| 578 | |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 579 | // DebugShell drops the user into a debug shell with any environment |
| 580 | // variables specified in env... (in VAR=VAL format) available to it. |
| 581 | // If there is no controlling TTY, DebugShell will emit a warning message |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 582 | // and take no futher action. The DebugShell also sets some environment |
| 583 | // variables that relate to the running test: |
| 584 | // - V23_TMP_DIR<#> contains the name of each temp directory created. |
| 585 | // - V23_BIN_DIR contains the name of the directory containing binaries. |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 586 | func (e *T) DebugShell(env ...string) { |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 587 | // Get the current working directory. |
| 588 | cwd, err := os.Getwd() |
| 589 | if err != nil { |
Cosmos Nicolaou | 234642b | 2015-02-04 18:30:52 -0800 | [diff] [blame] | 590 | e.Fatalf("Getwd() failed: %v", err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | // Transfer stdin, stdout, and stderr to the new process |
| 594 | // and also set target directory for the shell to start in. |
| 595 | dev := "/dev/tty" |
James Ring | 0d525c2 | 2014-12-30 12:03:31 -0800 | [diff] [blame] | 596 | fd, err := syscall.Open(dev, syscall.O_RDWR, 0) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 597 | if err != nil { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 598 | 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] | 599 | return |
| 600 | } |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 601 | |
| 602 | agentFile, err := e.shell.NewChildCredentials() |
| 603 | if err != nil { |
| 604 | vlog.Errorf("WARNING: failed to obtain credentials for the debug shell: %v", err) |
| 605 | } |
| 606 | |
James Ring | 0d525c2 | 2014-12-30 12:03:31 -0800 | [diff] [blame] | 607 | file := os.NewFile(uintptr(fd), dev) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 608 | attr := os.ProcAttr{ |
James Ring | 0d525c2 | 2014-12-30 12:03:31 -0800 | [diff] [blame] | 609 | Files: []*os.File{file, file, file}, |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 610 | Dir: cwd, |
| 611 | } |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 612 | // Set up agent for Child. |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 613 | attr.Files = append(attr.Files, agentFile) |
| 614 | attr.Env = append(attr.Env, fmt.Sprintf("%s=%d", agent.FdVarName, len(attr.Files)-1)) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 615 | |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 616 | // Set up environment for Child. |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 617 | for _, v := range e.shell.Env() { |
| 618 | attr.Env = append(attr.Env, v) |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 619 | } |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 620 | |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 621 | for i, td := range e.tempDirs { |
| 622 | attr.Env = append(attr.Env, fmt.Sprintf("V23_TMP_DIR%d=%s", i, td)) |
| 623 | } |
| 624 | |
| 625 | if len(e.cachedBinDir) > 0 { |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 626 | attr.Env = append(attr.Env, "V23_BIN_DIR="+e.BinDir()) |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 627 | } |
| 628 | attr.Env = append(attr.Env, env...) |
| 629 | |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 630 | // Start up a new shell. |
Cosmos Nicolaou | 234642b | 2015-02-04 18:30:52 -0800 | [diff] [blame] | 631 | writeStringOrDie(e, file, ">> Starting a new interactive shell\n") |
| 632 | writeStringOrDie(e, file, "Hit CTRL-D to resume the test\n") |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 633 | if len(e.builtBinaries) > 0 { |
Cosmos Nicolaou | 234642b | 2015-02-04 18:30:52 -0800 | [diff] [blame] | 634 | writeStringOrDie(e, file, "Built binaries:\n") |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 635 | for _, value := range e.builtBinaries { |
Cosmos Nicolaou | 234642b | 2015-02-04 18:30:52 -0800 | [diff] [blame] | 636 | writeStringOrDie(e, file, "\t"+value.Path()+"\n") |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 637 | } |
| 638 | } |
Cosmos Nicolaou | a866f26 | 2015-02-10 14:56:06 -0800 | [diff] [blame] | 639 | if len(e.cachedBinDir) > 0 { |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 640 | writeStringOrDie(e, file, fmt.Sprintf("Binaries are cached in %q\n", e.cachedBinDir)) |
Cosmos Nicolaou | a866f26 | 2015-02-10 14:56:06 -0800 | [diff] [blame] | 641 | } else { |
Cosmos Nicolaou | 980a870 | 2015-02-20 23:33:46 -0800 | [diff] [blame] | 642 | writeStringOrDie(e, file, fmt.Sprintf("Caching of binaries was not enabled, being written to %q\n", e.binDir)) |
Cosmos Nicolaou | a866f26 | 2015-02-10 14:56:06 -0800 | [diff] [blame] | 643 | } |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 644 | |
| 645 | shellPath := "/bin/sh" |
James Ring | 2fdeb45 | 2015-02-02 21:18:22 -0800 | [diff] [blame] | 646 | if shellPathFromEnv := os.Getenv("SHELL"); shellPathFromEnv != "" { |
James Ring | aa70149 | 2015-02-03 11:43:51 -0800 | [diff] [blame] | 647 | shellPath = shellPathFromEnv |
James Ring | 2fdeb45 | 2015-02-02 21:18:22 -0800 | [diff] [blame] | 648 | } |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 649 | proc, err := os.StartProcess(shellPath, []string{}, &attr) |
| 650 | if err != nil { |
Cosmos Nicolaou | 234642b | 2015-02-04 18:30:52 -0800 | [diff] [blame] | 651 | e.Fatalf("StartProcess(%q) failed: %v", shellPath, err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | // Wait until user exits the shell |
| 655 | state, err := proc.Wait() |
| 656 | if err != nil { |
Cosmos Nicolaou | 234642b | 2015-02-04 18:30:52 -0800 | [diff] [blame] | 657 | e.Fatalf("Wait(%v) failed: %v", shellPath, err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 658 | } |
| 659 | |
Cosmos Nicolaou | 234642b | 2015-02-04 18:30:52 -0800 | [diff] [blame] | 660 | writeStringOrDie(e, file, fmt.Sprintf("<< Exited shell: %s\n", state.String())) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 661 | } |
| 662 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 663 | // BinaryFromPath returns a new Binary that, when started, will |
| 664 | // execute the executable or script at the given path. |
| 665 | // |
| 666 | // E.g. env.BinaryFromPath("/bin/bash").Start("-c", "echo hello world").Output() -> "hello world" |
| 667 | func (e *T) BinaryFromPath(path string) *Binary { |
| 668 | return &Binary{ |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 669 | env: e, |
| 670 | envVars: nil, |
| 671 | path: path, |
James Ring | 90d21dc | 2015-02-02 18:10:38 -0800 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 675 | // BuildGoPkg expects a Go package path that identifies a "main" |
| 676 | // package and returns a Binary representing the newly built |
| 677 | // binary. |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 678 | func (e *T) BuildGoPkg(pkg string) *Binary { |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 679 | then := time.Now() |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 680 | loc := Caller(1) |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 681 | cached, built_path, err := buildPkg(e.BinDir(), pkg) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 682 | if err != nil { |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 683 | e.Fatalf("%s: buildPkg(%s) failed: %v", loc, pkg, err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 684 | return nil |
| 685 | } |
Cosmos Nicolaou | a866f26 | 2015-02-10 14:56:06 -0800 | [diff] [blame] | 686 | |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 687 | if _, err := os.Stat(built_path); err != nil { |
| 688 | e.Fatalf("%s: buildPkg(%s) failed to stat %q", loc, pkg, built_path) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 689 | } |
| 690 | taken := time.Now().Sub(then) |
| 691 | if cached { |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 692 | 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] | 693 | } else { |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 694 | 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] | 695 | } |
| 696 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 697 | binary := &Binary{ |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 698 | env: e, |
| 699 | envVars: nil, |
| 700 | path: built_path, |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 701 | } |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 702 | e.builtBinaries[pkg] = binary |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 703 | return binary |
| 704 | } |
| 705 | |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 706 | // NewTempFile creates a temporary file. Temporary files will be deleted |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 707 | // by Cleanup. |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 708 | func (e *T) NewTempFile() *os.File { |
| 709 | loc := Caller(1) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 710 | f, err := ioutil.TempFile("", "") |
| 711 | if err != nil { |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 712 | e.Fatalf("%s: TempFile() failed: %v", loc, err) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 713 | } |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 714 | vlog.Infof("%s: created temporary file at %s", loc, f.Name()) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 715 | e.tempFiles = append(e.tempFiles, f) |
| 716 | return f |
| 717 | } |
| 718 | |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 719 | // NewTempDir creates a temporary directory. Temporary directories and |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 720 | // their contents will be deleted by Cleanup. |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 721 | func (e *T) NewTempDir() string { |
| 722 | loc := Caller(1) |
James Ring | 23b5486 | 2015-01-07 11:54:48 -0800 | [diff] [blame] | 723 | f, err := ioutil.TempDir("", "") |
| 724 | if err != nil { |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 725 | e.Fatalf("%s: TempDir() failed: %v", loc, err) |
James Ring | 23b5486 | 2015-01-07 11:54:48 -0800 | [diff] [blame] | 726 | } |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 727 | vlog.Infof("%s: created temporary directory at %s", loc, f) |
James Ring | 23b5486 | 2015-01-07 11:54:48 -0800 | [diff] [blame] | 728 | e.tempDirs = append(e.tempDirs, f) |
| 729 | return f |
| 730 | } |
| 731 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 732 | func (e *T) appendInvocation(inv *Invocation) { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 733 | e.invocations = append(e.invocations, inv) |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 734 | } |
| 735 | |
James Ring | a71e49a | 2015-01-16 14:08:02 -0800 | [diff] [blame] | 736 | // Creates a new local testing environment. A local testing environment has a |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 737 | // a security principle available via Principal(). |
James Ring | a71e49a | 2015-01-16 14:08:02 -0800 | [diff] [blame] | 738 | // |
| 739 | // You should clean up the returned environment using the env.Cleanup() method. |
| 740 | // A typical end-to-end test will begin like: |
| 741 | // |
| 742 | // func TestFoo(t *testing.T) { |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 743 | // env := integration.NewT(t) |
James Ring | a71e49a | 2015-01-16 14:08:02 -0800 | [diff] [blame] | 744 | // defer env.Cleanup() |
| 745 | // |
| 746 | // ... |
| 747 | // } |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 748 | func New(t TB) *T { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 749 | ctx, shutdown := veyron2.Init() |
| 750 | |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 751 | vlog.Infof("creating root principal") |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 752 | principal := tsecurity.NewPrincipal("root") |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 753 | ctx, err := veyron2.SetPrincipal(ctx, principal) |
Ryan Brown | a08a221 | 2015-01-15 15:40:10 -0800 | [diff] [blame] | 754 | if err != nil { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 755 | t.Fatalf("failed to set principal: %v", err) |
Ryan Brown | a08a221 | 2015-01-15 15:40:10 -0800 | [diff] [blame] | 756 | } |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 757 | |
| 758 | shell, err := modules.NewShell(ctx, principal) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 759 | if err != nil { |
| 760 | t.Fatalf("NewShell() failed: %v", err) |
| 761 | } |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 762 | shell.SetStartTimeout(1 * time.Minute) |
Cosmos Nicolaou | fb3bee5 | 2015-02-05 11:59:14 -0800 | [diff] [blame] | 763 | shell.SetWaitTimeout(5 * time.Minute) |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 764 | |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 765 | // The V23_BIN_DIR environment variable can be |
| 766 | // used to identify a directory that multiple integration |
| 767 | // tests can use to share binaries. Whoever sets this |
| 768 | // environment variable is responsible for cleaning up the |
| 769 | // directory it points to. |
Cosmos Nicolaou | a866f26 | 2015-02-10 14:56:06 -0800 | [diff] [blame] | 770 | cachedBinDir := os.Getenv("V23_BIN_DIR") |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 771 | e := &T{ |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 772 | TB: t, |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 773 | principal: principal, |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 774 | builtBinaries: make(map[string]*Binary), |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 775 | shell: shell, |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 776 | tempFiles: []*os.File{}, |
James Ring | 23b5486 | 2015-01-07 11:54:48 -0800 | [diff] [blame] | 777 | tempDirs: []string{}, |
Cosmos Nicolaou | a866f26 | 2015-02-10 14:56:06 -0800 | [diff] [blame] | 778 | cachedBinDir: cachedBinDir, |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 779 | shutdown: shutdown, |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 780 | } |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 781 | if len(e.cachedBinDir) == 0 { |
| 782 | e.binDir = e.NewTempDir() |
| 783 | } |
| 784 | return e |
| 785 | } |
| 786 | |
| 787 | // BinDir returns the directory that binarie files are stored in. |
| 788 | func (e *T) BinDir() string { |
| 789 | if len(e.cachedBinDir) > 0 { |
| 790 | return e.cachedBinDir |
| 791 | } |
| 792 | return e.binDir |
James Ring | 6579510 | 2014-12-17 13:01:03 -0800 | [diff] [blame] | 793 | } |
| 794 | |
James Ring | bd2216f | 2015-01-15 20:06:18 -0800 | [diff] [blame] | 795 | // BuildPkg returns a path to a directory that contains the built binary for |
| 796 | // the given packages and a function that should be invoked to clean up the |
| 797 | // build artifacts. Note that the clients of this function should not modify |
| 798 | // the contents of this directory directly and instead defer to the cleanup |
| 799 | // function. |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 800 | func buildPkg(binDir, pkg string) (bool, string, error) { |
James Ring | bd2216f | 2015-01-15 20:06:18 -0800 | [diff] [blame] | 801 | binFile := filepath.Join(binDir, path.Base(pkg)) |
| 802 | if _, err := os.Stat(binFile); err != nil { |
| 803 | if !os.IsNotExist(err) { |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 804 | return false, "", err |
James Ring | bd2216f | 2015-01-15 20:06:18 -0800 | [diff] [blame] | 805 | } |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 806 | cmd := exec.Command("v23", "go", "build", "-o", binFile, pkg) |
Cosmos Nicolaou | 1fcb6a3 | 2015-02-17 07:46:02 -0800 | [diff] [blame] | 807 | if output, err := cmd.CombinedOutput(); err != nil { |
| 808 | 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] | 809 | return false, "", err |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 810 | } |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 811 | return false, binFile, nil |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 812 | } |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 813 | return true, binFile, nil |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Cosmos Nicolaou | 4a77c19 | 2015-02-08 15:29:18 -0800 | [diff] [blame] | 816 | // RunTest runs a single Vanadium 'v23 style' integration test. |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 817 | func RunTest(t *testing.T, fn func(i *T)) { |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 818 | if !testutil.IntegrationTestsEnabled { |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 819 | t.Skip() |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 820 | } |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 821 | i := New(t) |
Cosmos Nicolaou | cc5a4a8 | 2015-02-07 23:09:28 -0800 | [diff] [blame] | 822 | // defer the Cleanup method so that it will be called even if |
| 823 | // t.Fatalf/FailNow etc are called and can print out useful information. |
| 824 | defer i.Cleanup() |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 825 | fn(i) |
Jiri Simsa | e63e07d | 2014-12-04 11:12:08 -0800 | [diff] [blame] | 826 | } |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 827 | |
Cosmos Nicolaou | 4a77c19 | 2015-02-08 15:29:18 -0800 | [diff] [blame] | 828 | // RunRootMT builds and runs a root mount table instance. It populates |
| 829 | // the NAMESPACE_ROOT variable in the test environment so that all subsequent |
| 830 | // invocations will access this root mount table. |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 831 | func RunRootMT(i *T, args ...string) (*Binary, *Invocation) { |
| 832 | b := i.BuildGoPkg("v.io/core/veyron/services/mounttable/mounttabled") |
Cosmos Nicolaou | d3b1cb1 | 2015-02-20 00:06:33 -0800 | [diff] [blame] | 833 | inv := b.start(1, args...) |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 834 | name := inv.ExpectVar("NAME") |
| 835 | inv.Environment().SetVar("NAMESPACE_ROOT", name) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 836 | vlog.Infof("Running root mount table: %q", name) |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 837 | return b, inv |
Cosmos Nicolaou | d21f6b1 | 2015-02-07 11:40:03 -0800 | [diff] [blame] | 838 | } |
| 839 | |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 840 | // UseSharedBinDir ensures that a shared directory is used for binaries |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 841 | // across multiple instances of the test environment. This is achieved |
| 842 | // by setting the V23_BIN_DIR environment variable if it is not already |
| 843 | // set in the test processes environment (as will typically be the case when |
| 844 | // these tests are run from the v23 tool). It is intended to be called |
| 845 | // from TestMain. |
| 846 | func UseSharedBinDir() func() { |
| 847 | if v23BinDir := os.Getenv("V23_BIN_DIR"); len(v23BinDir) == 0 { |
| 848 | v23BinDir, err := ioutil.TempDir("", "bin-") |
| 849 | if err == nil { |
| 850 | vlog.Infof("Setting V23_BIN_DIR to %q", v23BinDir) |
| 851 | os.Setenv("V23_BIN_DIR", v23BinDir) |
| 852 | return func() { os.RemoveAll(v23BinDir) } |
| 853 | } |
| 854 | } else { |
| 855 | vlog.Infof("Using V23_BIN_DIR %q", v23BinDir) |
| 856 | } |
| 857 | return func() {} |
| 858 | } |