X Tutup
Skip to content

Commit 94c551b

Browse files
Fix broken --storage-path flag
Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
1 parent cc00ece commit 94c551b

File tree

9 files changed

+41
-20
lines changed

9 files changed

+41
-20
lines changed

commands/mcndirs/utils.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import (
77
"github.com/docker/machine/libmachine/mcnutils"
88
)
99

10+
var (
11+
BaseDir = os.Getenv("MACHINE_STORAGE_PATH")
12+
)
13+
1014
func GetBaseDir() string {
11-
baseDir := os.Getenv("MACHINE_STORAGE_PATH")
12-
if baseDir == "" {
13-
baseDir = filepath.Join(mcnutils.GetHomeDir(), ".docker", "machine")
15+
if BaseDir == "" {
16+
BaseDir = filepath.Join(mcnutils.GetHomeDir(), ".docker", "machine")
1417
}
15-
return baseDir
18+
return BaseDir
1619
}
1720

1821
func GetDockerDir() string {

commands/mcndirs/utils_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package mcndirs
22

33
import (
4-
"os"
54
"path"
65
"strings"
76
"testing"
@@ -11,6 +10,8 @@ import (
1110

1211
func TestGetBaseDir(t *testing.T) {
1312
// reset any override env var
13+
BaseDir = ""
14+
1415
homeDir := mcnutils.GetHomeDir()
1516
baseDir := GetBaseDir()
1617

@@ -21,13 +22,13 @@ func TestGetBaseDir(t *testing.T) {
2122

2223
func TestGetCustomBaseDir(t *testing.T) {
2324
root := "/tmp"
24-
os.Setenv("MACHINE_STORAGE_PATH", root)
25+
BaseDir = root
2526
baseDir := GetBaseDir()
2627

2728
if strings.Index(baseDir, root) != 0 {
2829
t.Fatalf("expected base dir with prefix %s; received %s", root, baseDir)
2930
}
30-
os.Setenv("MACHINE_STORAGE_PATH", "")
31+
BaseDir = ""
3132
}
3233

3334
func TestGetDockerDir(t *testing.T) {
@@ -41,7 +42,7 @@ func TestGetDockerDir(t *testing.T) {
4142

4243
func TestGetMachineDir(t *testing.T) {
4344
root := "/tmp"
44-
os.Setenv("MACHINE_STORAGE_PATH", root)
45+
BaseDir = root
4546
machineDir := GetMachineDir()
4647

4748
if strings.Index(machineDir, root) != 0 {
@@ -55,12 +56,12 @@ func TestGetMachineDir(t *testing.T) {
5556
if filename != "machines" {
5657
t.Fatalf("expected machine dir \"machines\"; received %s", filename)
5758
}
58-
os.Setenv("MACHINE_STORAGE_PATH", "")
59+
BaseDir = ""
5960
}
6061

6162
func TestGetMachineCertDir(t *testing.T) {
6263
root := "/tmp"
63-
os.Setenv("MACHINE_STORAGE_PATH", root)
64+
BaseDir = root
6465
clientDir := GetMachineCertDir()
6566

6667
if strings.Index(clientDir, root) != 0 {
@@ -74,5 +75,5 @@ func TestGetMachineCertDir(t *testing.T) {
7475
if filename != "certs" {
7576
t.Fatalf("expected machine client dir \"certs\"; received %s", filename)
7677
}
77-
os.Setenv("MACHINE_STORAGE_PATH", "")
78+
BaseDir = ""
7879
}

drivers/amazonec2/amazonec2_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"testing"
77

8+
"github.com/docker/machine/commands/mcndirs"
89
"github.com/docker/machine/drivers/amazonec2/amz"
910
)
1011

@@ -56,7 +57,7 @@ func getTestStorePath() (string, error) {
5657
if err != nil {
5758
return "", err
5859
}
59-
os.Setenv("MACHINE_STORAGE_PATH", tmpDir)
60+
mcndirs.BaseDir = tmpDir
6061
return tmpDir, nil
6162
}
6263

drivers/softlayer/driver_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"testing"
77

8+
"github.com/docker/machine/commands/mcndirs"
89
"github.com/stretchr/testify/assert"
910
)
1011

@@ -56,7 +57,7 @@ func getTestStorePath() (string, error) {
5657
if err != nil {
5758
return "", err
5859
}
59-
os.Setenv("MACHINE_STORAGE_PATH", tmpDir)
60+
mcndirs.BaseDir = tmpDir
6061
return tmpDir, nil
6162
}
6263

libmachine/cert/cert_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ func TestGenerateCACertificate(t *testing.T) {
1515
// cleanup
1616
defer os.RemoveAll(tmpDir)
1717

18-
os.Setenv("MACHINE_DIR", tmpDir)
1918
caCertPath := filepath.Join(tmpDir, "ca.pem")
2019
caKeyPath := filepath.Join(tmpDir, "key.pem")
2120
testOrg := "test-org"
@@ -30,7 +29,6 @@ func TestGenerateCACertificate(t *testing.T) {
3029
if _, err := os.Stat(caKeyPath); err != nil {
3130
t.Fatal(err)
3231
}
33-
os.Setenv("MACHINE_DIR", "")
3432
}
3533

3634
func TestGenerateCert(t *testing.T) {
@@ -41,7 +39,6 @@ func TestGenerateCert(t *testing.T) {
4139
// cleanup
4240
defer os.RemoveAll(tmpDir)
4341

44-
os.Setenv("MACHINE_DIR", tmpDir)
4542
caCertPath := filepath.Join(tmpDir, "ca.pem")
4643
caKeyPath := filepath.Join(tmpDir, "key.pem")
4744
certPath := filepath.Join(tmpDir, "cert.pem")
@@ -58,7 +55,6 @@ func TestGenerateCert(t *testing.T) {
5855
if _, err := os.Stat(caKeyPath); err != nil {
5956
t.Fatal(err)
6057
}
61-
os.Setenv("MACHINE_DIR", "")
6258

6359
if err := GenerateCert([]string{}, certPath, keyPath, caCertPath, caKeyPath, testOrg, bits); err != nil {
6460
t.Fatal(err)

libmachine/host/migrate_v0_v1_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package host
22

33
import (
4-
"os"
54
"reflect"
65
"testing"
76

7+
"github.com/docker/machine/commands/mcndirs"
88
"github.com/docker/machine/libmachine/auth"
99
"github.com/docker/machine/libmachine/engine"
1010
"github.com/docker/machine/libmachine/swarm"
1111
)
1212

1313
func TestMigrateHostV0ToV1(t *testing.T) {
14-
os.Setenv("MACHINE_STORAGE_PATH", "/tmp/migration")
14+
mcndirs.BaseDir = "/tmp/migration"
1515
originalHost := &HostV0{
1616
HostOptions: nil,
1717
SwarmDiscovery: "token://foobar",

libmachine/persist/filestore_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"testing"
1010

11+
"github.com/docker/machine/commands/mcndirs"
1112
_ "github.com/docker/machine/drivers/none"
1213
"github.com/docker/machine/libmachine/hosttest"
1314
)
@@ -23,7 +24,7 @@ func getTestStore() Filestore {
2324
os.Exit(1)
2425
}
2526

26-
os.Setenv("MACHINE_STORAGE_PATH", tmpDir)
27+
mcndirs.BaseDir = tmpDir
2728

2829
return Filestore{
2930
Path: tmpDir,

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func main() {
8181
ssh.SetDefaultClient(ssh.Native)
8282
}
8383
mcnutils.GithubApiToken = c.GlobalString("github-api-token")
84+
mcndirs.BaseDir = c.GlobalString("storage-path")
8485
return nil
8586
}
8687
app.Commands = commands.Commands

main_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/docker/machine/commands/mcndirs"
8+
)
9+
10+
func TestStorePathSetCorrectly(t *testing.T) {
11+
mcndirs.BaseDir = ""
12+
os.Args = []string{"docker-machine", "--storage-path", "/tmp/foo"}
13+
main()
14+
if mcndirs.BaseDir != "/tmp/foo" {
15+
t.Fatal("Expected MACHINE_STORAGE_PATH environment variable to be /tmp/foo but was ", os.Getenv("MACHINE_STORAGE_PATH"))
16+
}
17+
}

0 commit comments

Comments
 (0)
X Tutup