Test refactoring to support "project" flag.

This commit is contained in:
Phillip Wittrock 2015-11-19 11:13:15 -08:00
parent cd7657e00a
commit fd4b4ba2bc
3 changed files with 41 additions and 10 deletions

View File

@ -24,6 +24,7 @@ import (
) )
var zone = flag.String("zone", "us-central1-f", "Zone the instances are running in") var zone = flag.String("zone", "us-central1-f", "Zone the instances are running in")
var project = flag.String("project", "", "Project the instances are running in")
var gceInternalIpRegexp = regexp.MustCompile(" +networkIP: +([0-9.:]+)\n") var gceInternalIpRegexp = regexp.MustCompile(" +networkIP: +([0-9.:]+)\n")
var gceExternalIpRegexp = regexp.MustCompile(" +natIP: +([0-9.:]+)\n") var gceExternalIpRegexp = regexp.MustCompile(" +natIP: +([0-9.:]+)\n")
@ -34,7 +35,12 @@ func GetGceIp(hostname string) (string, error) {
return "127.0.0.1", nil return "127.0.0.1", nil
} }
out, err := exec.Command("gcloud", "compute", "instances", "describe", GetZoneFlag(), hostname).CombinedOutput() args := []string{"compute"}
args = append(args, getProjectFlag()...)
args = append(args, "instances", "describe")
args = append(args, getZoneFlag()...)
args = append(args, hostname)
out, err := exec.Command("gcloud", args...).CombinedOutput()
if err != nil { if err != nil {
return "", fmt.Errorf("failed to get instance information for %q with error %v and output %s", hostname, err, string(out)) return "", fmt.Errorf("failed to get instance information for %q with error %v and output %s", hostname, err, string(out))
} }
@ -52,6 +58,24 @@ func GetGceIp(hostname string) (string, error) {
return matches[1], nil return matches[1], nil
} }
func GetZoneFlag() string { func getZoneFlag() []string {
return fmt.Sprintf("--zone=%s", *zone) if *zone == "" {
return []string{}
}
return []string{"--zone", *zone}
}
func getProjectFlag() []string {
if *project == "" {
return []string{}
}
return []string{"--project", *project}
}
func GetGCComputeArgs(cmd string, cmdArgs ...string) []string {
args := []string{"compute"}
args = append(args, getProjectFlag()...)
args = append(args, cmd)
args = append(args, getZoneFlag()...)
args = append(args, cmdArgs...)
return args
} }

View File

@ -280,7 +280,8 @@ func (self shellActions) Run(command string, args ...string) (string, string) {
cmd = exec.Command(command, args...) cmd = exec.Command(command, args...)
} else { } else {
// We must SSH to the remote machine and run the command. // We must SSH to the remote machine and run the command.
cmd = exec.Command("gcloud", append([]string{"compute", "ssh", common.GetZoneFlag(), self.fm.Hostname().GceInstanceName, "--", command}, args...)...) args = append(common.GetGCComputeArgs("ssh", self.fm.Hostname().GceInstanceName, "--", command), args...)
cmd = exec.Command("gcloud", args...)
} }
var stdout bytes.Buffer var stdout bytes.Buffer
var stderr bytes.Buffer var stderr bytes.Buffer
@ -301,7 +302,8 @@ func (self shellActions) RunStress(command string, args ...string) (string, stri
cmd = exec.Command(command, args...) cmd = exec.Command(command, args...)
} else { } else {
// We must SSH to the remote machine and run the command. // We must SSH to the remote machine and run the command.
cmd = exec.Command("gcloud", append([]string{"compute", "ssh", common.GetZoneFlag(), self.fm.Hostname().GceInstanceName, "--", command}, args...)...) args = append(common.GetGCComputeArgs("ssh", self.fm.Hostname().GceInstanceName, "--", command), args...)
cmd = exec.Command("gcloud", args...)
} }
var stdout bytes.Buffer var stdout bytes.Buffer
var stderr bytes.Buffer var stderr bytes.Buffer

View File

@ -49,17 +49,20 @@ func RunCommand(cmd string, args ...string) error {
func PushAndRunTests(host, testDir string) error { func PushAndRunTests(host, testDir string) error {
// Push binary. // Push binary.
glog.Infof("Pushing cAdvisor binary to %q...", host) glog.Infof("Pushing cAdvisor binary to %q...", host)
err := RunCommand("gcloud", "compute", "ssh", common.GetZoneFlag(), host, "--", "mkdir", "-p", testDir) args := common.GetGCComputeArgs("ssh", host, "--", "mkdir", "-p", testDir)
err := RunCommand("gcloud", args...)
if err != nil { if err != nil {
return fmt.Errorf("failed to make remote testing directory: %v", err) return fmt.Errorf("failed to make remote testing directory: %v", err)
} }
defer func() { defer func() {
err := RunCommand("gcloud", "compute", "ssh", common.GetZoneFlag(), host, "--", "rm", "-rf", testDir) args := common.GetGCComputeArgs("ssh", host, "--", "rm", "-rf", testDir)
err := RunCommand("gcloud", args...)
if err != nil { if err != nil {
glog.Errorf("Failed to cleanup test directory: %v", err) glog.Errorf("Failed to cleanup test directory: %v", err)
} }
}() }()
err = RunCommand("gcloud", "compute", "copy-files", common.GetZoneFlag(), cadvisorBinary, fmt.Sprintf("%s:%s", host, testDir)) args = common.GetGCComputeArgs("copy-files", cadvisorBinary, fmt.Sprintf("%s:%s", host, testDir))
err = RunCommand("gcloud", args...)
if err != nil { if err != nil {
return fmt.Errorf("failed to copy binary: %v", err) return fmt.Errorf("failed to copy binary: %v", err)
} }
@ -70,13 +73,15 @@ func PushAndRunTests(host, testDir string) error {
portStr := strconv.Itoa(*port) portStr := strconv.Itoa(*port)
errChan := make(chan error) errChan := make(chan error)
go func() { go func() {
err = RunCommand("gcloud", "compute", "ssh", common.GetZoneFlag(), host, "--command", fmt.Sprintf("sudo %s --port %s --logtostderr", path.Join(testDir, cadvisorBinary), portStr)) args = common.GetGCComputeArgs("ssh", host, "--", fmt.Sprintf("sudo %s --port %s --logtostderr", path.Join(testDir, cadvisorBinary), portStr))
err = RunCommand("gcloud", args...)
if err != nil { if err != nil {
errChan <- fmt.Errorf("error running cAdvisor: %v", err) errChan <- fmt.Errorf("error running cAdvisor: %v", err)
} }
}() }()
defer func() { defer func() {
err := RunCommand("gcloud", "compute", "ssh", common.GetZoneFlag(), host, "--", "sudo", "pkill", cadvisorBinary) args = common.GetGCComputeArgs("ssh", host, "--", "sudo", "pkill", cadvisorBinary)
err := RunCommand("gcloud", args...)
if err != nil { if err != nil {
glog.Errorf("Failed to cleanup: %v", err) glog.Errorf("Failed to cleanup: %v", err)
} }