get dataproc logs when jobs fail in test (#2049)

The hadoop test passes for me locally but fails in CI, so this should give some extra debugging information (my user acct doesn't have permissions to read the dataproc logs from the failing test).

WIP for now because I want to make sure this method of reading the logs works.
This commit is contained in:
Dana Hoffman 2018-09-14 13:36:32 -07:00 committed by GitHub
parent dc27ea8e49
commit f7630cb5f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,10 +2,14 @@ package google
import (
"fmt"
"io/ioutil"
"log"
"strings"
"testing"
"regexp"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
@ -318,7 +322,32 @@ func testAccCheckDataprocJobCompletesSuccessfully(n string, job *dataproc.Job) r
if err != nil {
return err
}
if completeJob.Status.State != "DONE" {
if completeJob.Status.State == "ERROR" {
if !strings.HasPrefix(completeJob.DriverOutputResourceUri, "gs://") {
return fmt.Errorf("Job completed in ERROR state but no valid log URI found")
}
u := strings.SplitN(strings.TrimPrefix(completeJob.DriverOutputResourceUri, "gs://"), "/", 2)
if len(u) != 2 {
return fmt.Errorf("Job completed in ERROR state but no valid log URI found")
}
l, err := config.clientStorage.Objects.List(u[0]).Prefix(u[1]).Do()
if err != nil {
return errwrap.Wrapf("Job completed in ERROR state, found error when trying to list logs: {{err}}", err)
}
for _, item := range l.Items {
resp, err := config.clientStorage.Objects.Get(item.Bucket, item.Name).Download()
if err != nil {
return errwrap.Wrapf("Job completed in ERROR state, found error when trying to read logs: {{err}}", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errwrap.Wrapf("Job completed in ERROR state, found error when trying to read logs: {{err}}", err)
}
log.Printf("[ERROR] Job failed, driver logs:\n%s", body)
}
return fmt.Errorf("Job completed in ERROR state, check logs for details")
} else if completeJob.Status.State != "DONE" {
return fmt.Errorf("Job did not complete successfully, instead status: %s", completeJob.Status.State)
}