profiler: continuous test should finish when there is an error

Change-Id: If90c4c53e343f41d65de9c54cfe1487ec1aaee83
Reviewed-on: https://code-review.googlesource.com/c/34950
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alexey Alexandrov <aalexand@google.com>
diff --git a/profiler/integration_test.go b/profiler/integration_test.go
index 7602ef8..7c372da 100644
--- a/profiler/integration_test.go
+++ b/profiler/integration_test.go
@@ -41,11 +41,15 @@
 const (
 	cloudScope        = "https://www.googleapis.com/auth/cloud-platform"
 	benchFinishString = "busybench finished profiling"
+	errorString       = "failed to set up or run the benchmark"
 )
 
 const startupTemplate = `
 #! /bin/bash
 
+# Signal any unexpected error.
+trap 'echo "{{.ErrorString}}"' ERR
+
 (
 # Shut down the VM in 5 minutes after this script exits
 # to stop accounting the VM for billing and cores quota.
@@ -118,11 +122,13 @@
 			Service        string
 			GoVersion      string
 			Commit         string
+			ErrorString    string
 			MutexProfiling bool
 		}{
 			Service:        tc.name,
 			GoVersion:      tc.goVersion,
 			Commit:         *commit,
+			ErrorString:    errorString,
 			MutexProfiling: tc.mutexProfiling,
 		})
 	if err != nil {
@@ -266,7 +272,7 @@
 
 			timeoutCtx, cancel := context.WithTimeout(ctx, time.Minute*25)
 			defer cancel()
-			if err := gceTr.PollForSerialOutput(timeoutCtx, &tc.InstanceConfig, benchFinishString); err != nil {
+			if err := gceTr.PollForSerialOutput(timeoutCtx, &tc.InstanceConfig, benchFinishString, errorString); err != nil {
 				t.Fatalf("PollForSerialOutput() got error: %v", err)
 			}
 
diff --git a/profiler/proftest/proftest.go b/profiler/proftest/proftest.go
index 08a5047..3029e38 100644
--- a/profiler/proftest/proftest.go
+++ b/profiler/proftest/proftest.go
@@ -234,7 +234,7 @@
 // PollForSerialOutput polls serial port 2 of the GCE instance specified by
 // inst and returns when the finishString appears in the serial output
 // of the instance, or when the context times out.
-func (tr *GCETestRunner) PollForSerialOutput(ctx context.Context, inst *InstanceConfig, finishString string) error {
+func (tr *GCETestRunner) PollForSerialOutput(ctx context.Context, inst *InstanceConfig, finishString, errorString string) error {
 	var output string
 	defer func() {
 		log.Printf("Serial port output for %s:\n%s", inst.Name, output)
@@ -258,6 +258,9 @@
 			if output = resp.Contents; strings.Contains(output, finishString) {
 				return nil
 			}
+			if strings.Contains(output, errorString) {
+				return fmt.Errorf("failed to execute the prober benchmark script")
+			}
 		}
 	}
 }