diff --git a/commands/mcndirs/utils.go b/commands/mcndirs/utils.go index 6ed6b9ee01..6c660bba74 100644 --- a/commands/mcndirs/utils.go +++ b/commands/mcndirs/utils.go @@ -7,12 +7,15 @@ import ( "github.com/docker/machine/libmachine/mcnutils" ) +var ( + BaseDir = os.Getenv("MACHINE_STORAGE_PATH") +) + func GetBaseDir() string { - baseDir := os.Getenv("MACHINE_STORAGE_PATH") - if baseDir == "" { - baseDir = filepath.Join(mcnutils.GetHomeDir(), ".docker", "machine") + if BaseDir == "" { + BaseDir = filepath.Join(mcnutils.GetHomeDir(), ".docker", "machine") } - return baseDir + return BaseDir } func GetDockerDir() string { diff --git a/commands/mcndirs/utils_test.go b/commands/mcndirs/utils_test.go index 63fd6bada5..dbe08cca66 100644 --- a/commands/mcndirs/utils_test.go +++ b/commands/mcndirs/utils_test.go @@ -1,7 +1,6 @@ package mcndirs import ( - "os" "path" "strings" "testing" @@ -11,6 +10,8 @@ import ( func TestGetBaseDir(t *testing.T) { // reset any override env var + BaseDir = "" + homeDir := mcnutils.GetHomeDir() baseDir := GetBaseDir() @@ -21,13 +22,13 @@ func TestGetBaseDir(t *testing.T) { func TestGetCustomBaseDir(t *testing.T) { root := "/tmp" - os.Setenv("MACHINE_STORAGE_PATH", root) + BaseDir = root baseDir := GetBaseDir() if strings.Index(baseDir, root) != 0 { t.Fatalf("expected base dir with prefix %s; received %s", root, baseDir) } - os.Setenv("MACHINE_STORAGE_PATH", "") + BaseDir = "" } func TestGetDockerDir(t *testing.T) { @@ -41,7 +42,7 @@ func TestGetDockerDir(t *testing.T) { func TestGetMachineDir(t *testing.T) { root := "/tmp" - os.Setenv("MACHINE_STORAGE_PATH", root) + BaseDir = root machineDir := GetMachineDir() if strings.Index(machineDir, root) != 0 { @@ -55,12 +56,12 @@ func TestGetMachineDir(t *testing.T) { if filename != "machines" { t.Fatalf("expected machine dir \"machines\"; received %s", filename) } - os.Setenv("MACHINE_STORAGE_PATH", "") + BaseDir = "" } func TestGetMachineCertDir(t *testing.T) { root := "/tmp" - os.Setenv("MACHINE_STORAGE_PATH", root) + BaseDir = root clientDir := GetMachineCertDir() if strings.Index(clientDir, root) != 0 { @@ -74,5 +75,5 @@ func TestGetMachineCertDir(t *testing.T) { if filename != "certs" { t.Fatalf("expected machine client dir \"certs\"; received %s", filename) } - os.Setenv("MACHINE_STORAGE_PATH", "") + BaseDir = "" } diff --git a/drivers/amazonec2/amazonec2_test.go b/drivers/amazonec2/amazonec2_test.go index 7b615676c5..d243348706 100644 --- a/drivers/amazonec2/amazonec2_test.go +++ b/drivers/amazonec2/amazonec2_test.go @@ -5,6 +5,7 @@ import ( "os" "testing" + "github.com/docker/machine/commands/mcndirs" "github.com/docker/machine/drivers/amazonec2/amz" ) @@ -56,7 +57,7 @@ func getTestStorePath() (string, error) { if err != nil { return "", err } - os.Setenv("MACHINE_STORAGE_PATH", tmpDir) + mcndirs.BaseDir = tmpDir return tmpDir, nil } diff --git a/drivers/softlayer/driver_test.go b/drivers/softlayer/driver_test.go index 44688163e8..8875397662 100644 --- a/drivers/softlayer/driver_test.go +++ b/drivers/softlayer/driver_test.go @@ -5,6 +5,7 @@ import ( "os" "testing" + "github.com/docker/machine/commands/mcndirs" "github.com/stretchr/testify/assert" ) @@ -56,7 +57,7 @@ func getTestStorePath() (string, error) { if err != nil { return "", err } - os.Setenv("MACHINE_STORAGE_PATH", tmpDir) + mcndirs.BaseDir = tmpDir return tmpDir, nil } diff --git a/libmachine/cert/cert_test.go b/libmachine/cert/cert_test.go index 69df4deed6..50dde8fd13 100644 --- a/libmachine/cert/cert_test.go +++ b/libmachine/cert/cert_test.go @@ -15,7 +15,6 @@ func TestGenerateCACertificate(t *testing.T) { // cleanup defer os.RemoveAll(tmpDir) - os.Setenv("MACHINE_DIR", tmpDir) caCertPath := filepath.Join(tmpDir, "ca.pem") caKeyPath := filepath.Join(tmpDir, "key.pem") testOrg := "test-org" @@ -30,7 +29,6 @@ func TestGenerateCACertificate(t *testing.T) { if _, err := os.Stat(caKeyPath); err != nil { t.Fatal(err) } - os.Setenv("MACHINE_DIR", "") } func TestGenerateCert(t *testing.T) { @@ -41,7 +39,6 @@ func TestGenerateCert(t *testing.T) { // cleanup defer os.RemoveAll(tmpDir) - os.Setenv("MACHINE_DIR", tmpDir) caCertPath := filepath.Join(tmpDir, "ca.pem") caKeyPath := filepath.Join(tmpDir, "key.pem") certPath := filepath.Join(tmpDir, "cert.pem") @@ -58,7 +55,6 @@ func TestGenerateCert(t *testing.T) { if _, err := os.Stat(caKeyPath); err != nil { t.Fatal(err) } - os.Setenv("MACHINE_DIR", "") if err := GenerateCert([]string{}, certPath, keyPath, caCertPath, caKeyPath, testOrg, bits); err != nil { t.Fatal(err) diff --git a/libmachine/host/migrate_v0_v1_test.go b/libmachine/host/migrate_v0_v1_test.go index 199fb0eb57..c3131ebf55 100644 --- a/libmachine/host/migrate_v0_v1_test.go +++ b/libmachine/host/migrate_v0_v1_test.go @@ -1,17 +1,17 @@ package host import ( - "os" "reflect" "testing" + "github.com/docker/machine/commands/mcndirs" "github.com/docker/machine/libmachine/auth" "github.com/docker/machine/libmachine/engine" "github.com/docker/machine/libmachine/swarm" ) func TestMigrateHostV0ToV1(t *testing.T) { - os.Setenv("MACHINE_STORAGE_PATH", "/tmp/migration") + mcndirs.BaseDir = "/tmp/migration" originalHost := &HostV0{ HostOptions: nil, SwarmDiscovery: "token://foobar", diff --git a/libmachine/persist/filestore_test.go b/libmachine/persist/filestore_test.go index e81918935d..67722ab714 100644 --- a/libmachine/persist/filestore_test.go +++ b/libmachine/persist/filestore_test.go @@ -8,6 +8,7 @@ import ( "path/filepath" "testing" + "github.com/docker/machine/commands/mcndirs" _ "github.com/docker/machine/drivers/none" "github.com/docker/machine/libmachine/hosttest" ) @@ -23,7 +24,7 @@ func getTestStore() Filestore { os.Exit(1) } - os.Setenv("MACHINE_STORAGE_PATH", tmpDir) + mcndirs.BaseDir = tmpDir return Filestore{ Path: tmpDir, diff --git a/main.go b/main.go index fa3f123801..c43cff82d4 100644 --- a/main.go +++ b/main.go @@ -81,6 +81,7 @@ func main() { ssh.SetDefaultClient(ssh.Native) } mcnutils.GithubApiToken = c.GlobalString("github-api-token") + mcndirs.BaseDir = c.GlobalString("storage-path") return nil } app.Commands = commands.Commands diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000000..0287158a94 --- /dev/null +++ b/main_test.go @@ -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")) + } +}