mirror of https://github.com/docker/docs.git
				
				
				
			Merge pull request #1941 from nathanleclaire/fix_storage_path
Fix broken --storage-path flag
This commit is contained in:
		
						commit
						870d5c9340
					
				| 
						 | 
				
			
			@ -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 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 = ""
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										1
									
								
								main.go
								
								
								
								
							
							
						
						
									
										1
									
								
								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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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"))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue