Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the error handling when creating/delete GCE IPs in e2e tests. #15408

Merged
merged 1 commit into from
Oct 15, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 22 additions & 13 deletions test/e2e/google_compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,45 @@ import (
"github.com/golang/glog"
)

// TODO: These should really just use the GCE API client library or at least use
// better formatted output from the --format flag.

func createGCEStaticIP(name string) (string, error) {
// gcloud compute --project "abshah-kubernetes-001" addresses create "test-static-ip" --region "us-central1"
// abshah@abhidesk:~/go/src/code.google.com/p/google-api-go-client/compute/v1$ gcloud compute --project "abshah-kubernetes-001" addresses create "test-static-ip" --region "us-central1"
// Created [https://www.googleapis.com/compute/v1/projects/abshah-kubernetes-001/regions/us-central1/addresses/test-static-ip].
// NAME REGION ADDRESS STATUS
// test-static-ip us-central1 104.197.143.7 RESERVED

var output []byte
glog.Infof("Creating static IP with name %q in project %q", name, testContext.CloudConfig.ProjectID)
var outputBytes []byte
var err error
for attempts := 0; attempts < 4; attempts++ {
output, err = exec.Command("gcloud", "compute", "addresses", "create",
outputBytes, err = exec.Command("gcloud", "compute", "addresses", "create",
name, "--project", testContext.CloudConfig.ProjectID,
"--region", "us-central1", "-q").CombinedOutput()
if err == nil {
break
}
glog.Errorf("Creating static IP with name:%s in project: %s", name, testContext.CloudConfig.ProjectID)
glog.Errorf("output: %s", output)
glog.Errorf("output from failed attempt to create static IP: %s", outputBytes)
time.Sleep(time.Duration(5*attempts) * time.Second)
}
if err != nil {
return "", err
// Ditch the error, since the stderr in the output is what actually contains
// any useful info.
return "", fmt.Errorf("failed to create static IP: %s", outputBytes)
}
text := string(output)
if strings.Contains(text, "RESERVED") {
output := string(outputBytes)
if strings.Contains(output, "RESERVED") {
r, _ := regexp.Compile("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+")
staticIP := r.FindString(text)
staticIP := r.FindString(output)
if staticIP == "" {
glog.Errorf("Static IP creation output is \n %s", text)
return "", fmt.Errorf("Static IP not found in gcloud compute command output")
return "", fmt.Errorf("static IP not found in gcloud command output: %v", output)
} else {
return staticIP, nil
}
} else {
return "", fmt.Errorf("Static IP Could not be reserved.")
return "", fmt.Errorf("static IP %q could not be reserved: %v", name, output)
}
}

Expand All @@ -71,8 +75,13 @@ func deleteGCEStaticIP(name string) error {
// NAME REGION ADDRESS STATUS
// test-static-ip us-central1 104.197.143.7 RESERVED

_, err := exec.Command("gcloud", "compute", "addresses", "delete",
outputBytes, err := exec.Command("gcloud", "compute", "addresses", "delete",
name, "--project", testContext.CloudConfig.ProjectID,
"--region", "us-central1", "-q").CombinedOutput()
return err
if err != nil {
// Ditch the error, since the stderr in the output is what actually contains
// any useful info.
return fmt.Errorf("failed to delete static IP %q: %v", name, string(outputBytes))
}
return nil
}