github.com/dinever/docker@v1.11.1/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  	"github.com/docker/engine-api/types/container"
    12  )
    13  
    14  // TODO Windows: This will need addressing for a Windows daemon.
    15  func TestNetworkModeTest(t *testing.T) {
    16  	networkModes := map[container.NetworkMode][]bool{
    17  		// private, bridge, host, container, none, default
    18  		"":                         {true, false, false, false, false, false},
    19  		"something:weird":          {true, false, false, false, false, false},
    20  		"bridge":                   {true, true, false, false, false, false},
    21  		DefaultDaemonNetworkMode(): {true, true, false, false, false, false},
    22  		"host":           {false, false, true, false, false, false},
    23  		"container:name": {false, false, false, true, false, false},
    24  		"none":           {true, false, false, false, true, false},
    25  		"default":        {true, false, false, false, false, true},
    26  	}
    27  	networkModeNames := map[container.NetworkMode]string{
    28  		"":                         "",
    29  		"something:weird":          "something:weird",
    30  		"bridge":                   "bridge",
    31  		DefaultDaemonNetworkMode(): "bridge",
    32  		"host":           "host",
    33  		"container:name": "container",
    34  		"none":           "none",
    35  		"default":        "default",
    36  	}
    37  	for networkMode, state := range networkModes {
    38  		if networkMode.IsPrivate() != state[0] {
    39  			t.Fatalf("NetworkMode.IsPrivate for %v should have been %v but was %v", networkMode, state[0], networkMode.IsPrivate())
    40  		}
    41  		if networkMode.IsBridge() != state[1] {
    42  			t.Fatalf("NetworkMode.IsBridge for %v should have been %v but was %v", networkMode, state[1], networkMode.IsBridge())
    43  		}
    44  		if networkMode.IsHost() != state[2] {
    45  			t.Fatalf("NetworkMode.IsHost for %v should have been %v but was %v", networkMode, state[2], networkMode.IsHost())
    46  		}
    47  		if networkMode.IsContainer() != state[3] {
    48  			t.Fatalf("NetworkMode.IsContainer for %v should have been %v but was %v", networkMode, state[3], networkMode.IsContainer())
    49  		}
    50  		if networkMode.IsNone() != state[4] {
    51  			t.Fatalf("NetworkMode.IsNone for %v should have been %v but was %v", networkMode, state[4], networkMode.IsNone())
    52  		}
    53  		if networkMode.IsDefault() != state[5] {
    54  			t.Fatalf("NetworkMode.IsDefault for %v should have been %v but was %v", networkMode, state[5], networkMode.IsDefault())
    55  		}
    56  		if networkMode.NetworkName() != networkModeNames[networkMode] {
    57  			t.Fatalf("Expected name %v, got %v", networkModeNames[networkMode], networkMode.NetworkName())
    58  		}
    59  	}
    60  }
    61  
    62  func TestIpcModeTest(t *testing.T) {
    63  	ipcModes := map[container.IpcMode][]bool{
    64  		// private, host, container, valid
    65  		"":                         {true, false, false, true},
    66  		"something:weird":          {true, false, false, false},
    67  		":weird":                   {true, false, false, true},
    68  		"host":                     {false, true, false, true},
    69  		"container:name":           {false, false, true, true},
    70  		"container:name:something": {false, false, true, false},
    71  		"container:":               {false, false, true, false},
    72  	}
    73  	for ipcMode, state := range ipcModes {
    74  		if ipcMode.IsPrivate() != state[0] {
    75  			t.Fatalf("IpcMode.IsPrivate for %v should have been %v but was %v", ipcMode, state[0], ipcMode.IsPrivate())
    76  		}
    77  		if ipcMode.IsHost() != state[1] {
    78  			t.Fatalf("IpcMode.IsHost for %v should have been %v but was %v", ipcMode, state[1], ipcMode.IsHost())
    79  		}
    80  		if ipcMode.IsContainer() != state[2] {
    81  			t.Fatalf("IpcMode.IsContainer for %v should have been %v but was %v", ipcMode, state[2], ipcMode.IsContainer())
    82  		}
    83  		if ipcMode.Valid() != state[3] {
    84  			t.Fatalf("IpcMode.Valid for %v should have been %v but was %v", ipcMode, state[3], ipcMode.Valid())
    85  		}
    86  	}
    87  	containerIpcModes := map[container.IpcMode]string{
    88  		"":                      "",
    89  		"something":             "",
    90  		"something:weird":       "weird",
    91  		"container":             "",
    92  		"container:":            "",
    93  		"container:name":        "name",
    94  		"container:name1:name2": "name1:name2",
    95  	}
    96  	for ipcMode, container := range containerIpcModes {
    97  		if ipcMode.Container() != container {
    98  			t.Fatalf("Expected %v for %v but was %v", container, ipcMode, ipcMode.Container())
    99  		}
   100  	}
   101  }
   102  
   103  func TestUTSModeTest(t *testing.T) {
   104  	utsModes := map[container.UTSMode][]bool{
   105  		// private, host, valid
   106  		"":                {true, false, true},
   107  		"something:weird": {true, false, false},
   108  		"host":            {false, true, true},
   109  		"host:name":       {true, false, true},
   110  	}
   111  	for utsMode, state := range utsModes {
   112  		if utsMode.IsPrivate() != state[0] {
   113  			t.Fatalf("UtsMode.IsPrivate for %v should have been %v but was %v", utsMode, state[0], utsMode.IsPrivate())
   114  		}
   115  		if utsMode.IsHost() != state[1] {
   116  			t.Fatalf("UtsMode.IsHost for %v should have been %v but was %v", utsMode, state[1], utsMode.IsHost())
   117  		}
   118  		if utsMode.Valid() != state[2] {
   119  			t.Fatalf("UtsMode.Valid for %v should have been %v but was %v", utsMode, state[2], utsMode.Valid())
   120  		}
   121  	}
   122  }
   123  
   124  func TestUsernsModeTest(t *testing.T) {
   125  	usrensMode := map[container.UsernsMode][]bool{
   126  		// private, host, valid
   127  		"":                {true, false, true},
   128  		"something:weird": {true, false, false},
   129  		"host":            {false, true, true},
   130  		"host:name":       {true, false, true},
   131  	}
   132  	for usernsMode, state := range usrensMode {
   133  		if usernsMode.IsPrivate() != state[0] {
   134  			t.Fatalf("UsernsMode.IsPrivate for %v should have been %v but was %v", usernsMode, state[0], usernsMode.IsPrivate())
   135  		}
   136  		if usernsMode.IsHost() != state[1] {
   137  			t.Fatalf("UsernsMode.IsHost for %v should have been %v but was %v", usernsMode, state[1], usernsMode.IsHost())
   138  		}
   139  		if usernsMode.Valid() != state[2] {
   140  			t.Fatalf("UsernsMode.Valid for %v should have been %v but was %v", usernsMode, state[2], usernsMode.Valid())
   141  		}
   142  	}
   143  }
   144  
   145  func TestPidModeTest(t *testing.T) {
   146  	pidModes := map[container.PidMode][]bool{
   147  		// private, host, valid
   148  		"":                {true, false, true},
   149  		"something:weird": {true, false, false},
   150  		"host":            {false, true, true},
   151  		"host:name":       {true, false, true},
   152  	}
   153  	for pidMode, state := range pidModes {
   154  		if pidMode.IsPrivate() != state[0] {
   155  			t.Fatalf("PidMode.IsPrivate for %v should have been %v but was %v", pidMode, state[0], pidMode.IsPrivate())
   156  		}
   157  		if pidMode.IsHost() != state[1] {
   158  			t.Fatalf("PidMode.IsHost for %v should have been %v but was %v", pidMode, state[1], pidMode.IsHost())
   159  		}
   160  		if pidMode.Valid() != state[2] {
   161  			t.Fatalf("PidMode.Valid for %v should have been %v but was %v", pidMode, state[2], pidMode.Valid())
   162  		}
   163  	}
   164  }
   165  
   166  func TestRestartPolicy(t *testing.T) {
   167  	restartPolicies := map[container.RestartPolicy][]bool{
   168  		// none, always, failure
   169  		container.RestartPolicy{}:                {false, false, false},
   170  		container.RestartPolicy{"something", 0}:  {false, false, false},
   171  		container.RestartPolicy{"no", 0}:         {true, false, false},
   172  		container.RestartPolicy{"always", 0}:     {false, true, false},
   173  		container.RestartPolicy{"on-failure", 0}: {false, false, true},
   174  	}
   175  	for restartPolicy, state := range restartPolicies {
   176  		if restartPolicy.IsNone() != state[0] {
   177  			t.Fatalf("RestartPolicy.IsNone for %v should have been %v but was %v", restartPolicy, state[0], restartPolicy.IsNone())
   178  		}
   179  		if restartPolicy.IsAlways() != state[1] {
   180  			t.Fatalf("RestartPolicy.IsAlways for %v should have been %v but was %v", restartPolicy, state[1], restartPolicy.IsAlways())
   181  		}
   182  		if restartPolicy.IsOnFailure() != state[2] {
   183  			t.Fatalf("RestartPolicy.IsOnFailure for %v should have been %v but was %v", restartPolicy, state[2], restartPolicy.IsOnFailure())
   184  		}
   185  	}
   186  }
   187  func TestDecodeHostConfig(t *testing.T) {
   188  	fixtures := []struct {
   189  		file string
   190  	}{
   191  		{"fixtures/unix/container_hostconfig_1_14.json"},
   192  		{"fixtures/unix/container_hostconfig_1_19.json"},
   193  	}
   194  
   195  	for _, f := range fixtures {
   196  		b, err := ioutil.ReadFile(f.file)
   197  		if err != nil {
   198  			t.Fatal(err)
   199  		}
   200  
   201  		c, err := DecodeHostConfig(bytes.NewReader(b))
   202  		if err != nil {
   203  			t.Fatal(fmt.Errorf("Error parsing %s: %v", f, err))
   204  		}
   205  
   206  		if c.Privileged != false {
   207  			t.Fatalf("Expected privileged false, found %v\n", c.Privileged)
   208  		}
   209  
   210  		if l := len(c.Binds); l != 1 {
   211  			t.Fatalf("Expected 1 bind, found %d\n", l)
   212  		}
   213  
   214  		if len(c.CapAdd) != 1 && c.CapAdd[0] != "NET_ADMIN" {
   215  			t.Fatalf("Expected CapAdd NET_ADMIN, got %v", c.CapAdd)
   216  		}
   217  
   218  		if len(c.CapDrop) != 1 && c.CapDrop[0] != "NET_ADMIN" {
   219  			t.Fatalf("Expected CapDrop MKNOD, got %v", c.CapDrop)
   220  		}
   221  	}
   222  }