Merge pull request #232 from nathanleclaire/cmdtest

Write test for getHostState
This commit is contained in:
Evan Hazlett 2015-01-07 11:46:07 -08:00
commit 44e66b50ac
2 changed files with 148 additions and 30 deletions

View File

@ -44,36 +44,6 @@ func (h hostListItemByName) Less(i, j int) bool {
return strings.ToLower(h[i].Name) < strings.ToLower(h[j].Name) return strings.ToLower(h[i].Name) < strings.ToLower(h[j].Name)
} }
func getHostState(host Host, store Store, hostListItems chan<- hostListItem) {
currentState, err := host.Driver.GetState()
if err != nil {
log.Errorf("error getting state for host %s: %s", host.Name, err)
}
url, err := host.GetURL()
if err != nil {
if err == drivers.ErrHostIsNotRunning {
url = ""
} else {
log.Errorf("error getting URL for host %s: %s", host.Name, err)
}
}
isActive, err := store.IsActive(&host)
if err != nil {
log.Errorf("error determining whether host %q is active: %s",
host.Name, err)
}
hostListItems <- hostListItem{
Name: host.Name,
Active: isActive,
DriverName: host.Driver.DriverName(),
State: currentState,
URL: url,
}
}
var Commands = []cli.Command{ var Commands = []cli.Command{
{ {
Name: "active", Name: "active",
@ -389,3 +359,33 @@ func getHost(c *cli.Context) *Host {
} }
return host return host
} }
func getHostState(host Host, store Store, hostListItems chan<- hostListItem) {
currentState, err := host.Driver.GetState()
if err != nil {
log.Errorf("error getting state for host %s: %s", host.Name, err)
}
url, err := host.GetURL()
if err != nil {
if err == drivers.ErrHostIsNotRunning {
url = ""
} else {
log.Errorf("error getting URL for host %s: %s", host.Name, err)
}
}
isActive, err := store.IsActive(&host)
if err != nil {
log.Errorf("error determining whether host %q is active: %s",
host.Name, err)
}
hostListItems <- hostListItem{
Name: host.Name,
Active: isActive,
DriverName: host.Driver.DriverName(),
State: currentState,
URL: url,
}
}

118
commands_test.go Normal file
View File

@ -0,0 +1,118 @@
package main
import (
"io/ioutil"
"os/exec"
"testing"
drivers "github.com/docker/machine/drivers"
"github.com/docker/machine/state"
)
type FakeDriver struct {
MockState state.State
}
func (d *FakeDriver) DriverName() string {
return "fakedriver"
}
func (d *FakeDriver) SetConfigFromFlags(flags drivers.DriverOptions) error {
return nil
}
func (d *FakeDriver) GetURL() (string, error) {
return "", nil
}
func (d *FakeDriver) GetIP() (string, error) {
return "", nil
}
func (d *FakeDriver) GetState() (state.State, error) {
return d.MockState, nil
}
func (d *FakeDriver) Create() error {
return nil
}
func (d *FakeDriver) Remove() error {
return nil
}
func (d *FakeDriver) Start() error {
return nil
}
func (d *FakeDriver) Stop() error {
return nil
}
func (d *FakeDriver) Restart() error {
return nil
}
func (d *FakeDriver) Kill() error {
return nil
}
func (d *FakeDriver) Upgrade() error {
return nil
}
func (d *FakeDriver) GetSSHCommand(args ...string) (*exec.Cmd, error) {
return &exec.Cmd{}, nil
}
func TestGetHostState(t *testing.T) {
storePath, err := ioutil.TempDir("", ".docker")
if err != nil {
t.Fatal("Error creating tmp dir:", err)
}
hostListItems := make(chan hostListItem)
store := NewStore(storePath)
hosts := []Host{
{
Name: "foo",
DriverName: "fakedriver",
Driver: &FakeDriver{
MockState: state.Running,
},
storePath: storePath,
},
{
Name: "bar",
DriverName: "fakedriver",
Driver: &FakeDriver{
MockState: state.Stopped,
},
storePath: storePath,
},
{
Name: "baz",
DriverName: "fakedriver",
Driver: &FakeDriver{
MockState: state.Running,
},
storePath: storePath,
},
}
expected := map[string]state.State{
"foo": state.Running,
"bar": state.Stopped,
"baz": state.Running,
}
items := []hostListItem{}
for _, host := range hosts {
go getHostState(host, *store, hostListItems)
}
for i := 0; i < len(hosts); i++ {
items = append(items, <-hostListItems)
}
for _, item := range items {
if expected[item.Name] != item.State {
t.Fatal("Expected state did not match for item", item)
}
}
}