github.com/goern/docker@v1.9.0-rc1/runconfig/hostconfig_test.go (about)

     1  // +build !windows
     2  
     3  package runconfig
     4  
     5  import (
     6  	"bytes"
     7  	"fmt"
     8  	"io/ioutil"
     9  	"testing"
    10  )
    11  
    12  // TODO Windows: This will need addressing for a Windows daemon.
    13  func TestNetworkModeTest(t *testing.T) {
    14  	networkModes := map[NetworkMode][]bool{
    15  		// private, bridge, host, container, none, default
    16  		"":                         {true, false, false, false, false, false},
    17  		"something:weird":          {true, false, false, false, false, false},
    18  		"bridge":                   {true, true, false, false, false, false},
    19  		DefaultDaemonNetworkMode(): {true, true, false, false, false, false},
    20  		"host":           {false, false, true, false, false, false},
    21  		"container:name": {false, false, false, true, false, false},
    22  		"none":           {true, false, false, false, true, false},
    23  		"default":        {true, false, false, false, false, true},
    24  	}
    25  	networkModeNames := map[NetworkMode]string{
    26  		"":                         "",
    27  		"something:weird":          "something:weird",
    28  		"bridge":                   "bridge",
    29  		DefaultDaemonNetworkMode(): "bridge",
    30  		"host":           "host",
    31  		"container:name": "container",
    32  		"none":           "none",
    33  		"default":        "default",
    34  	}
    35  	for networkMode, state := range networkModes {
    36  		if networkMode.IsPrivate() != state[0] {
    37  			t.Fatalf("NetworkMode.IsPrivate for %v should have been %v but was %v", networkMode, state[0], networkMode.IsPrivate())
    38  		}
    39  		if networkMode.IsBridge() != state[1] {
    40  			t.Fatalf("NetworkMode.IsBridge for %v should have been %v but was %v", networkMode, state[1], networkMode.IsBridge())
    41  		}
    42  		if networkMode.IsHost() != state[2] {
    43  			t.Fatalf("NetworkMode.IsHost for %v should have been %v but was %v", networkMode, state[2], networkMode.IsHost())
    44  		}
    45  		if networkMode.IsContainer() != state[3] {
    46  			t.Fatalf("NetworkMode.IsContainer for %v should have been %v but was %v", networkMode, state[3], networkMode.IsContainer())
    47  		}
    48  		if networkMode.IsNone() != state[4] {
    49  			t.Fatalf("NetworkMode.IsNone for %v should have been %v but was %v", networkMode, state[4], networkMode.IsNone())
    50  		}
    51  		if networkMode.IsDefault() != state[5] {
    52  			t.Fatalf("NetworkMode.IsDefault for %v should have been %v but was %v", networkMode, state[5], networkMode.IsDefault())
    53  		}
    54  		if networkMode.NetworkName() != networkModeNames[networkMode] {
    55  			t.Fatalf("Expected name %v, got %v", networkModeNames[networkMode], networkMode.NetworkName())
    56  		}
    57  	}
    58  }
    59  
    60  func TestIpcModeTest(t *testing.T) {
    61  	ipcModes := map[IpcMode][]bool{
    62  		// private, host, container, valid
    63  		"":                         {true, false, false, true},
    64  		"something:weird":          {true, false, false, false},
    65  		":weird":                   {true, false, false, true},
    66  		"host":                     {false, true, false, true},
    67  		"container:name":           {false, false, true, true},
    68  		"container:name:something": {false, false, true, false},
    69  		"container:":               {false, false, true, false},
    70  	}
    71  	for ipcMode, state := range ipcModes {
    72  		if ipcMode.IsPrivate() != state[0] {
    73  			t.Fatalf("IpcMode.IsPrivate for %v should have been %v but was %v", ipcMode, state[0], ipcMode.IsPrivate())
    74  		}
    75  		if ipcMode.IsHost() != state[1] {
    76  			t.Fatalf("IpcMode.IsHost for %v should have been %v but was %v", ipcMode, state[1], ipcMode.IsHost())
    77  		}
    78  		if ipcMode.IsContainer() != state[2] {
    79  			t.Fatalf("IpcMode.IsContainer for %v should have been %v but was %v", ipcMode, state[2], ipcMode.IsContainer())
    80  		}
    81  		if ipcMode.Valid() != state[3] {
    82  			t.Fatalf("IpcMode.Valid for %v should have been %v but was %v", ipcMode, state[3], ipcMode.Valid())
    83  		}
    84  	}
    85  	containerIpcModes := map[IpcMode]string{
    86  		"":                      "",
    87  		"something":             "",
    88  		"something:weird":       "weird",
    89  		"container":             "",
    90  		"container:":            "",
    91  		"container:name":        "name",
    92  		"container:name1:name2": "name1:name2",
    93  	}
    94  	for ipcMode, container := range containerIpcModes {
    95  		if ipcMode.Container() != container {
    96  			t.Fatalf("Expected %v for %v but was %v", container, ipcMode, ipcMode.Container())
    97  		}
    98  	}
    99  }
   100  
   101  func TestUTSModeTest(t *testing.T) {
   102  	utsModes := map[UTSMode][]bool{
   103  		// private, host, valid
   104  		"":                {true, false, true},
   105  		"something:weird": {true, false, false},
   106  		"host":            {false, true, true},
   107  		"host:name":       {true, false, true},
   108  	}
   109  	for utsMode, state := range utsModes {
   110  		if utsMode.IsPrivate() != state[0] {
   111  			t.Fatalf("UtsMode.IsPrivate for %v should have been %v but was %v", utsMode, state[0], utsMode.IsPrivate())
   112  		}
   113  		if utsMode.IsHost() != state[1] {
   114  			t.Fatalf("UtsMode.IsHost for %v should have been %v but was %v", utsMode, state[1], utsMode.IsHost())
   115  		}
   116  		if utsMode.Valid() != state[2] {
   117  			t.Fatalf("UtsMode.Valid for %v should have been %v but was %v", utsMode, state[2], utsMode.Valid())
   118  		}
   119  	}
   120  }
   121  
   122  func TestPidModeTest(t *testing.T) {
   123  	pidModes := map[PidMode][]bool{
   124  		// private, host, valid
   125  		"":                {true, false, true},
   126  		"something:weird": {true, false, false},
   127  		"host":            {false, true, true},
   128  		"host:name":       {true, false, true},
   129  	}
   130  	for pidMode, state := range pidModes {
   131  		if pidMode.IsPrivate() != state[0] {
   132  			t.Fatalf("PidMode.IsPrivate for %v should have been %v but was %v", pidMode, state[0], pidMode.IsPrivate())
   133  		}
   134  		if pidMode.IsHost() != state[1] {
   135  			t.Fatalf("PidMode.IsHost for %v should have been %v but was %v", pidMode, state[1], pidMode.IsHost())
   136  		}
   137  		if pidMode.Valid() != state[2] {
   138  			t.Fatalf("PidMode.Valid for %v should have been %v but was %v", pidMode, state[2], pidMode.Valid())
   139  		}
   140  	}
   141  }
   142  
   143  func TestRestartPolicy(t *testing.T) {
   144  	restartPolicies := map[RestartPolicy][]bool{
   145  		// none, always, failure
   146  		RestartPolicy{}:                {false, false, false},
   147  		RestartPolicy{"something", 0}:  {false, false, false},
   148  		RestartPolicy{"no", 0}:         {true, false, false},
   149  		RestartPolicy{"always", 0}:     {false, true, false},
   150  		RestartPolicy{"on-failure", 0}: {false, false, true},
   151  	}
   152  	for restartPolicy, state := range restartPolicies {
   153  		if restartPolicy.IsNone() != state[0] {
   154  			t.Fatalf("RestartPolicy.IsNone for %v should have been %v but was %v", restartPolicy, state[0], restartPolicy.IsNone())
   155  		}
   156  		if restartPolicy.IsAlways() != state[1] {
   157  			t.Fatalf("RestartPolicy.IsAlways for %v should have been %v but was %v", restartPolicy, state[1], restartPolicy.IsAlways())
   158  		}
   159  		if restartPolicy.IsOnFailure() != state[2] {
   160  			t.Fatalf("RestartPolicy.IsOnFailure for %v should have been %v but was %v", restartPolicy, state[2], restartPolicy.IsOnFailure())
   161  		}
   162  	}
   163  }
   164  
   165  func TestLxcConfigMarshalJSON(t *testing.T) {
   166  	lxcConfigs := map[*LxcConfig]string{
   167  		nil:          "",
   168  		&LxcConfig{}: "null",
   169  		&LxcConfig{
   170  			[]KeyValuePair{{"key1", "value1"}},
   171  		}: `[{"Key":"key1","Value":"value1"}]`,
   172  	}
   173  
   174  	for lxcconfig, expected := range lxcConfigs {
   175  		data, err := lxcconfig.MarshalJSON()
   176  		if err != nil {
   177  			t.Fatal(err)
   178  		}
   179  		if string(data) != expected {
   180  			t.Fatalf("Expected %v, got %v", expected, string(data))
   181  		}
   182  	}
   183  }
   184  
   185  func TestLxcConfigUnmarshalJSON(t *testing.T) {
   186  	keyvaluePairs := map[string][]KeyValuePair{
   187  		"":   {{"key1", "value1"}},
   188  		"[]": {},
   189  		`[{"Key":"key2","Value":"value2"}]`: {{"key2", "value2"}},
   190  	}
   191  	for json, expectedParts := range keyvaluePairs {
   192  		lxcConfig := &LxcConfig{
   193  			[]KeyValuePair{{"key1", "value1"}},
   194  		}
   195  		if err := lxcConfig.UnmarshalJSON([]byte(json)); err != nil {
   196  			t.Fatal(err)
   197  		}
   198  
   199  		actualParts := lxcConfig.Slice()
   200  		if len(actualParts) != len(expectedParts) {
   201  			t.Fatalf("Expected %v keyvaluePairs, got %v (%v)", len(expectedParts), len(actualParts), expectedParts)
   202  		}
   203  		for index, part := range actualParts {
   204  			if part != expectedParts[index] {
   205  				t.Fatalf("Expected %v, got %v", expectedParts, actualParts)
   206  				break
   207  			}
   208  		}
   209  	}
   210  }
   211  
   212  func TestMergeConfigs(t *testing.T) {
   213  	expectedHostname := "hostname"
   214  	expectedContainerIDFile := "containerIdFile"
   215  	config := &Config{
   216  		Hostname: expectedHostname,
   217  	}
   218  	hostConfig := &HostConfig{
   219  		ContainerIDFile: expectedContainerIDFile,
   220  	}
   221  	containerConfigWrapper := MergeConfigs(config, hostConfig)
   222  	if containerConfigWrapper.Config.Hostname != expectedHostname {
   223  		t.Fatalf("containerConfigWrapper config hostname expected %v got %v", expectedHostname, containerConfigWrapper.Config.Hostname)
   224  	}
   225  	if containerConfigWrapper.InnerHostConfig.ContainerIDFile != expectedContainerIDFile {
   226  		t.Fatalf("containerConfigWrapper hostconfig containerIdfile expected %v got %v", expectedContainerIDFile, containerConfigWrapper.InnerHostConfig.ContainerIDFile)
   227  	}
   228  	if containerConfigWrapper.Cpuset != "" {
   229  		t.Fatalf("Expected empty Cpuset, got %v", containerConfigWrapper.Cpuset)
   230  	}
   231  }
   232  
   233  func TestDecodeHostConfig(t *testing.T) {
   234  	fixtures := []struct {
   235  		file string
   236  	}{
   237  		{"fixtures/container_hostconfig_1_14.json"},
   238  		{"fixtures/container_hostconfig_1_19.json"},
   239  	}
   240  
   241  	for _, f := range fixtures {
   242  		b, err := ioutil.ReadFile(f.file)
   243  		if err != nil {
   244  			t.Fatal(err)
   245  		}
   246  
   247  		c, err := DecodeHostConfig(bytes.NewReader(b))
   248  		if err != nil {
   249  			t.Fatal(fmt.Errorf("Error parsing %s: %v", f, err))
   250  		}
   251  
   252  		if c.Privileged != false {
   253  			t.Fatalf("Expected privileged false, found %v\n", c.Privileged)
   254  		}
   255  
   256  		if l := len(c.Binds); l != 1 {
   257  			t.Fatalf("Expected 1 bind, found %d\n", l)
   258  		}
   259  
   260  		if c.CapAdd.Len() != 1 && c.CapAdd.Slice()[0] != "NET_ADMIN" {
   261  			t.Fatalf("Expected CapAdd NET_ADMIN, got %v", c.CapAdd)
   262  		}
   263  
   264  		if c.CapDrop.Len() != 1 && c.CapDrop.Slice()[0] != "NET_ADMIN" {
   265  			t.Fatalf("Expected CapDrop MKNOD, got %v", c.CapDrop)
   266  		}
   267  	}
   268  }