提交 cae9bc41 编写于 作者: D dlorenc

Fix bug in start.

We have to call save after start, because the IP/port might change.
上级 46fcb928
......@@ -60,6 +60,9 @@ func StartHost(api libmachine.API, config MachineConfig) (*host.Host, error) {
if err := h.Driver.Start(); err != nil {
return nil, fmt.Errorf("Error starting stopped host: %s", err)
}
if err := api.Save(h); err != nil {
return nil, fmt.Errorf("Error saving started host: %s", err)
}
}
return h, nil
} else {
......
......@@ -32,7 +32,7 @@ import (
)
func TestCreateHost(t *testing.T) {
api := &tests.MockAPI{}
api := tests.NewMockAPI()
exists, _ := api.Exists(constants.MachineName)
if exists {
......@@ -97,7 +97,7 @@ func TestStartClusterError(t *testing.T) {
}
func TestStartHostExists(t *testing.T) {
api := &tests.MockAPI{}
api := tests.NewMockAPI()
// Create an initial host.
_, err := createHost(api, MachineConfig{})
if err != nil {
......@@ -124,7 +124,7 @@ func TestStartHostExists(t *testing.T) {
}
func TestStartStoppedHost(t *testing.T) {
api := &tests.MockAPI{}
api := tests.NewMockAPI()
// Create an initial host.
h, err := createHost(api, MachineConfig{})
if err != nil {
......@@ -145,10 +145,14 @@ func TestStartStoppedHost(t *testing.T) {
if s, _ := h.Driver.GetState(); s != state.Running {
t.Fatalf("Machine not started.")
}
if !api.SaveCalled {
t.Fatalf("Machine must be saved after starting.")
}
}
func TestStartHost(t *testing.T) {
api := &tests.MockAPI{}
api := tests.NewMockAPI()
h, err := StartHost(api, MachineConfig{})
if err != nil {
......@@ -166,14 +170,14 @@ func TestStartHost(t *testing.T) {
}
func TestStopHostError(t *testing.T) {
api := &tests.MockAPI{}
api := tests.NewMockAPI()
if err := StopHost(api); err == nil {
t.Fatal("An error should be thrown when stopping non-existing machine.")
}
}
func TestStopHost(t *testing.T) {
api := &tests.MockAPI{}
api := tests.NewMockAPI()
h, _ := createHost(api, MachineConfig{})
if err := StopHost(api); err != nil {
t.Fatal("An error should be thrown when stopping non-existing machine.")
......@@ -204,7 +208,7 @@ Error 2`
}
func TestDeleteHost(t *testing.T) {
api := &tests.MockAPI{}
api := tests.NewMockAPI()
createHost(api, MachineConfig{})
if err := DeleteHost(api); err != nil {
......@@ -213,7 +217,7 @@ func TestDeleteHost(t *testing.T) {
}
func TestDeleteHostErrorDeletingVM(t *testing.T) {
api := &tests.MockAPI{}
api := tests.NewMockAPI()
h, _ := createHost(api, MachineConfig{})
d := &tests.MockDriver{RemoveError: true}
......@@ -226,7 +230,8 @@ func TestDeleteHostErrorDeletingVM(t *testing.T) {
}
func TestDeleteHostErrorDeletingFiles(t *testing.T) {
api := &tests.MockAPI{RemoveError: true}
api := tests.NewMockAPI()
api.RemoveError = true
createHost(api, MachineConfig{})
if err := DeleteHost(api); err == nil {
......@@ -235,9 +240,8 @@ func TestDeleteHostErrorDeletingFiles(t *testing.T) {
}
func TestDeleteHostMultipleErrors(t *testing.T) {
api := &tests.MockAPI{
RemoveError: true,
}
api := tests.NewMockAPI()
api.RemoveError = true
h, _ := createHost(api, MachineConfig{})
d := &tests.MockDriver{RemoveError: true}
......@@ -259,7 +263,7 @@ func TestDeleteHostMultipleErrors(t *testing.T) {
}
func TestGetHostStatus(t *testing.T) {
api := &tests.MockAPI{}
api := tests.NewMockAPI()
checkState := func(expected string) {
s, err := GetHostStatus(api)
......
......@@ -29,9 +29,17 @@ import (
// MockAPI is a struct used to mock out libmachine.API
type MockAPI struct {
Hosts []*host.Host
Hosts map[string]*host.Host
CreateError bool
RemoveError bool
SaveCalled bool
}
func NewMockAPI() *MockAPI {
m := MockAPI{
Hosts: make(map[string]*host.Host),
}
return &m
}
// Close closes the API.
......@@ -65,13 +73,8 @@ func (api *MockAPI) Create(h *host.Host) error {
// Exists determines if the host already exists.
func (api *MockAPI) Exists(name string) (bool, error) {
for _, host := range api.Hosts {
if name == host.Name {
return true, nil
}
}
return false, nil
_, ok := api.Hosts[name]
return ok, nil
}
// List the existing hosts.
......@@ -81,15 +84,14 @@ func (api *MockAPI) List() ([]string, error) {
// Load loads a host from disk.
func (api *MockAPI) Load(name string) (*host.Host, error) {
for _, host := range api.Hosts {
if name == host.Name {
return host, nil
h, ok := api.Hosts[name]
if !ok {
return nil, mcnerror.ErrHostDoesNotExist{
Name: name,
}
}
return nil, mcnerror.ErrHostDoesNotExist{
Name: name,
}
return h, nil
}
// Remove a host.
......@@ -98,22 +100,14 @@ func (api *MockAPI) Remove(name string) error {
return fmt.Errorf("Error removing %s", name)
}
newHosts := []*host.Host{}
for _, host := range api.Hosts {
if name != host.Name {
newHosts = append(newHosts, host)
}
}
api.Hosts = newHosts
delete(api.Hosts, name)
return nil
}
// Save saves a host to disk.
func (api *MockAPI) Save(host *host.Host) error {
api.Hosts = append(api.Hosts, host)
api.Hosts[host.Name] = host
api.SaveCalled = true
return nil
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册