Test refactoring to support "project" flag.
This commit is contained in:
parent
cd7657e00a
commit
fd4b4ba2bc
@ -24,6 +24,7 @@ import (
|
||||
)
|
||||
|
||||
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 gceExternalIpRegexp = regexp.MustCompile(" +natIP: +([0-9.:]+)\n")
|
||||
@ -34,7 +35,12 @@ func GetGceIp(hostname string) (string, error) {
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
func GetZoneFlag() string {
|
||||
return fmt.Sprintf("--zone=%s", *zone)
|
||||
func getZoneFlag() []string {
|
||||
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
|
||||
}
|
||||
|
@ -280,7 +280,8 @@ func (self shellActions) Run(command string, args ...string) (string, string) {
|
||||
cmd = exec.Command(command, args...)
|
||||
} else {
|
||||
// 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 stderr bytes.Buffer
|
||||
@ -301,7 +302,8 @@ func (self shellActions) RunStress(command string, args ...string) (string, stri
|
||||
cmd = exec.Command(command, args...)
|
||||
} else {
|
||||
// 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 stderr bytes.Buffer
|
||||
|
@ -49,17 +49,20 @@ func RunCommand(cmd string, args ...string) error {
|
||||
func PushAndRunTests(host, testDir string) error {
|
||||
// Push binary.
|
||||
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 {
|
||||
return fmt.Errorf("failed to make remote testing directory: %v", err)
|
||||
}
|
||||
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 {
|
||||
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 {
|
||||
return fmt.Errorf("failed to copy binary: %v", err)
|
||||
}
|
||||
@ -70,13 +73,15 @@ func PushAndRunTests(host, testDir string) error {
|
||||
portStr := strconv.Itoa(*port)
|
||||
errChan := make(chan error)
|
||||
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 {
|
||||
errChan <- fmt.Errorf("error running cAdvisor: %v", err)
|
||||
}
|
||||
}()
|
||||
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 {
|
||||
glog.Errorf("Failed to cleanup: %v", err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user