diff --git a/client/client.go b/client/client.go index 1c57f36d..eba99aac 100644 --- a/client/client.go +++ b/client/client.go @@ -142,22 +142,22 @@ func (self *Client) httpGetJsonData(data, postData interface{}, url, infoName st resp, err = http.Get(url) } if err != nil { - return fmt.Errorf("unable to get %q: %v", infoName, err) + return fmt.Errorf("unable to get %q from %q: %v", infoName, url, err) } if resp == nil { - return fmt.Errorf("received empty response from %q", infoName) + return fmt.Errorf("received empty response for %q from %q", infoName, url) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { - err = fmt.Errorf("unable to read all %q: %v", infoName, err) + err = fmt.Errorf("unable to read all %q from %q: %v", infoName, url, err) return err } if resp.StatusCode != 200 { - return fmt.Errorf("request failed with error: %q", strings.TrimSpace(string(body))) + return fmt.Errorf("request %q failed with error: %q", url, strings.TrimSpace(string(body))) } if err = json.Unmarshal(body, data); err != nil { - err = fmt.Errorf("unable to unmarshal %q (Body: %q) with error: %v", infoName, string(body), err) + err = fmt.Errorf("unable to unmarshal %q (Body: %q) from %q with error: %v", infoName, string(body), url, err) return err } return nil diff --git a/client/client_test.go b/client/client_test.go index 94978720..db972d32 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -21,6 +21,7 @@ import ( "net/http/httptest" "path" "reflect" + "strings" "testing" "time" @@ -143,7 +144,7 @@ func TestRequestFails(t *testing.T) { t.Fatalf("Expected non-nil error") } expectedError := fmt.Sprintf("request failed with error: %q", errorText) - if err.Error() != expectedError { + if strings.Contains(err.Error(), expectedError) { t.Fatalf("Expected error %q but received %q", expectedError, err) } } diff --git a/integration/framework/framework.go b/integration/framework/framework.go index 0223bc55..0a6b7b1b 100644 --- a/integration/framework/framework.go +++ b/integration/framework/framework.go @@ -18,6 +18,7 @@ import ( "flag" "fmt" "os/exec" + "regexp" "strings" "testing" "time" @@ -63,10 +64,22 @@ func New(t *testing.T) Framework { 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{ host: HostInfo{ - Host: *host, - Port: *port, + Host: hostname, + Port: *port, + GceInstanceName: gceInstanceName, }, t: t, cleanups: make([]func(), 0), @@ -104,8 +117,9 @@ type realFramework struct { } type HostInfo struct { - Host string - Port int + Host string + Port int + GceInstanceName string } // Returns: http://:/ @@ -113,6 +127,21 @@ func (self HostInfo) FullHost() string { 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 { return self.t } @@ -151,7 +180,7 @@ func (self *realFramework) Client() *client.Client { func (self *realFramework) RunPause() string { return self.Run(DockerRunArgs{ Image: "kubernetes/pause", - }, "sleep", "inf") + }) } // Run the specified command in a Docker busybox container. @@ -169,40 +198,51 @@ type DockerRunArgs struct { Args []string } +// TODO(vmarmol): Use the Docker remote API. +// TODO(vmarmol): Refactor a set of "RunCommand" actions. // Runs a Docker container in the background. Uses the specified DockerRunArgs and command. // // e.g.: // RunDockerContainer(DockerRunArgs{Image: "busybox"}, "ping", "www.google.com") // -> docker run busybox ping www.google.com 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" { // 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 { self.t.Fatalf("Failed to run docker container with run args %+v due to error: %v and output: %q", args, err, out) return "" } - // The last lime is the container ID. - elements := strings.Split(string(out), "\n") - if len(elements) < 2 { - self.t.Fatalf("Failed to find Docker container ID in output %q", out) + output = string(out) + } else { + // We must SSH to the remote machine and run the command. + 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 "" } - containerId := elements[len(elements)-2] - self.cleanups = append(self.cleanups, func() { - out, err := exec.Command("docker", "rm", "-f", containerId).CombinedOutput() - if err != nil { - glog.Errorf("Failed to remove container %q with error: %v and output: %q", containerId, err, out) - } - }) - return containerId + output = string(out) } - // TODO(vmarmol): Implement. - // We must SSH to the remote machine and run the command. + // The last line is the container ID. + elements := strings.Fields(output) + containerId := elements[len(elements)-1] - self.t.Fatalf("Non-localhost Run not implemented") - return "" + self.cleanups = append(self.cleanups, func() { + 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 { + glog.Errorf("Failed to remove container %q with error: %v and output: %q", containerId, err, out) + } + }) + return containerId } // Runs retryFunc until no error is returned. After dur time the last error is returned. diff --git a/integration/runner/main.go b/integration/runner/main.go new file mode 100644 index 00000000..77102dc8 --- /dev/null +++ b/integration/runner/main.go @@ -0,0 +1,118 @@ +// Copyright 2015 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "flag" + "fmt" + "os" + "os/exec" + "path" + "time" + + "github.com/golang/glog" +) + +const cadvisorBinary = "cadvisor" + +func RunCommand(cmd string, args ...string) error { + output, err := exec.Command(cmd, args...).CombinedOutput() + if err != nil { + return fmt.Errorf("command %q %q failed with error: %v and output: %q", cmd, args, err, output) + } + + return nil +} + +func Run() error { + start := time.Now() + defer func() { + glog.Infof("Execution time %v", time.Since(start)) + }() + defer glog.Flush() + + host := flag.Args()[0] + testDir := fmt.Sprintf("/tmp/cadvisor-%d", os.Getpid()) + glog.Infof("Running integration tests in host %q", host) + + // Build cAdvisor. + glog.Infof("Building cAdvisor...") + err := RunCommand("godep", "go", "build", "github.com/google/cadvisor") + if err != nil { + return err + } + defer func() { + err := RunCommand("rm", cadvisorBinary) + if err != nil { + glog.Error(err) + } + }() + + // Ship it to the destination host. + glog.Infof("Pushing cAdvisor binary to remote host...") + err = RunCommand("gcutil", "ssh", host, "mkdir", "-p", testDir) + if err != nil { + return err + } + defer func() { + err := RunCommand("gcutil", "ssh", host, "rm", "-rf", testDir) + if err != nil { + glog.Error(err) + } + }() + err = RunCommand("gcutil", "push", host, cadvisorBinary, testDir) + if err != nil { + return err + } + + // TODO(vmarmol): Get logs in case of failures. + // Start it. + glog.Infof("Running cAdvisor on the remote host...") + err = RunCommand("gcutil", "ssh", host, "sudo", "screen", "-d", "-m", path.Join(testDir, cadvisorBinary), "--logtostderr", "&>", "/dev/null") + if err != nil { + return err + } + defer func() { + err := RunCommand("gcutil", "ssh", host, "sudo", "killall", cadvisorBinary) + if err != nil { + glog.Error(err) + } + }() + + // Run the tests. + glog.Infof("Running integration tests targeting remote host...") + err = RunCommand("godep", "go", "test", "github.com/google/cadvisor/integration/tests/...", "--host", host) + if err != nil { + return err + } + + glog.Infof("All tests pass!") + return nil +} + +func main() { + flag.Parse() + + // Check usage. + if len(flag.Args()) != 1 { + glog.Fatalf("USAGE: runner ") + } + + // Run the tests. + err := Run() + if err != nil { + glog.Fatal(err) + } +} diff --git a/integration/runner/run.sh b/integration/runner/run.sh new file mode 100755 index 00000000..1257a45c --- /dev/null +++ b/integration/runner/run.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e +set -x + +# Check usage. +if [ $# != 1 ]; then + echo "USAGE: run.sh " + exit 1 +fi + +HOST=$1 + +# Build the runner. +godep go build github.com/google/cadvisor/integration/runner + +# Run it. +./runner --logtostderr $HOST diff --git a/integration/tests/api/docker_test.go b/integration/tests/api/docker_test.go index 41fd3af7..4cd80556 100644 --- a/integration/tests/api/docker_test.go +++ b/integration/tests/api/docker_test.go @@ -198,7 +198,6 @@ func TestDockerContainerSpec(t *testing.T) { assert.True(containerInfo.Spec.HasMemory, "Memory should be isolated") assert.Equal(containerInfo.Spec.Memory.Limit, memoryLimit, "Container should have memory limit of %d, has %d", memoryLimit, containerInfo.Spec.Memory.Limit) assert.True(containerInfo.Spec.HasNetwork, "Network should be isolated") - assert.True(containerInfo.Spec.HasFilesystem, "Filesystem should be isolated") assert.True(containerInfo.Spec.HasDiskIo, "Blkio should be isolated") }