Skip to content

Commit

Permalink
Don't use screen to run cAdvisor in the background.
Browse files Browse the repository at this point in the history
Some distros don't have screen. Here we wait for cAdvisor to come up
before continuing.
  • Loading branch information
vmarmol committed Feb 17, 2015
1 parent 31769a8 commit e42ca57
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 31 deletions.
50 changes: 50 additions & 0 deletions integration/common/gce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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 common

import (
"fmt"
"os/exec"
"regexp"

"github.com/GoogleCloudPlatform/gcloud-golang/compute/metadata"
)

var gceInternalIpRegexp = regexp.MustCompile(" +ip +\\| +([0-9.:]+) +")
var gceExternalIpRegexp = regexp.MustCompile("external-ip +\\| +([0-9.:]+) +")

// Gets the IP of the specified GCE instance.
func GetGceIp(hostname string) (string, error) {
if hostname == "localhost" {
return "127.0.0.1", nil
}

out, err := exec.Command("gcutil", "getinstance", hostname).CombinedOutput()
if err != nil {
return "", err
}

// Use the internal IP within GCE and the external one outside.
var matches []string
if metadata.OnGCE() {
matches = gceInternalIpRegexp.FindStringSubmatch(string(out))
} else {
matches = gceExternalIpRegexp.FindStringSubmatch(string(out))
}
if len(matches) == 0 {
return "", fmt.Errorf("failed to find IP from output %q", string(out))
}
return matches[1], nil
}
27 changes: 2 additions & 25 deletions integration/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ import (
"flag"
"fmt"
"os/exec"
"regexp"
"strings"
"testing"
"time"

"github.com/GoogleCloudPlatform/gcloud-golang/compute/metadata"
"github.com/golang/glog"
"github.com/google/cadvisor/client"
"github.com/google/cadvisor/integration/common"
)

var host = flag.String("host", "localhost", "Address of the host being tested")
Expand Down Expand Up @@ -70,7 +69,7 @@ func New(t *testing.T) Framework {
hostname := *host
if hostname != "localhost" {
gceInstanceName = hostname
gceIp, err := getGceIp(hostname)
gceIp, err := common.GetGceIp(hostname)
if err == nil {
hostname = gceIp
}
Expand Down Expand Up @@ -128,28 +127,6 @@ func (self HostInfo) FullHost() string {
return fmt.Sprintf("http://%s:%d/", self.Host, self.Port)
}

var gceInternalIpRegexp = regexp.MustCompile(" +ip +\\| +([0-9.:]+) +")
var gceExternalIpRegexp = 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
}

// Use the internal IP within GCE and the external one outside.
var matches []string
if metadata.OnGCE() {
matches = gceInternalIpRegexp.FindStringSubmatch(string(out))
} else {
matches = gceExternalIpRegexp.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
}
Expand Down
48 changes: 42 additions & 6 deletions integration/runner/main.go → integration/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@ package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/exec"
"path"
"strconv"
"time"

"github.com/golang/glog"
"github.com/google/cadvisor/integration/common"
)

const cadvisorBinary = "cadvisor"

var cadvisorTimeout = flag.Duration("cadvisor_timeout", 15*time.Second, "Time to wait for cAdvisor to come up on the remote host")
var port = flag.Int("port", 8080, "Port in which to start cAdvisor in the remote host")

func RunCommand(cmd string, args ...string) error {
output, err := exec.Command(cmd, args...).CombinedOutput()
if err != nil {
Expand Down Expand Up @@ -78,22 +84,52 @@ func Run() error {
}

// TODO(vmarmol): Get logs in case of failures.
// Start it.
// Start cAdvisor.
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
}
portStr := strconv.Itoa(*port)
errChan := make(chan error)
go func() {
err = RunCommand("gcutil", "ssh", host, "sudo", path.Join(testDir, cadvisorBinary), "--port", portStr, "--logtostderr", "&>", "/dev/null")
if err != nil {
errChan <- err
}
}()
defer func() {
err := RunCommand("gcutil", "ssh", host, "sudo", "killall", cadvisorBinary)
if err != nil {
glog.Error(err)
}
}()

ipAddress, err := common.GetGceIp(host)
if err != nil {
return err
}

// Wait for cAdvisor to come up.
endTime := time.Now().Add(*cadvisorTimeout)
done := false
for endTime.After(time.Now()) && !done {
select {
case err := <-errChan:
// Quit early if there was an error.
return err
case <-time.After(500 * time.Millisecond):
// Stop waiting when cAdvisor is healthy..
resp, err := http.Get(fmt.Sprintf("http://%s:%s/healthz", ipAddress, portStr))
if err == nil && resp.StatusCode == http.StatusOK {
done = true
break
}
}
}
if !done {
return fmt.Errorf("timed out waiting for cAdvisor to come up at host %q", host)
}

// Run the tests.
glog.Infof("Running integration tests targeting remote host...")
err = RunCommand("godep", "go", "test", "github.com/google/cadvisor/integration/tests/...", "--host", host)
err = RunCommand("godep", "go", "test", "github.com/google/cadvisor/integration/tests/...", "--host", host, "--port", portStr)
if err != nil {
return err
}
Expand Down

0 comments on commit e42ca57

Please sign in to comment.