Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit 6d0cd59

Browse files
committed
commands/mcndirs: Add
1 parent feb4c89 commit 6d0cd59

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

commands/mcndirs/utils.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package mcndirs
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/docker/machine/libmachine/mcnutils"
8+
)
9+
10+
var (
11+
BaseDir = os.Getenv("MACHINE_STORAGE_PATH")
12+
)
13+
14+
func GetBaseDir() string {
15+
if BaseDir == "" {
16+
BaseDir = filepath.Join(mcnutils.GetHomeDir(), ".docker", "machine")
17+
}
18+
return BaseDir
19+
}
20+
21+
func GetMachineDir() string {
22+
return filepath.Join(GetBaseDir(), "machines")
23+
}
24+
25+
func GetMachineCertDir() string {
26+
return filepath.Join(GetBaseDir(), "certs")
27+
}

commands/mcndirs/utils_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package mcndirs
2+
3+
import (
4+
"path"
5+
"strings"
6+
"testing"
7+
8+
"github.com/docker/machine/libmachine/mcnutils"
9+
)
10+
11+
func TestGetBaseDir(t *testing.T) {
12+
// reset any override env var
13+
BaseDir = ""
14+
15+
homeDir := mcnutils.GetHomeDir()
16+
baseDir := GetBaseDir()
17+
18+
if strings.Index(baseDir, homeDir) != 0 {
19+
t.Fatalf("expected base dir with prefix %s; received %s", homeDir, baseDir)
20+
}
21+
}
22+
23+
func TestGetCustomBaseDir(t *testing.T) {
24+
root := "/tmp"
25+
BaseDir = root
26+
baseDir := GetBaseDir()
27+
28+
if strings.Index(baseDir, root) != 0 {
29+
t.Fatalf("expected base dir with prefix %s; received %s", root, baseDir)
30+
}
31+
BaseDir = ""
32+
}
33+
34+
func TestGetMachineDir(t *testing.T) {
35+
root := "/tmp"
36+
BaseDir = root
37+
machineDir := GetMachineDir()
38+
39+
if strings.Index(machineDir, root) != 0 {
40+
t.Fatalf("expected machine dir with prefix %s; received %s", root, machineDir)
41+
}
42+
43+
path, filename := path.Split(machineDir)
44+
if strings.Index(path, root) != 0 {
45+
t.Fatalf("expected base path of %s; received %s", root, path)
46+
}
47+
if filename != "machines" {
48+
t.Fatalf("expected machine dir \"machines\"; received %s", filename)
49+
}
50+
BaseDir = ""
51+
}
52+
53+
func TestGetMachineCertDir(t *testing.T) {
54+
root := "/tmp"
55+
BaseDir = root
56+
clientDir := GetMachineCertDir()
57+
58+
if strings.Index(clientDir, root) != 0 {
59+
t.Fatalf("expected machine client cert dir with prefix %s; received %s", root, clientDir)
60+
}
61+
62+
path, filename := path.Split(clientDir)
63+
if strings.Index(path, root) != 0 {
64+
t.Fatalf("expected base path of %s; received %s", root, path)
65+
}
66+
if filename != "certs" {
67+
t.Fatalf("expected machine client dir \"certs\"; received %s", filename)
68+
}
69+
BaseDir = ""
70+
}

0 commit comments

Comments
 (0)