From 21bdea3dcce1d2a33e729cdf7c143e82ccf36846 Mon Sep 17 00:00:00 2001 From: Victor Marmol Date: Thu, 22 Jan 2015 15:01:09 -0800 Subject: [PATCH] Add integration test runner. Runner builds a cAdvisor, prepares it on a remote machine, and executes the integration tests targetting the remote machines. Script builds the runner and runs it. --- client/client_test.go | 3 +- integration/framework/framework.go | 3 +- integration/runner/main.go | 118 +++++++++++++++++++++++++++++ integration/runner/run.sh | 32 ++++++++ 4 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 integration/runner/main.go create mode 100755 integration/runner/run.sh 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 2137d943..0a6b7b1b 100644 --- a/integration/framework/framework.go +++ b/integration/framework/framework.go @@ -198,6 +198,7 @@ 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. // @@ -226,7 +227,7 @@ func (self *realFramework) Run(args DockerRunArgs, cmd ...string) string { output = string(out) } - // The last lime is the container ID. + // The last line is the container ID. elements := strings.Fields(output) containerId := elements[len(elements)-1] 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