Merge branch 'master' into profiler-test

Change-Id: I4e83680309651ecf22c6dcd6de928ac8b51e2bad
diff --git a/internal/kokoro/build.sh b/internal/kokoro/build.sh
index 869be0d..6b5a464 100755
--- a/internal/kokoro/build.sh
+++ b/internal/kokoro/build.sh
@@ -6,39 +6,4 @@
 # Display commands being run
 set -x
 
-# cd to project dir on Kokoro instance
-cd git/gocloud
-
-go version
-
-# Set $GOPATH
-export GOPATH="$HOME/go"
-GOCLOUD_HOME=$GOPATH/src/cloud.google.com/go
-mkdir -p $GOCLOUD_HOME
-
-# Move code into $GOPATH and get dependencies
-cp -R ./* $GOCLOUD_HOME
-cd $GOCLOUD_HOME
-go get -v ./...
-
-# # Don't run integration tests until we can protect against code from 
-# # untrusted forks reading and storing our service account key.
-# cd internal/kokoro
-# # Don't print out encryption keys, etc
-# set +x
-# key=$(cat $KOKORO_ARTIFACTS_DIR/keystore/*_encrypted_ba2d6f7723ed_key)
-# iv=$(cat $KOKORO_ARTIFACTS_DIR/keystore/*_encrypted_ba2d6f7723ed_iv)
-# pass=$(cat $KOKORO_ARTIFACTS_DIR/keystore/*_encrypted_ba2d6f7723ed_pass)
-
-# openssl aes-256-cbc -K $key -iv $iv -pass pass:$pass -in kokoro-key.json.enc -out key.json -d
-# set -x
-
-# export GCLOUD_TESTS_GOLANG_KEY="$(pwd)/key.json"
-# export GCLOUD_TESTS_GOLANG_PROJECT_ID="dulcet-port-762"
-# cd $GOCLOUD_HOME
-
-# Run tests and tee output to log file, to be pushed to GCS as artifact.
-go test -race -v -short ./... 2>&1 | tee $KOKORO_ARTIFACTS_DIR/$KOKORO_GERRIT_CHANGE_NUMBER.txt
-
-# Make sure README.md is up to date.
-make -C internal/readme test diff
+source git/gocloud/profiler/integration-test.sh .
diff --git a/profiler/busybench/busybench.go b/profiler/busybench/busybench.go
index 44d25b1..b25407e 100644
--- a/profiler/busybench/busybench.go
+++ b/profiler/busybench/busybench.go
@@ -16,13 +16,14 @@
 
 import (
 	"bytes"
-	"cloud.google.com/go/profiler"
 	"compress/gzip"
 	"flag"
 	"log"
 	"math/rand"
 	"sync"
 	"time"
+
+	"cloud.google.com/go/profiler"
 )
 
 var (
diff --git a/profiler/integration-test.sh b/profiler/integration-test.sh
index f5f3f33..bc3e473 100644
--- a/profiler/integration-test.sh
+++ b/profiler/integration-test.sh
@@ -9,6 +9,8 @@
 cd git/gocloud
 COMMIT=$(git rev-parse HEAD)
 
+go version
+
 # Set $GOPATH
 export GOPATH="$HOME/go"
 GOCLOUD_HOME=$GOPATH/src/cloud.google.com/go
@@ -33,4 +35,4 @@
 
 cd $GOCLOUD_HOME/profiler
 go get -t -tags=integration .
-go test -timeout=60m -parallel=5 -tags=integration -run TestAgentIntegration -commit="$COMMIT"
+go test -timeout=60m -parallel=4 -tags=integration -run TestAgentIntegration -commit="$COMMIT"
diff --git a/profiler/integration_test.go b/profiler/integration_test.go
index 53496e1..c9f2fd6 100644
--- a/profiler/integration_test.go
+++ b/profiler/integration_test.go
@@ -84,6 +84,7 @@
 retry git clone https://code.googlesource.com/gocloud $GOCLOUD_HOME >/dev/null
 
 cd $GOCLOUD_HOME/profiler/busybench
+git checkout profiler-test
 git reset --hard {{.Commit}}
 retry go get >/dev/null
 
@@ -96,7 +97,7 @@
 
 const dockerfileFmt = `FROM golang
 RUN git clone https://code.googlesource.com/gocloud /go/src/cloud.google.com/go \
-    && cd /go/src/cloud.google.com/go/profiler/busybench && git reset --hard %s \
+    && cd /go/src/cloud.google.com/go/profiler/busybench && git checkout profiler-test && git reset --hard %s \
     && go get && go install
 CMD ["busybench", "--service", "%s"]
  `
diff --git a/profiler/profiler_test.go b/profiler/profiler_test.go
index 8c5cff3..1b64f9b 100644
--- a/profiler/profiler_test.go
+++ b/profiler/profiler_test.go
@@ -413,6 +413,15 @@
 		if !testutil.Equal(a.profileTypes, tt.wantProfileTypes) {
 			t.Errorf("initializeAgent() got profile types: %v, want %v", a.profileTypes, tt.wantProfileTypes)
 		}
+
+		wantProfileTypes := []pb.ProfileType{pb.ProfileType_CPU, pb.ProfileType_HEAP, pb.ProfileType_THREADS}
+		if tt.enableMutex {
+			wantProfileTypes = append(wantProfileTypes, pb.ProfileType_CONTENTION)
+		}
+		if !testutil.Equal(a.profileTypes, wantProfileTypes) {
+			t.Errorf("initializeAgent() got profile types: %v, want %v", a.profileTypes, wantProfileTypes)
+		}
+
 	}
 }
 
diff --git a/profiler/proftest/proftest.go b/profiler/proftest/proftest.go
index 2641fcb..73601d1 100644
--- a/profiler/proftest/proftest.go
+++ b/profiler/proftest/proftest.go
@@ -144,7 +144,7 @@
 		return err
 	}
 
-	_, err = tr.ComputeService.Instances.Insert(inst.ProjectID, inst.Zone, &compute.Instance{
+	op, err := tr.ComputeService.Instances.Insert(inst.ProjectID, inst.Zone, &compute.Instance{
 		MachineType: fmt.Sprintf("zones/%s/machineTypes/%s", inst.Zone, inst.MachineType),
 		Name:        inst.Name,
 		Disks: []*compute.AttachedDisk{{
@@ -177,7 +177,28 @@
 		}},
 	}).Do()
 
-	return err
+	// Poll status of the operation to create the instance.
+	for {
+		select {
+		case <-ctx.Done():
+			return ctx.Err()
+		case <-time.After(20 * time.Second):
+			if op.Status == "DONE" {
+				if op.Error != nil {
+					var errMsgs []string
+					for _, e := range op.Error.Errors {
+						if e.Message != "" {
+							errMsgs = append(errMsgs, e.Message)
+						} else {
+							errMsgs = append(errMsgs, e.Code)
+						}
+					}
+					return fmt.Errorf("Failed to create instance: %v", errMsgs)
+				}
+				return nil
+			}
+		}
+	}
 }
 
 // DeleteInstance deletes an instance with project id, name, and zone matched