github.com/dougm/docker@v1.5.0/runconfig/config_test.go (about)

     1  package runconfig
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/nat"
     9  )
    10  
    11  func parse(t *testing.T, args string) (*Config, *HostConfig, error) {
    12  	config, hostConfig, _, err := parseRun(strings.Split(args+" ubuntu bash", " "))
    13  	return config, hostConfig, err
    14  }
    15  
    16  func mustParse(t *testing.T, args string) (*Config, *HostConfig) {
    17  	config, hostConfig, err := parse(t, args)
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	return config, hostConfig
    22  }
    23  
    24  // check if (a == c && b == d) || (a == d && b == c)
    25  // because maps are randomized
    26  func compareRandomizedStrings(a, b, c, d string) error {
    27  	if a == c && b == d {
    28  		return nil
    29  	}
    30  	if a == d && b == c {
    31  		return nil
    32  	}
    33  	return fmt.Errorf("strings don't match")
    34  }
    35  
    36  func TestParseRunLinks(t *testing.T) {
    37  	if _, hostConfig := mustParse(t, "--link a:b"); len(hostConfig.Links) == 0 || hostConfig.Links[0] != "a:b" {
    38  		t.Fatalf("Error parsing links. Expected []string{\"a:b\"}, received: %v", hostConfig.Links)
    39  	}
    40  	if _, hostConfig := mustParse(t, "--link a:b --link c:d"); len(hostConfig.Links) < 2 || hostConfig.Links[0] != "a:b" || hostConfig.Links[1] != "c:d" {
    41  		t.Fatalf("Error parsing links. Expected []string{\"a:b\", \"c:d\"}, received: %v", hostConfig.Links)
    42  	}
    43  	if _, hostConfig := mustParse(t, ""); len(hostConfig.Links) != 0 {
    44  		t.Fatalf("Error parsing links. No link expected, received: %v", hostConfig.Links)
    45  	}
    46  
    47  	if _, _, err := parse(t, "--link a"); err == nil {
    48  		t.Fatalf("Error parsing links. `--link a` should be an error but is not")
    49  	}
    50  	if _, _, err := parse(t, "--link"); err == nil {
    51  		t.Fatalf("Error parsing links. `--link` should be an error but is not")
    52  	}
    53  }
    54  
    55  func TestParseRunAttach(t *testing.T) {
    56  	if config, _ := mustParse(t, "-a stdin"); !config.AttachStdin || config.AttachStdout || config.AttachStderr {
    57  		t.Fatalf("Error parsing attach flags. Expect only Stdin enabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
    58  	}
    59  	if config, _ := mustParse(t, "-a stdin -a stdout"); !config.AttachStdin || !config.AttachStdout || config.AttachStderr {
    60  		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)
    61  	}
    62  	if config, _ := mustParse(t, "-a stdin -a stdout -a stderr"); !config.AttachStdin || !config.AttachStdout || !config.AttachStderr {
    63  		t.Fatalf("Error parsing attach flags. Expect all attach enabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
    64  	}
    65  	if config, _ := mustParse(t, ""); config.AttachStdin || !config.AttachStdout || !config.AttachStderr {
    66  		t.Fatalf("Error parsing attach flags. Expect Stdin disabled. 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  	if config, hostConfig := mustParse(t, "-v /tmp"); hostConfig.Binds != nil {
    97  		t.Fatalf("Error parsing volume flags, `-v /tmp` should not mount-bind anything. Received %v", hostConfig.Binds)
    98  	} else if _, exists := config.Volumes["/tmp"]; !exists {
    99  		t.Fatalf("Error parsing volume flags, `-v /tmp` is missing from volumes. Received %v", config.Volumes)
   100  	}
   101  
   102  	if config, hostConfig := mustParse(t, "-v /tmp -v /var"); hostConfig.Binds != nil {
   103  		t.Fatalf("Error parsing volume flags, `-v /tmp -v /var` should not mount-bind anything. Received %v", hostConfig.Binds)
   104  	} else if _, exists := config.Volumes["/tmp"]; !exists {
   105  		t.Fatalf("Error parsing volume flags, `-v /tmp` is missing from volumes. Recevied %v", config.Volumes)
   106  	} else if _, exists := config.Volumes["/var"]; !exists {
   107  		t.Fatalf("Error parsing volume flags, `-v /var` is missing from volumes. Received %v", config.Volumes)
   108  	}
   109  
   110  	if _, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp"); hostConfig.Binds == nil || hostConfig.Binds[0] != "/hostTmp:/containerTmp" {
   111  		t.Fatalf("Error parsing volume flags, `-v /hostTmp:/containerTmp` should mount-bind /hostTmp into /containeTmp. Received %v", hostConfig.Binds)
   112  	}
   113  
   114  	if _, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp -v /hostVar:/containerVar"); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], "/hostTmp:/containerTmp", "/hostVar:/containerVar") != nil {
   115  		t.Fatalf("Error parsing volume flags, `-v /hostTmp:/containerTmp -v /hostVar:/containerVar` should mount-bind /hostTmp into /containeTmp and /hostVar into /hostContainer. Received %v", hostConfig.Binds)
   116  	}
   117  
   118  	if _, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp:ro -v /hostVar:/containerVar:rw"); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], "/hostTmp:/containerTmp:ro", "/hostVar:/containerVar:rw") != nil {
   119  		t.Fatalf("Error parsing volume flags, `-v /hostTmp:/containerTmp:ro -v /hostVar:/containerVar:rw` should mount-bind /hostTmp into /containeTmp and /hostVar into /hostContainer. Received %v", hostConfig.Binds)
   120  	}
   121  
   122  	if config, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp -v /containerVar"); hostConfig.Binds == nil || len(hostConfig.Binds) > 1 || hostConfig.Binds[0] != "/hostTmp:/containerTmp" {
   123  		t.Fatalf("Error parsing volume flags, `-v /hostTmp:/containerTmp -v /containerVar` should mount-bind only /hostTmp into /containeTmp. Received %v", hostConfig.Binds)
   124  	} else if _, exists := config.Volumes["/containerVar"]; !exists {
   125  		t.Fatalf("Error parsing volume flags, `-v /containerVar` is missing from volumes. Received %v", config.Volumes)
   126  	}
   127  
   128  	if config, hostConfig := mustParse(t, ""); hostConfig.Binds != nil {
   129  		t.Fatalf("Error parsing volume flags, without volume, nothing should be mount-binded. Received %v", hostConfig.Binds)
   130  	} else if len(config.Volumes) != 0 {
   131  		t.Fatalf("Error parsing volume flags, without volume, no volume should be present. Received %v", config.Volumes)
   132  	}
   133  
   134  	if _, _, err := parse(t, "-v /"); err == nil {
   135  		t.Fatalf("Expected error, but got none")
   136  	}
   137  
   138  	if _, _, err := parse(t, "-v /:/"); err == nil {
   139  		t.Fatalf("Error parsing volume flags, `-v /:/` should fail but didn't")
   140  	}
   141  	if _, _, err := parse(t, "-v"); err == nil {
   142  		t.Fatalf("Error parsing volume flags, `-v` should fail but didn't")
   143  	}
   144  	if _, _, err := parse(t, "-v /tmp:"); err == nil {
   145  		t.Fatalf("Error parsing volume flags, `-v /tmp:` should fail but didn't")
   146  	}
   147  	if _, _, err := parse(t, "-v /tmp:ro"); err == nil {
   148  		t.Fatalf("Error parsing volume flags, `-v /tmp:ro` should fail but didn't")
   149  	}
   150  	if _, _, err := parse(t, "-v /tmp::"); err == nil {
   151  		t.Fatalf("Error parsing volume flags, `-v /tmp::` should fail but didn't")
   152  	}
   153  	if _, _, err := parse(t, "-v :"); err == nil {
   154  		t.Fatalf("Error parsing volume flags, `-v :` should fail but didn't")
   155  	}
   156  	if _, _, err := parse(t, "-v ::"); err == nil {
   157  		t.Fatalf("Error parsing volume flags, `-v ::` should fail but didn't")
   158  	}
   159  	if _, _, err := parse(t, "-v /tmp:/tmp:/tmp:/tmp"); err == nil {
   160  		t.Fatalf("Error parsing volume flags, `-v /tmp:/tmp:/tmp:/tmp` should fail but didn't")
   161  	}
   162  }
   163  
   164  func TestCompare(t *testing.T) {
   165  	volumes1 := make(map[string]struct{})
   166  	volumes1["/test1"] = struct{}{}
   167  	config1 := Config{
   168  		PortSpecs: []string{"1111:1111", "2222:2222"},
   169  		Env:       []string{"VAR1=1", "VAR2=2"},
   170  		Volumes:   volumes1,
   171  	}
   172  	config3 := Config{
   173  		PortSpecs: []string{"0000:0000", "2222:2222"},
   174  		Env:       []string{"VAR1=1", "VAR2=2"},
   175  		Volumes:   volumes1,
   176  	}
   177  	volumes2 := make(map[string]struct{})
   178  	volumes2["/test2"] = struct{}{}
   179  	config5 := Config{
   180  		PortSpecs: []string{"0000:0000", "2222:2222"},
   181  		Env:       []string{"VAR1=1", "VAR2=2"},
   182  		Volumes:   volumes2,
   183  	}
   184  	if Compare(&config1, &config3) {
   185  		t.Fatalf("Compare should return false, PortSpecs are different")
   186  	}
   187  	if Compare(&config1, &config5) {
   188  		t.Fatalf("Compare should return false, Volumes are different")
   189  	}
   190  	if !Compare(&config1, &config1) {
   191  		t.Fatalf("Compare should return true")
   192  	}
   193  }
   194  
   195  func TestMerge(t *testing.T) {
   196  	volumesImage := make(map[string]struct{})
   197  	volumesImage["/test1"] = struct{}{}
   198  	volumesImage["/test2"] = struct{}{}
   199  	configImage := &Config{
   200  		PortSpecs: []string{"1111:1111", "2222:2222"},
   201  		Env:       []string{"VAR1=1", "VAR2=2"},
   202  		Volumes:   volumesImage,
   203  	}
   204  
   205  	volumesUser := make(map[string]struct{})
   206  	volumesUser["/test3"] = struct{}{}
   207  	configUser := &Config{
   208  		PortSpecs: []string{"3333:2222", "3333:3333"},
   209  		Env:       []string{"VAR2=3", "VAR3=3"},
   210  		Volumes:   volumesUser,
   211  	}
   212  
   213  	if err := Merge(configUser, configImage); err != nil {
   214  		t.Error(err)
   215  	}
   216  
   217  	if len(configUser.ExposedPorts) != 3 {
   218  		t.Fatalf("Expected 3 ExposedPorts, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
   219  	}
   220  	for portSpecs := range configUser.ExposedPorts {
   221  		if portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
   222  			t.Fatalf("Expected 1111 or 2222 or 3333, found %s", portSpecs)
   223  		}
   224  	}
   225  	if len(configUser.Env) != 3 {
   226  		t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
   227  	}
   228  	for _, env := range configUser.Env {
   229  		if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
   230  			t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
   231  		}
   232  	}
   233  
   234  	if len(configUser.Volumes) != 3 {
   235  		t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
   236  	}
   237  	for v := range configUser.Volumes {
   238  		if v != "/test1" && v != "/test2" && v != "/test3" {
   239  			t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
   240  		}
   241  	}
   242  
   243  	ports, _, err := nat.ParsePortSpecs([]string{"0000"})
   244  	if err != nil {
   245  		t.Error(err)
   246  	}
   247  	configImage2 := &Config{
   248  		ExposedPorts: ports,
   249  	}
   250  
   251  	if err := Merge(configUser, configImage2); err != nil {
   252  		t.Error(err)
   253  	}
   254  
   255  	if len(configUser.ExposedPorts) != 4 {
   256  		t.Fatalf("Expected 4 ExposedPorts, 0000, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
   257  	}
   258  	for portSpecs := range configUser.ExposedPorts {
   259  		if portSpecs.Port() != "0" && portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
   260  			t.Fatalf("Expected %q or %q or %q or %q, found %s", 0, 1111, 2222, 3333, portSpecs)
   261  		}
   262  	}
   263  
   264  }