docker_cli_network_unix_test.go 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  1. // +build !windows
  2. package main
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "time"
  14. "github.com/docker/docker/api/types"
  15. "github.com/docker/docker/api/types/versions/v1p20"
  16. "github.com/docker/docker/integration-cli/checker"
  17. "github.com/docker/docker/integration-cli/daemon"
  18. "github.com/docker/docker/pkg/stringid"
  19. icmd "github.com/docker/docker/pkg/testutil/cmd"
  20. "github.com/docker/docker/runconfig"
  21. "github.com/docker/libnetwork/driverapi"
  22. remoteapi "github.com/docker/libnetwork/drivers/remote/api"
  23. "github.com/docker/libnetwork/ipamapi"
  24. remoteipam "github.com/docker/libnetwork/ipams/remote/api"
  25. "github.com/docker/libnetwork/netlabel"
  26. "github.com/go-check/check"
  27. "github.com/vishvananda/netlink"
  28. )
  29. const dummyNetworkDriver = "dummy-network-driver"
  30. const dummyIPAMDriver = "dummy-ipam-driver"
  31. var remoteDriverNetworkRequest remoteapi.CreateNetworkRequest
  32. func init() {
  33. check.Suite(&DockerNetworkSuite{
  34. ds: &DockerSuite{},
  35. })
  36. }
  37. type DockerNetworkSuite struct {
  38. server *httptest.Server
  39. ds *DockerSuite
  40. d *daemon.Daemon
  41. }
  42. func (s *DockerNetworkSuite) SetUpTest(c *check.C) {
  43. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  44. Experimental: testEnv.ExperimentalDaemon(),
  45. })
  46. }
  47. func (s *DockerNetworkSuite) TearDownTest(c *check.C) {
  48. if s.d != nil {
  49. s.d.Stop(c)
  50. s.ds.TearDownTest(c)
  51. }
  52. }
  53. func (s *DockerNetworkSuite) SetUpSuite(c *check.C) {
  54. mux := http.NewServeMux()
  55. s.server = httptest.NewServer(mux)
  56. c.Assert(s.server, check.NotNil, check.Commentf("Failed to start an HTTP Server"))
  57. setupRemoteNetworkDrivers(c, mux, s.server.URL, dummyNetworkDriver, dummyIPAMDriver)
  58. }
  59. func setupRemoteNetworkDrivers(c *check.C, mux *http.ServeMux, url, netDrv, ipamDrv string) {
  60. mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
  61. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  62. fmt.Fprintf(w, `{"Implements": ["%s", "%s"]}`, driverapi.NetworkPluginEndpointType, ipamapi.PluginEndpointType)
  63. })
  64. // Network driver implementation
  65. mux.HandleFunc(fmt.Sprintf("/%s.GetCapabilities", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  66. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  67. fmt.Fprintf(w, `{"Scope":"local"}`)
  68. })
  69. mux.HandleFunc(fmt.Sprintf("/%s.CreateNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  70. err := json.NewDecoder(r.Body).Decode(&remoteDriverNetworkRequest)
  71. if err != nil {
  72. http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
  73. return
  74. }
  75. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  76. fmt.Fprintf(w, "null")
  77. })
  78. mux.HandleFunc(fmt.Sprintf("/%s.DeleteNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  79. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  80. fmt.Fprintf(w, "null")
  81. })
  82. mux.HandleFunc(fmt.Sprintf("/%s.CreateEndpoint", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  83. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  84. fmt.Fprintf(w, `{"Interface":{"MacAddress":"a0:b1:c2:d3:e4:f5"}}`)
  85. })
  86. mux.HandleFunc(fmt.Sprintf("/%s.Join", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  87. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  88. veth := &netlink.Veth{
  89. LinkAttrs: netlink.LinkAttrs{Name: "randomIfName", TxQLen: 0}, PeerName: "cnt0"}
  90. if err := netlink.LinkAdd(veth); err != nil {
  91. fmt.Fprintf(w, `{"Error":"failed to add veth pair: `+err.Error()+`"}`)
  92. } else {
  93. fmt.Fprintf(w, `{"InterfaceName":{ "SrcName":"cnt0", "DstPrefix":"veth"}}`)
  94. }
  95. })
  96. mux.HandleFunc(fmt.Sprintf("/%s.Leave", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  97. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  98. fmt.Fprintf(w, "null")
  99. })
  100. mux.HandleFunc(fmt.Sprintf("/%s.DeleteEndpoint", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  101. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  102. if link, err := netlink.LinkByName("cnt0"); err == nil {
  103. netlink.LinkDel(link)
  104. }
  105. fmt.Fprintf(w, "null")
  106. })
  107. // IPAM Driver implementation
  108. var (
  109. poolRequest remoteipam.RequestPoolRequest
  110. poolReleaseReq remoteipam.ReleasePoolRequest
  111. addressRequest remoteipam.RequestAddressRequest
  112. addressReleaseReq remoteipam.ReleaseAddressRequest
  113. lAS = "localAS"
  114. gAS = "globalAS"
  115. pool = "172.28.0.0/16"
  116. poolID = lAS + "/" + pool
  117. gw = "172.28.255.254/16"
  118. )
  119. mux.HandleFunc(fmt.Sprintf("/%s.GetDefaultAddressSpaces", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  120. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  121. fmt.Fprintf(w, `{"LocalDefaultAddressSpace":"`+lAS+`", "GlobalDefaultAddressSpace": "`+gAS+`"}`)
  122. })
  123. mux.HandleFunc(fmt.Sprintf("/%s.RequestPool", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  124. err := json.NewDecoder(r.Body).Decode(&poolRequest)
  125. if err != nil {
  126. http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
  127. return
  128. }
  129. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  130. if poolRequest.AddressSpace != lAS && poolRequest.AddressSpace != gAS {
  131. fmt.Fprintf(w, `{"Error":"Unknown address space in pool request: `+poolRequest.AddressSpace+`"}`)
  132. } else if poolRequest.Pool != "" && poolRequest.Pool != pool {
  133. fmt.Fprintf(w, `{"Error":"Cannot handle explicit pool requests yet"}`)
  134. } else {
  135. fmt.Fprintf(w, `{"PoolID":"`+poolID+`", "Pool":"`+pool+`"}`)
  136. }
  137. })
  138. mux.HandleFunc(fmt.Sprintf("/%s.RequestAddress", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  139. err := json.NewDecoder(r.Body).Decode(&addressRequest)
  140. if err != nil {
  141. http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
  142. return
  143. }
  144. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  145. // make sure libnetwork is now querying on the expected pool id
  146. if addressRequest.PoolID != poolID {
  147. fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
  148. } else if addressRequest.Address != "" {
  149. fmt.Fprintf(w, `{"Error":"Cannot handle explicit address requests yet"}`)
  150. } else {
  151. fmt.Fprintf(w, `{"Address":"`+gw+`"}`)
  152. }
  153. })
  154. mux.HandleFunc(fmt.Sprintf("/%s.ReleaseAddress", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  155. err := json.NewDecoder(r.Body).Decode(&addressReleaseReq)
  156. if err != nil {
  157. http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
  158. return
  159. }
  160. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  161. // make sure libnetwork is now asking to release the expected address from the expected poolid
  162. if addressRequest.PoolID != poolID {
  163. fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
  164. } else if addressReleaseReq.Address != gw {
  165. fmt.Fprintf(w, `{"Error":"unknown address"}`)
  166. } else {
  167. fmt.Fprintf(w, "null")
  168. }
  169. })
  170. mux.HandleFunc(fmt.Sprintf("/%s.ReleasePool", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  171. err := json.NewDecoder(r.Body).Decode(&poolReleaseReq)
  172. if err != nil {
  173. http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
  174. return
  175. }
  176. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  177. // make sure libnetwork is now asking to release the expected poolid
  178. if addressRequest.PoolID != poolID {
  179. fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
  180. } else {
  181. fmt.Fprintf(w, "null")
  182. }
  183. })
  184. err := os.MkdirAll("/etc/docker/plugins", 0755)
  185. c.Assert(err, checker.IsNil)
  186. fileName := fmt.Sprintf("/etc/docker/plugins/%s.spec", netDrv)
  187. err = ioutil.WriteFile(fileName, []byte(url), 0644)
  188. c.Assert(err, checker.IsNil)
  189. ipamFileName := fmt.Sprintf("/etc/docker/plugins/%s.spec", ipamDrv)
  190. err = ioutil.WriteFile(ipamFileName, []byte(url), 0644)
  191. c.Assert(err, checker.IsNil)
  192. }
  193. func (s *DockerNetworkSuite) TearDownSuite(c *check.C) {
  194. if s.server == nil {
  195. return
  196. }
  197. s.server.Close()
  198. err := os.RemoveAll("/etc/docker/plugins")
  199. c.Assert(err, checker.IsNil)
  200. }
  201. func assertNwIsAvailable(c *check.C, name string) {
  202. if !isNwPresent(c, name) {
  203. c.Fatalf("Network %s not found in network ls o/p", name)
  204. }
  205. }
  206. func assertNwNotAvailable(c *check.C, name string) {
  207. if isNwPresent(c, name) {
  208. c.Fatalf("Found network %s in network ls o/p", name)
  209. }
  210. }
  211. func isNwPresent(c *check.C, name string) bool {
  212. out, _ := dockerCmd(c, "network", "ls")
  213. lines := strings.Split(out, "\n")
  214. for i := 1; i < len(lines)-1; i++ {
  215. netFields := strings.Fields(lines[i])
  216. if netFields[1] == name {
  217. return true
  218. }
  219. }
  220. return false
  221. }
  222. // assertNwList checks network list retrieved with ls command
  223. // equals to expected network list
  224. // note: out should be `network ls [option]` result
  225. func assertNwList(c *check.C, out string, expectNws []string) {
  226. lines := strings.Split(out, "\n")
  227. var nwList []string
  228. for _, line := range lines[1 : len(lines)-1] {
  229. netFields := strings.Fields(line)
  230. // wrap all network name in nwList
  231. nwList = append(nwList, netFields[1])
  232. }
  233. // network ls should contains all expected networks
  234. c.Assert(nwList, checker.DeepEquals, expectNws)
  235. }
  236. func getNwResource(c *check.C, name string) *types.NetworkResource {
  237. out, _ := dockerCmd(c, "network", "inspect", name)
  238. nr := []types.NetworkResource{}
  239. err := json.Unmarshal([]byte(out), &nr)
  240. c.Assert(err, check.IsNil)
  241. return &nr[0]
  242. }
  243. func (s *DockerNetworkSuite) TestDockerNetworkLsDefault(c *check.C) {
  244. defaults := []string{"bridge", "host", "none"}
  245. for _, nn := range defaults {
  246. assertNwIsAvailable(c, nn)
  247. }
  248. }
  249. func (s *DockerSuite) TestNetworkLsFormat(c *check.C) {
  250. testRequires(c, DaemonIsLinux)
  251. out, _ := dockerCmd(c, "network", "ls", "--format", "{{.Name}}")
  252. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  253. expected := []string{"bridge", "host", "none"}
  254. var names []string
  255. names = append(names, lines...)
  256. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with truncated names: %v, got: %v", expected, names))
  257. }
  258. func (s *DockerSuite) TestNetworkLsFormatDefaultFormat(c *check.C) {
  259. testRequires(c, DaemonIsLinux)
  260. config := `{
  261. "networksFormat": "{{ .Name }} default"
  262. }`
  263. d, err := ioutil.TempDir("", "integration-cli-")
  264. c.Assert(err, checker.IsNil)
  265. defer os.RemoveAll(d)
  266. err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
  267. c.Assert(err, checker.IsNil)
  268. out, _ := dockerCmd(c, "--config", d, "network", "ls")
  269. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  270. expected := []string{"bridge default", "host default", "none default"}
  271. var names []string
  272. names = append(names, lines...)
  273. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with truncated names: %v, got: %v", expected, names))
  274. }
  275. func (s *DockerNetworkSuite) TestDockerNetworkCreatePredefined(c *check.C) {
  276. predefined := []string{"bridge", "host", "none", "default"}
  277. for _, net := range predefined {
  278. // predefined networks can't be created again
  279. out, _, err := dockerCmdWithError("network", "create", net)
  280. c.Assert(err, checker.NotNil, check.Commentf("%v", out))
  281. }
  282. }
  283. func (s *DockerNetworkSuite) TestDockerNetworkCreateHostBind(c *check.C) {
  284. dockerCmd(c, "network", "create", "--subnet=192.168.10.0/24", "--gateway=192.168.10.1", "-o", "com.docker.network.bridge.host_binding_ipv4=192.168.10.1", "testbind")
  285. assertNwIsAvailable(c, "testbind")
  286. out, _ := runSleepingContainer(c, "--net=testbind", "-p", "5000:5000")
  287. id := strings.TrimSpace(out)
  288. c.Assert(waitRun(id), checker.IsNil)
  289. out, _ = dockerCmd(c, "ps")
  290. c.Assert(out, checker.Contains, "192.168.10.1:5000->5000/tcp")
  291. }
  292. func (s *DockerNetworkSuite) TestDockerNetworkRmPredefined(c *check.C) {
  293. predefined := []string{"bridge", "host", "none", "default"}
  294. for _, net := range predefined {
  295. // predefined networks can't be removed
  296. out, _, err := dockerCmdWithError("network", "rm", net)
  297. c.Assert(err, checker.NotNil, check.Commentf("%v", out))
  298. }
  299. }
  300. func (s *DockerNetworkSuite) TestDockerNetworkLsFilter(c *check.C) {
  301. testNet := "testnet1"
  302. testLabel := "foo"
  303. testValue := "bar"
  304. out, _ := dockerCmd(c, "network", "create", "dev")
  305. defer func() {
  306. dockerCmd(c, "network", "rm", "dev")
  307. dockerCmd(c, "network", "rm", testNet)
  308. }()
  309. networkID := strings.TrimSpace(out)
  310. // filter with partial ID
  311. // only show 'dev' network
  312. out, _ = dockerCmd(c, "network", "ls", "-f", "id="+networkID[0:5])
  313. assertNwList(c, out, []string{"dev"})
  314. out, _ = dockerCmd(c, "network", "ls", "-f", "name=dge")
  315. assertNwList(c, out, []string{"bridge"})
  316. // only show built-in network (bridge, none, host)
  317. out, _ = dockerCmd(c, "network", "ls", "-f", "type=builtin")
  318. assertNwList(c, out, []string{"bridge", "host", "none"})
  319. // only show custom networks (dev)
  320. out, _ = dockerCmd(c, "network", "ls", "-f", "type=custom")
  321. assertNwList(c, out, []string{"dev"})
  322. // show all networks with filter
  323. // it should be equivalent of ls without option
  324. out, _ = dockerCmd(c, "network", "ls", "-f", "type=custom", "-f", "type=builtin")
  325. assertNwList(c, out, []string{"bridge", "dev", "host", "none"})
  326. out, _ = dockerCmd(c, "network", "create", "--label", testLabel+"="+testValue, testNet)
  327. assertNwIsAvailable(c, testNet)
  328. out, _ = dockerCmd(c, "network", "ls", "-f", "label="+testLabel)
  329. assertNwList(c, out, []string{testNet})
  330. out, _ = dockerCmd(c, "network", "ls", "-f", "label="+testLabel+"="+testValue)
  331. assertNwList(c, out, []string{testNet})
  332. out, _ = dockerCmd(c, "network", "ls", "-f", "label=nonexistent")
  333. outArr := strings.Split(strings.TrimSpace(out), "\n")
  334. c.Assert(len(outArr), check.Equals, 1, check.Commentf("%s\n", out))
  335. out, _ = dockerCmd(c, "network", "ls", "-f", "driver=null")
  336. assertNwList(c, out, []string{"none"})
  337. out, _ = dockerCmd(c, "network", "ls", "-f", "driver=host")
  338. assertNwList(c, out, []string{"host"})
  339. out, _ = dockerCmd(c, "network", "ls", "-f", "driver=bridge")
  340. assertNwList(c, out, []string{"bridge", "dev", testNet})
  341. }
  342. func (s *DockerNetworkSuite) TestDockerNetworkCreateDelete(c *check.C) {
  343. dockerCmd(c, "network", "create", "test")
  344. assertNwIsAvailable(c, "test")
  345. dockerCmd(c, "network", "rm", "test")
  346. assertNwNotAvailable(c, "test")
  347. }
  348. func (s *DockerNetworkSuite) TestDockerNetworkCreateLabel(c *check.C) {
  349. testNet := "testnetcreatelabel"
  350. testLabel := "foo"
  351. testValue := "bar"
  352. dockerCmd(c, "network", "create", "--label", testLabel+"="+testValue, testNet)
  353. assertNwIsAvailable(c, testNet)
  354. out, _, err := dockerCmdWithError("network", "inspect", "--format={{ .Labels."+testLabel+" }}", testNet)
  355. c.Assert(err, check.IsNil)
  356. c.Assert(strings.TrimSpace(out), check.Equals, testValue)
  357. dockerCmd(c, "network", "rm", testNet)
  358. assertNwNotAvailable(c, testNet)
  359. }
  360. func (s *DockerSuite) TestDockerNetworkDeleteNotExists(c *check.C) {
  361. out, _, err := dockerCmdWithError("network", "rm", "test")
  362. c.Assert(err, checker.NotNil, check.Commentf("%v", out))
  363. }
  364. func (s *DockerSuite) TestDockerNetworkDeleteMultiple(c *check.C) {
  365. dockerCmd(c, "network", "create", "testDelMulti0")
  366. assertNwIsAvailable(c, "testDelMulti0")
  367. dockerCmd(c, "network", "create", "testDelMulti1")
  368. assertNwIsAvailable(c, "testDelMulti1")
  369. dockerCmd(c, "network", "create", "testDelMulti2")
  370. assertNwIsAvailable(c, "testDelMulti2")
  371. out, _ := dockerCmd(c, "run", "-d", "--net", "testDelMulti2", "busybox", "top")
  372. containerID := strings.TrimSpace(out)
  373. waitRun(containerID)
  374. // delete three networks at the same time, since testDelMulti2
  375. // contains active container, its deletion should fail.
  376. out, _, err := dockerCmdWithError("network", "rm", "testDelMulti0", "testDelMulti1", "testDelMulti2")
  377. // err should not be nil due to deleting testDelMulti2 failed.
  378. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  379. // testDelMulti2 should fail due to network has active endpoints
  380. c.Assert(out, checker.Contains, "has active endpoints")
  381. assertNwNotAvailable(c, "testDelMulti0")
  382. assertNwNotAvailable(c, "testDelMulti1")
  383. // testDelMulti2 can't be deleted, so it should exist
  384. assertNwIsAvailable(c, "testDelMulti2")
  385. }
  386. func (s *DockerSuite) TestDockerNetworkInspect(c *check.C) {
  387. out, _ := dockerCmd(c, "network", "inspect", "host")
  388. networkResources := []types.NetworkResource{}
  389. err := json.Unmarshal([]byte(out), &networkResources)
  390. c.Assert(err, check.IsNil)
  391. c.Assert(networkResources, checker.HasLen, 1)
  392. out, _ = dockerCmd(c, "network", "inspect", "--format={{ .Name }}", "host")
  393. c.Assert(strings.TrimSpace(out), check.Equals, "host")
  394. }
  395. func (s *DockerSuite) TestDockerNetworkInspectWithID(c *check.C) {
  396. out, _ := dockerCmd(c, "network", "create", "test2")
  397. networkID := strings.TrimSpace(out)
  398. assertNwIsAvailable(c, "test2")
  399. out, _ = dockerCmd(c, "network", "inspect", "--format={{ .Id }}", "test2")
  400. c.Assert(strings.TrimSpace(out), check.Equals, networkID)
  401. out, _ = dockerCmd(c, "network", "inspect", "--format={{ .ID }}", "test2")
  402. c.Assert(strings.TrimSpace(out), check.Equals, networkID)
  403. }
  404. func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
  405. result := dockerCmdWithResult("network", "inspect", "host", "none")
  406. c.Assert(result, icmd.Matches, icmd.Success)
  407. networkResources := []types.NetworkResource{}
  408. err := json.Unmarshal([]byte(result.Stdout()), &networkResources)
  409. c.Assert(err, check.IsNil)
  410. c.Assert(networkResources, checker.HasLen, 2)
  411. // Should print an error, return an exitCode 1 *but* should print the host network
  412. result = dockerCmdWithResult("network", "inspect", "host", "nonexistent")
  413. c.Assert(result, icmd.Matches, icmd.Expected{
  414. ExitCode: 1,
  415. Err: "Error: No such network: nonexistent",
  416. Out: "host",
  417. })
  418. networkResources = []types.NetworkResource{}
  419. err = json.Unmarshal([]byte(result.Stdout()), &networkResources)
  420. c.Assert(err, check.IsNil)
  421. c.Assert(networkResources, checker.HasLen, 1)
  422. // Should print an error and return an exitCode, nothing else
  423. result = dockerCmdWithResult("network", "inspect", "nonexistent")
  424. c.Assert(result, icmd.Matches, icmd.Expected{
  425. ExitCode: 1,
  426. Err: "Error: No such network: nonexistent",
  427. Out: "[]",
  428. })
  429. }
  430. func (s *DockerSuite) TestDockerInspectNetworkWithContainerName(c *check.C) {
  431. dockerCmd(c, "network", "create", "brNetForInspect")
  432. assertNwIsAvailable(c, "brNetForInspect")
  433. defer func() {
  434. dockerCmd(c, "network", "rm", "brNetForInspect")
  435. assertNwNotAvailable(c, "brNetForInspect")
  436. }()
  437. out, _ := dockerCmd(c, "run", "-d", "--name", "testNetInspect1", "--net", "brNetForInspect", "busybox", "top")
  438. c.Assert(waitRun("testNetInspect1"), check.IsNil)
  439. containerID := strings.TrimSpace(out)
  440. defer func() {
  441. // we don't stop container by name, because we'll rename it later
  442. dockerCmd(c, "stop", containerID)
  443. }()
  444. out, _ = dockerCmd(c, "network", "inspect", "brNetForInspect")
  445. networkResources := []types.NetworkResource{}
  446. err := json.Unmarshal([]byte(out), &networkResources)
  447. c.Assert(err, check.IsNil)
  448. c.Assert(networkResources, checker.HasLen, 1)
  449. container, ok := networkResources[0].Containers[containerID]
  450. c.Assert(ok, checker.True)
  451. c.Assert(container.Name, checker.Equals, "testNetInspect1")
  452. // rename container and check docker inspect output update
  453. newName := "HappyNewName"
  454. dockerCmd(c, "rename", "testNetInspect1", newName)
  455. // check whether network inspect works properly
  456. out, _ = dockerCmd(c, "network", "inspect", "brNetForInspect")
  457. newNetRes := []types.NetworkResource{}
  458. err = json.Unmarshal([]byte(out), &newNetRes)
  459. c.Assert(err, check.IsNil)
  460. c.Assert(newNetRes, checker.HasLen, 1)
  461. container1, ok := newNetRes[0].Containers[containerID]
  462. c.Assert(ok, checker.True)
  463. c.Assert(container1.Name, checker.Equals, newName)
  464. }
  465. func (s *DockerNetworkSuite) TestDockerNetworkConnectDisconnect(c *check.C) {
  466. dockerCmd(c, "network", "create", "test")
  467. assertNwIsAvailable(c, "test")
  468. nr := getNwResource(c, "test")
  469. c.Assert(nr.Name, checker.Equals, "test")
  470. c.Assert(len(nr.Containers), checker.Equals, 0)
  471. // run a container
  472. out, _ := dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
  473. c.Assert(waitRun("test"), check.IsNil)
  474. containerID := strings.TrimSpace(out)
  475. // connect the container to the test network
  476. dockerCmd(c, "network", "connect", "test", containerID)
  477. // inspect the network to make sure container is connected
  478. nr = getNetworkResource(c, nr.ID)
  479. c.Assert(len(nr.Containers), checker.Equals, 1)
  480. c.Assert(nr.Containers[containerID], check.NotNil)
  481. // check if container IP matches network inspect
  482. ip, _, err := net.ParseCIDR(nr.Containers[containerID].IPv4Address)
  483. c.Assert(err, check.IsNil)
  484. containerIP := findContainerIP(c, "test", "test")
  485. c.Assert(ip.String(), checker.Equals, containerIP)
  486. // disconnect container from the network
  487. dockerCmd(c, "network", "disconnect", "test", containerID)
  488. nr = getNwResource(c, "test")
  489. c.Assert(nr.Name, checker.Equals, "test")
  490. c.Assert(len(nr.Containers), checker.Equals, 0)
  491. // run another container
  492. out, _ = dockerCmd(c, "run", "-d", "--net", "test", "--name", "test2", "busybox", "top")
  493. c.Assert(waitRun("test2"), check.IsNil)
  494. containerID = strings.TrimSpace(out)
  495. nr = getNwResource(c, "test")
  496. c.Assert(nr.Name, checker.Equals, "test")
  497. c.Assert(len(nr.Containers), checker.Equals, 1)
  498. // force disconnect the container to the test network
  499. dockerCmd(c, "network", "disconnect", "-f", "test", containerID)
  500. nr = getNwResource(c, "test")
  501. c.Assert(nr.Name, checker.Equals, "test")
  502. c.Assert(len(nr.Containers), checker.Equals, 0)
  503. dockerCmd(c, "network", "rm", "test")
  504. assertNwNotAvailable(c, "test")
  505. }
  506. func (s *DockerNetworkSuite) TestDockerNetworkIPAMMultipleNetworks(c *check.C) {
  507. // test0 bridge network
  508. dockerCmd(c, "network", "create", "--subnet=192.168.0.0/16", "test1")
  509. assertNwIsAvailable(c, "test1")
  510. // test2 bridge network does not overlap
  511. dockerCmd(c, "network", "create", "--subnet=192.169.0.0/16", "test2")
  512. assertNwIsAvailable(c, "test2")
  513. // for networks w/o ipam specified, docker will choose proper non-overlapping subnets
  514. dockerCmd(c, "network", "create", "test3")
  515. assertNwIsAvailable(c, "test3")
  516. dockerCmd(c, "network", "create", "test4")
  517. assertNwIsAvailable(c, "test4")
  518. dockerCmd(c, "network", "create", "test5")
  519. assertNwIsAvailable(c, "test5")
  520. // test network with multiple subnets
  521. // bridge network doesn't support multiple subnets. hence, use a dummy driver that supports
  522. dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, "--subnet=192.168.0.0/16", "--subnet=192.170.0.0/16", "test6")
  523. assertNwIsAvailable(c, "test6")
  524. // test network with multiple subnets with valid ipam combinations
  525. // also check same subnet across networks when the driver supports it.
  526. dockerCmd(c, "network", "create", "-d", dummyNetworkDriver,
  527. "--subnet=192.168.0.0/16", "--subnet=192.170.0.0/16",
  528. "--gateway=192.168.0.100", "--gateway=192.170.0.100",
  529. "--ip-range=192.168.1.0/24",
  530. "--aux-address", "a=192.168.1.5", "--aux-address", "b=192.168.1.6",
  531. "--aux-address", "c=192.170.1.5", "--aux-address", "d=192.170.1.6",
  532. "test7")
  533. assertNwIsAvailable(c, "test7")
  534. // cleanup
  535. for i := 1; i < 8; i++ {
  536. dockerCmd(c, "network", "rm", fmt.Sprintf("test%d", i))
  537. }
  538. }
  539. func (s *DockerNetworkSuite) TestDockerNetworkCustomIPAM(c *check.C) {
  540. // Create a bridge network using custom ipam driver
  541. dockerCmd(c, "network", "create", "--ipam-driver", dummyIPAMDriver, "br0")
  542. assertNwIsAvailable(c, "br0")
  543. // Verify expected network ipam fields are there
  544. nr := getNetworkResource(c, "br0")
  545. c.Assert(nr.Driver, checker.Equals, "bridge")
  546. c.Assert(nr.IPAM.Driver, checker.Equals, dummyIPAMDriver)
  547. // remove network and exercise remote ipam driver
  548. dockerCmd(c, "network", "rm", "br0")
  549. assertNwNotAvailable(c, "br0")
  550. }
  551. func (s *DockerNetworkSuite) TestDockerNetworkIPAMOptions(c *check.C) {
  552. // Create a bridge network using custom ipam driver and options
  553. dockerCmd(c, "network", "create", "--ipam-driver", dummyIPAMDriver, "--ipam-opt", "opt1=drv1", "--ipam-opt", "opt2=drv2", "br0")
  554. assertNwIsAvailable(c, "br0")
  555. // Verify expected network ipam options
  556. nr := getNetworkResource(c, "br0")
  557. opts := nr.IPAM.Options
  558. c.Assert(opts["opt1"], checker.Equals, "drv1")
  559. c.Assert(opts["opt2"], checker.Equals, "drv2")
  560. }
  561. func (s *DockerNetworkSuite) TestDockerNetworkInspectDefault(c *check.C) {
  562. nr := getNetworkResource(c, "none")
  563. c.Assert(nr.Driver, checker.Equals, "null")
  564. c.Assert(nr.Scope, checker.Equals, "local")
  565. c.Assert(nr.Internal, checker.Equals, false)
  566. c.Assert(nr.EnableIPv6, checker.Equals, false)
  567. c.Assert(nr.IPAM.Driver, checker.Equals, "default")
  568. c.Assert(len(nr.IPAM.Config), checker.Equals, 0)
  569. nr = getNetworkResource(c, "host")
  570. c.Assert(nr.Driver, checker.Equals, "host")
  571. c.Assert(nr.Scope, checker.Equals, "local")
  572. c.Assert(nr.Internal, checker.Equals, false)
  573. c.Assert(nr.EnableIPv6, checker.Equals, false)
  574. c.Assert(nr.IPAM.Driver, checker.Equals, "default")
  575. c.Assert(len(nr.IPAM.Config), checker.Equals, 0)
  576. nr = getNetworkResource(c, "bridge")
  577. c.Assert(nr.Driver, checker.Equals, "bridge")
  578. c.Assert(nr.Scope, checker.Equals, "local")
  579. c.Assert(nr.Internal, checker.Equals, false)
  580. c.Assert(nr.EnableIPv6, checker.Equals, false)
  581. c.Assert(nr.IPAM.Driver, checker.Equals, "default")
  582. c.Assert(len(nr.IPAM.Config), checker.Equals, 1)
  583. c.Assert(nr.IPAM.Config[0].Subnet, checker.NotNil)
  584. c.Assert(nr.IPAM.Config[0].Gateway, checker.NotNil)
  585. }
  586. func (s *DockerNetworkSuite) TestDockerNetworkInspectCustomUnspecified(c *check.C) {
  587. // if unspecified, network subnet will be selected from inside preferred pool
  588. dockerCmd(c, "network", "create", "test01")
  589. assertNwIsAvailable(c, "test01")
  590. nr := getNetworkResource(c, "test01")
  591. c.Assert(nr.Driver, checker.Equals, "bridge")
  592. c.Assert(nr.Scope, checker.Equals, "local")
  593. c.Assert(nr.Internal, checker.Equals, false)
  594. c.Assert(nr.EnableIPv6, checker.Equals, false)
  595. c.Assert(nr.IPAM.Driver, checker.Equals, "default")
  596. c.Assert(len(nr.IPAM.Config), checker.Equals, 1)
  597. c.Assert(nr.IPAM.Config[0].Subnet, checker.NotNil)
  598. c.Assert(nr.IPAM.Config[0].Gateway, checker.NotNil)
  599. dockerCmd(c, "network", "rm", "test01")
  600. assertNwNotAvailable(c, "test01")
  601. }
  602. func (s *DockerNetworkSuite) TestDockerNetworkInspectCustomSpecified(c *check.C) {
  603. dockerCmd(c, "network", "create", "--driver=bridge", "--ipv6", "--subnet=fd80:24e2:f998:72d6::/64", "--subnet=172.28.0.0/16", "--ip-range=172.28.5.0/24", "--gateway=172.28.5.254", "br0")
  604. assertNwIsAvailable(c, "br0")
  605. nr := getNetworkResource(c, "br0")
  606. c.Assert(nr.Driver, checker.Equals, "bridge")
  607. c.Assert(nr.Scope, checker.Equals, "local")
  608. c.Assert(nr.Internal, checker.Equals, false)
  609. c.Assert(nr.EnableIPv6, checker.Equals, true)
  610. c.Assert(nr.IPAM.Driver, checker.Equals, "default")
  611. c.Assert(len(nr.IPAM.Config), checker.Equals, 2)
  612. c.Assert(nr.IPAM.Config[0].Subnet, checker.Equals, "172.28.0.0/16")
  613. c.Assert(nr.IPAM.Config[0].IPRange, checker.Equals, "172.28.5.0/24")
  614. c.Assert(nr.IPAM.Config[0].Gateway, checker.Equals, "172.28.5.254")
  615. c.Assert(nr.Internal, checker.False)
  616. dockerCmd(c, "network", "rm", "br0")
  617. assertNwNotAvailable(c, "test01")
  618. }
  619. func (s *DockerNetworkSuite) TestDockerNetworkIPAMInvalidCombinations(c *check.C) {
  620. // network with ip-range out of subnet range
  621. _, _, err := dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--ip-range=192.170.0.0/16", "test")
  622. c.Assert(err, check.NotNil)
  623. // network with multiple gateways for a single subnet
  624. _, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--gateway=192.168.0.1", "--gateway=192.168.0.2", "test")
  625. c.Assert(err, check.NotNil)
  626. // Multiple overlapping subnets in the same network must fail
  627. _, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--subnet=192.168.1.0/16", "test")
  628. c.Assert(err, check.NotNil)
  629. // overlapping subnets across networks must fail
  630. // create a valid test0 network
  631. dockerCmd(c, "network", "create", "--subnet=192.168.0.0/16", "test0")
  632. assertNwIsAvailable(c, "test0")
  633. // create an overlapping test1 network
  634. _, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.128.0/17", "test1")
  635. c.Assert(err, check.NotNil)
  636. dockerCmd(c, "network", "rm", "test0")
  637. assertNwNotAvailable(c, "test0")
  638. }
  639. func (s *DockerNetworkSuite) TestDockerNetworkDriverOptions(c *check.C) {
  640. dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, "-o", "opt1=drv1", "-o", "opt2=drv2", "testopt")
  641. assertNwIsAvailable(c, "testopt")
  642. gopts := remoteDriverNetworkRequest.Options[netlabel.GenericData]
  643. c.Assert(gopts, checker.NotNil)
  644. opts, ok := gopts.(map[string]interface{})
  645. c.Assert(ok, checker.Equals, true)
  646. c.Assert(opts["opt1"], checker.Equals, "drv1")
  647. c.Assert(opts["opt2"], checker.Equals, "drv2")
  648. dockerCmd(c, "network", "rm", "testopt")
  649. assertNwNotAvailable(c, "testopt")
  650. }
  651. func (s *DockerNetworkSuite) TestDockerPluginV2NetworkDriver(c *check.C) {
  652. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  653. var (
  654. npName = "tiborvass/test-docker-netplugin"
  655. npTag = "latest"
  656. npNameWithTag = npName + ":" + npTag
  657. )
  658. _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", npNameWithTag)
  659. c.Assert(err, checker.IsNil)
  660. out, _, err := dockerCmdWithError("plugin", "ls")
  661. c.Assert(err, checker.IsNil)
  662. c.Assert(out, checker.Contains, npName)
  663. c.Assert(out, checker.Contains, npTag)
  664. c.Assert(out, checker.Contains, "true")
  665. dockerCmd(c, "network", "create", "-d", npNameWithTag, "v2net")
  666. assertNwIsAvailable(c, "v2net")
  667. dockerCmd(c, "network", "rm", "v2net")
  668. assertNwNotAvailable(c, "v2net")
  669. }
  670. func (s *DockerDaemonSuite) TestDockerNetworkNoDiscoveryDefaultBridgeNetwork(c *check.C) {
  671. testRequires(c, ExecSupport)
  672. // On default bridge network built-in service discovery should not happen
  673. hostsFile := "/etc/hosts"
  674. bridgeName := "external-bridge"
  675. bridgeIP := "192.169.255.254/24"
  676. createInterface(c, "bridge", bridgeName, bridgeIP)
  677. defer deleteInterface(c, bridgeName)
  678. s.d.StartWithBusybox(c, "--bridge", bridgeName)
  679. defer s.d.Restart(c)
  680. // run two containers and store first container's etc/hosts content
  681. out, err := s.d.Cmd("run", "-d", "busybox", "top")
  682. c.Assert(err, check.IsNil)
  683. cid1 := strings.TrimSpace(out)
  684. defer s.d.Cmd("stop", cid1)
  685. hosts, err := s.d.Cmd("exec", cid1, "cat", hostsFile)
  686. c.Assert(err, checker.IsNil)
  687. out, err = s.d.Cmd("run", "-d", "--name", "container2", "busybox", "top")
  688. c.Assert(err, check.IsNil)
  689. cid2 := strings.TrimSpace(out)
  690. // verify first container's etc/hosts file has not changed after spawning the second named container
  691. hostsPost, err := s.d.Cmd("exec", cid1, "cat", hostsFile)
  692. c.Assert(err, checker.IsNil)
  693. c.Assert(string(hosts), checker.Equals, string(hostsPost),
  694. check.Commentf("Unexpected %s change on second container creation", hostsFile))
  695. // stop container 2 and verify first container's etc/hosts has not changed
  696. _, err = s.d.Cmd("stop", cid2)
  697. c.Assert(err, check.IsNil)
  698. hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
  699. c.Assert(err, checker.IsNil)
  700. c.Assert(string(hosts), checker.Equals, string(hostsPost),
  701. check.Commentf("Unexpected %s change on second container creation", hostsFile))
  702. // but discovery is on when connecting to non default bridge network
  703. network := "anotherbridge"
  704. out, err = s.d.Cmd("network", "create", network)
  705. c.Assert(err, check.IsNil, check.Commentf(out))
  706. defer s.d.Cmd("network", "rm", network)
  707. out, err = s.d.Cmd("network", "connect", network, cid1)
  708. c.Assert(err, check.IsNil, check.Commentf(out))
  709. hosts, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
  710. c.Assert(err, checker.IsNil)
  711. hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
  712. c.Assert(err, checker.IsNil)
  713. c.Assert(string(hosts), checker.Equals, string(hostsPost),
  714. check.Commentf("Unexpected %s change on second network connection", hostsFile))
  715. }
  716. func (s *DockerNetworkSuite) TestDockerNetworkAnonymousEndpoint(c *check.C) {
  717. testRequires(c, ExecSupport, NotArm)
  718. hostsFile := "/etc/hosts"
  719. cstmBridgeNw := "custom-bridge-nw"
  720. cstmBridgeNw1 := "custom-bridge-nw1"
  721. dockerCmd(c, "network", "create", "-d", "bridge", cstmBridgeNw)
  722. assertNwIsAvailable(c, cstmBridgeNw)
  723. // run two anonymous containers and store their etc/hosts content
  724. out, _ := dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "busybox", "top")
  725. cid1 := strings.TrimSpace(out)
  726. hosts1 := readContainerFileWithExec(c, cid1, hostsFile)
  727. out, _ = dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "busybox", "top")
  728. cid2 := strings.TrimSpace(out)
  729. hosts2 := readContainerFileWithExec(c, cid2, hostsFile)
  730. // verify first container etc/hosts file has not changed
  731. hosts1post := readContainerFileWithExec(c, cid1, hostsFile)
  732. c.Assert(string(hosts1), checker.Equals, string(hosts1post),
  733. check.Commentf("Unexpected %s change on anonymous container creation", hostsFile))
  734. // Connect the 2nd container to a new network and verify the
  735. // first container /etc/hosts file still hasn't changed.
  736. dockerCmd(c, "network", "create", "-d", "bridge", cstmBridgeNw1)
  737. assertNwIsAvailable(c, cstmBridgeNw1)
  738. dockerCmd(c, "network", "connect", cstmBridgeNw1, cid2)
  739. hosts2 = readContainerFileWithExec(c, cid2, hostsFile)
  740. hosts1post = readContainerFileWithExec(c, cid1, hostsFile)
  741. c.Assert(string(hosts1), checker.Equals, string(hosts1post),
  742. check.Commentf("Unexpected %s change on container connect", hostsFile))
  743. // start a named container
  744. cName := "AnyName"
  745. out, _ = dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "--name", cName, "busybox", "top")
  746. cid3 := strings.TrimSpace(out)
  747. // verify that container 1 and 2 can ping the named container
  748. dockerCmd(c, "exec", cid1, "ping", "-c", "1", cName)
  749. dockerCmd(c, "exec", cid2, "ping", "-c", "1", cName)
  750. // Stop named container and verify first two containers' etc/hosts file hasn't changed
  751. dockerCmd(c, "stop", cid3)
  752. hosts1post = readContainerFileWithExec(c, cid1, hostsFile)
  753. c.Assert(string(hosts1), checker.Equals, string(hosts1post),
  754. check.Commentf("Unexpected %s change on name container creation", hostsFile))
  755. hosts2post := readContainerFileWithExec(c, cid2, hostsFile)
  756. c.Assert(string(hosts2), checker.Equals, string(hosts2post),
  757. check.Commentf("Unexpected %s change on name container creation", hostsFile))
  758. // verify that container 1 and 2 can't ping the named container now
  759. _, _, err := dockerCmdWithError("exec", cid1, "ping", "-c", "1", cName)
  760. c.Assert(err, check.NotNil)
  761. _, _, err = dockerCmdWithError("exec", cid2, "ping", "-c", "1", cName)
  762. c.Assert(err, check.NotNil)
  763. }
  764. func (s *DockerNetworkSuite) TestDockerNetworkLinkOnDefaultNetworkOnly(c *check.C) {
  765. // Legacy Link feature must work only on default network, and not across networks
  766. cnt1 := "container1"
  767. cnt2 := "container2"
  768. network := "anotherbridge"
  769. // Run first container on default network
  770. dockerCmd(c, "run", "-d", "--name", cnt1, "busybox", "top")
  771. // Create another network and run the second container on it
  772. dockerCmd(c, "network", "create", network)
  773. assertNwIsAvailable(c, network)
  774. dockerCmd(c, "run", "-d", "--net", network, "--name", cnt2, "busybox", "top")
  775. // Try launching a container on default network, linking to the first container. Must succeed
  776. dockerCmd(c, "run", "-d", "--link", fmt.Sprintf("%s:%s", cnt1, cnt1), "busybox", "top")
  777. // Try launching a container on default network, linking to the second container. Must fail
  778. _, _, err := dockerCmdWithError("run", "-d", "--link", fmt.Sprintf("%s:%s", cnt2, cnt2), "busybox", "top")
  779. c.Assert(err, checker.NotNil)
  780. // Connect second container to default network. Now a container on default network can link to it
  781. dockerCmd(c, "network", "connect", "bridge", cnt2)
  782. dockerCmd(c, "run", "-d", "--link", fmt.Sprintf("%s:%s", cnt2, cnt2), "busybox", "top")
  783. }
  784. func (s *DockerNetworkSuite) TestDockerNetworkOverlayPortMapping(c *check.C) {
  785. // Verify exposed ports are present in ps output when running a container on
  786. // a network managed by a driver which does not provide the default gateway
  787. // for the container
  788. nwn := "ov"
  789. ctn := "bb"
  790. port1 := 80
  791. port2 := 443
  792. expose1 := fmt.Sprintf("--expose=%d", port1)
  793. expose2 := fmt.Sprintf("--expose=%d", port2)
  794. dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, nwn)
  795. assertNwIsAvailable(c, nwn)
  796. dockerCmd(c, "run", "-d", "--net", nwn, "--name", ctn, expose1, expose2, "busybox", "top")
  797. // Check docker ps o/p for last created container reports the unpublished ports
  798. unpPort1 := fmt.Sprintf("%d/tcp", port1)
  799. unpPort2 := fmt.Sprintf("%d/tcp", port2)
  800. out, _ := dockerCmd(c, "ps", "-n=1")
  801. // Missing unpublished ports in docker ps output
  802. c.Assert(out, checker.Contains, unpPort1)
  803. // Missing unpublished ports in docker ps output
  804. c.Assert(out, checker.Contains, unpPort2)
  805. }
  806. func (s *DockerNetworkSuite) TestDockerNetworkDriverUngracefulRestart(c *check.C) {
  807. testRequires(c, DaemonIsLinux, NotUserNamespace)
  808. dnd := "dnd"
  809. did := "did"
  810. mux := http.NewServeMux()
  811. server := httptest.NewServer(mux)
  812. setupRemoteNetworkDrivers(c, mux, server.URL, dnd, did)
  813. s.d.StartWithBusybox(c)
  814. _, err := s.d.Cmd("network", "create", "-d", dnd, "--subnet", "1.1.1.0/24", "net1")
  815. c.Assert(err, checker.IsNil)
  816. _, err = s.d.Cmd("run", "-itd", "--net", "net1", "--name", "foo", "--ip", "1.1.1.10", "busybox", "sh")
  817. c.Assert(err, checker.IsNil)
  818. // Kill daemon and restart
  819. c.Assert(s.d.Kill(), checker.IsNil)
  820. server.Close()
  821. startTime := time.Now().Unix()
  822. s.d.Restart(c)
  823. lapse := time.Now().Unix() - startTime
  824. if lapse > 60 {
  825. // In normal scenarios, daemon restart takes ~1 second.
  826. // Plugin retry mechanism can delay the daemon start. systemd may not like it.
  827. // Avoid accessing plugins during daemon bootup
  828. c.Logf("daemon restart took too long : %d seconds", lapse)
  829. }
  830. // Restart the custom dummy plugin
  831. mux = http.NewServeMux()
  832. server = httptest.NewServer(mux)
  833. setupRemoteNetworkDrivers(c, mux, server.URL, dnd, did)
  834. // trying to reuse the same ip must succeed
  835. _, err = s.d.Cmd("run", "-itd", "--net", "net1", "--name", "bar", "--ip", "1.1.1.10", "busybox", "sh")
  836. c.Assert(err, checker.IsNil)
  837. }
  838. func (s *DockerNetworkSuite) TestDockerNetworkMacInspect(c *check.C) {
  839. // Verify endpoint MAC address is correctly populated in container's network settings
  840. nwn := "ov"
  841. ctn := "bb"
  842. dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, nwn)
  843. assertNwIsAvailable(c, nwn)
  844. dockerCmd(c, "run", "-d", "--net", nwn, "--name", ctn, "busybox", "top")
  845. mac := inspectField(c, ctn, "NetworkSettings.Networks."+nwn+".MacAddress")
  846. c.Assert(mac, checker.Equals, "a0:b1:c2:d3:e4:f5")
  847. }
  848. func (s *DockerSuite) TestInspectAPIMultipleNetworks(c *check.C) {
  849. dockerCmd(c, "network", "create", "mybridge1")
  850. dockerCmd(c, "network", "create", "mybridge2")
  851. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  852. id := strings.TrimSpace(out)
  853. c.Assert(waitRun(id), check.IsNil)
  854. dockerCmd(c, "network", "connect", "mybridge1", id)
  855. dockerCmd(c, "network", "connect", "mybridge2", id)
  856. body := getInspectBody(c, "v1.20", id)
  857. var inspect120 v1p20.ContainerJSON
  858. err := json.Unmarshal(body, &inspect120)
  859. c.Assert(err, checker.IsNil)
  860. versionedIP := inspect120.NetworkSettings.IPAddress
  861. body = getInspectBody(c, "v1.21", id)
  862. var inspect121 types.ContainerJSON
  863. err = json.Unmarshal(body, &inspect121)
  864. c.Assert(err, checker.IsNil)
  865. c.Assert(inspect121.NetworkSettings.Networks, checker.HasLen, 3)
  866. bridge := inspect121.NetworkSettings.Networks["bridge"]
  867. c.Assert(bridge.IPAddress, checker.Equals, versionedIP)
  868. c.Assert(bridge.IPAddress, checker.Equals, inspect121.NetworkSettings.IPAddress)
  869. }
  870. func connectContainerToNetworks(c *check.C, d *daemon.Daemon, cName string, nws []string) {
  871. // Run a container on the default network
  872. out, err := d.Cmd("run", "-d", "--name", cName, "busybox", "top")
  873. c.Assert(err, checker.IsNil, check.Commentf(out))
  874. // Attach the container to other networks
  875. for _, nw := range nws {
  876. out, err = d.Cmd("network", "create", nw)
  877. c.Assert(err, checker.IsNil, check.Commentf(out))
  878. out, err = d.Cmd("network", "connect", nw, cName)
  879. c.Assert(err, checker.IsNil, check.Commentf(out))
  880. }
  881. }
  882. func verifyContainerIsConnectedToNetworks(c *check.C, d *daemon.Daemon, cName string, nws []string) {
  883. // Verify container is connected to all the networks
  884. for _, nw := range nws {
  885. out, err := d.Cmd("inspect", "-f", fmt.Sprintf("{{.NetworkSettings.Networks.%s}}", nw), cName)
  886. c.Assert(err, checker.IsNil, check.Commentf(out))
  887. c.Assert(out, checker.Not(checker.Equals), "<no value>\n")
  888. }
  889. }
  890. func (s *DockerNetworkSuite) TestDockerNetworkMultipleNetworksGracefulDaemonRestart(c *check.C) {
  891. cName := "bb"
  892. nwList := []string{"nw1", "nw2", "nw3"}
  893. s.d.StartWithBusybox(c)
  894. connectContainerToNetworks(c, s.d, cName, nwList)
  895. verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
  896. // Reload daemon
  897. s.d.Restart(c)
  898. _, err := s.d.Cmd("start", cName)
  899. c.Assert(err, checker.IsNil)
  900. verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
  901. }
  902. func (s *DockerNetworkSuite) TestDockerNetworkMultipleNetworksUngracefulDaemonRestart(c *check.C) {
  903. cName := "cc"
  904. nwList := []string{"nw1", "nw2", "nw3"}
  905. s.d.StartWithBusybox(c)
  906. connectContainerToNetworks(c, s.d, cName, nwList)
  907. verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
  908. // Kill daemon and restart
  909. c.Assert(s.d.Kill(), checker.IsNil)
  910. s.d.Restart(c)
  911. // Restart container
  912. _, err := s.d.Cmd("start", cName)
  913. c.Assert(err, checker.IsNil)
  914. verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
  915. }
  916. func (s *DockerNetworkSuite) TestDockerNetworkRunNetByID(c *check.C) {
  917. out, _ := dockerCmd(c, "network", "create", "one")
  918. containerOut, _, err := dockerCmdWithError("run", "-d", "--net", strings.TrimSpace(out), "busybox", "top")
  919. c.Assert(err, checker.IsNil, check.Commentf(containerOut))
  920. }
  921. func (s *DockerNetworkSuite) TestDockerNetworkHostModeUngracefulDaemonRestart(c *check.C) {
  922. testRequires(c, DaemonIsLinux, NotUserNamespace)
  923. s.d.StartWithBusybox(c)
  924. // Run a few containers on host network
  925. for i := 0; i < 10; i++ {
  926. cName := fmt.Sprintf("hostc-%d", i)
  927. out, err := s.d.Cmd("run", "-d", "--name", cName, "--net=host", "--restart=always", "busybox", "top")
  928. c.Assert(err, checker.IsNil, check.Commentf(out))
  929. // verfiy container has finished starting before killing daemon
  930. err = s.d.WaitRun(cName)
  931. c.Assert(err, checker.IsNil)
  932. }
  933. // Kill daemon ungracefully and restart
  934. c.Assert(s.d.Kill(), checker.IsNil)
  935. s.d.Restart(c)
  936. // make sure all the containers are up and running
  937. for i := 0; i < 10; i++ {
  938. err := s.d.WaitRun(fmt.Sprintf("hostc-%d", i))
  939. c.Assert(err, checker.IsNil)
  940. }
  941. }
  942. func (s *DockerNetworkSuite) TestDockerNetworkConnectToHostFromOtherNetwork(c *check.C) {
  943. dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  944. c.Assert(waitRun("container1"), check.IsNil)
  945. dockerCmd(c, "network", "disconnect", "bridge", "container1")
  946. out, _, err := dockerCmdWithError("network", "connect", "host", "container1")
  947. c.Assert(err, checker.NotNil, check.Commentf(out))
  948. c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetwork.Error())
  949. }
  950. func (s *DockerNetworkSuite) TestDockerNetworkDisconnectFromHost(c *check.C) {
  951. dockerCmd(c, "run", "-d", "--name", "container1", "--net=host", "busybox", "top")
  952. c.Assert(waitRun("container1"), check.IsNil)
  953. out, _, err := dockerCmdWithError("network", "disconnect", "host", "container1")
  954. c.Assert(err, checker.NotNil, check.Commentf("Should err out disconnect from host"))
  955. c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetwork.Error())
  956. }
  957. func (s *DockerNetworkSuite) TestDockerNetworkConnectWithPortMapping(c *check.C) {
  958. testRequires(c, NotArm)
  959. dockerCmd(c, "network", "create", "test1")
  960. dockerCmd(c, "run", "-d", "--name", "c1", "-p", "5000:5000", "busybox", "top")
  961. c.Assert(waitRun("c1"), check.IsNil)
  962. dockerCmd(c, "network", "connect", "test1", "c1")
  963. }
  964. func verifyPortMap(c *check.C, container, port, originalMapping string, mustBeEqual bool) {
  965. chk := checker.Equals
  966. if !mustBeEqual {
  967. chk = checker.Not(checker.Equals)
  968. }
  969. currentMapping, _ := dockerCmd(c, "port", container, port)
  970. c.Assert(currentMapping, chk, originalMapping)
  971. }
  972. func (s *DockerNetworkSuite) TestDockerNetworkConnectDisconnectWithPortMapping(c *check.C) {
  973. // Connect and disconnect a container with explicit and non-explicit
  974. // host port mapping to/from networks which do cause and do not cause
  975. // the container default gateway to change, and verify docker port cmd
  976. // returns congruent information
  977. testRequires(c, NotArm)
  978. cnt := "c1"
  979. dockerCmd(c, "network", "create", "aaa")
  980. dockerCmd(c, "network", "create", "ccc")
  981. dockerCmd(c, "run", "-d", "--name", cnt, "-p", "9000:90", "-p", "70", "busybox", "top")
  982. c.Assert(waitRun(cnt), check.IsNil)
  983. curPortMap, _ := dockerCmd(c, "port", cnt, "70")
  984. curExplPortMap, _ := dockerCmd(c, "port", cnt, "90")
  985. // Connect to a network which causes the container's default gw switch
  986. dockerCmd(c, "network", "connect", "aaa", cnt)
  987. verifyPortMap(c, cnt, "70", curPortMap, false)
  988. verifyPortMap(c, cnt, "90", curExplPortMap, true)
  989. // Read current mapping
  990. curPortMap, _ = dockerCmd(c, "port", cnt, "70")
  991. // Disconnect from a network which causes the container's default gw switch
  992. dockerCmd(c, "network", "disconnect", "aaa", cnt)
  993. verifyPortMap(c, cnt, "70", curPortMap, false)
  994. verifyPortMap(c, cnt, "90", curExplPortMap, true)
  995. // Read current mapping
  996. curPortMap, _ = dockerCmd(c, "port", cnt, "70")
  997. // Connect to a network which does not cause the container's default gw switch
  998. dockerCmd(c, "network", "connect", "ccc", cnt)
  999. verifyPortMap(c, cnt, "70", curPortMap, true)
  1000. verifyPortMap(c, cnt, "90", curExplPortMap, true)
  1001. }
  1002. func (s *DockerNetworkSuite) TestDockerNetworkConnectWithMac(c *check.C) {
  1003. macAddress := "02:42:ac:11:00:02"
  1004. dockerCmd(c, "network", "create", "mynetwork")
  1005. dockerCmd(c, "run", "--name=test", "-d", "--mac-address", macAddress, "busybox", "top")
  1006. c.Assert(waitRun("test"), check.IsNil)
  1007. mac1 := inspectField(c, "test", "NetworkSettings.Networks.bridge.MacAddress")
  1008. c.Assert(strings.TrimSpace(mac1), checker.Equals, macAddress)
  1009. dockerCmd(c, "network", "connect", "mynetwork", "test")
  1010. mac2 := inspectField(c, "test", "NetworkSettings.Networks.mynetwork.MacAddress")
  1011. c.Assert(strings.TrimSpace(mac2), checker.Not(checker.Equals), strings.TrimSpace(mac1))
  1012. }
  1013. func (s *DockerNetworkSuite) TestDockerNetworkInspectCreatedContainer(c *check.C) {
  1014. dockerCmd(c, "create", "--name", "test", "busybox")
  1015. networks := inspectField(c, "test", "NetworkSettings.Networks")
  1016. c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should return 'bridge' network"))
  1017. }
  1018. func (s *DockerNetworkSuite) TestDockerNetworkRestartWithMultipleNetworks(c *check.C) {
  1019. dockerCmd(c, "network", "create", "test")
  1020. dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top")
  1021. c.Assert(waitRun("foo"), checker.IsNil)
  1022. dockerCmd(c, "network", "connect", "test", "foo")
  1023. dockerCmd(c, "restart", "foo")
  1024. networks := inspectField(c, "foo", "NetworkSettings.Networks")
  1025. c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should contain 'bridge' network"))
  1026. c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' network"))
  1027. }
  1028. func (s *DockerNetworkSuite) TestDockerNetworkConnectDisconnectToStoppedContainer(c *check.C) {
  1029. dockerCmd(c, "network", "create", "test")
  1030. dockerCmd(c, "create", "--name=foo", "busybox", "top")
  1031. dockerCmd(c, "network", "connect", "test", "foo")
  1032. networks := inspectField(c, "foo", "NetworkSettings.Networks")
  1033. c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' network"))
  1034. // Restart docker daemon to test the config has persisted to disk
  1035. s.d.Restart(c)
  1036. networks = inspectField(c, "foo", "NetworkSettings.Networks")
  1037. c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' network"))
  1038. // start the container and test if we can ping it from another container in the same network
  1039. dockerCmd(c, "start", "foo")
  1040. c.Assert(waitRun("foo"), checker.IsNil)
  1041. ip := inspectField(c, "foo", "NetworkSettings.Networks.test.IPAddress")
  1042. ip = strings.TrimSpace(ip)
  1043. dockerCmd(c, "run", "--net=test", "busybox", "sh", "-c", fmt.Sprintf("ping -c 1 %s", ip))
  1044. dockerCmd(c, "stop", "foo")
  1045. // Test disconnect
  1046. dockerCmd(c, "network", "disconnect", "test", "foo")
  1047. networks = inspectField(c, "foo", "NetworkSettings.Networks")
  1048. c.Assert(networks, checker.Not(checker.Contains), "test", check.Commentf("Should not contain 'test' network"))
  1049. // Restart docker daemon to test the config has persisted to disk
  1050. s.d.Restart(c)
  1051. networks = inspectField(c, "foo", "NetworkSettings.Networks")
  1052. c.Assert(networks, checker.Not(checker.Contains), "test", check.Commentf("Should not contain 'test' network"))
  1053. }
  1054. func (s *DockerNetworkSuite) TestDockerNetworkDisconnectContainerNonexistingNetwork(c *check.C) {
  1055. dockerCmd(c, "network", "create", "test")
  1056. dockerCmd(c, "run", "--net=test", "-d", "--name=foo", "busybox", "top")
  1057. networks := inspectField(c, "foo", "NetworkSettings.Networks")
  1058. c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' network"))
  1059. // Stop container and remove network
  1060. dockerCmd(c, "stop", "foo")
  1061. dockerCmd(c, "network", "rm", "test")
  1062. // Test disconnecting stopped container from nonexisting network
  1063. dockerCmd(c, "network", "disconnect", "-f", "test", "foo")
  1064. networks = inspectField(c, "foo", "NetworkSettings.Networks")
  1065. c.Assert(networks, checker.Not(checker.Contains), "test", check.Commentf("Should not contain 'test' network"))
  1066. }
  1067. func (s *DockerNetworkSuite) TestDockerNetworkConnectPreferredIP(c *check.C) {
  1068. // create two networks
  1069. dockerCmd(c, "network", "create", "--ipv6", "--subnet=172.28.0.0/16", "--subnet=2001:db8:1234::/64", "n0")
  1070. assertNwIsAvailable(c, "n0")
  1071. dockerCmd(c, "network", "create", "--ipv6", "--subnet=172.30.0.0/16", "--ip-range=172.30.5.0/24", "--subnet=2001:db8:abcd::/64", "--ip-range=2001:db8:abcd::/80", "n1")
  1072. assertNwIsAvailable(c, "n1")
  1073. // run a container on first network specifying the ip addresses
  1074. dockerCmd(c, "run", "-d", "--name", "c0", "--net=n0", "--ip", "172.28.99.88", "--ip6", "2001:db8:1234::9988", "busybox", "top")
  1075. c.Assert(waitRun("c0"), check.IsNil)
  1076. verifyIPAddressConfig(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
  1077. verifyIPAddresses(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
  1078. // connect the container to the second network specifying an ip addresses
  1079. dockerCmd(c, "network", "connect", "--ip", "172.30.55.44", "--ip6", "2001:db8:abcd::5544", "n1", "c0")
  1080. verifyIPAddressConfig(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
  1081. verifyIPAddresses(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
  1082. // Stop and restart the container
  1083. dockerCmd(c, "stop", "c0")
  1084. dockerCmd(c, "start", "c0")
  1085. // verify requested addresses are applied and configs are still there
  1086. verifyIPAddressConfig(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
  1087. verifyIPAddresses(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
  1088. verifyIPAddressConfig(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
  1089. verifyIPAddresses(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
  1090. // Still it should fail to connect to the default network with a specified IP (whatever ip)
  1091. out, _, err := dockerCmdWithError("network", "connect", "--ip", "172.21.55.44", "bridge", "c0")
  1092. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  1093. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndIP.Error())
  1094. }
  1095. func (s *DockerNetworkSuite) TestDockerNetworkConnectPreferredIPStoppedContainer(c *check.C) {
  1096. // create a container
  1097. dockerCmd(c, "create", "--name", "c0", "busybox", "top")
  1098. // create a network
  1099. dockerCmd(c, "network", "create", "--ipv6", "--subnet=172.30.0.0/16", "--subnet=2001:db8:abcd::/64", "n0")
  1100. assertNwIsAvailable(c, "n0")
  1101. // connect the container to the network specifying an ip addresses
  1102. dockerCmd(c, "network", "connect", "--ip", "172.30.55.44", "--ip6", "2001:db8:abcd::5544", "n0", "c0")
  1103. verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
  1104. // start the container, verify config has not changed and ip addresses are assigned
  1105. dockerCmd(c, "start", "c0")
  1106. c.Assert(waitRun("c0"), check.IsNil)
  1107. verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
  1108. verifyIPAddresses(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
  1109. // stop the container and check ip config has not changed
  1110. dockerCmd(c, "stop", "c0")
  1111. verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
  1112. }
  1113. func (s *DockerNetworkSuite) TestDockerNetworkUnsupportedRequiredIP(c *check.C) {
  1114. // requested IP is not supported on predefined networks
  1115. for _, mode := range []string{"none", "host", "bridge", "default"} {
  1116. checkUnsupportedNetworkAndIP(c, mode)
  1117. }
  1118. // requested IP is not supported on networks with no user defined subnets
  1119. dockerCmd(c, "network", "create", "n0")
  1120. assertNwIsAvailable(c, "n0")
  1121. out, _, err := dockerCmdWithError("run", "-d", "--ip", "172.28.99.88", "--net", "n0", "busybox", "top")
  1122. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  1123. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkNoSubnetAndIP.Error())
  1124. out, _, err = dockerCmdWithError("run", "-d", "--ip6", "2001:db8:1234::9988", "--net", "n0", "busybox", "top")
  1125. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  1126. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkNoSubnetAndIP.Error())
  1127. dockerCmd(c, "network", "rm", "n0")
  1128. assertNwNotAvailable(c, "n0")
  1129. }
  1130. func checkUnsupportedNetworkAndIP(c *check.C, nwMode string) {
  1131. out, _, err := dockerCmdWithError("run", "-d", "--net", nwMode, "--ip", "172.28.99.88", "--ip6", "2001:db8:1234::9988", "busybox", "top")
  1132. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  1133. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndIP.Error())
  1134. }
  1135. func verifyIPAddressConfig(c *check.C, cName, nwname, ipv4, ipv6 string) {
  1136. if ipv4 != "" {
  1137. out := inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAMConfig.IPv4Address", nwname))
  1138. c.Assert(strings.TrimSpace(out), check.Equals, ipv4)
  1139. }
  1140. if ipv6 != "" {
  1141. out := inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAMConfig.IPv6Address", nwname))
  1142. c.Assert(strings.TrimSpace(out), check.Equals, ipv6)
  1143. }
  1144. }
  1145. func verifyIPAddresses(c *check.C, cName, nwname, ipv4, ipv6 string) {
  1146. out := inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAddress", nwname))
  1147. c.Assert(strings.TrimSpace(out), check.Equals, ipv4)
  1148. out = inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.GlobalIPv6Address", nwname))
  1149. c.Assert(strings.TrimSpace(out), check.Equals, ipv6)
  1150. }
  1151. func (s *DockerNetworkSuite) TestDockerNetworkConnectLinkLocalIP(c *check.C) {
  1152. // create one test network
  1153. dockerCmd(c, "network", "create", "--ipv6", "--subnet=2001:db8:1234::/64", "n0")
  1154. assertNwIsAvailable(c, "n0")
  1155. // run a container with incorrect link-local address
  1156. _, _, err := dockerCmdWithError("run", "--link-local-ip", "169.253.5.5", "busybox", "top")
  1157. c.Assert(err, check.NotNil)
  1158. _, _, err = dockerCmdWithError("run", "--link-local-ip", "2001:db8::89", "busybox", "top")
  1159. c.Assert(err, check.NotNil)
  1160. // run two containers with link-local ip on the test network
  1161. dockerCmd(c, "run", "-d", "--name", "c0", "--net=n0", "--link-local-ip", "169.254.7.7", "--link-local-ip", "fe80::254:77", "busybox", "top")
  1162. c.Assert(waitRun("c0"), check.IsNil)
  1163. dockerCmd(c, "run", "-d", "--name", "c1", "--net=n0", "--link-local-ip", "169.254.8.8", "--link-local-ip", "fe80::254:88", "busybox", "top")
  1164. c.Assert(waitRun("c1"), check.IsNil)
  1165. // run a container on the default network and connect it to the test network specifying a link-local address
  1166. dockerCmd(c, "run", "-d", "--name", "c2", "busybox", "top")
  1167. c.Assert(waitRun("c2"), check.IsNil)
  1168. dockerCmd(c, "network", "connect", "--link-local-ip", "169.254.9.9", "n0", "c2")
  1169. // verify the three containers can ping each other via the link-local addresses
  1170. _, _, err = dockerCmdWithError("exec", "c0", "ping", "-c", "1", "169.254.8.8")
  1171. c.Assert(err, check.IsNil)
  1172. _, _, err = dockerCmdWithError("exec", "c1", "ping", "-c", "1", "169.254.9.9")
  1173. c.Assert(err, check.IsNil)
  1174. _, _, err = dockerCmdWithError("exec", "c2", "ping", "-c", "1", "169.254.7.7")
  1175. c.Assert(err, check.IsNil)
  1176. // Stop and restart the three containers
  1177. dockerCmd(c, "stop", "c0")
  1178. dockerCmd(c, "stop", "c1")
  1179. dockerCmd(c, "stop", "c2")
  1180. dockerCmd(c, "start", "c0")
  1181. dockerCmd(c, "start", "c1")
  1182. dockerCmd(c, "start", "c2")
  1183. // verify the ping again
  1184. _, _, err = dockerCmdWithError("exec", "c0", "ping", "-c", "1", "169.254.8.8")
  1185. c.Assert(err, check.IsNil)
  1186. _, _, err = dockerCmdWithError("exec", "c1", "ping", "-c", "1", "169.254.9.9")
  1187. c.Assert(err, check.IsNil)
  1188. _, _, err = dockerCmdWithError("exec", "c2", "ping", "-c", "1", "169.254.7.7")
  1189. c.Assert(err, check.IsNil)
  1190. }
  1191. func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectLink(c *check.C) {
  1192. testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
  1193. dockerCmd(c, "network", "create", "-d", "bridge", "foo1")
  1194. dockerCmd(c, "network", "create", "-d", "bridge", "foo2")
  1195. dockerCmd(c, "run", "-d", "--net=foo1", "--name=first", "busybox", "top")
  1196. c.Assert(waitRun("first"), check.IsNil)
  1197. // run a container in a user-defined network with a link for an existing container
  1198. // and a link for a container that doesn't exist
  1199. dockerCmd(c, "run", "-d", "--net=foo1", "--name=second", "--link=first:FirstInFoo1",
  1200. "--link=third:bar", "busybox", "top")
  1201. c.Assert(waitRun("second"), check.IsNil)
  1202. // ping to first and its alias FirstInFoo1 must succeed
  1203. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  1204. c.Assert(err, check.IsNil)
  1205. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo1")
  1206. c.Assert(err, check.IsNil)
  1207. // connect first container to foo2 network
  1208. dockerCmd(c, "network", "connect", "foo2", "first")
  1209. // connect second container to foo2 network with a different alias for first container
  1210. dockerCmd(c, "network", "connect", "--link=first:FirstInFoo2", "foo2", "second")
  1211. // ping the new alias in network foo2
  1212. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo2")
  1213. c.Assert(err, check.IsNil)
  1214. // disconnect first container from foo1 network
  1215. dockerCmd(c, "network", "disconnect", "foo1", "first")
  1216. // link in foo1 network must fail
  1217. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo1")
  1218. c.Assert(err, check.NotNil)
  1219. // link in foo2 network must succeed
  1220. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo2")
  1221. c.Assert(err, check.IsNil)
  1222. }
  1223. func (s *DockerNetworkSuite) TestDockerNetworkDisconnectDefault(c *check.C) {
  1224. netWorkName1 := "test1"
  1225. netWorkName2 := "test2"
  1226. containerName := "foo"
  1227. dockerCmd(c, "network", "create", netWorkName1)
  1228. dockerCmd(c, "network", "create", netWorkName2)
  1229. dockerCmd(c, "create", "--name", containerName, "busybox", "top")
  1230. dockerCmd(c, "network", "connect", netWorkName1, containerName)
  1231. dockerCmd(c, "network", "connect", netWorkName2, containerName)
  1232. dockerCmd(c, "network", "disconnect", "bridge", containerName)
  1233. dockerCmd(c, "start", containerName)
  1234. c.Assert(waitRun(containerName), checker.IsNil)
  1235. networks := inspectField(c, containerName, "NetworkSettings.Networks")
  1236. c.Assert(networks, checker.Contains, netWorkName1, check.Commentf(fmt.Sprintf("Should contain '%s' network", netWorkName1)))
  1237. c.Assert(networks, checker.Contains, netWorkName2, check.Commentf(fmt.Sprintf("Should contain '%s' network", netWorkName2)))
  1238. c.Assert(networks, checker.Not(checker.Contains), "bridge", check.Commentf("Should not contain 'bridge' network"))
  1239. }
  1240. func (s *DockerNetworkSuite) TestDockerNetworkConnectWithAliasOnDefaultNetworks(c *check.C) {
  1241. testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
  1242. defaults := []string{"bridge", "host", "none"}
  1243. out, _ := dockerCmd(c, "run", "-d", "--net=none", "busybox", "top")
  1244. containerID := strings.TrimSpace(out)
  1245. for _, net := range defaults {
  1246. res, _, err := dockerCmdWithError("network", "connect", "--alias", "alias"+net, net, containerID)
  1247. c.Assert(err, checker.NotNil)
  1248. c.Assert(res, checker.Contains, runconfig.ErrUnsupportedNetworkAndAlias.Error())
  1249. }
  1250. }
  1251. func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectAlias(c *check.C) {
  1252. testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
  1253. dockerCmd(c, "network", "create", "-d", "bridge", "net1")
  1254. dockerCmd(c, "network", "create", "-d", "bridge", "net2")
  1255. cid, _ := dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo", "busybox", "top")
  1256. c.Assert(waitRun("first"), check.IsNil)
  1257. dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top")
  1258. c.Assert(waitRun("second"), check.IsNil)
  1259. // ping first container and its alias
  1260. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  1261. c.Assert(err, check.IsNil)
  1262. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  1263. c.Assert(err, check.IsNil)
  1264. // ping first container's short-id alias
  1265. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid))
  1266. c.Assert(err, check.IsNil)
  1267. // connect first container to net2 network
  1268. dockerCmd(c, "network", "connect", "--alias=bar", "net2", "first")
  1269. // connect second container to foo2 network with a different alias for first container
  1270. dockerCmd(c, "network", "connect", "net2", "second")
  1271. // ping the new alias in network foo2
  1272. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "bar")
  1273. c.Assert(err, check.IsNil)
  1274. // disconnect first container from net1 network
  1275. dockerCmd(c, "network", "disconnect", "net1", "first")
  1276. // ping to net1 scoped alias "foo" must fail
  1277. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  1278. c.Assert(err, check.NotNil)
  1279. // ping to net2 scoped alias "bar" must still succeed
  1280. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "bar")
  1281. c.Assert(err, check.IsNil)
  1282. // ping to net2 scoped alias short-id must still succeed
  1283. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid))
  1284. c.Assert(err, check.IsNil)
  1285. // verify the alias option is rejected when running on predefined network
  1286. out, _, err := dockerCmdWithError("run", "--rm", "--name=any", "--net-alias=any", "busybox", "top")
  1287. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  1288. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndAlias.Error())
  1289. // verify the alias option is rejected when connecting to predefined network
  1290. out, _, err = dockerCmdWithError("network", "connect", "--alias=any", "bridge", "first")
  1291. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  1292. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndAlias.Error())
  1293. }
  1294. func (s *DockerSuite) TestUserDefinedNetworkConnectivity(c *check.C) {
  1295. testRequires(c, DaemonIsLinux, NotUserNamespace)
  1296. dockerCmd(c, "network", "create", "-d", "bridge", "br.net1")
  1297. dockerCmd(c, "run", "-d", "--net=br.net1", "--name=c1.net1", "busybox", "top")
  1298. c.Assert(waitRun("c1.net1"), check.IsNil)
  1299. dockerCmd(c, "run", "-d", "--net=br.net1", "--name=c2.net1", "busybox", "top")
  1300. c.Assert(waitRun("c2.net1"), check.IsNil)
  1301. // ping first container by its unqualified name
  1302. _, _, err := dockerCmdWithError("exec", "c2.net1", "ping", "-c", "1", "c1.net1")
  1303. c.Assert(err, check.IsNil)
  1304. // ping first container by its qualified name
  1305. _, _, err = dockerCmdWithError("exec", "c2.net1", "ping", "-c", "1", "c1.net1.br.net1")
  1306. c.Assert(err, check.IsNil)
  1307. // ping with first qualified name masked by an additional domain. should fail
  1308. _, _, err = dockerCmdWithError("exec", "c2.net1", "ping", "-c", "1", "c1.net1.br.net1.google.com")
  1309. c.Assert(err, check.NotNil)
  1310. }
  1311. func (s *DockerSuite) TestEmbeddedDNSInvalidInput(c *check.C) {
  1312. testRequires(c, DaemonIsLinux, NotUserNamespace)
  1313. dockerCmd(c, "network", "create", "-d", "bridge", "nw1")
  1314. // Sending garbage to embedded DNS shouldn't crash the daemon
  1315. dockerCmd(c, "run", "-i", "--net=nw1", "--name=c1", "debian:jessie", "bash", "-c", "echo InvalidQuery > /dev/udp/127.0.0.11/53")
  1316. }
  1317. func (s *DockerSuite) TestDockerNetworkConnectFailsNoInspectChange(c *check.C) {
  1318. dockerCmd(c, "run", "-d", "--name=bb", "busybox", "top")
  1319. c.Assert(waitRun("bb"), check.IsNil)
  1320. ns0 := inspectField(c, "bb", "NetworkSettings.Networks.bridge")
  1321. // A failing redundant network connect should not alter current container's endpoint settings
  1322. _, _, err := dockerCmdWithError("network", "connect", "bridge", "bb")
  1323. c.Assert(err, check.NotNil)
  1324. ns1 := inspectField(c, "bb", "NetworkSettings.Networks.bridge")
  1325. c.Assert(ns1, check.Equals, ns0)
  1326. }
  1327. func (s *DockerSuite) TestDockerNetworkInternalMode(c *check.C) {
  1328. dockerCmd(c, "network", "create", "--driver=bridge", "--internal", "internal")
  1329. assertNwIsAvailable(c, "internal")
  1330. nr := getNetworkResource(c, "internal")
  1331. c.Assert(nr.Internal, checker.True)
  1332. dockerCmd(c, "run", "-d", "--net=internal", "--name=first", "busybox", "top")
  1333. c.Assert(waitRun("first"), check.IsNil)
  1334. dockerCmd(c, "run", "-d", "--net=internal", "--name=second", "busybox", "top")
  1335. c.Assert(waitRun("second"), check.IsNil)
  1336. out, _, err := dockerCmdWithError("exec", "first", "ping", "-W", "4", "-c", "1", "www.google.com")
  1337. c.Assert(err, check.NotNil)
  1338. c.Assert(out, checker.Contains, "ping: bad address")
  1339. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  1340. c.Assert(err, check.IsNil)
  1341. }
  1342. // Test for special characters in network names. only [a-zA-Z0-9][a-zA-Z0-9_.-] are
  1343. // valid characters
  1344. func (s *DockerNetworkSuite) TestDockerNetworkCreateDeleteSpecialCharacters(c *check.C) {
  1345. _, _, err := dockerCmdWithError("network", "create", "test@#$")
  1346. c.Assert(err, check.NotNil)
  1347. dockerCmd(c, "network", "create", "test-1_0.net")
  1348. assertNwIsAvailable(c, "test-1_0.net")
  1349. dockerCmd(c, "network", "rm", "test-1_0.net")
  1350. assertNwNotAvailable(c, "test-1_0.net")
  1351. }
  1352. func (s *DockerDaemonSuite) TestDaemonRestartRestoreBridgeNetwork(t *check.C) {
  1353. testRequires(t, DaemonIsLinux)
  1354. s.d.StartWithBusybox(t, "--live-restore")
  1355. defer s.d.Stop(t)
  1356. oldCon := "old"
  1357. _, err := s.d.Cmd("run", "-d", "--name", oldCon, "-p", "80:80", "busybox", "top")
  1358. if err != nil {
  1359. t.Fatal(err)
  1360. }
  1361. oldContainerIP, err := s.d.Cmd("inspect", "-f", "{{ .NetworkSettings.Networks.bridge.IPAddress }}", oldCon)
  1362. if err != nil {
  1363. t.Fatal(err)
  1364. }
  1365. // Kill the daemon
  1366. if err := s.d.Kill(); err != nil {
  1367. t.Fatal(err)
  1368. }
  1369. // restart the daemon
  1370. s.d.Start(t, "--live-restore")
  1371. // start a new container, the new container's ip should not be the same with
  1372. // old running container.
  1373. newCon := "new"
  1374. _, err = s.d.Cmd("run", "-d", "--name", newCon, "busybox", "top")
  1375. if err != nil {
  1376. t.Fatal(err)
  1377. }
  1378. newContainerIP, err := s.d.Cmd("inspect", "-f", "{{ .NetworkSettings.Networks.bridge.IPAddress }}", newCon)
  1379. if err != nil {
  1380. t.Fatal(err)
  1381. }
  1382. if strings.Compare(strings.TrimSpace(oldContainerIP), strings.TrimSpace(newContainerIP)) == 0 {
  1383. t.Fatalf("new container ip should not equal to old running container ip")
  1384. }
  1385. // start a new container, the new container should ping old running container
  1386. _, err = s.d.Cmd("run", "-t", "busybox", "ping", "-c", "1", oldContainerIP)
  1387. if err != nil {
  1388. t.Fatal(err)
  1389. }
  1390. // start a new container, trying to publish port 80:80 should fail
  1391. out, err := s.d.Cmd("run", "-p", "80:80", "-d", "busybox", "top")
  1392. if err == nil || !strings.Contains(out, "Bind for 0.0.0.0:80 failed: port is already allocated") {
  1393. t.Fatalf("80 port is allocated to old running container, it should failed on allocating to new container")
  1394. }
  1395. // kill old running container and try to allocate again
  1396. _, err = s.d.Cmd("kill", oldCon)
  1397. if err != nil {
  1398. t.Fatal(err)
  1399. }
  1400. id, err := s.d.Cmd("run", "-p", "80:80", "-d", "busybox", "top")
  1401. if err != nil {
  1402. t.Fatal(err)
  1403. }
  1404. // Cleanup because these containers will not be shut down by daemon
  1405. out, err = s.d.Cmd("stop", newCon)
  1406. if err != nil {
  1407. t.Fatalf("err: %v %v", err, string(out))
  1408. }
  1409. _, err = s.d.Cmd("stop", strings.TrimSpace(id))
  1410. if err != nil {
  1411. t.Fatal(err)
  1412. }
  1413. }
  1414. func (s *DockerNetworkSuite) TestDockerNetworkFlagAlias(c *check.C) {
  1415. dockerCmd(c, "network", "create", "user")
  1416. output, status := dockerCmd(c, "run", "--rm", "--network=user", "--network-alias=foo", "busybox", "true")
  1417. c.Assert(status, checker.Equals, 0, check.Commentf("unexpected status code %d (%s)", status, output))
  1418. output, status, _ = dockerCmdWithError("run", "--rm", "--net=user", "--network=user", "busybox", "true")
  1419. c.Assert(status, checker.Equals, 0, check.Commentf("unexpected status code %d (%s)", status, output))
  1420. output, status, _ = dockerCmdWithError("run", "--rm", "--network=user", "--net-alias=foo", "--network-alias=bar", "busybox", "true")
  1421. c.Assert(status, checker.Equals, 0, check.Commentf("unexpected status code %d (%s)", status, output))
  1422. }
  1423. func (s *DockerNetworkSuite) TestDockerNetworkValidateIP(c *check.C) {
  1424. _, _, err := dockerCmdWithError("network", "create", "--ipv6", "--subnet=172.28.0.0/16", "--subnet=2001:db8:1234::/64", "mynet")
  1425. c.Assert(err, check.IsNil)
  1426. assertNwIsAvailable(c, "mynet")
  1427. _, _, err = dockerCmdWithError("run", "-d", "--name", "mynet0", "--net=mynet", "--ip", "172.28.99.88", "--ip6", "2001:db8:1234::9988", "busybox", "top")
  1428. c.Assert(err, check.IsNil)
  1429. c.Assert(waitRun("mynet0"), check.IsNil)
  1430. verifyIPAddressConfig(c, "mynet0", "mynet", "172.28.99.88", "2001:db8:1234::9988")
  1431. verifyIPAddresses(c, "mynet0", "mynet", "172.28.99.88", "2001:db8:1234::9988")
  1432. _, _, err = dockerCmdWithError("run", "--net=mynet", "--ip", "mynet_ip", "--ip6", "2001:db8:1234::9999", "busybox", "top")
  1433. c.Assert(err.Error(), checker.Contains, "invalid IPv4 address")
  1434. _, _, err = dockerCmdWithError("run", "--net=mynet", "--ip", "172.28.99.99", "--ip6", "mynet_ip6", "busybox", "top")
  1435. c.Assert(err.Error(), checker.Contains, "invalid IPv6 address")
  1436. // This is a case of IPv4 address to `--ip6`
  1437. _, _, err = dockerCmdWithError("run", "--net=mynet", "--ip6", "172.28.99.99", "busybox", "top")
  1438. c.Assert(err.Error(), checker.Contains, "invalid IPv6 address")
  1439. // This is a special case of an IPv4-mapped IPv6 address
  1440. _, _, err = dockerCmdWithError("run", "--net=mynet", "--ip6", "::ffff:172.28.99.99", "busybox", "top")
  1441. c.Assert(err.Error(), checker.Contains, "invalid IPv6 address")
  1442. }
  1443. // Test case for 26220
  1444. func (s *DockerNetworkSuite) TestDockerNetworkDisconnectFromBridge(c *check.C) {
  1445. out, _ := dockerCmd(c, "network", "inspect", "--format", "{{.Id}}", "bridge")
  1446. network := strings.TrimSpace(out)
  1447. name := "test"
  1448. dockerCmd(c, "create", "--name", name, "busybox", "top")
  1449. _, _, err := dockerCmdWithError("network", "disconnect", network, name)
  1450. c.Assert(err, check.IsNil)
  1451. }