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"
)
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 {

View File

@ -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 = ""
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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)

View File

@ -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",

View File

@ -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,

View File

@ -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

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"))
}
}