github.com/sevki/docker@v1.7.1/runconfig/config_test.go (about)

     1  package runconfig
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/docker/docker/nat"
    12  )
    13  
    14  func parse(t *testing.T, args string) (*Config, *HostConfig, error) {
    15  	config, hostConfig, _, err := parseRun(strings.Split(args+" ubuntu bash", " "))
    16  	return config, hostConfig, err
    17  }
    18  
    19  func mustParse(t *testing.T, args string) (*Config, *HostConfig) {
    20  	config, hostConfig, err := parse(t, args)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	return config, hostConfig
    25  }
    26  
    27  // check if (a == c && b == d) || (a == d && b == c)
    28  // because maps are randomized
    29  func compareRandomizedStrings(a, b, c, d string) error {
    30  	if a == c && b == d {
    31  		return nil
    32  	}
    33  	if a == d && b == c {
    34  		return nil
    35  	}
    36  	return fmt.Errorf("strings don't match")
    37  }
    38  
    39  func TestParseRunLinks(t *testing.T) {
    40  	if _, hostConfig := mustParse(t, "--link a:b"); len(hostConfig.Links) == 0 || hostConfig.Links[0] != "a:b" {
    41  		t.Fatalf("Error parsing links. Expected []string{\"a:b\"}, received: %v", hostConfig.Links)
    42  	}
    43  	if _, hostConfig := mustParse(t, "--link a:b --link c:d"); len(hostConfig.Links) < 2 || hostConfig.Links[0] != "a:b" || hostConfig.Links[1] != "c:d" {
    44  		t.Fatalf("Error parsing links. Expected []string{\"a:b\", \"c:d\"}, received: %v", hostConfig.Links)
    45  	}
    46  	if _, hostConfig := mustParse(t, ""); len(hostConfig.Links) != 0 {
    47  		t.Fatalf("Error parsing links. No link expected, received: %v", hostConfig.Links)
    48  	}
    49  }
    50  
    51  func TestParseRunAttach(t *testing.T) {
    52  	if config, _ := mustParse(t, "-a stdin"); !config.AttachStdin || config.AttachStdout || config.AttachStderr {
    53  		t.Fatalf("Error parsing attach flags. Expect only Stdin enabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
    54  	}
    55  	if config, _ := mustParse(t, "-a stdin -a stdout"); !config.AttachStdin || !config.AttachStdout || config.AttachStderr {
    56  		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)
    57  	}
    58  	if config, _ := mustParse(t, "-a stdin -a stdout -a stderr"); !config.AttachStdin || !config.AttachStdout || !config.AttachStderr {
    59  		t.Fatalf("Error parsing attach flags. Expect all attach enabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
    60  	}
    61  	if config, _ := mustParse(t, ""); config.AttachStdin || !config.AttachStdout || !config.AttachStderr {
    62  		t.Fatalf("Error parsing attach flags. Expect Stdin disabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
    63  	}
    64  
    65  	if _, _, err := parse(t, "-a"); err == nil {
    66  		t.Fatalf("Error parsing attach flags, `-a` should be an error but is not")
    67  	}
    68  	if _, _, err := parse(t, "-a invalid"); err == nil {
    69  		t.Fatalf("Error parsing attach flags, `-a invalid` should be an error but is not")
    70  	}
    71  	if _, _, err := parse(t, "-a invalid -a stdout"); err == nil {
    72  		t.Fatalf("Error parsing attach flags, `-a stdout -a invalid` should be an error but is not")
    73  	}
    74  	if _, _, err := parse(t, "-a stdout -a stderr -d"); err == nil {
    75  		t.Fatalf("Error parsing attach flags, `-a stdout -a stderr -d` should be an error but is not")
    76  	}
    77  	if _, _, err := parse(t, "-a stdin -d"); err == nil {
    78  		t.Fatalf("Error parsing attach flags, `-a stdin -d` should be an error but is not")
    79  	}
    80  	if _, _, err := parse(t, "-a stdout -d"); err == nil {
    81  		t.Fatalf("Error parsing attach flags, `-a stdout -d` should be an error but is not")
    82  	}
    83  	if _, _, err := parse(t, "-a stderr -d"); err == nil {
    84  		t.Fatalf("Error parsing attach flags, `-a stderr -d` should be an error but is not")
    85  	}
    86  	if _, _, err := parse(t, "-d --rm"); err == nil {
    87  		t.Fatalf("Error parsing attach flags, `-d --rm` should be an error but is not")
    88  	}
    89  }
    90  
    91  func TestParseRunVolumes(t *testing.T) {
    92  	if config, hostConfig := mustParse(t, "-v /tmp"); hostConfig.Binds != nil {
    93  		t.Fatalf("Error parsing volume flags, `-v /tmp` should not mount-bind anything. Received %v", hostConfig.Binds)
    94  	} else if _, exists := config.Volumes["/tmp"]; !exists {
    95  		t.Fatalf("Error parsing volume flags, `-v /tmp` is missing from volumes. Received %v", config.Volumes)
    96  	}
    97  
    98  	if config, hostConfig := mustParse(t, "-v /tmp -v /var"); hostConfig.Binds != nil {
    99  		t.Fatalf("Error parsing volume flags, `-v /tmp -v /var` should not mount-bind anything. Received %v", hostConfig.Binds)
   100  	} else if _, exists := config.Volumes["/tmp"]; !exists {
   101  		t.Fatalf("Error parsing volume flags, `-v /tmp` is missing from volumes. Received %v", config.Volumes)
   102  	} else if _, exists := config.Volumes["/var"]; !exists {
   103  		t.Fatalf("Error parsing volume flags, `-v /var` is missing from volumes. Received %v", config.Volumes)
   104  	}
   105  
   106  	if _, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp"); hostConfig.Binds == nil || hostConfig.Binds[0] != "/hostTmp:/containerTmp" {
   107  		t.Fatalf("Error parsing volume flags, `-v /hostTmp:/containerTmp` should mount-bind /hostTmp into /containeTmp. Received %v", hostConfig.Binds)
   108  	}
   109  
   110  	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 {
   111  		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)
   112  	}
   113  
   114  	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 {
   115  		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)
   116  	}
   117  
   118  	if _, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp:roZ -v /hostVar:/containerVar:rwZ"); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], "/hostTmp:/containerTmp:roZ", "/hostVar:/containerVar:rwZ") != nil {
   119  		t.Fatalf("Error parsing volume flags, `-v /hostTmp:/containerTmp:roZ -v /hostVar:/containerVar:rwZ` should mount-bind /hostTmp into /containeTmp and /hostVar into /hostContainer. Received %v", hostConfig.Binds)
   120  	}
   121  
   122  	if _, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp:Z -v /hostVar:/containerVar:z"); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], "/hostTmp:/containerTmp:Z", "/hostVar:/containerVar:z") != nil {
   123  		t.Fatalf("Error parsing volume flags, `-v /hostTmp:/containerTmp:Z -v /hostVar:/containerVar:z` should mount-bind /hostTmp into /containeTmp and /hostVar into /hostContainer. Received %v", hostConfig.Binds)
   124  	}
   125  
   126  	if config, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp -v /containerVar"); hostConfig.Binds == nil || len(hostConfig.Binds) > 1 || hostConfig.Binds[0] != "/hostTmp:/containerTmp" {
   127  		t.Fatalf("Error parsing volume flags, `-v /hostTmp:/containerTmp -v /containerVar` should mount-bind only /hostTmp into /containeTmp. Received %v", hostConfig.Binds)
   128  	} else if _, exists := config.Volumes["/containerVar"]; !exists {
   129  		t.Fatalf("Error parsing volume flags, `-v /containerVar` is missing from volumes. Received %v", config.Volumes)
   130  	}
   131  
   132  	if config, hostConfig := mustParse(t, ""); hostConfig.Binds != nil {
   133  		t.Fatalf("Error parsing volume flags, without volume, nothing should be mount-binded. Received %v", hostConfig.Binds)
   134  	} else if len(config.Volumes) != 0 {
   135  		t.Fatalf("Error parsing volume flags, without volume, no volume should be present. Received %v", config.Volumes)
   136  	}
   137  
   138  	if _, _, err := parse(t, "-v /"); err == nil {
   139  		t.Fatalf("Expected error, but got none")
   140  	}
   141  
   142  	if _, _, err := parse(t, "-v /:/"); err == nil {
   143  		t.Fatalf("Error parsing volume flags, `-v /:/` should fail but didn't")
   144  	}
   145  	if _, _, err := parse(t, "-v"); err == nil {
   146  		t.Fatalf("Error parsing volume flags, `-v` should fail but didn't")
   147  	}
   148  	if _, _, err := parse(t, "-v /tmp:"); err == nil {
   149  		t.Fatalf("Error parsing volume flags, `-v /tmp:` should fail but didn't")
   150  	}
   151  	if _, _, err := parse(t, "-v /tmp:ro"); err == nil {
   152  		t.Fatalf("Error parsing volume flags, `-v /tmp:ro` should fail but didn't")
   153  	}
   154  	if _, _, err := parse(t, "-v /tmp::"); err == nil {
   155  		t.Fatalf("Error parsing volume flags, `-v /tmp::` should fail but didn't")
   156  	}
   157  	if _, _, err := parse(t, "-v :"); err == nil {
   158  		t.Fatalf("Error parsing volume flags, `-v :` should fail but didn't")
   159  	}
   160  	if _, _, err := parse(t, "-v ::"); err == nil {
   161  		t.Fatalf("Error parsing volume flags, `-v ::` should fail but didn't")
   162  	}
   163  	if _, _, err := parse(t, "-v /tmp:/tmp:/tmp:/tmp"); err == nil {
   164  		t.Fatalf("Error parsing volume flags, `-v /tmp:/tmp:/tmp:/tmp` should fail but didn't")
   165  	}
   166  }
   167  
   168  func TestCompare(t *testing.T) {
   169  	volumes1 := make(map[string]struct{})
   170  	volumes1["/test1"] = struct{}{}
   171  	config1 := Config{
   172  		PortSpecs: []string{"1111:1111", "2222:2222"},
   173  		Env:       []string{"VAR1=1", "VAR2=2"},
   174  		Volumes:   volumes1,
   175  	}
   176  	config3 := Config{
   177  		PortSpecs: []string{"0000:0000", "2222:2222"},
   178  		Env:       []string{"VAR1=1", "VAR2=2"},
   179  		Volumes:   volumes1,
   180  	}
   181  	volumes2 := make(map[string]struct{})
   182  	volumes2["/test2"] = struct{}{}
   183  	config5 := Config{
   184  		PortSpecs: []string{"0000:0000", "2222:2222"},
   185  		Env:       []string{"VAR1=1", "VAR2=2"},
   186  		Volumes:   volumes2,
   187  	}
   188  	if Compare(&config1, &config3) {
   189  		t.Fatalf("Compare should return false, PortSpecs are different")
   190  	}
   191  	if Compare(&config1, &config5) {
   192  		t.Fatalf("Compare should return false, Volumes are different")
   193  	}
   194  	if !Compare(&config1, &config1) {
   195  		t.Fatalf("Compare should return true")
   196  	}
   197  }
   198  
   199  func TestMerge(t *testing.T) {
   200  	volumesImage := make(map[string]struct{})
   201  	volumesImage["/test1"] = struct{}{}
   202  	volumesImage["/test2"] = struct{}{}
   203  	configImage := &Config{
   204  		PortSpecs: []string{"1111:1111", "2222:2222"},
   205  		Env:       []string{"VAR1=1", "VAR2=2"},
   206  		Volumes:   volumesImage,
   207  	}
   208  
   209  	volumesUser := make(map[string]struct{})
   210  	volumesUser["/test3"] = struct{}{}
   211  	configUser := &Config{
   212  		PortSpecs: []string{"3333:2222", "3333:3333"},
   213  		Env:       []string{"VAR2=3", "VAR3=3"},
   214  		Volumes:   volumesUser,
   215  	}
   216  
   217  	if err := Merge(configUser, configImage); err != nil {
   218  		t.Error(err)
   219  	}
   220  
   221  	if len(configUser.ExposedPorts) != 3 {
   222  		t.Fatalf("Expected 3 ExposedPorts, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
   223  	}
   224  	for portSpecs := range configUser.ExposedPorts {
   225  		if portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
   226  			t.Fatalf("Expected 1111 or 2222 or 3333, found %s", portSpecs)
   227  		}
   228  	}
   229  	if len(configUser.Env) != 3 {
   230  		t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
   231  	}
   232  	for _, env := range configUser.Env {
   233  		if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
   234  			t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
   235  		}
   236  	}
   237  
   238  	if len(configUser.Volumes) != 3 {
   239  		t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
   240  	}
   241  	for v := range configUser.Volumes {
   242  		if v != "/test1" && v != "/test2" && v != "/test3" {
   243  			t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
   244  		}
   245  	}
   246  
   247  	ports, _, err := nat.ParsePortSpecs([]string{"0000"})
   248  	if err != nil {
   249  		t.Error(err)
   250  	}
   251  	configImage2 := &Config{
   252  		ExposedPorts: ports,
   253  	}
   254  
   255  	if err := Merge(configUser, configImage2); err != nil {
   256  		t.Error(err)
   257  	}
   258  
   259  	if len(configUser.ExposedPorts) != 4 {
   260  		t.Fatalf("Expected 4 ExposedPorts, 0000, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
   261  	}
   262  	for portSpecs := range configUser.ExposedPorts {
   263  		if portSpecs.Port() != "0" && portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
   264  			t.Fatalf("Expected %q or %q or %q or %q, found %s", 0, 1111, 2222, 3333, portSpecs)
   265  		}
   266  	}
   267  }
   268  
   269  func TestDecodeContainerConfig(t *testing.T) {
   270  	fixtures := []struct {
   271  		file       string
   272  		entrypoint *Entrypoint
   273  	}{
   274  		{"fixtures/container_config_1_14.json", NewEntrypoint()},
   275  		{"fixtures/container_config_1_17.json", NewEntrypoint("bash")},
   276  		{"fixtures/container_config_1_19.json", NewEntrypoint("bash")},
   277  	}
   278  
   279  	for _, f := range fixtures {
   280  		b, err := ioutil.ReadFile(f.file)
   281  		if err != nil {
   282  			t.Fatal(err)
   283  		}
   284  
   285  		c, h, err := DecodeContainerConfig(bytes.NewReader(b))
   286  		if err != nil {
   287  			t.Fatal(fmt.Errorf("Error parsing %s: %v", f, err))
   288  		}
   289  
   290  		if c.Image != "ubuntu" {
   291  			t.Fatalf("Expected ubuntu image, found %s\n", c.Image)
   292  		}
   293  
   294  		if c.Entrypoint.Len() != f.entrypoint.Len() {
   295  			t.Fatalf("Expected %v, found %v\n", f.entrypoint, c.Entrypoint)
   296  		}
   297  
   298  		if h.Memory != 1000 {
   299  			t.Fatalf("Expected memory to be 1000, found %d\n", h.Memory)
   300  		}
   301  	}
   302  }
   303  
   304  func TestEntrypointUnmarshalString(t *testing.T) {
   305  	var e *Entrypoint
   306  	echo, err := json.Marshal("echo")
   307  	if err != nil {
   308  		t.Fatal(err)
   309  	}
   310  	if err := json.Unmarshal(echo, &e); err != nil {
   311  		t.Fatal(err)
   312  	}
   313  
   314  	slice := e.Slice()
   315  	if len(slice) != 1 {
   316  		t.Fatalf("expected 1 element after unmarshal: %q", slice)
   317  	}
   318  
   319  	if slice[0] != "echo" {
   320  		t.Fatalf("expected `echo`, got: %q", slice[0])
   321  	}
   322  }
   323  
   324  func TestEntrypointUnmarshalSlice(t *testing.T) {
   325  	var e *Entrypoint
   326  	echo, err := json.Marshal([]string{"echo"})
   327  	if err != nil {
   328  		t.Fatal(err)
   329  	}
   330  	if err := json.Unmarshal(echo, &e); err != nil {
   331  		t.Fatal(err)
   332  	}
   333  
   334  	slice := e.Slice()
   335  	if len(slice) != 1 {
   336  		t.Fatalf("expected 1 element after unmarshal: %q", slice)
   337  	}
   338  
   339  	if slice[0] != "echo" {
   340  		t.Fatalf("expected `echo`, got: %q", slice[0])
   341  	}
   342  }
   343  
   344  func TestCommandUnmarshalSlice(t *testing.T) {
   345  	var e *Command
   346  	echo, err := json.Marshal([]string{"echo"})
   347  	if err != nil {
   348  		t.Fatal(err)
   349  	}
   350  	if err := json.Unmarshal(echo, &e); err != nil {
   351  		t.Fatal(err)
   352  	}
   353  
   354  	slice := e.Slice()
   355  	if len(slice) != 1 {
   356  		t.Fatalf("expected 1 element after unmarshal: %q", slice)
   357  	}
   358  
   359  	if slice[0] != "echo" {
   360  		t.Fatalf("expected `echo`, got: %q", slice[0])
   361  	}
   362  }
   363  
   364  func TestCommandUnmarshalString(t *testing.T) {
   365  	var e *Command
   366  	echo, err := json.Marshal("echo")
   367  	if err != nil {
   368  		t.Fatal(err)
   369  	}
   370  	if err := json.Unmarshal(echo, &e); err != nil {
   371  		t.Fatal(err)
   372  	}
   373  
   374  	slice := e.Slice()
   375  	if len(slice) != 1 {
   376  		t.Fatalf("expected 1 element after unmarshal: %q", slice)
   377  	}
   378  
   379  	if slice[0] != "echo" {
   380  		t.Fatalf("expected `echo`, got: %q", slice[0])
   381  	}
   382  }