Merge pull request #24 from dlorenc/master

Add more testing of cluster.go.
pull/25/head^2
dlorenc 2016-04-26 11:05:25 -07:00
commit 0e9cf81ef5
8 changed files with 116 additions and 32 deletions

View File

@ -17,7 +17,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"strings"
"time" "time"
"github.com/docker/machine/drivers/virtualbox" "github.com/docker/machine/drivers/virtualbox"
@ -42,15 +41,12 @@ func StartHost(api libmachine.API) (*host.Host, error) {
} }
} }
// StartCluster starts as k8s cluster on the specified Host. type sshAble interface {
func StartCluster(h *host.Host) (string, error) { RunSSHCommand(string) (string, error)
host, err := h.Driver.GetURL() }
if err != nil {
return "", err
}
kubeHost := strings.Replace(host, "tcp://", "http://", -1)
kubeHost = strings.Replace(kubeHost, ":2376", ":8080", -1)
// StartCluster starts as k8s cluster on the specified Host.
func StartCluster(h sshAble) error {
for _, cmd := range []string{ for _, cmd := range []string{
// Download and install weave, if it doesn't exist. // Download and install weave, if it doesn't exist.
`if [ ! -e /usr/local/bin/weave ]; `if [ ! -e /usr/local/bin/weave ];
@ -75,11 +71,11 @@ func StartCluster(h *host.Host) (string, error) {
output, err := h.RunSSHCommand(cmd) output, err := h.RunSSHCommand(cmd)
log.Println(output) log.Println(output)
if err != nil { if err != nil {
return "", err return err
} }
} }
return kubeHost, nil return nil
} }
func createHost(api libmachine.API) (*host.Host, error) { func createHost(api libmachine.API) (*host.Host, error) {

View File

@ -1,8 +1,10 @@
package cluster package cluster
import ( import (
"fmt"
"testing" "testing"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/state" "github.com/docker/machine/libmachine/state"
"github.com/kubernetes/minikube/cli/constants" "github.com/kubernetes/minikube/cli/constants"
"github.com/kubernetes/minikube/cli/tests" "github.com/kubernetes/minikube/cli/tests"
@ -37,3 +39,73 @@ func TestCreateHost(t *testing.T) {
t.Fatalf("Wrong driver name: %v. Should be virtualbox.", h.DriverName) t.Fatalf("Wrong driver name: %v. Should be virtualbox.", h.DriverName)
} }
} }
type mockHost struct {
Commands []string
}
func (m mockHost) RunSSHCommand(cmd string) (string, error) {
m.Commands = append(m.Commands, cmd)
return "", nil
}
func TestStartCluster(t *testing.T) {
h := mockHost{}
err := StartCluster(h)
if err != nil {
t.Fatalf("Error starting cluster: %s", err)
}
}
type mockHostError struct{}
func (m mockHostError) RunSSHCommand(cmd string) (string, error) {
return "", fmt.Errorf("Error calling command: %s", cmd)
}
func TestStartClusterError(t *testing.T) {
h := mockHostError{}
err := StartCluster(h)
if err == nil {
t.Fatal("Error not thrown starting cluster.")
}
}
func TestStartHostExists(t *testing.T) {
api := &tests.MockAPI{}
// Create an initial host.
_, err := createHost(api)
if err != nil {
t.Fatalf("Error creating host: %v", err)
}
// Make sure the next call to Create will fail, to assert it doesn't get called again.
api.CreateError = true
if err := api.Create(&host.Host{}); err == nil {
t.Fatal("api.Create did not fail, but should have.")
}
// This should pass without calling Create because the host exists already.
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)
}
}
func TestStartHost(t *testing.T) {
api := &tests.MockAPI{}
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 exists, _ := api.Exists(h.Name); !exists {
t.Fatal("Machine not saved.")
}
}

View File

@ -39,6 +39,14 @@ clusters optimized for development workflows.
`, `,
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
localbinary.CurrentBinaryIsDockerMachine = true localbinary.CurrentBinaryIsDockerMachine = true
for _, path := range dirs {
if err := os.MkdirAll(path, 0777); err != nil {
log.Panicf("Error creating minikube directory: %s", err)
}
}
},
Run: func(cmd *cobra.Command, args []string) {
if os.Getenv(localbinary.PluginEnvKey) == localbinary.PluginEnvVal { if os.Getenv(localbinary.PluginEnvKey) == localbinary.PluginEnvVal {
driverName := os.Getenv(localbinary.PluginEnvDriverName) driverName := os.Getenv(localbinary.PluginEnvDriverName)
switch driverName { switch driverName {
@ -50,12 +58,6 @@ clusters optimized for development workflows.
} }
return return
} }
for _, path := range dirs {
if err := os.MkdirAll(path, 0777); err != nil {
log.Panicf("Error creating minikube directory: %s", err)
}
}
}, },
} }

View File

@ -22,10 +22,10 @@ func makeTempDir() string {
return tempDir return tempDir
} }
func runCommand() { func runCommand(f func(*cobra.Command, []string)) {
cmd := cobra.Command{} cmd := cobra.Command{}
var args []string var args []string
RootCmd.PersistentPreRun(&cmd, args) f(&cmd, args)
} }
func TestPreRunDirectories(t *testing.T) { func TestPreRunDirectories(t *testing.T) {
@ -33,7 +33,7 @@ func TestPreRunDirectories(t *testing.T) {
tempDir := makeTempDir() tempDir := makeTempDir()
defer os.RemoveAll(tempDir) defer os.RemoveAll(tempDir)
runCommand() runCommand(RootCmd.PersistentPreRun)
for _, dir := range dirs { for _, dir := range dirs {
_, err := os.Stat(dir) _, err := os.Stat(dir)
@ -46,13 +46,13 @@ func TestPreRunDirectories(t *testing.T) {
func TestPreRunNotDriver(t *testing.T) { func TestPreRunNotDriver(t *testing.T) {
tempDir := makeTempDir() tempDir := makeTempDir()
defer os.RemoveAll(tempDir) defer os.RemoveAll(tempDir)
runCommand() runCommand(RootCmd.PersistentPreRun)
if !localbinary.CurrentBinaryIsDockerMachine { if !localbinary.CurrentBinaryIsDockerMachine {
t.Fatal("CurrentBinaryIsDockerMachine not set. This will break driver initialization.") t.Fatal("CurrentBinaryIsDockerMachine not set. This will break driver initialization.")
} }
} }
func TestPreRunDriver(t *testing.T) { func TestRunDriver(t *testing.T) {
// This test is a bit complicated. It verifies that when the root command is // This test is a bit complicated. It verifies that when the root command is
// called with the proper environment variables, we setup the libmachine driver. // called with the proper environment variables, we setup the libmachine driver.
@ -70,7 +70,7 @@ func TestPreRunDriver(t *testing.T) {
}() }()
// Run the command asynchronously. It should listen on a port for connections. // Run the command asynchronously. It should listen on a port for connections.
go runCommand() go runCommand(RootCmd.Run)
// The command will write out what port it's listening on over stdout. // The command will write out what port it's listening on over stdout.
reader := bufio.NewReader(r) reader := bufio.NewReader(r)

View File

@ -17,6 +17,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"strings"
"github.com/docker/machine/libmachine" "github.com/docker/machine/libmachine"
"github.com/kubernetes/minikube/cli/cluster" "github.com/kubernetes/minikube/cli/cluster"
@ -43,11 +44,18 @@ func runStart(cmd *cobra.Command, args []string) {
log.Println("Error starting host: ", err) log.Println("Error starting host: ", err)
os.Exit(1) os.Exit(1)
} }
kubeHost, err := cluster.StartCluster(host)
if err != nil { if err := cluster.StartCluster(host); err != nil {
log.Println("Error starting cluster: ", err) log.Println("Error starting cluster: ", err)
os.Exit(1) os.Exit(1)
} }
kubeHost, err := host.Driver.GetURL()
if err != nil {
log.Println("Error connecting to cluster: ", err)
}
kubeHost = strings.Replace(kubeHost, "tcp://", "http://", -1)
kubeHost = strings.Replace(kubeHost, ":2376", ":8080", -1)
log.Printf("Kubernetes is available at %s.\n", kubeHost) log.Printf("Kubernetes is available at %s.\n", kubeHost)
log.Println("Run this command to use the cluster: ") log.Println("Run this command to use the cluster: ")
log.Printf("kubectl config set-cluster minikube --insecure-skip-tls-verify=true --server=%s\n", kubeHost) log.Printf("kubectl config set-cluster minikube --insecure-skip-tls-verify=true --server=%s\n", kubeHost)

View File

@ -5,11 +5,13 @@ import (
"path/filepath" "path/filepath"
) )
// MachineName is the name to use for the VM.
const MachineName = "minikubeVM" const MachineName = "minikubeVM"
// Fix for windows // Fix for windows
var Minipath = filepath.Join(os.Getenv("HOME"), "minikube") var Minipath = filepath.Join(os.Getenv("HOME"), ".minikube")
// MakeMiniPath is a utility to calculate a relative path to our directory.
func MakeMiniPath(fileName string) string { func MakeMiniPath(fileName string) string {
return filepath.Join(Minipath, fileName) return filepath.Join(Minipath, fileName)
} }

View File

@ -2,6 +2,7 @@ package tests
import ( import (
"encoding/json" "encoding/json"
"fmt"
"github.com/docker/machine/libmachine" "github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/auth" "github.com/docker/machine/libmachine/auth"
@ -10,6 +11,12 @@ import (
"github.com/docker/machine/libmachine/state" "github.com/docker/machine/libmachine/state"
) )
// MockAPI is a struct used to mock out libmachine.API
type MockAPI struct {
Hosts []*host.Host
CreateError bool
}
// Close closes the API. // Close closes the API.
func (api *MockAPI) Close() error { func (api *MockAPI) Close() error {
return nil return nil
@ -33,6 +40,9 @@ func (api *MockAPI) NewHost(driverName string, rawDriver []byte) (*host.Host, er
// Create creates the actual host. // Create creates the actual host.
func (api *MockAPI) Create(h *host.Host) error { func (api *MockAPI) Create(h *host.Host) error {
if api.CreateError {
return fmt.Errorf("Error creating host.")
}
return h.Driver.Create() return h.Driver.Create()
} }

View File

@ -2,16 +2,10 @@ package tests
import ( import (
"github.com/docker/machine/libmachine/drivers" "github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/mcnflag" "github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/state" "github.com/docker/machine/libmachine/state"
) )
// MockAPI is a struct used to mock out libmachine.API
type MockAPI struct {
Hosts []*host.Host
}
// MockDriver is a struct used to mock out libmachine.Driver // MockDriver is a struct used to mock out libmachine.Driver
type MockDriver struct { type MockDriver struct {
drivers.BaseDriver drivers.BaseDriver