Move machine driver creation code to machine/client.go.

pull/32/head
Dan Lorenc 2016-04-29 17:31:21 -07:00
parent f7762d147d
commit 5c0ee33e44
5 changed files with 85 additions and 73 deletions

View File

@ -18,9 +18,6 @@ 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"
)
@ -38,27 +35,12 @@ var RootCmd = &cobra.Command{
clusters optimized for development workflows.
`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
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 {
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
}
},
}
// Execute adds all child commands to the root command sets flags appropriately.

View File

@ -1,14 +1,11 @@
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"
)
@ -42,46 +39,3 @@ func TestPreRunDirectories(t *testing.T) {
}
}
}
func TestPreRunNotDriver(t *testing.T) {
tempDir := makeTempDir()
defer os.RemoveAll(tempDir)
runCommand(RootCmd.PersistentPreRun)
if !localbinary.CurrentBinaryIsDockerMachine {
t.Fatal("CurrentBinaryIsDockerMachine not set. This will break driver initialization.")
}
}
func TestRunDriver(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(RootCmd.Run)
// 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

@ -19,15 +19,21 @@ import (
"github.com/docker/machine/drivers/virtualbox"
"github.com/docker/machine/libmachine/drivers/plugin"
"github.com/docker/machine/libmachine/drivers/plugin/localbinary"
)
// StartDriver starts the specified libmachine driver.
func StartDriver(driverName string) {
switch driverName {
case "virtualbox":
plugin.RegisterDriver(virtualbox.NewDriver("", ""))
default:
fmt.Fprintf(os.Stderr, "Unsupported driver: %s\n", driverName)
os.Exit(1)
// StartDriver starts the desired machine driver if necessary.
func StartDriver() {
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
}
localbinary.CurrentBinaryIsDockerMachine = true
}

View File

@ -0,0 +1,65 @@
package machine
import (
"bufio"
"io/ioutil"
"log"
"net"
"os"
"testing"
"github.com/docker/machine/libmachine/drivers/plugin/localbinary"
"github.com/kubernetes/minikube/cli/constants"
)
func makeTempDir() string {
tempDir, err := ioutil.TempDir("", "minipath")
if err != nil {
log.Fatal(err)
}
constants.Minipath = tempDir
return tempDir
}
func TestRunNotDriver(t *testing.T) {
tempDir := makeTempDir()
defer os.RemoveAll(tempDir)
StartDriver()
if !localbinary.CurrentBinaryIsDockerMachine {
t.Fatal("CurrentBinaryIsDockerMachine not set. This will break driver initialization.")
}
}
func TestRunDriver(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 StartDriver()
// 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

@ -13,8 +13,13 @@ limitations under the License.
package main
import "github.com/kubernetes/minikube/cli/cmd"
import (
"github.com/kubernetes/minikube/cli/cmd"
"github.com/kubernetes/minikube/cli/machine"
)
func main() {
machine.StartDriver()
cmd.Execute()
}