veyron/services/mgmt/build: Fix tests when using a custom Go compiler
(that requires a custom GOROOT).

This should fix the test errors in Jenkins.

Change-Id: Ic66de54b8df07934e21a331701520868f343193a
diff --git a/services/mgmt/build/buildd/main.go b/services/mgmt/build/buildd/main.go
index 7f7aa55..26d8b14 100644
--- a/services/mgmt/build/buildd/main.go
+++ b/services/mgmt/build/buildd/main.go
@@ -2,6 +2,7 @@
 
 import (
 	"flag"
+	"os"
 
 	"veyron/lib/signals"
 	vflag "veyron/security/flag"
@@ -19,8 +20,9 @@
 	protocol = flag.String("protocol", "tcp", "protocol to listen on")
 	address  = flag.String("address", ":0", "address to listen on")
 
-	gobin = flag.String("gobin", "go", "path to the Go compiler")
-	name  = flag.String("name", "", "name to mount the build server as")
+	gobin  = flag.String("gobin", "go", "path to the Go compiler")
+	goroot = flag.String("goroot", os.Getenv("GOROOT"), "GOROOT to use with the Go compiler")
+	name   = flag.String("name", "", "name to mount the build server as")
 )
 
 func main() {
@@ -38,7 +40,7 @@
 		vlog.Errorf("Listen(%v, %v) failed: %v", *protocol, *address, err)
 		return
 	}
-	if err := server.Serve(*name, ipc.LeafDispatcher(build.NewServerBuilder(impl.NewInvoker(*gobin)), vflag.NewAuthorizerOrDie())); err != nil {
+	if err := server.Serve(*name, ipc.LeafDispatcher(build.NewServerBuilder(impl.NewInvoker(*gobin, *goroot)), vflag.NewAuthorizerOrDie())); err != nil {
 		vlog.Errorf("Serve(%v) failed: %v", *name, err)
 		return
 	}
diff --git a/services/mgmt/build/impl/impl_test.go b/services/mgmt/build/impl/impl_test.go
index 4bf9ec0..cb92f4d 100644
--- a/services/mgmt/build/impl/impl_test.go
+++ b/services/mgmt/build/impl/impl_test.go
@@ -19,15 +19,17 @@
 	rt.Init()
 }
 
-// findGoBinary returns the path to the given Go binary.
-func findGoBinary(t *testing.T, name string) string {
+// findGoBinary returns the path to the given Go binary and
+// the GOROOT environment variable to use.
+func findGoBinary(t *testing.T, name string) (bin, goroot string) {
 	root := os.Getenv("VEYRON_ROOT")
 	if root == "" {
 		t.Fatalf("VEYRON_ROOT is not set")
 	}
-	envbin := filepath.Join(root, "environment", "go", runtime.GOOS, runtime.GOARCH, "go", "bin", name)
+	envroot := filepath.Join(root, "environment", "go", runtime.GOOS, runtime.GOARCH, "go")
+	envbin := filepath.Join(envroot, "bin", name)
 	if _, err := os.Stat(envbin); err == nil {
-		return envbin
+		return envbin, envroot
 	} else if !os.IsNotExist(err) {
 		t.Fatalf("Stat(%v) failed: %v", envbin, err)
 	}
@@ -39,12 +41,12 @@
 			t.Fatalf("LookPath(%q) failed: %v", name, err)
 		}
 	}
-	return pathbin
+	return pathbin, ""
 }
 
 // startServer starts the build server.
 func startServer(t *testing.T) (build.Builder, func()) {
-	gobin := findGoBinary(t, "go")
+	gobin, goroot := findGoBinary(t, "go")
 	server, err := rt.R().NewServer()
 	if err != nil {
 		t.Fatalf("NewServer() failed: %v", err)
@@ -55,7 +57,7 @@
 		t.Fatalf("Listen(%v, %v) failed: %v", protocol, hostname, err)
 	}
 	unpublished := ""
-	if err := server.Serve(unpublished, ipc.LeafDispatcher(build.NewServerBuilder(NewInvoker(gobin)), nil)); err != nil {
+	if err := server.Serve(unpublished, ipc.LeafDispatcher(build.NewServerBuilder(NewInvoker(gobin, goroot)), nil)); err != nil {
 		t.Fatalf("Serve(%q) failed: %v", unpublished, err)
 	}
 	name := "/" + endpoint.String()
diff --git a/services/mgmt/build/impl/invoker.go b/services/mgmt/build/impl/invoker.go
index 22f9f24..f55cee5 100644
--- a/services/mgmt/build/impl/invoker.go
+++ b/services/mgmt/build/impl/invoker.go
@@ -23,14 +23,15 @@
 
 // invoker holds the state of a build server invocation.
 type invoker struct {
-	// gobin is the path to the Go compiler binary.
-	gobin string
+	// Path to the binary and the value of the GOROOT environment variable.
+	gobin, goroot string
 }
 
 // NewInvoker is the invoker factory.
-func NewInvoker(gobin string) *invoker {
+func NewInvoker(gobin, goroot string) *invoker {
 	return &invoker{
-		gobin: gobin,
+		gobin:  gobin,
+		goroot: goroot,
 	}
 }
 
@@ -78,6 +79,9 @@
 	cmd.Env = append(cmd.Env, "GOARCH="+string(arch))
 	cmd.Env = append(cmd.Env, "GOOS="+string(opsys))
 	cmd.Env = append(cmd.Env, "GOPATH="+filepath.Dir(srcDir))
+	if i.goroot != "" {
+		cmd.Env = append(cmd.Env, "GOROOT="+i.goroot)
+	}
 	var output bytes.Buffer
 	cmd.Stdout = &output
 	cmd.Stderr = &output