github.com/Shopify/docker@v1.13.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/docker/api/types/container" 12 "github.com/docker/docker/pkg/sysinfo" 13 ) 14 15 // TODO Windows: This will need addressing for a Windows daemon. 16 func TestNetworkModeTest(t *testing.T) { 17 networkModes := map[container.NetworkMode][]bool{ 18 // private, bridge, host, container, none, default 19 "": {true, false, false, false, false, false}, 20 "something:weird": {true, false, false, false, false, false}, 21 "bridge": {true, true, false, false, false, false}, 22 DefaultDaemonNetworkMode(): {true, true, false, false, false, false}, 23 "host": {false, false, true, false, false, false}, 24 "container:name": {false, false, false, true, false, false}, 25 "none": {true, false, false, false, true, false}, 26 "default": {true, false, false, false, false, true}, 27 } 28 networkModeNames := map[container.NetworkMode]string{ 29 "": "", 30 "something:weird": "something:weird", 31 "bridge": "bridge", 32 DefaultDaemonNetworkMode(): "bridge", 33 "host": "host", 34 "container:name": "container", 35 "none": "none", 36 "default": "default", 37 } 38 for networkMode, state := range networkModes { 39 if networkMode.IsPrivate() != state[0] { 40 t.Fatalf("NetworkMode.IsPrivate for %v should have been %v but was %v", networkMode, state[0], networkMode.IsPrivate()) 41 } 42 if networkMode.IsBridge() != state[1] { 43 t.Fatalf("NetworkMode.IsBridge for %v should have been %v but was %v", networkMode, state[1], networkMode.IsBridge()) 44 } 45 if networkMode.IsHost() != state[2] { 46 t.Fatalf("NetworkMode.IsHost for %v should have been %v but was %v", networkMode, state[2], networkMode.IsHost()) 47 } 48 if networkMode.IsContainer() != state[3] { 49 t.Fatalf("NetworkMode.IsContainer for %v should have been %v but was %v", networkMode, state[3], networkMode.IsContainer()) 50 } 51 if networkMode.IsNone() != state[4] { 52 t.Fatalf("NetworkMode.IsNone for %v should have been %v but was %v", networkMode, state[4], networkMode.IsNone()) 53 } 54 if networkMode.IsDefault() != state[5] { 55 t.Fatalf("NetworkMode.IsDefault for %v should have been %v but was %v", networkMode, state[5], networkMode.IsDefault()) 56 } 57 if networkMode.NetworkName() != networkModeNames[networkMode] { 58 t.Fatalf("Expected name %v, got %v", networkModeNames[networkMode], networkMode.NetworkName()) 59 } 60 } 61 } 62 63 func TestIpcModeTest(t *testing.T) { 64 ipcModes := map[container.IpcMode][]bool{ 65 // private, host, container, valid 66 "": {true, false, false, true}, 67 "something:weird": {true, false, false, false}, 68 ":weird": {true, false, false, true}, 69 "host": {false, true, false, true}, 70 "container:name": {false, false, true, true}, 71 "container:name:something": {false, false, true, false}, 72 "container:": {false, false, true, false}, 73 } 74 for ipcMode, state := range ipcModes { 75 if ipcMode.IsPrivate() != state[0] { 76 t.Fatalf("IpcMode.IsPrivate for %v should have been %v but was %v", ipcMode, state[0], ipcMode.IsPrivate()) 77 } 78 if ipcMode.IsHost() != state[1] { 79 t.Fatalf("IpcMode.IsHost for %v should have been %v but was %v", ipcMode, state[1], ipcMode.IsHost()) 80 } 81 if ipcMode.IsContainer() != state[2] { 82 t.Fatalf("IpcMode.IsContainer for %v should have been %v but was %v", ipcMode, state[2], ipcMode.IsContainer()) 83 } 84 if ipcMode.Valid() != state[3] { 85 t.Fatalf("IpcMode.Valid for %v should have been %v but was %v", ipcMode, state[3], ipcMode.Valid()) 86 } 87 } 88 containerIpcModes := map[container.IpcMode]string{ 89 "": "", 90 "something": "", 91 "something:weird": "weird", 92 "container": "", 93 "container:": "", 94 "container:name": "name", 95 "container:name1:name2": "name1:name2", 96 } 97 for ipcMode, container := range containerIpcModes { 98 if ipcMode.Container() != container { 99 t.Fatalf("Expected %v for %v but was %v", container, ipcMode, ipcMode.Container()) 100 } 101 } 102 } 103 104 func TestUTSModeTest(t *testing.T) { 105 utsModes := map[container.UTSMode][]bool{ 106 // private, host, valid 107 "": {true, false, true}, 108 "something:weird": {true, false, false}, 109 "host": {false, true, true}, 110 "host:name": {true, false, true}, 111 } 112 for utsMode, state := range utsModes { 113 if utsMode.IsPrivate() != state[0] { 114 t.Fatalf("UtsMode.IsPrivate for %v should have been %v but was %v", utsMode, state[0], utsMode.IsPrivate()) 115 } 116 if utsMode.IsHost() != state[1] { 117 t.Fatalf("UtsMode.IsHost for %v should have been %v but was %v", utsMode, state[1], utsMode.IsHost()) 118 } 119 if utsMode.Valid() != state[2] { 120 t.Fatalf("UtsMode.Valid for %v should have been %v but was %v", utsMode, state[2], utsMode.Valid()) 121 } 122 } 123 } 124 125 func TestUsernsModeTest(t *testing.T) { 126 usrensMode := map[container.UsernsMode][]bool{ 127 // private, host, valid 128 "": {true, false, true}, 129 "something:weird": {true, false, false}, 130 "host": {false, true, true}, 131 "host:name": {true, false, true}, 132 } 133 for usernsMode, state := range usrensMode { 134 if usernsMode.IsPrivate() != state[0] { 135 t.Fatalf("UsernsMode.IsPrivate for %v should have been %v but was %v", usernsMode, state[0], usernsMode.IsPrivate()) 136 } 137 if usernsMode.IsHost() != state[1] { 138 t.Fatalf("UsernsMode.IsHost for %v should have been %v but was %v", usernsMode, state[1], usernsMode.IsHost()) 139 } 140 if usernsMode.Valid() != state[2] { 141 t.Fatalf("UsernsMode.Valid for %v should have been %v but was %v", usernsMode, state[2], usernsMode.Valid()) 142 } 143 } 144 } 145 146 func TestPidModeTest(t *testing.T) { 147 pidModes := map[container.PidMode][]bool{ 148 // private, host, valid 149 "": {true, false, true}, 150 "something:weird": {true, false, false}, 151 "host": {false, true, true}, 152 "host:name": {true, false, true}, 153 } 154 for pidMode, state := range pidModes { 155 if pidMode.IsPrivate() != state[0] { 156 t.Fatalf("PidMode.IsPrivate for %v should have been %v but was %v", pidMode, state[0], pidMode.IsPrivate()) 157 } 158 if pidMode.IsHost() != state[1] { 159 t.Fatalf("PidMode.IsHost for %v should have been %v but was %v", pidMode, state[1], pidMode.IsHost()) 160 } 161 if pidMode.Valid() != state[2] { 162 t.Fatalf("PidMode.Valid for %v should have been %v but was %v", pidMode, state[2], pidMode.Valid()) 163 } 164 } 165 } 166 167 func TestRestartPolicy(t *testing.T) { 168 restartPolicies := map[container.RestartPolicy][]bool{ 169 // none, always, failure 170 container.RestartPolicy{}: {true, false, false}, 171 container.RestartPolicy{"something", 0}: {false, false, false}, 172 container.RestartPolicy{"no", 0}: {true, false, false}, 173 container.RestartPolicy{"always", 0}: {false, true, false}, 174 container.RestartPolicy{"on-failure", 0}: {false, false, true}, 175 } 176 for restartPolicy, state := range restartPolicies { 177 if restartPolicy.IsNone() != state[0] { 178 t.Fatalf("RestartPolicy.IsNone for %v should have been %v but was %v", restartPolicy, state[0], restartPolicy.IsNone()) 179 } 180 if restartPolicy.IsAlways() != state[1] { 181 t.Fatalf("RestartPolicy.IsAlways for %v should have been %v but was %v", restartPolicy, state[1], restartPolicy.IsAlways()) 182 } 183 if restartPolicy.IsOnFailure() != state[2] { 184 t.Fatalf("RestartPolicy.IsOnFailure for %v should have been %v but was %v", restartPolicy, state[2], restartPolicy.IsOnFailure()) 185 } 186 } 187 } 188 func TestDecodeHostConfig(t *testing.T) { 189 fixtures := []struct { 190 file string 191 }{ 192 {"fixtures/unix/container_hostconfig_1_14.json"}, 193 {"fixtures/unix/container_hostconfig_1_19.json"}, 194 } 195 196 for _, f := range fixtures { 197 b, err := ioutil.ReadFile(f.file) 198 if err != nil { 199 t.Fatal(err) 200 } 201 202 c, err := DecodeHostConfig(bytes.NewReader(b)) 203 if err != nil { 204 t.Fatal(fmt.Errorf("Error parsing %s: %v", f, err)) 205 } 206 207 if c.Privileged != false { 208 t.Fatalf("Expected privileged false, found %v\n", c.Privileged) 209 } 210 211 if l := len(c.Binds); l != 1 { 212 t.Fatalf("Expected 1 bind, found %d\n", l) 213 } 214 215 if len(c.CapAdd) != 1 && c.CapAdd[0] != "NET_ADMIN" { 216 t.Fatalf("Expected CapAdd NET_ADMIN, got %v", c.CapAdd) 217 } 218 219 if len(c.CapDrop) != 1 && c.CapDrop[0] != "NET_ADMIN" { 220 t.Fatalf("Expected CapDrop MKNOD, got %v", c.CapDrop) 221 } 222 } 223 } 224 225 func TestValidateResources(t *testing.T) { 226 type resourceTest struct { 227 ConfigCPURealtimePeriod int64 228 ConfigCPURealtimeRuntime int64 229 SysInfoCPURealtimePeriod bool 230 SysInfoCPURealtimeRuntime bool 231 ErrorExpected bool 232 FailureMsg string 233 } 234 235 tests := []resourceTest{ 236 { 237 ConfigCPURealtimePeriod: 1000, 238 ConfigCPURealtimeRuntime: 1000, 239 SysInfoCPURealtimePeriod: true, 240 SysInfoCPURealtimeRuntime: true, 241 ErrorExpected: false, 242 FailureMsg: "Expected valid configuration", 243 }, 244 { 245 ConfigCPURealtimePeriod: 5000, 246 ConfigCPURealtimeRuntime: 5000, 247 SysInfoCPURealtimePeriod: false, 248 SysInfoCPURealtimeRuntime: true, 249 ErrorExpected: true, 250 FailureMsg: "Expected failure when cpu-rt-period is set but kernel doesn't support it", 251 }, 252 { 253 ConfigCPURealtimePeriod: 5000, 254 ConfigCPURealtimeRuntime: 5000, 255 SysInfoCPURealtimePeriod: true, 256 SysInfoCPURealtimeRuntime: false, 257 ErrorExpected: true, 258 FailureMsg: "Expected failure when cpu-rt-runtime is set but kernel doesn't support it", 259 }, 260 { 261 ConfigCPURealtimePeriod: 5000, 262 ConfigCPURealtimeRuntime: 10000, 263 SysInfoCPURealtimePeriod: true, 264 SysInfoCPURealtimeRuntime: false, 265 ErrorExpected: true, 266 FailureMsg: "Expected failure when cpu-rt-runtime is greater than cpu-rt-period", 267 }, 268 } 269 270 for _, rt := range tests { 271 var hc container.HostConfig 272 hc.Resources.CPURealtimePeriod = rt.ConfigCPURealtimePeriod 273 hc.Resources.CPURealtimeRuntime = rt.ConfigCPURealtimeRuntime 274 275 var si sysinfo.SysInfo 276 si.CPURealtimePeriod = rt.SysInfoCPURealtimePeriod 277 si.CPURealtimeRuntime = rt.SysInfoCPURealtimeRuntime 278 279 if err := ValidateResources(&hc, &si); (err != nil) != rt.ErrorExpected { 280 t.Fatal(rt.FailureMsg, err) 281 } 282 } 283 }