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.
This commit is contained in:
Victor Marmol 2015-01-22 15:01:09 -08:00
parent 5d579b43c9
commit 21bdea3dcc
4 changed files with 154 additions and 2 deletions

View File

@ -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)
}
}

View File

@ -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]

118
integration/runner/main.go Normal file
View File

@ -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 <host to test>")
}
// Run the tests.
err := Run()
if err != nil {
glog.Fatal(err)
}
}

32
integration/runner/run.sh Executable file
View File

@ -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 <host to run tests on>"
exit 1
fi
HOST=$1
# Build the runner.
godep go build github.com/google/cadvisor/integration/runner
# Run it.
./runner --logtostderr $HOST