Add stop command and tests. (#25)

Fix bug in start command where stopped hosts weren't started.
pull/26/head^2
dlorenc 2016-04-26 11:33:02 -07:00
parent 0e9cf81ef5
commit 969b6b0d7c
4 changed files with 165 additions and 3 deletions

View File

@ -22,6 +22,7 @@ import (
"github.com/docker/machine/drivers/virtualbox"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/state"
"github.com/kubernetes/minikube/cli/constants"
)
@ -33,7 +34,16 @@ func StartHost(api libmachine.API) (*host.Host, error) {
log.Println("Machine exists!")
h, err := api.Load(constants.MachineName)
if err != nil {
return nil, fmt.Errorf("Error loading existing host.")
return nil, fmt.Errorf("Error loading existing host: %s", err)
}
s, err := h.Driver.GetState()
if err != nil {
return nil, fmt.Errorf("Error getting state for host: %s", err)
}
if s != state.Running {
if err := h.Driver.Start(); err != nil {
return nil, fmt.Errorf("Error starting stopped host: %s", err)
}
}
return h, nil
} else {
@ -41,6 +51,18 @@ func StartHost(api libmachine.API) (*host.Host, error) {
}
}
// StopHost stops the host VM.
func StopHost(api libmachine.API) error {
host, err := api.Load(constants.MachineName)
if err != nil {
return err
}
if err := host.Stop(); err != nil {
return err
}
return nil
}
type sshAble interface {
RunSSHCommand(string) (string, error)
}
@ -49,8 +71,7 @@ type sshAble interface {
func StartCluster(h sshAble) error {
for _, cmd := range []string{
// Download and install weave, if it doesn't exist.
`if [ ! -e /usr/local/bin/weave ];
then
`if [ ! -e /usr/local/bin/weave ]; then
sudo curl -L git.io/weave -o /usr/local/bin/weave
sudo chmod a+x /usr/local/bin/weave;
fi`,

View File

@ -93,6 +93,33 @@ func TestStartHostExists(t *testing.T) {
if h.Name != constants.MachineName {
t.Fatalf("Machine created with incorrect name: %s", h.Name)
}
if s, _ := h.Driver.GetState(); s != state.Running {
t.Fatalf("Machine not started.")
}
}
func TestStartStoppedHost(t *testing.T) {
api := &tests.MockAPI{}
// Create an initial host.
h, err := createHost(api)
if err != nil {
t.Fatalf("Error creating host: %v", err)
}
d := tests.MockDriver{}
h.Driver = &d
d.CurrentState = state.Stopped
h, err = StartHost(api)
if err != nil {
t.Fatal("Error starting host.")
}
if h.Name != constants.MachineName {
t.Fatalf("Machine created with incorrect name: %s", h.Name)
}
if s, _ := h.Driver.GetState(); s != state.Running {
t.Fatalf("Machine not started.")
}
}
func TestStartHost(t *testing.T) {
@ -108,4 +135,25 @@ func TestStartHost(t *testing.T) {
if exists, _ := api.Exists(h.Name); !exists {
t.Fatal("Machine not saved.")
}
if s, _ := h.Driver.GetState(); s != state.Running {
t.Fatalf("Machine not started.")
}
}
func TestStopHostError(t *testing.T) {
api := &tests.MockAPI{}
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{}
h, _ := createHost(api)
if err := StopHost(api); err != nil {
t.Fatal("An error should be thrown when stopping non-existing machine.")
}
if s, _ := h.Driver.GetState(); s != state.Stopped {
t.Fatalf("Machine not stopped. Currently in state: %s", s)
}
}

62
cli/cmd/stop.go Normal file
View File

@ -0,0 +1,62 @@
/*
Copyright 2015 The Kubernetes Authors 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 cmd
import (
"fmt"
"os"
"github.com/docker/machine/libmachine"
"github.com/kubernetes/minikube/cli/cluster"
"github.com/kubernetes/minikube/cli/constants"
"github.com/spf13/cobra"
)
// stopCmd represents the stop command
var stopCmd = &cobra.Command{
Use: "stop",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Stopping local Kubernetes cluster...")
api := libmachine.NewClient(constants.Minipath, constants.MakeMiniPath("certs"))
defer api.Close()
if err := cluster.StopHost(api); err != nil {
fmt.Println("Error stopping machine: %s", err)
os.Exit(1)
}
fmt.Println("Machine stopped.")
},
}
func init() {
RootCmd.AddCommand(stopCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// stopCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// stopCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

31
coverage.out Normal file
View File

@ -0,0 +1,31 @@
mode: set
github.com/kubernetes/minikube/cli/cluster/cluster.go:30.56,31.66 1 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:31.66,33.3 1 0
github.com/kubernetes/minikube/cli/cluster/cluster.go:33.3,33.19 1 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:33.19,36.17 3 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:39.3,40.17 2 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:43.3,43.25 1 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:48.3,48.16 1 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:36.17,38.4 1 0
github.com/kubernetes/minikube/cli/cluster/cluster.go:40.17,42.4 1 0
github.com/kubernetes/minikube/cli/cluster/cluster.go:43.25,44.43 1 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:44.43,46.5 1 0
github.com/kubernetes/minikube/cli/cluster/cluster.go:49.3,51.3 1 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:55.41,57.16 2 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:60.2,60.36 1 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:63.2,63.12 1 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:57.16,59.3 1 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:60.36,62.3 1 0
github.com/kubernetes/minikube/cli/cluster/cluster.go:71.36,91.140 1 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:99.2,99.12 1 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:91.140,94.17 3 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:94.17,96.4 1 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:102.57,106.16 4 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:110.2,112.16 3 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:116.2,119.38 3 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:125.2,125.36 1 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:128.2,128.15 1 1
github.com/kubernetes/minikube/cli/cluster/cluster.go:106.16,108.3 1 0
github.com/kubernetes/minikube/cli/cluster/cluster.go:112.16,114.3 1 0
github.com/kubernetes/minikube/cli/cluster/cluster.go:119.38,123.3 2 0
github.com/kubernetes/minikube/cli/cluster/cluster.go:125.36,127.3 1 0