github.com/ruphin/docker@v1.10.1/runconfig/opts/parse_test.go (about)

     1  package opts
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"runtime"
    10  	"strings"
    11  	"testing"
    12  
    13  	flag "github.com/docker/docker/pkg/mflag"
    14  	"github.com/docker/docker/runconfig"
    15  	"github.com/docker/engine-api/types/container"
    16  	networktypes "github.com/docker/engine-api/types/network"
    17  	"github.com/docker/go-connections/nat"
    18  )
    19  
    20  func parseRun(args []string) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, *flag.FlagSet, error) {
    21  	cmd := flag.NewFlagSet("run", flag.ContinueOnError)
    22  	cmd.SetOutput(ioutil.Discard)
    23  	cmd.Usage = nil
    24  	return Parse(cmd, args)
    25  }
    26  
    27  func parse(t *testing.T, args string) (*container.Config, *container.HostConfig, error) {
    28  	config, hostConfig, _, _, err := parseRun(strings.Split(args+" ubuntu bash", " "))
    29  	return config, hostConfig, err
    30  }
    31  
    32  func mustParse(t *testing.T, args string) (*container.Config, *container.HostConfig) {
    33  	config, hostConfig, err := parse(t, args)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	return config, hostConfig
    38  }
    39  
    40  func TestParseRunLinks(t *testing.T) {
    41  	if _, hostConfig := mustParse(t, "--link a:b"); len(hostConfig.Links) == 0 || hostConfig.Links[0] != "a:b" {
    42  		t.Fatalf("Error parsing links. Expected []string{\"a:b\"}, received: %v", hostConfig.Links)
    43  	}
    44  	if _, hostConfig := mustParse(t, "--link a:b --link c:d"); len(hostConfig.Links) < 2 || hostConfig.Links[0] != "a:b" || hostConfig.Links[1] != "c:d" {
    45  		t.Fatalf("Error parsing links. Expected []string{\"a:b\", \"c:d\"}, received: %v", hostConfig.Links)
    46  	}
    47  	if _, hostConfig := mustParse(t, ""); len(hostConfig.Links) != 0 {
    48  		t.Fatalf("Error parsing links. No link expected, received: %v", hostConfig.Links)
    49  	}
    50  }
    51  
    52  func TestParseRunAttach(t *testing.T) {
    53  	if config, _ := mustParse(t, "-a stdin"); !config.AttachStdin || config.AttachStdout || config.AttachStderr {
    54  		t.Fatalf("Error parsing attach flags. Expect only Stdin enabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
    55  	}
    56  	if config, _ := mustParse(t, "-a stdin -a stdout"); !config.AttachStdin || !config.AttachStdout || config.AttachStderr {
    57  		t.Fatalf("Error parsing attach flags. Expect only Stdin and Stdout enabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
    58  	}
    59  	if config, _ := mustParse(t, "-a stdin -a stdout -a stderr"); !config.AttachStdin || !config.AttachStdout || !config.AttachStderr {
    60  		t.Fatalf("Error parsing attach flags. Expect all attach enabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
    61  	}
    62  	if config, _ := mustParse(t, ""); config.AttachStdin || !config.AttachStdout || !config.AttachStderr {
    63  		t.Fatalf("Error parsing attach flags. Expect Stdin disabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
    64  	}
    65  	if config, _ := mustParse(t, "-i"); !config.AttachStdin || !config.AttachStdout || !config.AttachStderr {
    66  		t.Fatalf("Error parsing attach flags. Expect Stdin enabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
    67  	}
    68  
    69  	if _, _, err := parse(t, "-a"); err == nil {
    70  		t.Fatalf("Error parsing attach flags, `-a` should be an error but is not")
    71  	}
    72  	if _, _, err := parse(t, "-a invalid"); err == nil {
    73  		t.Fatalf("Error parsing attach flags, `-a invalid` should be an error but is not")
    74  	}
    75  	if _, _, err := parse(t, "-a invalid -a stdout"); err == nil {
    76  		t.Fatalf("Error parsing attach flags, `-a stdout -a invalid` should be an error but is not")
    77  	}
    78  	if _, _, err := parse(t, "-a stdout -a stderr -d"); err == nil {
    79  		t.Fatalf("Error parsing attach flags, `-a stdout -a stderr -d` should be an error but is not")
    80  	}
    81  	if _, _, err := parse(t, "-a stdin -d"); err == nil {
    82  		t.Fatalf("Error parsing attach flags, `-a stdin -d` should be an error but is not")
    83  	}
    84  	if _, _, err := parse(t, "-a stdout -d"); err == nil {
    85  		t.Fatalf("Error parsing attach flags, `-a stdout -d` should be an error but is not")
    86  	}
    87  	if _, _, err := parse(t, "-a stderr -d"); err == nil {
    88  		t.Fatalf("Error parsing attach flags, `-a stderr -d` should be an error but is not")
    89  	}
    90  	if _, _, err := parse(t, "-d --rm"); err == nil {
    91  		t.Fatalf("Error parsing attach flags, `-d --rm` should be an error but is not")
    92  	}
    93  }
    94  
    95  func TestParseRunVolumes(t *testing.T) {
    96  
    97  	// A single volume
    98  	arr, tryit := setupPlatformVolume([]string{`/tmp`}, []string{`c:\tmp`})
    99  	if config, hostConfig := mustParse(t, tryit); hostConfig.Binds != nil {
   100  		t.Fatalf("Error parsing volume flags, %q should not mount-bind anything. Received %v", tryit, hostConfig.Binds)
   101  	} else if _, exists := config.Volumes[arr[0]]; !exists {
   102  		t.Fatalf("Error parsing volume flags, %q is missing from volumes. Received %v", tryit, config.Volumes)
   103  	}
   104  
   105  	// Two volumes
   106  	arr, tryit = setupPlatformVolume([]string{`/tmp`, `/var`}, []string{`c:\tmp`, `c:\var`})
   107  	if config, hostConfig := mustParse(t, tryit); hostConfig.Binds != nil {
   108  		t.Fatalf("Error parsing volume flags, %q should not mount-bind anything. Received %v", tryit, hostConfig.Binds)
   109  	} else if _, exists := config.Volumes[arr[0]]; !exists {
   110  		t.Fatalf("Error parsing volume flags, %s is missing from volumes. Received %v", arr[0], config.Volumes)
   111  	} else if _, exists := config.Volumes[arr[1]]; !exists {
   112  		t.Fatalf("Error parsing volume flags, %s is missing from volumes. Received %v", arr[1], config.Volumes)
   113  	}
   114  
   115  	// A single bind-mount
   116  	arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp`}, []string{os.Getenv("TEMP") + `:c:\containerTmp`})
   117  	if config, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || hostConfig.Binds[0] != arr[0] {
   118  		t.Fatalf("Error parsing volume flags, %q should mount-bind the path before the colon into the path after the colon. Received %v %v", arr[0], hostConfig.Binds, config.Volumes)
   119  	}
   120  
   121  	// Two bind-mounts.
   122  	arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp`, `/hostVar:/containerVar`}, []string{os.Getenv("ProgramData") + `:c:\ContainerPD`, os.Getenv("TEMP") + `:c:\containerTmp`})
   123  	if _, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
   124  		t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
   125  	}
   126  
   127  	// Two bind-mounts, first read-only, second read-write.
   128  	// TODO Windows: The Windows version uses read-write as that's the only mode it supports. Can change this post TP4
   129  	arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp:ro`, `/hostVar:/containerVar:rw`}, []string{os.Getenv("TEMP") + `:c:\containerTmp:rw`, os.Getenv("ProgramData") + `:c:\ContainerPD:rw`})
   130  	if _, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
   131  		t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
   132  	}
   133  
   134  	// Similar to previous test but with alternate modes which are only supported by Linux
   135  	if runtime.GOOS != "windows" {
   136  		arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp:ro,Z`, `/hostVar:/containerVar:rw,Z`}, []string{})
   137  		if _, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
   138  			t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
   139  		}
   140  
   141  		arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp:Z`, `/hostVar:/containerVar:z`}, []string{})
   142  		if _, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
   143  			t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
   144  		}
   145  	}
   146  
   147  	// One bind mount and one volume
   148  	arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp`, `/containerVar`}, []string{os.Getenv("TEMP") + `:c:\containerTmp`, `c:\containerTmp`})
   149  	if config, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || len(hostConfig.Binds) > 1 || hostConfig.Binds[0] != arr[0] {
   150  		t.Fatalf("Error parsing volume flags, %s and %s should only one and only one bind mount %s. Received %s", arr[0], arr[1], arr[0], hostConfig.Binds)
   151  	} else if _, exists := config.Volumes[arr[1]]; !exists {
   152  		t.Fatalf("Error parsing volume flags %s and %s. %s is missing from volumes. Received %v", arr[0], arr[1], arr[1], config.Volumes)
   153  	}
   154  
   155  	// Root to non-c: drive letter (Windows specific)
   156  	if runtime.GOOS == "windows" {
   157  		arr, tryit = setupPlatformVolume([]string{}, []string{os.Getenv("SystemDrive") + `\:d:`})
   158  		if config, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || len(hostConfig.Binds) > 1 || hostConfig.Binds[0] != arr[0] || len(config.Volumes) != 0 {
   159  			t.Fatalf("Error parsing %s. Should have a single bind mount and no volumes", arr[0])
   160  		}
   161  	}
   162  
   163  }
   164  
   165  // This tests the cases for binds which are generated through
   166  // DecodeContainerConfig rather than Parse()
   167  func TestDecodeContainerConfigVolumes(t *testing.T) {
   168  
   169  	// Root to root
   170  	bindsOrVols, _ := setupPlatformVolume([]string{`/:/`}, []string{os.Getenv("SystemDrive") + `\:c:\`})
   171  	if _, _, err := callDecodeContainerConfig(nil, bindsOrVols); err == nil {
   172  		t.Fatalf("binds %v should have failed", bindsOrVols)
   173  	}
   174  	if _, _, err := callDecodeContainerConfig(bindsOrVols, nil); err == nil {
   175  		t.Fatalf("volume %v should have failed", bindsOrVols)
   176  	}
   177  
   178  	// No destination path
   179  	bindsOrVols, _ = setupPlatformVolume([]string{`/tmp:`}, []string{os.Getenv("TEMP") + `\:`})
   180  	if _, _, err := callDecodeContainerConfig(nil, bindsOrVols); err == nil {
   181  		t.Fatalf("binds %v should have failed", bindsOrVols)
   182  	}
   183  	if _, _, err := callDecodeContainerConfig(bindsOrVols, nil); err == nil {
   184  		t.Fatalf("binds %v should have failed", bindsOrVols)
   185  	}
   186  
   187  	//	// No destination path or mode
   188  	bindsOrVols, _ = setupPlatformVolume([]string{`/tmp::`}, []string{os.Getenv("TEMP") + `\::`})
   189  	if _, _, err := callDecodeContainerConfig(nil, bindsOrVols); err == nil {
   190  		t.Fatalf("binds %v should have failed", bindsOrVols)
   191  	}
   192  	if _, _, err := callDecodeContainerConfig(bindsOrVols, nil); err == nil {
   193  		t.Fatalf("binds %v should have failed", bindsOrVols)
   194  	}
   195  
   196  	// A whole lot of nothing
   197  	bindsOrVols = []string{`:`}
   198  	if _, _, err := callDecodeContainerConfig(nil, bindsOrVols); err == nil {
   199  		t.Fatalf("binds %v should have failed", bindsOrVols)
   200  	}
   201  	if _, _, err := callDecodeContainerConfig(bindsOrVols, nil); err == nil {
   202  		t.Fatalf("binds %v should have failed", bindsOrVols)
   203  	}
   204  
   205  	// A whole lot of nothing with no mode
   206  	bindsOrVols = []string{`::`}
   207  	if _, _, err := callDecodeContainerConfig(nil, bindsOrVols); err == nil {
   208  		t.Fatalf("binds %v should have failed", bindsOrVols)
   209  	}
   210  	if _, _, err := callDecodeContainerConfig(bindsOrVols, nil); err == nil {
   211  		t.Fatalf("binds %v should have failed", bindsOrVols)
   212  	}
   213  
   214  	// Too much including an invalid mode
   215  	wTmp := os.Getenv("TEMP")
   216  	bindsOrVols, _ = setupPlatformVolume([]string{`/tmp:/tmp:/tmp:/tmp`}, []string{wTmp + ":" + wTmp + ":" + wTmp + ":" + wTmp})
   217  	if _, _, err := callDecodeContainerConfig(nil, bindsOrVols); err == nil {
   218  		t.Fatalf("binds %v should have failed", bindsOrVols)
   219  	}
   220  	if _, _, err := callDecodeContainerConfig(bindsOrVols, nil); err == nil {
   221  		t.Fatalf("binds %v should have failed", bindsOrVols)
   222  	}
   223  
   224  	// Windows specific error tests
   225  	if runtime.GOOS == "windows" {
   226  		// Volume which does not include a drive letter
   227  		bindsOrVols = []string{`\tmp`}
   228  		if _, _, err := callDecodeContainerConfig(nil, bindsOrVols); err == nil {
   229  			t.Fatalf("binds %v should have failed", bindsOrVols)
   230  		}
   231  		if _, _, err := callDecodeContainerConfig(bindsOrVols, nil); err == nil {
   232  			t.Fatalf("binds %v should have failed", bindsOrVols)
   233  		}
   234  
   235  		// Root to C-Drive
   236  		bindsOrVols = []string{os.Getenv("SystemDrive") + `\:c:`}
   237  		if _, _, err := callDecodeContainerConfig(nil, bindsOrVols); err == nil {
   238  			t.Fatalf("binds %v should have failed", bindsOrVols)
   239  		}
   240  		if _, _, err := callDecodeContainerConfig(bindsOrVols, nil); err == nil {
   241  			t.Fatalf("binds %v should have failed", bindsOrVols)
   242  		}
   243  
   244  		// Container path that does not include a drive letter
   245  		bindsOrVols = []string{`c:\windows:\somewhere`}
   246  		if _, _, err := callDecodeContainerConfig(nil, bindsOrVols); err == nil {
   247  			t.Fatalf("binds %v should have failed", bindsOrVols)
   248  		}
   249  		if _, _, err := callDecodeContainerConfig(bindsOrVols, nil); err == nil {
   250  			t.Fatalf("binds %v should have failed", bindsOrVols)
   251  		}
   252  	}
   253  
   254  	// Linux-specific error tests
   255  	if runtime.GOOS != "windows" {
   256  		// Just root
   257  		bindsOrVols = []string{`/`}
   258  		if _, _, err := callDecodeContainerConfig(nil, bindsOrVols); err == nil {
   259  			t.Fatalf("binds %v should have failed", bindsOrVols)
   260  		}
   261  		if _, _, err := callDecodeContainerConfig(bindsOrVols, nil); err == nil {
   262  			t.Fatalf("binds %v should have failed", bindsOrVols)
   263  		}
   264  
   265  		// A single volume that looks like a bind mount passed in Volumes.
   266  		// This should be handled as a bind mount, not a volume.
   267  		vols := []string{`/foo:/bar`}
   268  		if config, hostConfig, err := callDecodeContainerConfig(vols, nil); err != nil {
   269  			t.Fatal("Volume /foo:/bar should have succeeded as a volume name")
   270  		} else if hostConfig.Binds != nil {
   271  			t.Fatalf("Error parsing volume flags, /foo:/bar should not mount-bind anything. Received %v", hostConfig.Binds)
   272  		} else if _, exists := config.Volumes[vols[0]]; !exists {
   273  			t.Fatalf("Error parsing volume flags, /foo:/bar is missing from volumes. Received %v", config.Volumes)
   274  		}
   275  
   276  	}
   277  }
   278  
   279  // callDecodeContainerConfig is a utility function used by TestDecodeContainerConfigVolumes
   280  // to call DecodeContainerConfig. It effectively does what a client would
   281  // do when calling the daemon by constructing a JSON stream of a
   282  // ContainerConfigWrapper which is populated by the set of volume specs
   283  // passed into it. It returns a config and a hostconfig which can be
   284  // validated to ensure DecodeContainerConfig has manipulated the structures
   285  // correctly.
   286  func callDecodeContainerConfig(volumes []string, binds []string) (*container.Config, *container.HostConfig, error) {
   287  	var (
   288  		b   []byte
   289  		err error
   290  		c   *container.Config
   291  		h   *container.HostConfig
   292  	)
   293  	w := runconfig.ContainerConfigWrapper{
   294  		Config: &container.Config{
   295  			Volumes: map[string]struct{}{},
   296  		},
   297  		HostConfig: &container.HostConfig{
   298  			NetworkMode: "none",
   299  			Binds:       binds,
   300  		},
   301  	}
   302  	for _, v := range volumes {
   303  		w.Config.Volumes[v] = struct{}{}
   304  	}
   305  	if b, err = json.Marshal(w); err != nil {
   306  		return nil, nil, fmt.Errorf("Error on marshal %s", err.Error())
   307  	}
   308  	c, h, _, err = runconfig.DecodeContainerConfig(bytes.NewReader(b))
   309  	if err != nil {
   310  		return nil, nil, fmt.Errorf("Error parsing %s: %v", string(b), err)
   311  	}
   312  	if c == nil || h == nil {
   313  		return nil, nil, fmt.Errorf("Empty config or hostconfig")
   314  	}
   315  
   316  	return c, h, err
   317  }
   318  
   319  // check if (a == c && b == d) || (a == d && b == c)
   320  // because maps are randomized
   321  func compareRandomizedStrings(a, b, c, d string) error {
   322  	if a == c && b == d {
   323  		return nil
   324  	}
   325  	if a == d && b == c {
   326  		return nil
   327  	}
   328  	return fmt.Errorf("strings don't match")
   329  }
   330  
   331  // setupPlatformVolume takes two arrays of volume specs - a Unix style
   332  // spec and a Windows style spec. Depending on the platform being unit tested,
   333  // it returns one of them, along with a volume string that would be passed
   334  // on the docker CLI (eg -v /bar -v /foo).
   335  func setupPlatformVolume(u []string, w []string) ([]string, string) {
   336  	var a []string
   337  	if runtime.GOOS == "windows" {
   338  		a = w
   339  	} else {
   340  		a = u
   341  	}
   342  	s := ""
   343  	for _, v := range a {
   344  		s = s + "-v " + v + " "
   345  	}
   346  	return a, s
   347  }
   348  
   349  // Simple parse with MacAddress validation
   350  func TestParseWithMacAddress(t *testing.T) {
   351  	invalidMacAddress := "--mac-address=invalidMacAddress"
   352  	validMacAddress := "--mac-address=92:d0:c6:0a:29:33"
   353  	if _, _, _, _, err := parseRun([]string{invalidMacAddress, "img", "cmd"}); err != nil && err.Error() != "invalidMacAddress is not a valid mac address" {
   354  		t.Fatalf("Expected an error with %v mac-address, got %v", invalidMacAddress, err)
   355  	}
   356  	if config, _ := mustParse(t, validMacAddress); config.MacAddress != "92:d0:c6:0a:29:33" {
   357  		t.Fatalf("Expected the config to have '92:d0:c6:0a:29:33' as MacAddress, got '%v'", config.MacAddress)
   358  	}
   359  }
   360  
   361  func TestParseWithMemory(t *testing.T) {
   362  	invalidMemory := "--memory=invalid"
   363  	validMemory := "--memory=1G"
   364  	if _, _, _, _, err := parseRun([]string{invalidMemory, "img", "cmd"}); err != nil && err.Error() != "invalid size: 'invalid'" {
   365  		t.Fatalf("Expected an error with '%v' Memory, got '%v'", invalidMemory, err)
   366  	}
   367  	if _, hostconfig := mustParse(t, validMemory); hostconfig.Memory != 1073741824 {
   368  		t.Fatalf("Expected the config to have '1G' as Memory, got '%v'", hostconfig.Memory)
   369  	}
   370  }
   371  
   372  func TestParseWithMemorySwap(t *testing.T) {
   373  	invalidMemory := "--memory-swap=invalid"
   374  	validMemory := "--memory-swap=1G"
   375  	anotherValidMemory := "--memory-swap=-1"
   376  	if _, _, _, _, err := parseRun([]string{invalidMemory, "img", "cmd"}); err == nil || err.Error() != "invalid size: 'invalid'" {
   377  		t.Fatalf("Expected an error with '%v' MemorySwap, got '%v'", invalidMemory, err)
   378  	}
   379  	if _, hostconfig := mustParse(t, validMemory); hostconfig.MemorySwap != 1073741824 {
   380  		t.Fatalf("Expected the config to have '1073741824' as MemorySwap, got '%v'", hostconfig.MemorySwap)
   381  	}
   382  	if _, hostconfig := mustParse(t, anotherValidMemory); hostconfig.MemorySwap != -1 {
   383  		t.Fatalf("Expected the config to have '-1' as MemorySwap, got '%v'", hostconfig.MemorySwap)
   384  	}
   385  }
   386  
   387  func TestParseHostname(t *testing.T) {
   388  	hostname := "--hostname=hostname"
   389  	hostnameWithDomain := "--hostname=hostname.domainname"
   390  	hostnameWithDomainTld := "--hostname=hostname.domainname.tld"
   391  	if config, _ := mustParse(t, hostname); config.Hostname != "hostname" && config.Domainname != "" {
   392  		t.Fatalf("Expected the config to have 'hostname' as hostname, got '%v'", config.Hostname)
   393  	}
   394  	if config, _ := mustParse(t, hostnameWithDomain); config.Hostname != "hostname" && config.Domainname != "domainname" {
   395  		t.Fatalf("Expected the config to have 'hostname' as hostname, got '%v'", config.Hostname)
   396  	}
   397  	if config, _ := mustParse(t, hostnameWithDomainTld); config.Hostname != "hostname" && config.Domainname != "domainname.tld" {
   398  		t.Fatalf("Expected the config to have 'hostname' as hostname, got '%v'", config.Hostname)
   399  	}
   400  }
   401  
   402  func TestParseWithExpose(t *testing.T) {
   403  	invalids := map[string]string{
   404  		":":                   "Invalid port format for --expose: :",
   405  		"8080:9090":           "Invalid port format for --expose: 8080:9090",
   406  		"/tcp":                "Invalid range format for --expose: /tcp, error: Empty string specified for ports.",
   407  		"/udp":                "Invalid range format for --expose: /udp, error: Empty string specified for ports.",
   408  		"NaN/tcp":             `Invalid range format for --expose: NaN/tcp, error: strconv.ParseUint: parsing "NaN": invalid syntax`,
   409  		"NaN-NaN/tcp":         `Invalid range format for --expose: NaN-NaN/tcp, error: strconv.ParseUint: parsing "NaN": invalid syntax`,
   410  		"8080-NaN/tcp":        `Invalid range format for --expose: 8080-NaN/tcp, error: strconv.ParseUint: parsing "NaN": invalid syntax`,
   411  		"1234567890-8080/tcp": `Invalid range format for --expose: 1234567890-8080/tcp, error: strconv.ParseUint: parsing "1234567890": value out of range`,
   412  	}
   413  	valids := map[string][]nat.Port{
   414  		"8080/tcp":      {"8080/tcp"},
   415  		"8080/udp":      {"8080/udp"},
   416  		"8080/ncp":      {"8080/ncp"},
   417  		"8080-8080/udp": {"8080/udp"},
   418  		"8080-8082/tcp": {"8080/tcp", "8081/tcp", "8082/tcp"},
   419  	}
   420  	for expose, expectedError := range invalids {
   421  		if _, _, _, _, err := parseRun([]string{fmt.Sprintf("--expose=%v", expose), "img", "cmd"}); err == nil || err.Error() != expectedError {
   422  			t.Fatalf("Expected error '%v' with '--expose=%v', got '%v'", expectedError, expose, err)
   423  		}
   424  	}
   425  	for expose, exposedPorts := range valids {
   426  		config, _, _, _, err := parseRun([]string{fmt.Sprintf("--expose=%v", expose), "img", "cmd"})
   427  		if err != nil {
   428  			t.Fatal(err)
   429  		}
   430  		if len(config.ExposedPorts) != len(exposedPorts) {
   431  			t.Fatalf("Expected %v exposed port, got %v", len(exposedPorts), len(config.ExposedPorts))
   432  		}
   433  		for _, port := range exposedPorts {
   434  			if _, ok := config.ExposedPorts[port]; !ok {
   435  				t.Fatalf("Expected %v, got %v", exposedPorts, config.ExposedPorts)
   436  			}
   437  		}
   438  	}
   439  	// Merge with actual published port
   440  	config, _, _, _, err := parseRun([]string{"--publish=80", "--expose=80-81/tcp", "img", "cmd"})
   441  	if err != nil {
   442  		t.Fatal(err)
   443  	}
   444  	if len(config.ExposedPorts) != 2 {
   445  		t.Fatalf("Expected 2 exposed ports, got %v", config.ExposedPorts)
   446  	}
   447  	ports := []nat.Port{"80/tcp", "81/tcp"}
   448  	for _, port := range ports {
   449  		if _, ok := config.ExposedPorts[port]; !ok {
   450  			t.Fatalf("Expected %v, got %v", ports, config.ExposedPorts)
   451  		}
   452  	}
   453  }
   454  
   455  func TestParseDevice(t *testing.T) {
   456  	valids := map[string]container.DeviceMapping{
   457  		"/dev/snd": {
   458  			PathOnHost:        "/dev/snd",
   459  			PathInContainer:   "/dev/snd",
   460  			CgroupPermissions: "rwm",
   461  		},
   462  		"/dev/snd:rw": {
   463  			PathOnHost:        "/dev/snd",
   464  			PathInContainer:   "/dev/snd",
   465  			CgroupPermissions: "rw",
   466  		},
   467  		"/dev/snd:/something": {
   468  			PathOnHost:        "/dev/snd",
   469  			PathInContainer:   "/something",
   470  			CgroupPermissions: "rwm",
   471  		},
   472  		"/dev/snd:/something:rw": {
   473  			PathOnHost:        "/dev/snd",
   474  			PathInContainer:   "/something",
   475  			CgroupPermissions: "rw",
   476  		},
   477  	}
   478  	for device, deviceMapping := range valids {
   479  		_, hostconfig, _, _, err := parseRun([]string{fmt.Sprintf("--device=%v", device), "img", "cmd"})
   480  		if err != nil {
   481  			t.Fatal(err)
   482  		}
   483  		if len(hostconfig.Devices) != 1 {
   484  			t.Fatalf("Expected 1 devices, got %v", hostconfig.Devices)
   485  		}
   486  		if hostconfig.Devices[0] != deviceMapping {
   487  			t.Fatalf("Expected %v, got %v", deviceMapping, hostconfig.Devices)
   488  		}
   489  	}
   490  
   491  }
   492  
   493  func TestParseModes(t *testing.T) {
   494  	// ipc ko
   495  	if _, _, _, _, err := parseRun([]string{"--ipc=container:", "img", "cmd"}); err == nil || err.Error() != "--ipc: invalid IPC mode" {
   496  		t.Fatalf("Expected an error with message '--ipc: invalid IPC mode', got %v", err)
   497  	}
   498  	// ipc ok
   499  	_, hostconfig, _, _, err := parseRun([]string{"--ipc=host", "img", "cmd"})
   500  	if err != nil {
   501  		t.Fatal(err)
   502  	}
   503  	if !hostconfig.IpcMode.Valid() {
   504  		t.Fatalf("Expected a valid IpcMode, got %v", hostconfig.IpcMode)
   505  	}
   506  	// pid ko
   507  	if _, _, _, _, err := parseRun([]string{"--pid=container:", "img", "cmd"}); err == nil || err.Error() != "--pid: invalid PID mode" {
   508  		t.Fatalf("Expected an error with message '--pid: invalid PID mode', got %v", err)
   509  	}
   510  	// pid ok
   511  	_, hostconfig, _, _, err = parseRun([]string{"--pid=host", "img", "cmd"})
   512  	if err != nil {
   513  		t.Fatal(err)
   514  	}
   515  	if !hostconfig.PidMode.Valid() {
   516  		t.Fatalf("Expected a valid PidMode, got %v", hostconfig.PidMode)
   517  	}
   518  	// uts ko
   519  	if _, _, _, _, err := parseRun([]string{"--uts=container:", "img", "cmd"}); err == nil || err.Error() != "--uts: invalid UTS mode" {
   520  		t.Fatalf("Expected an error with message '--uts: invalid UTS mode', got %v", err)
   521  	}
   522  	// uts ok
   523  	_, hostconfig, _, _, err = parseRun([]string{"--uts=host", "img", "cmd"})
   524  	if err != nil {
   525  		t.Fatal(err)
   526  	}
   527  	if !hostconfig.UTSMode.Valid() {
   528  		t.Fatalf("Expected a valid UTSMode, got %v", hostconfig.UTSMode)
   529  	}
   530  	// shm-size ko
   531  	if _, _, _, _, err = parseRun([]string{"--shm-size=a128m", "img", "cmd"}); err == nil || err.Error() != "invalid size: 'a128m'" {
   532  		t.Fatalf("Expected an error with message 'invalid size: a128m', got %v", err)
   533  	}
   534  	// shm-size ok
   535  	_, hostconfig, _, _, err = parseRun([]string{"--shm-size=128m", "img", "cmd"})
   536  	if err != nil {
   537  		t.Fatal(err)
   538  	}
   539  	if hostconfig.ShmSize != 134217728 {
   540  		t.Fatalf("Expected a valid ShmSize, got %d", hostconfig.ShmSize)
   541  	}
   542  }
   543  
   544  func TestParseRestartPolicy(t *testing.T) {
   545  	invalids := map[string]string{
   546  		"something":          "invalid restart policy something",
   547  		"always:2":           "maximum restart count not valid with restart policy of \"always\"",
   548  		"always:2:3":         "maximum restart count not valid with restart policy of \"always\"",
   549  		"on-failure:invalid": `strconv.ParseInt: parsing "invalid": invalid syntax`,
   550  		"on-failure:2:5":     "restart count format is not valid, usage: 'on-failure:N' or 'on-failure'",
   551  	}
   552  	valids := map[string]container.RestartPolicy{
   553  		"": {},
   554  		"always": {
   555  			Name:              "always",
   556  			MaximumRetryCount: 0,
   557  		},
   558  		"on-failure:1": {
   559  			Name:              "on-failure",
   560  			MaximumRetryCount: 1,
   561  		},
   562  	}
   563  	for restart, expectedError := range invalids {
   564  		if _, _, _, _, err := parseRun([]string{fmt.Sprintf("--restart=%s", restart), "img", "cmd"}); err == nil || err.Error() != expectedError {
   565  			t.Fatalf("Expected an error with message '%v' for %v, got %v", expectedError, restart, err)
   566  		}
   567  	}
   568  	for restart, expected := range valids {
   569  		_, hostconfig, _, _, err := parseRun([]string{fmt.Sprintf("--restart=%v", restart), "img", "cmd"})
   570  		if err != nil {
   571  			t.Fatal(err)
   572  		}
   573  		if hostconfig.RestartPolicy != expected {
   574  			t.Fatalf("Expected %v, got %v", expected, hostconfig.RestartPolicy)
   575  		}
   576  	}
   577  }
   578  
   579  func TestParseLoggingOpts(t *testing.T) {
   580  	// logging opts ko
   581  	if _, _, _, _, err := parseRun([]string{"--log-driver=none", "--log-opt=anything", "img", "cmd"}); err == nil || err.Error() != "Invalid logging opts for driver none" {
   582  		t.Fatalf("Expected an error with message 'Invalid logging opts for driver none', got %v", err)
   583  	}
   584  	// logging opts ok
   585  	_, hostconfig, _, _, err := parseRun([]string{"--log-driver=syslog", "--log-opt=something", "img", "cmd"})
   586  	if err != nil {
   587  		t.Fatal(err)
   588  	}
   589  	if hostconfig.LogConfig.Type != "syslog" || len(hostconfig.LogConfig.Config) != 1 {
   590  		t.Fatalf("Expected a 'syslog' LogConfig with one config, got %v", hostconfig.RestartPolicy)
   591  	}
   592  }
   593  
   594  func TestParseEnvfileVariables(t *testing.T) {
   595  	e := "open nonexistent: no such file or directory"
   596  	if runtime.GOOS == "windows" {
   597  		e = "open nonexistent: The system cannot find the file specified."
   598  	}
   599  	// env ko
   600  	if _, _, _, _, err := parseRun([]string{"--env-file=nonexistent", "img", "cmd"}); err == nil || err.Error() != e {
   601  		t.Fatalf("Expected an error with message '%s', got %v", e, err)
   602  	}
   603  	// env ok
   604  	config, _, _, _, err := parseRun([]string{"--env-file=fixtures/valid.env", "img", "cmd"})
   605  	if err != nil {
   606  		t.Fatal(err)
   607  	}
   608  	if len(config.Env) != 1 || config.Env[0] != "ENV1=value1" {
   609  		t.Fatalf("Expected a a config with [ENV1=value1], got %v", config.Env)
   610  	}
   611  	config, _, _, _, err = parseRun([]string{"--env-file=fixtures/valid.env", "--env=ENV2=value2", "img", "cmd"})
   612  	if err != nil {
   613  		t.Fatal(err)
   614  	}
   615  	if len(config.Env) != 2 || config.Env[0] != "ENV1=value1" || config.Env[1] != "ENV2=value2" {
   616  		t.Fatalf("Expected a a config with [ENV1=value1 ENV2=value2], got %v", config.Env)
   617  	}
   618  }
   619  
   620  func TestParseLabelfileVariables(t *testing.T) {
   621  	e := "open nonexistent: no such file or directory"
   622  	if runtime.GOOS == "windows" {
   623  		e = "open nonexistent: The system cannot find the file specified."
   624  	}
   625  	// label ko
   626  	if _, _, _, _, err := parseRun([]string{"--label-file=nonexistent", "img", "cmd"}); err == nil || err.Error() != e {
   627  		t.Fatalf("Expected an error with message '%s', got %v", e, err)
   628  	}
   629  	// label ok
   630  	config, _, _, _, err := parseRun([]string{"--label-file=fixtures/valid.label", "img", "cmd"})
   631  	if err != nil {
   632  		t.Fatal(err)
   633  	}
   634  	if len(config.Labels) != 1 || config.Labels["LABEL1"] != "value1" {
   635  		t.Fatalf("Expected a a config with [LABEL1:value1], got %v", config.Labels)
   636  	}
   637  	config, _, _, _, err = parseRun([]string{"--label-file=fixtures/valid.label", "--label=LABEL2=value2", "img", "cmd"})
   638  	if err != nil {
   639  		t.Fatal(err)
   640  	}
   641  	if len(config.Labels) != 2 || config.Labels["LABEL1"] != "value1" || config.Labels["LABEL2"] != "value2" {
   642  		t.Fatalf("Expected a a config with [LABEL1:value1 LABEL2:value2], got %v", config.Labels)
   643  	}
   644  }
   645  
   646  func TestParseEntryPoint(t *testing.T) {
   647  	config, _, _, _, err := parseRun([]string{"--entrypoint=anything", "cmd", "img"})
   648  	if err != nil {
   649  		t.Fatal(err)
   650  	}
   651  	if config.Entrypoint.Len() != 1 && config.Entrypoint.Slice()[0] != "anything" {
   652  		t.Fatalf("Expected entrypoint 'anything', got %v", config.Entrypoint)
   653  	}
   654  }
   655  
   656  func TestValidateLink(t *testing.T) {
   657  	valid := []string{
   658  		"name",
   659  		"dcdfbe62ecd0:alias",
   660  		"7a67485460b7642516a4ad82ecefe7f57d0c4916f530561b71a50a3f9c4e33da",
   661  		"angry_torvalds:linus",
   662  	}
   663  	invalid := map[string]string{
   664  		"":               "empty string specified for links",
   665  		"too:much:of:it": "bad format for links: too:much:of:it",
   666  	}
   667  
   668  	for _, link := range valid {
   669  		if _, err := ValidateLink(link); err != nil {
   670  			t.Fatalf("ValidateLink(`%q`) should succeed: error %q", link, err)
   671  		}
   672  	}
   673  
   674  	for link, expectedError := range invalid {
   675  		if _, err := ValidateLink(link); err == nil {
   676  			t.Fatalf("ValidateLink(`%q`) should have failed validation", link)
   677  		} else {
   678  			if !strings.Contains(err.Error(), expectedError) {
   679  				t.Fatalf("ValidateLink(`%q`) error should contain %q", link, expectedError)
   680  			}
   681  		}
   682  	}
   683  }
   684  
   685  func TestParseLink(t *testing.T) {
   686  	name, alias, err := ParseLink("name:alias")
   687  	if err != nil {
   688  		t.Fatalf("Expected not to error out on a valid name:alias format but got: %v", err)
   689  	}
   690  	if name != "name" {
   691  		t.Fatalf("Link name should have been name, got %s instead", name)
   692  	}
   693  	if alias != "alias" {
   694  		t.Fatalf("Link alias should have been alias, got %s instead", alias)
   695  	}
   696  	// short format definition
   697  	name, alias, err = ParseLink("name")
   698  	if err != nil {
   699  		t.Fatalf("Expected not to error out on a valid name only format but got: %v", err)
   700  	}
   701  	if name != "name" {
   702  		t.Fatalf("Link name should have been name, got %s instead", name)
   703  	}
   704  	if alias != "name" {
   705  		t.Fatalf("Link alias should have been name, got %s instead", alias)
   706  	}
   707  	// empty string link definition is not allowed
   708  	if _, _, err := ParseLink(""); err == nil || !strings.Contains(err.Error(), "empty string specified for links") {
   709  		t.Fatalf("Expected error 'empty string specified for links' but got: %v", err)
   710  	}
   711  	// more than two colons are not allowed
   712  	if _, _, err := ParseLink("link:alias:wrong"); err == nil || !strings.Contains(err.Error(), "bad format for links: link:alias:wrong") {
   713  		t.Fatalf("Expected error 'bad format for links: link:alias:wrong' but got: %v", err)
   714  	}
   715  }
   716  
   717  func TestValidateDevice(t *testing.T) {
   718  	valid := []string{
   719  		"/home",
   720  		"/home:/home",
   721  		"/home:/something/else",
   722  		"/with space",
   723  		"/home:/with space",
   724  		"relative:/absolute-path",
   725  		"hostPath:/containerPath:r",
   726  		"/hostPath:/containerPath:rw",
   727  		"/hostPath:/containerPath:mrw",
   728  	}
   729  	invalid := map[string]string{
   730  		"":        "bad format for path: ",
   731  		"./":      "./ is not an absolute path",
   732  		"../":     "../ is not an absolute path",
   733  		"/:../":   "../ is not an absolute path",
   734  		"/:path":  "path is not an absolute path",
   735  		":":       "bad format for path: :",
   736  		"/tmp:":   " is not an absolute path",
   737  		":test":   "bad format for path: :test",
   738  		":/test":  "bad format for path: :/test",
   739  		"tmp:":    " is not an absolute path",
   740  		":test:":  "bad format for path: :test:",
   741  		"::":      "bad format for path: ::",
   742  		":::":     "bad format for path: :::",
   743  		"/tmp:::": "bad format for path: /tmp:::",
   744  		":/tmp::": "bad format for path: :/tmp::",
   745  		"path:ro": "ro is not an absolute path",
   746  		"path:rr": "rr is not an absolute path",
   747  		"a:/b:ro": "bad mode specified: ro",
   748  		"a:/b:rr": "bad mode specified: rr",
   749  	}
   750  
   751  	for _, path := range valid {
   752  		if _, err := ValidateDevice(path); err != nil {
   753  			t.Fatalf("ValidateDevice(`%q`) should succeed: error %q", path, err)
   754  		}
   755  	}
   756  
   757  	for path, expectedError := range invalid {
   758  		if _, err := ValidateDevice(path); err == nil {
   759  			t.Fatalf("ValidateDevice(`%q`) should have failed validation", path)
   760  		} else {
   761  			if err.Error() != expectedError {
   762  				t.Fatalf("ValidateDevice(`%q`) error should contain %q, got %q", path, expectedError, err.Error())
   763  			}
   764  		}
   765  	}
   766  }
   767  
   768  func TestVolumeSplitN(t *testing.T) {
   769  	for _, x := range []struct {
   770  		input    string
   771  		n        int
   772  		expected []string
   773  	}{
   774  		{`C:\foo:d:`, -1, []string{`C:\foo`, `d:`}},
   775  		{`:C:\foo:d:`, -1, nil},
   776  		{`/foo:/bar:ro`, 3, []string{`/foo`, `/bar`, `ro`}},
   777  		{`/foo:/bar:ro`, 2, []string{`/foo`, `/bar:ro`}},
   778  		{`C:\foo\:/foo`, -1, []string{`C:\foo\`, `/foo`}},
   779  
   780  		{`d:\`, -1, []string{`d:\`}},
   781  		{`d:`, -1, []string{`d:`}},
   782  		{`d:\path`, -1, []string{`d:\path`}},
   783  		{`d:\path with space`, -1, []string{`d:\path with space`}},
   784  		{`d:\pathandmode:rw`, -1, []string{`d:\pathandmode`, `rw`}},
   785  		{`c:\:d:\`, -1, []string{`c:\`, `d:\`}},
   786  		{`c:\windows\:d:`, -1, []string{`c:\windows\`, `d:`}},
   787  		{`c:\windows:d:\s p a c e`, -1, []string{`c:\windows`, `d:\s p a c e`}},
   788  		{`c:\windows:d:\s p a c e:RW`, -1, []string{`c:\windows`, `d:\s p a c e`, `RW`}},
   789  		{`c:\program files:d:\s p a c e i n h o s t d i r`, -1, []string{`c:\program files`, `d:\s p a c e i n h o s t d i r`}},
   790  		{`0123456789name:d:`, -1, []string{`0123456789name`, `d:`}},
   791  		{`MiXeDcAsEnAmE:d:`, -1, []string{`MiXeDcAsEnAmE`, `d:`}},
   792  		{`name:D:`, -1, []string{`name`, `D:`}},
   793  		{`name:D::rW`, -1, []string{`name`, `D:`, `rW`}},
   794  		{`name:D::RW`, -1, []string{`name`, `D:`, `RW`}},
   795  		{`c:/:d:/forward/slashes/are/good/too`, -1, []string{`c:/`, `d:/forward/slashes/are/good/too`}},
   796  		{`c:\Windows`, -1, []string{`c:\Windows`}},
   797  		{`c:\Program Files (x86)`, -1, []string{`c:\Program Files (x86)`}},
   798  
   799  		{``, -1, nil},
   800  		{`.`, -1, []string{`.`}},
   801  		{`..\`, -1, []string{`..\`}},
   802  		{`c:\:..\`, -1, []string{`c:\`, `..\`}},
   803  		{`c:\:d:\:xyzzy`, -1, []string{`c:\`, `d:\`, `xyzzy`}},
   804  	} {
   805  		res := volumeSplitN(x.input, x.n)
   806  		if len(res) < len(x.expected) {
   807  			t.Fatalf("input: %v, expected: %v, got: %v", x.input, x.expected, res)
   808  		}
   809  		for i, e := range res {
   810  			if e != x.expected[i] {
   811  				t.Fatalf("input: %v, expected: %v, got: %v", x.input, x.expected, res)
   812  			}
   813  		}
   814  	}
   815  }