Implement remote command execution in the test framework.
Specifically, handles remote execution in GCE hosts.
This commit is contained in:
parent
eb6f9a9982
commit
4347ccb7fd
@ -18,6 +18,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -63,10 +64,22 @@ func New(t *testing.T) Framework {
|
|||||||
t.Skip("Skipping framework test in short mode")
|
t.Skip("Skipping framework test in short mode")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try to see if non-localhost hosts are GCE instances.
|
||||||
|
var gceInstanceName string
|
||||||
|
hostname := *host
|
||||||
|
if hostname != "localhost" {
|
||||||
|
gceInstanceName = hostname
|
||||||
|
gceIp, err := getGceIp(hostname)
|
||||||
|
if err == nil {
|
||||||
|
hostname = gceIp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &realFramework{
|
return &realFramework{
|
||||||
host: HostInfo{
|
host: HostInfo{
|
||||||
Host: *host,
|
Host: hostname,
|
||||||
Port: *port,
|
Port: *port,
|
||||||
|
GceInstanceName: gceInstanceName,
|
||||||
},
|
},
|
||||||
t: t,
|
t: t,
|
||||||
cleanups: make([]func(), 0),
|
cleanups: make([]func(), 0),
|
||||||
@ -106,6 +119,7 @@ type realFramework struct {
|
|||||||
type HostInfo struct {
|
type HostInfo struct {
|
||||||
Host string
|
Host string
|
||||||
Port int
|
Port int
|
||||||
|
GceInstanceName string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns: http://<host>:<port>/
|
// Returns: http://<host>:<port>/
|
||||||
@ -113,6 +127,21 @@ func (self HostInfo) FullHost() string {
|
|||||||
return fmt.Sprintf("http://%s:%d/", self.Host, self.Port)
|
return fmt.Sprintf("http://%s:%d/", self.Host, self.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var gceIpRegexp = regexp.MustCompile("external-ip +\\| +([0-9.:]+) +")
|
||||||
|
|
||||||
|
func getGceIp(hostname string) (string, error) {
|
||||||
|
out, err := exec.Command("gcutil", "getinstance", hostname).CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
matches := gceIpRegexp.FindStringSubmatch(string(out))
|
||||||
|
if len(matches) == 0 {
|
||||||
|
return "", fmt.Errorf("failed to find IP from output %q", string(out))
|
||||||
|
}
|
||||||
|
return matches[1], nil
|
||||||
|
}
|
||||||
|
|
||||||
func (self *realFramework) T() *testing.T {
|
func (self *realFramework) T() *testing.T {
|
||||||
return self.t
|
return self.t
|
||||||
}
|
}
|
||||||
@ -151,7 +180,7 @@ func (self *realFramework) Client() *client.Client {
|
|||||||
func (self *realFramework) RunPause() string {
|
func (self *realFramework) RunPause() string {
|
||||||
return self.Run(DockerRunArgs{
|
return self.Run(DockerRunArgs{
|
||||||
Image: "kubernetes/pause",
|
Image: "kubernetes/pause",
|
||||||
}, "sleep", "inf")
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the specified command in a Docker busybox container.
|
// Run the specified command in a Docker busybox container.
|
||||||
@ -169,40 +198,50 @@ type DockerRunArgs struct {
|
|||||||
Args []string
|
Args []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(vmarmol): Refactor a set of "RunCommand" actions.
|
||||||
// Runs a Docker container in the background. Uses the specified DockerRunArgs and command.
|
// Runs a Docker container in the background. Uses the specified DockerRunArgs and command.
|
||||||
//
|
//
|
||||||
// e.g.:
|
// e.g.:
|
||||||
// RunDockerContainer(DockerRunArgs{Image: "busybox"}, "ping", "www.google.com")
|
// RunDockerContainer(DockerRunArgs{Image: "busybox"}, "ping", "www.google.com")
|
||||||
// -> docker run busybox ping www.google.com
|
// -> docker run busybox ping www.google.com
|
||||||
func (self *realFramework) Run(args DockerRunArgs, cmd ...string) string {
|
func (self *realFramework) Run(args DockerRunArgs, cmd ...string) string {
|
||||||
|
dockerCommand := append(append(append([]string{"docker", "run", "-d"}, args.Args...), args.Image), cmd...)
|
||||||
|
|
||||||
|
var output string
|
||||||
if self.host.Host == "localhost" {
|
if self.host.Host == "localhost" {
|
||||||
// Just run locally.
|
// Just run locally.
|
||||||
out, err := exec.Command("docker", append(append(append([]string{"run", "-d"}, args.Args...), args.Image), cmd...)...).CombinedOutput()
|
out, err := exec.Command("sudo", dockerCommand...).Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
self.t.Fatalf("Failed to run docker container with run args %+v due to error: %v and output: %q", args, err, out)
|
self.t.Fatalf("Failed to run docker container with run args %+v due to error: %v and output: %q", args, err, out)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
// The last lime is the container ID.
|
output = string(out)
|
||||||
elements := strings.Split(string(out), "\n")
|
} else {
|
||||||
if len(elements) < 2 {
|
// We must SSH to the remote machine and run the command.
|
||||||
self.t.Fatalf("Failed to find Docker container ID in output %q", out)
|
out, err := exec.Command("gcutil", append([]string{"ssh", self.host.GceInstanceName, "sudo"}, dockerCommand...)...).Output()
|
||||||
|
if err != nil {
|
||||||
|
self.t.Fatalf("Failed to run docker container remotely in %q with run args %+v due to error: %v and output: %q", self.host.Host, args, err, out)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
containerId := elements[len(elements)-2]
|
output = string(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The last lime is the container ID.
|
||||||
|
elements := strings.Fields(output)
|
||||||
|
containerId := elements[len(elements)-1]
|
||||||
|
|
||||||
self.cleanups = append(self.cleanups, func() {
|
self.cleanups = append(self.cleanups, func() {
|
||||||
out, err := exec.Command("docker", "rm", "-f", containerId).CombinedOutput()
|
cleanupCommand := []string{"sudo", "docker", "rm", "-f", containerId}
|
||||||
|
if self.host.Host != "localhost" {
|
||||||
|
cleanupCommand = append([]string{"gcutil", "ssh", self.host.GceInstanceName}, cleanupCommand...)
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := exec.Command(cleanupCommand[0], cleanupCommand[1:]...).CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Failed to remove container %q with error: %v and output: %q", containerId, err, out)
|
glog.Errorf("Failed to remove container %q with error: %v and output: %q", containerId, err, out)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return containerId
|
return containerId
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(vmarmol): Implement.
|
|
||||||
// We must SSH to the remote machine and run the command.
|
|
||||||
|
|
||||||
self.t.Fatalf("Non-localhost Run not implemented")
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Runs retryFunc until no error is returned. After dur time the last error is returned.
|
// Runs retryFunc until no error is returned. After dur time the last error is returned.
|
||||||
|
Loading…
Reference in New Issue
Block a user