Fix broken --storage-path flag

Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
This commit is contained in:
Nathan LeClaire 2015-10-06 15:27:37 -07:00
parent cc00ece828
commit 94c551b964
9 changed files with 41 additions and 20 deletions

View File

@ -7,12 +7,15 @@ import (
"github.com/docker/machine/libmachine/mcnutils" "github.com/docker/machine/libmachine/mcnutils"
) )
var (
BaseDir = os.Getenv("MACHINE_STORAGE_PATH")
)
func GetBaseDir() string { func GetBaseDir() string {
baseDir := os.Getenv("MACHINE_STORAGE_PATH") if BaseDir == "" {
if baseDir == "" { BaseDir = filepath.Join(mcnutils.GetHomeDir(), ".docker", "machine")
baseDir = filepath.Join(mcnutils.GetHomeDir(), ".docker", "machine")
} }
return baseDir return BaseDir
} }
func GetDockerDir() string { func GetDockerDir() string {

View File

@ -1,7 +1,6 @@
package mcndirs package mcndirs
import ( import (
"os"
"path" "path"
"strings" "strings"
"testing" "testing"
@ -11,6 +10,8 @@ import (
func TestGetBaseDir(t *testing.T) { func TestGetBaseDir(t *testing.T) {
// reset any override env var // reset any override env var
BaseDir = ""
homeDir := mcnutils.GetHomeDir() homeDir := mcnutils.GetHomeDir()
baseDir := GetBaseDir() baseDir := GetBaseDir()
@ -21,13 +22,13 @@ func TestGetBaseDir(t *testing.T) {
func TestGetCustomBaseDir(t *testing.T) { func TestGetCustomBaseDir(t *testing.T) {
root := "/tmp" root := "/tmp"
os.Setenv("MACHINE_STORAGE_PATH", root) BaseDir = root
baseDir := GetBaseDir() baseDir := GetBaseDir()
if strings.Index(baseDir, root) != 0 { if strings.Index(baseDir, root) != 0 {
t.Fatalf("expected base dir with prefix %s; received %s", root, baseDir) t.Fatalf("expected base dir with prefix %s; received %s", root, baseDir)
} }
os.Setenv("MACHINE_STORAGE_PATH", "") BaseDir = ""
} }
func TestGetDockerDir(t *testing.T) { func TestGetDockerDir(t *testing.T) {
@ -41,7 +42,7 @@ func TestGetDockerDir(t *testing.T) {
func TestGetMachineDir(t *testing.T) { func TestGetMachineDir(t *testing.T) {
root := "/tmp" root := "/tmp"
os.Setenv("MACHINE_STORAGE_PATH", root) BaseDir = root
machineDir := GetMachineDir() machineDir := GetMachineDir()
if strings.Index(machineDir, root) != 0 { if strings.Index(machineDir, root) != 0 {
@ -55,12 +56,12 @@ func TestGetMachineDir(t *testing.T) {
if filename != "machines" { if filename != "machines" {
t.Fatalf("expected machine dir \"machines\"; received %s", filename) t.Fatalf("expected machine dir \"machines\"; received %s", filename)
} }
os.Setenv("MACHINE_STORAGE_PATH", "") BaseDir = ""
} }
func TestGetMachineCertDir(t *testing.T) { func TestGetMachineCertDir(t *testing.T) {
root := "/tmp" root := "/tmp"
os.Setenv("MACHINE_STORAGE_PATH", root) BaseDir = root
clientDir := GetMachineCertDir() clientDir := GetMachineCertDir()
if strings.Index(clientDir, root) != 0 { if strings.Index(clientDir, root) != 0 {
@ -74,5 +75,5 @@ func TestGetMachineCertDir(t *testing.T) {
if filename != "certs" { if filename != "certs" {
t.Fatalf("expected machine client dir \"certs\"; received %s", filename) t.Fatalf("expected machine client dir \"certs\"; received %s", filename)
} }
os.Setenv("MACHINE_STORAGE_PATH", "") BaseDir = ""
} }

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"testing" "testing"
"github.com/docker/machine/commands/mcndirs"
"github.com/docker/machine/drivers/amazonec2/amz" "github.com/docker/machine/drivers/amazonec2/amz"
) )
@ -56,7 +57,7 @@ func getTestStorePath() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
os.Setenv("MACHINE_STORAGE_PATH", tmpDir) mcndirs.BaseDir = tmpDir
return tmpDir, nil return tmpDir, nil
} }

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"testing" "testing"
"github.com/docker/machine/commands/mcndirs"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -56,7 +57,7 @@ func getTestStorePath() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
os.Setenv("MACHINE_STORAGE_PATH", tmpDir) mcndirs.BaseDir = tmpDir
return tmpDir, nil return tmpDir, nil
} }

View File

@ -15,7 +15,6 @@ func TestGenerateCACertificate(t *testing.T) {
// cleanup // cleanup
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
os.Setenv("MACHINE_DIR", tmpDir)
caCertPath := filepath.Join(tmpDir, "ca.pem") caCertPath := filepath.Join(tmpDir, "ca.pem")
caKeyPath := filepath.Join(tmpDir, "key.pem") caKeyPath := filepath.Join(tmpDir, "key.pem")
testOrg := "test-org" testOrg := "test-org"
@ -30,7 +29,6 @@ func TestGenerateCACertificate(t *testing.T) {
if _, err := os.Stat(caKeyPath); err != nil { if _, err := os.Stat(caKeyPath); err != nil {
t.Fatal(err) t.Fatal(err)
} }
os.Setenv("MACHINE_DIR", "")
} }
func TestGenerateCert(t *testing.T) { func TestGenerateCert(t *testing.T) {
@ -41,7 +39,6 @@ func TestGenerateCert(t *testing.T) {
// cleanup // cleanup
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
os.Setenv("MACHINE_DIR", tmpDir)
caCertPath := filepath.Join(tmpDir, "ca.pem") caCertPath := filepath.Join(tmpDir, "ca.pem")
caKeyPath := filepath.Join(tmpDir, "key.pem") caKeyPath := filepath.Join(tmpDir, "key.pem")
certPath := filepath.Join(tmpDir, "cert.pem") certPath := filepath.Join(tmpDir, "cert.pem")
@ -58,7 +55,6 @@ func TestGenerateCert(t *testing.T) {
if _, err := os.Stat(caKeyPath); err != nil { if _, err := os.Stat(caKeyPath); err != nil {
t.Fatal(err) t.Fatal(err)
} }
os.Setenv("MACHINE_DIR", "")
if err := GenerateCert([]string{}, certPath, keyPath, caCertPath, caKeyPath, testOrg, bits); err != nil { if err := GenerateCert([]string{}, certPath, keyPath, caCertPath, caKeyPath, testOrg, bits); err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -1,17 +1,17 @@
package host package host
import ( import (
"os"
"reflect" "reflect"
"testing" "testing"
"github.com/docker/machine/commands/mcndirs"
"github.com/docker/machine/libmachine/auth" "github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/engine" "github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/swarm" "github.com/docker/machine/libmachine/swarm"
) )
func TestMigrateHostV0ToV1(t *testing.T) { func TestMigrateHostV0ToV1(t *testing.T) {
os.Setenv("MACHINE_STORAGE_PATH", "/tmp/migration") mcndirs.BaseDir = "/tmp/migration"
originalHost := &HostV0{ originalHost := &HostV0{
HostOptions: nil, HostOptions: nil,
SwarmDiscovery: "token://foobar", SwarmDiscovery: "token://foobar",

View File

@ -8,6 +8,7 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/docker/machine/commands/mcndirs"
_ "github.com/docker/machine/drivers/none" _ "github.com/docker/machine/drivers/none"
"github.com/docker/machine/libmachine/hosttest" "github.com/docker/machine/libmachine/hosttest"
) )
@ -23,7 +24,7 @@ func getTestStore() Filestore {
os.Exit(1) os.Exit(1)
} }
os.Setenv("MACHINE_STORAGE_PATH", tmpDir) mcndirs.BaseDir = tmpDir
return Filestore{ return Filestore{
Path: tmpDir, Path: tmpDir,

View File

@ -81,6 +81,7 @@ func main() {
ssh.SetDefaultClient(ssh.Native) ssh.SetDefaultClient(ssh.Native)
} }
mcnutils.GithubApiToken = c.GlobalString("github-api-token") mcnutils.GithubApiToken = c.GlobalString("github-api-token")
mcndirs.BaseDir = c.GlobalString("storage-path")
return nil return nil
} }
app.Commands = commands.Commands app.Commands = commands.Commands

17
main_test.go Normal file
View File

@ -0,0 +1,17 @@
package main
import (
"os"
"testing"
"github.com/docker/machine/commands/mcndirs"
)
func TestStorePathSetCorrectly(t *testing.T) {
mcndirs.BaseDir = ""
os.Args = []string{"docker-machine", "--storage-path", "/tmp/foo"}
main()
if mcndirs.BaseDir != "/tmp/foo" {
t.Fatal("Expected MACHINE_STORAGE_PATH environment variable to be /tmp/foo but was ", os.Getenv("MACHINE_STORAGE_PATH"))
}
}