Merge pull request #23 from dlorenc/master

Exit with a non-zero code if we error.
pull/24/head
dlorenc 2016-04-25 15:57:14 -07:00
commit 482cc7626a
3 changed files with 112 additions and 6 deletions

View File

@ -18,10 +18,18 @@ import (
"log"
"os"
"github.com/docker/machine/drivers/virtualbox"
"github.com/docker/machine/libmachine/drivers/plugin"
"github.com/docker/machine/libmachine/drivers/plugin/localbinary"
"github.com/kubernetes/minikube/cli/constants"
"github.com/spf13/cobra"
)
var dirs = [...]string{
constants.Minipath,
constants.MakeMiniPath("certs"),
constants.MakeMiniPath("machines")}
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "cli",
@ -30,10 +38,18 @@ var RootCmd = &cobra.Command{
clusters optimized for development workflows.
`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
dirs := [...]string{
constants.Minipath,
constants.MakeMiniPath("certs"),
constants.MakeMiniPath("machines")}
localbinary.CurrentBinaryIsDockerMachine = true
if os.Getenv(localbinary.PluginEnvKey) == localbinary.PluginEnvVal {
driverName := os.Getenv(localbinary.PluginEnvDriverName)
switch driverName {
case "virtualbox":
plugin.RegisterDriver(virtualbox.NewDriver("", ""))
default:
fmt.Fprintf(os.Stderr, "Unsupported driver: %s\n", driverName)
os.Exit(1)
}
return
}
for _, path := range dirs {
if err := os.MkdirAll(path, 0777); err != nil {

87
cli/cmd/root_test.go Normal file
View File

@ -0,0 +1,87 @@
package cmd
import (
"bufio"
"io/ioutil"
"log"
"net"
"os"
"testing"
"github.com/docker/machine/libmachine/drivers/plugin/localbinary"
"github.com/kubernetes/minikube/cli/constants"
"github.com/spf13/cobra"
)
func makeTempDir() string {
tempDir, err := ioutil.TempDir("", "minipath")
if err != nil {
log.Fatal(err)
}
constants.Minipath = tempDir
return tempDir
}
func runCommand() {
cmd := cobra.Command{}
var args []string
RootCmd.PersistentPreRun(&cmd, args)
}
func TestPreRunDirectories(t *testing.T) {
// Make sure we create the required directories.
tempDir := makeTempDir()
defer os.RemoveAll(tempDir)
runCommand()
for _, dir := range dirs {
_, err := os.Stat(dir)
if os.IsNotExist(err) {
t.Fatalf("Directory %s does not exist.", dir)
}
}
}
func TestPreRunNotDriver(t *testing.T) {
tempDir := makeTempDir()
defer os.RemoveAll(tempDir)
runCommand()
if !localbinary.CurrentBinaryIsDockerMachine {
t.Fatal("CurrentBinaryIsDockerMachine not set. This will break driver initialization.")
}
}
func TestPreRunDriver(t *testing.T) {
// 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.
tempDir := makeTempDir()
defer os.RemoveAll(tempDir)
os.Setenv(localbinary.PluginEnvKey, localbinary.PluginEnvVal)
os.Setenv(localbinary.PluginEnvDriverName, "virtualbox")
// Capture stdout and reset it later.
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
defer func() {
os.Stdout = old
}()
// Run the command asynchronously. It should listen on a port for connections.
go runCommand()
// The command will write out what port it's listening on over stdout.
reader := bufio.NewReader(r)
addr, _, err := reader.ReadLine()
if err != nil {
t.Fatal("Failed to read address over stdout.")
}
os.Stdout = old
// Now that we got the port, make sure we can connect.
if _, err := net.Dial("tcp", string(addr)); err != nil {
t.Fatal("Driver not listening.")
}
}

View File

@ -16,6 +16,7 @@ package cmd
import (
"fmt"
"log"
"os"
"github.com/docker/machine/libmachine"
"github.com/kubernetes/minikube/cli/cluster"
@ -39,11 +40,13 @@ func runStart(cmd *cobra.Command, args []string) {
defer api.Close()
host, err := cluster.StartHost(api)
if err != nil {
fmt.Println("Error starting host: ", err)
log.Println("Error starting host: ", err)
os.Exit(1)
}
kubeHost, err := cluster.StartCluster(host)
if err != nil {
fmt.Println("Error starting cluster: ", err)
log.Println("Error starting cluster: ", err)
os.Exit(1)
}
log.Printf("Kubernetes is available at %s.\n", kubeHost)
log.Println("Run this command to use the cluster: ")