Merge pull request #15408 from a-robinson/tests

Improve the error handling when creating/delete GCE IPs in e2e tests.
This commit is contained in:
Eric Tune 2015-10-15 13:57:40 -07:00
commit 442f61623c

View File

@ -26,6 +26,9 @@ import (
"github.com/golang/glog" "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) { func createGCEStaticIP(name string) (string, error) {
// gcloud compute --project "abshah-kubernetes-001" addresses create "test-static-ip" --region "us-central1" // 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" // 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"
@ -33,34 +36,35 @@ func createGCEStaticIP(name string) (string, error) {
// NAME REGION ADDRESS STATUS // NAME REGION ADDRESS STATUS
// test-static-ip us-central1 104.197.143.7 RESERVED // 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 var err error
for attempts := 0; attempts < 4; attempts++ { 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, name, "--project", testContext.CloudConfig.ProjectID,
"--region", "us-central1", "-q").CombinedOutput() "--region", "us-central1", "-q").CombinedOutput()
if err == nil { if err == nil {
break break
} }
glog.Errorf("Creating static IP with name:%s in project: %s", name, testContext.CloudConfig.ProjectID) glog.Errorf("output from failed attempt to create static IP: %s", outputBytes)
glog.Errorf("output: %s", output)
time.Sleep(time.Duration(5*attempts) * time.Second) time.Sleep(time.Duration(5*attempts) * time.Second)
} }
if err != nil { 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) output := string(outputBytes)
if strings.Contains(text, "RESERVED") { if strings.Contains(output, "RESERVED") {
r, _ := regexp.Compile("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+") r, _ := regexp.Compile("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+")
staticIP := r.FindString(text) staticIP := r.FindString(output)
if staticIP == "" { if staticIP == "" {
glog.Errorf("Static IP creation output is \n %s", text) return "", fmt.Errorf("static IP not found in gcloud command output: %v", output)
return "", fmt.Errorf("Static IP not found in gcloud compute command output")
} else { } else {
return staticIP, nil return staticIP, nil
} }
} else { } else {
return "", fmt.Errorf("Static IP Could not be reserved.") return "", fmt.Errorf("static IP %q could not be reserved: %v", name, output)
} }
} }
@ -71,8 +75,13 @@ func deleteGCEStaticIP(name string) error {
// NAME REGION ADDRESS STATUS // NAME REGION ADDRESS STATUS
// test-static-ip us-central1 104.197.143.7 RESERVED // 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, name, "--project", testContext.CloudConfig.ProjectID,
"--region", "us-central1", "-q").CombinedOutput() "--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
} }