docker_cli_network_unix_test.go 70 KB

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