services/mgmt/build/buildd: port buildd integration test to new test env

Change-Id: Ifba397fd051ccac8a1f3a177d8fdbd0fa6f02cf8
diff --git a/lib/testutil/integration/util.go b/lib/testutil/integration/util.go
index 8547e8f..0255ab3 100644
--- a/lib/testutil/integration/util.go
+++ b/lib/testutil/integration/util.go
@@ -68,6 +68,10 @@
 
 	// Path returns the path to the binary.
 	Path() string
+
+	// Returns a copy of this binary that, when Start is called, will use
+	// the given environment variables.
+	WithEnv(env []string) TestBinary
 }
 
 type Invocation interface {
@@ -129,6 +133,10 @@
 	// The path to the binary.
 	path string
 
+	// Environment variables that will be used when creating invocations
+	// via Start.
+	envVars []string
+
 	// The cleanup function to run when the binary exits.
 	cleanupFunc func()
 }
@@ -183,7 +191,7 @@
 
 func (i *integrationTestBinaryInvocation) WaitOrDie(stdout, stderr io.Writer) {
 	if err := i.Wait(stdout, stderr); err != nil {
-		i.env.t.Fatalf("Wait() for pid %d failed: %v", (*i.handle).Pid(), err)
+		i.env.t.Fatalf("FATAL: Wait() for pid %d failed: %v", (*i.handle).Pid(), err)
 	}
 }
 
@@ -205,7 +213,7 @@
 		locationString = fmt.Sprintf("(requested at %s:%d) ", filepath.Base(file), line)
 	}
 	b.env.t.Logf("%sstarting %s %s", locationString, b.Path(), strings.Join(args, " "))
-	handle, err := b.env.shell.Start("exec", nil, append([]string{b.Path()}, args...)...)
+	handle, err := b.env.shell.Start("exec", b.envVars, append([]string{b.Path()}, args...)...)
 	if err != nil {
 		b.env.t.Fatalf("Start(%v, %v) failed: %v", b.Path(), strings.Join(args, ", "), err)
 	}
@@ -216,6 +224,12 @@
 	}
 }
 
+func (b *integrationTestBinary) WithEnv(env []string) TestBinary {
+	newBin := *b
+	newBin.envVars = env
+	return &newBin
+}
+
 func (e *integrationTestEnvironment) RootMT() string {
 	return e.mtEndpoint
 }
@@ -319,6 +333,7 @@
 	e.t.Logf("done building %s, written to %s.", binary_path, output_path)
 	binary := &integrationTestBinary{
 		env:         e,
+		envVars:     nil,
 		path:        output_path,
 		cleanupFunc: cleanup,
 	}
diff --git a/services/mgmt/build/buildd/testdata/integration_test.go b/services/mgmt/build/buildd/testdata/integration_test.go
index 2cc87c7..d6d4384 100644
--- a/services/mgmt/build/buildd/testdata/integration_test.go
+++ b/services/mgmt/build/buildd/testdata/integration_test.go
@@ -2,12 +2,13 @@
 
 import (
 	"bytes"
-	"fmt"
 	"io/ioutil"
 	"os"
 	"os/exec"
 	"path/filepath"
+	"runtime"
 	"strings"
+	"syscall"
 	"testing"
 
 	"v.io/core/veyron/lib/modules"
@@ -16,11 +17,6 @@
 	_ "v.io/core/veyron/profiles"
 )
 
-var binPkgs = []string{
-	"v.io/core/veyron/services/mgmt/build/buildd",
-	"v.io/core/veyron/tools/build",
-}
-
 var testProgram = `package main
 
 import "fmt"
@@ -28,44 +24,13 @@
 func main() { fmt.Println("Hello World!") }
 `
 
-func goRoot(bin string) (string, error) {
-	var out bytes.Buffer
-	cmd := exec.Command(bin, "env", "GOROOT")
-	cmd.Stdout = &out
-	cmd.Stderr = &out
-	if err := cmd.Run(); err != nil {
-		return "", fmt.Errorf("%q failed: %v\n%v", strings.Join(cmd.Args, " "), err, out.String())
-	}
-	cleanOut := strings.TrimSpace(out.String())
-	if cleanOut == "" {
-		return "", fmt.Errorf("%v does not set GOROOT", bin)
-	}
-	return cleanOut, nil
-}
-
 func TestHelperProcess(t *testing.T) {
 	modules.DispatchInTest()
 }
 
 func TestBuildServerIntegration(t *testing.T) {
-	// Build the required binaries.
-	binDir, cleanup, err := integration.BuildPkgs(binPkgs)
-	if err != nil {
-		t.Fatalf("%v", err)
-	}
-	defer cleanup()
-
-	// Start a root mount table.
-	shell, err := modules.NewShell(nil)
-	if err != nil {
-		t.Fatalf("NewShell() failed: %v", err)
-	}
-	defer shell.Cleanup(os.Stdin, os.Stderr)
-	handle, mtName, err := integration.StartRootMT(shell)
-	if err != nil {
-		t.Fatalf("%v", err)
-	}
-	defer handle.CloseStdin()
+	env := integration.NewTestEnvironment(t)
+	defer env.Cleanup()
 
 	// Generate credentials.
 	serverCred, serverPrin := security.NewCredentials("server")
@@ -74,34 +39,24 @@
 	defer os.RemoveAll(clientCred)
 
 	// Start the build server.
-	buildServerBin := filepath.Join(binDir, "buildd")
+	buildServerBin := env.BuildGoPkg("v.io/core/veyron/services/mgmt/build/buildd")
 	buildServerName := "test-build-server"
 	goBin, err := exec.LookPath("go")
 	if err != nil {
 		t.Fatalf("%v", err)
 	}
-	goRoot, err := goRoot(goBin)
-	if err != nil {
-		t.Fatalf("%v", err)
-	}
+	goRoot := runtime.GOROOT()
 	args := []string{
 		"-name=" + buildServerName, "-gobin=" + goBin, "-goroot=" + goRoot,
 		"-veyron.tcp.address=127.0.0.1:0",
 		"-veyron.credentials=" + serverCred,
-		"-veyron.namespace.root=" + mtName,
+		"-veyron.namespace.root=" + env.RootMT(),
 	}
-	serverProcess, err := integration.StartServer(buildServerBin, args)
-	if err != nil {
-		t.Fatalf("%v", err)
-	}
-	defer serverProcess.Kill()
+	serverProcess := buildServerBin.Start(args...)
+	defer serverProcess.Kill(syscall.SIGTERM)
 
 	// Create and build a test source file.
-	testGoPath, err := ioutil.TempDir("", "")
-	if err != nil {
-		t.Fatalf("TempDir() failed: %v", err)
-	}
-	defer os.RemoveAll(testGoPath)
+	testGoPath := env.TempDir()
 	testBinDir := filepath.Join(testGoPath, "bin")
 	if err := os.MkdirAll(testBinDir, os.FileMode(0700)); err != nil {
 		t.Fatalf("MkdirAll(%v) failed: %v", testBinDir, err)
@@ -115,19 +70,14 @@
 	if err := ioutil.WriteFile(testSrcFile, []byte(testProgram), os.FileMode(0600)); err != nil {
 		t.Fatalf("WriteFile(%v) failed: %v", testSrcFile, err)
 	}
-	var buildOut bytes.Buffer
 	buildArgs := []string{
 		"-veyron.credentials=" + clientCred,
-		"-veyron.namespace.root=" + mtName,
+		"-veyron.namespace.root=" + env.RootMT(),
 		"build", buildServerName, "test",
 	}
-	buildCmd := exec.Command(filepath.Join(binDir, "build"), buildArgs...)
-	buildCmd.Stdout = &buildOut
-	buildCmd.Stderr = &buildOut
-	buildCmd.Env = append(buildCmd.Env, "GOPATH="+testGoPath, "GOROOT="+goRoot, "TMPDIR="+testBinDir)
-	if err := buildCmd.Run(); err != nil {
-		t.Fatalf("%q failed: %v\n%v", strings.Join(buildCmd.Args, " "), err, buildOut.String())
-	}
+	buildEnv := []string{"GOPATH=" + testGoPath, "GOROOT=" + goRoot, "TMPDIR=" + testBinDir}
+	buildBin := env.BuildGoPkg("v.io/core/veyron/tools/build")
+	buildBin.WithEnv(buildEnv).Start(buildArgs...).WaitOrDie(os.Stdout, os.Stderr)
 	var testOut bytes.Buffer
 	testCmd := exec.Command(testBinFile)
 	testCmd.Stdout = &testOut