docker_cli_network_unix_test.go 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  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/daemon"
  17. "github.com/docker/docker/pkg/integration/checker"
  18. icmd "github.com/docker/docker/pkg/integration/cmd"
  19. "github.com/docker/docker/pkg/stringid"
  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: experimentalDaemon,
  45. })
  46. }
  47. func (s *DockerNetworkSuite) TearDownTest(c *check.C) {
  48. if s.d != nil {
  49. s.d.Stop()
  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(networkResources, checker.HasLen, 1)
  421. // Should print an error and return an exitCode, nothing else
  422. result = dockerCmdWithResult("network", "inspect", "nonexistent")
  423. c.Assert(result, icmd.Matches, icmd.Expected{
  424. ExitCode: 1,
  425. Err: "Error: No such network: nonexistent",
  426. Out: "[]",
  427. })
  428. }
  429. func (s *DockerSuite) TestDockerInspectNetworkWithContainerName(c *check.C) {
  430. dockerCmd(c, "network", "create", "brNetForInspect")
  431. assertNwIsAvailable(c, "brNetForInspect")
  432. defer func() {
  433. dockerCmd(c, "network", "rm", "brNetForInspect")
  434. assertNwNotAvailable(c, "brNetForInspect")
  435. }()
  436. out, _ := dockerCmd(c, "run", "-d", "--name", "testNetInspect1", "--net", "brNetForInspect", "busybox", "top")
  437. c.Assert(waitRun("testNetInspect1"), check.IsNil)
  438. containerID := strings.TrimSpace(out)
  439. defer func() {
  440. // we don't stop container by name, because we'll rename it later
  441. dockerCmd(c, "stop", containerID)
  442. }()
  443. out, _ = dockerCmd(c, "network", "inspect", "brNetForInspect")
  444. networkResources := []types.NetworkResource{}
  445. err := json.Unmarshal([]byte(out), &networkResources)
  446. c.Assert(err, check.IsNil)
  447. c.Assert(networkResources, checker.HasLen, 1)
  448. container, ok := networkResources[0].Containers[containerID]
  449. c.Assert(ok, checker.True)
  450. c.Assert(container.Name, checker.Equals, "testNetInspect1")
  451. // rename container and check docker inspect output update
  452. newName := "HappyNewName"
  453. dockerCmd(c, "rename", "testNetInspect1", newName)
  454. // check whether network inspect works properly
  455. out, _ = dockerCmd(c, "network", "inspect", "brNetForInspect")
  456. newNetRes := []types.NetworkResource{}
  457. err = json.Unmarshal([]byte(out), &newNetRes)
  458. c.Assert(err, check.IsNil)
  459. c.Assert(newNetRes, checker.HasLen, 1)
  460. container1, ok := newNetRes[0].Containers[containerID]
  461. c.Assert(ok, checker.True)
  462. c.Assert(container1.Name, checker.Equals, newName)
  463. }
  464. func (s *DockerNetworkSuite) TestDockerNetworkConnectDisconnect(c *check.C) {
  465. dockerCmd(c, "network", "create", "test")
  466. assertNwIsAvailable(c, "test")
  467. nr := getNwResource(c, "test")
  468. c.Assert(nr.Name, checker.Equals, "test")
  469. c.Assert(len(nr.Containers), checker.Equals, 0)
  470. // run a container
  471. out, _ := dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
  472. c.Assert(waitRun("test"), check.IsNil)
  473. containerID := strings.TrimSpace(out)
  474. // connect the container to the test network
  475. dockerCmd(c, "network", "connect", "test", containerID)
  476. // inspect the network to make sure container is connected
  477. nr = getNetworkResource(c, nr.ID)
  478. c.Assert(len(nr.Containers), checker.Equals, 1)
  479. c.Assert(nr.Containers[containerID], check.NotNil)
  480. // check if container IP matches network inspect
  481. ip, _, err := net.ParseCIDR(nr.Containers[containerID].IPv4Address)
  482. c.Assert(err, check.IsNil)
  483. containerIP := findContainerIP(c, "test", "test")
  484. c.Assert(ip.String(), checker.Equals, containerIP)
  485. // disconnect container from the network
  486. dockerCmd(c, "network", "disconnect", "test", containerID)
  487. nr = getNwResource(c, "test")
  488. c.Assert(nr.Name, checker.Equals, "test")
  489. c.Assert(len(nr.Containers), checker.Equals, 0)
  490. // run another container
  491. out, _ = dockerCmd(c, "run", "-d", "--net", "test", "--name", "test2", "busybox", "top")
  492. c.Assert(waitRun("test2"), check.IsNil)
  493. containerID = strings.TrimSpace(out)
  494. nr = getNwResource(c, "test")
  495. c.Assert(nr.Name, checker.Equals, "test")
  496. c.Assert(len(nr.Containers), checker.Equals, 1)
  497. // force disconnect the container to the test network
  498. dockerCmd(c, "network", "disconnect", "-f", "test", containerID)
  499. nr = getNwResource(c, "test")
  500. c.Assert(nr.Name, checker.Equals, "test")
  501. c.Assert(len(nr.Containers), checker.Equals, 0)
  502. dockerCmd(c, "network", "rm", "test")
  503. assertNwNotAvailable(c, "test")
  504. }
  505. func (s *DockerNetworkSuite) TestDockerNetworkIPAMMultipleNetworks(c *check.C) {
  506. // test0 bridge network
  507. dockerCmd(c, "network", "create", "--subnet=192.168.0.0/16", "test1")
  508. assertNwIsAvailable(c, "test1")
  509. // test2 bridge network does not overlap
  510. dockerCmd(c, "network", "create", "--subnet=192.169.0.0/16", "test2")
  511. assertNwIsAvailable(c, "test2")
  512. // for networks w/o ipam specified, docker will choose proper non-overlapping subnets
  513. dockerCmd(c, "network", "create", "test3")
  514. assertNwIsAvailable(c, "test3")
  515. dockerCmd(c, "network", "create", "test4")
  516. assertNwIsAvailable(c, "test4")
  517. dockerCmd(c, "network", "create", "test5")
  518. assertNwIsAvailable(c, "test5")
  519. // test network with multiple subnets
  520. // bridge network doesn't support multiple subnets. hence, use a dummy driver that supports
  521. dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, "--subnet=192.168.0.0/16", "--subnet=192.170.0.0/16", "test6")
  522. assertNwIsAvailable(c, "test6")
  523. // test network with multiple subnets with valid ipam combinations
  524. // also check same subnet across networks when the driver supports it.
  525. dockerCmd(c, "network", "create", "-d", dummyNetworkDriver,
  526. "--subnet=192.168.0.0/16", "--subnet=192.170.0.0/16",
  527. "--gateway=192.168.0.100", "--gateway=192.170.0.100",
  528. "--ip-range=192.168.1.0/24",
  529. "--aux-address", "a=192.168.1.5", "--aux-address", "b=192.168.1.6",
  530. "--aux-address", "c=192.170.1.5", "--aux-address", "d=192.170.1.6",
  531. "test7")
  532. assertNwIsAvailable(c, "test7")
  533. // cleanup
  534. for i := 1; i < 8; i++ {
  535. dockerCmd(c, "network", "rm", fmt.Sprintf("test%d", i))
  536. }
  537. }
  538. func (s *DockerNetworkSuite) TestDockerNetworkCustomIPAM(c *check.C) {
  539. // Create a bridge network using custom ipam driver
  540. dockerCmd(c, "network", "create", "--ipam-driver", dummyIPAMDriver, "br0")
  541. assertNwIsAvailable(c, "br0")
  542. // Verify expected network ipam fields are there
  543. nr := getNetworkResource(c, "br0")
  544. c.Assert(nr.Driver, checker.Equals, "bridge")
  545. c.Assert(nr.IPAM.Driver, checker.Equals, dummyIPAMDriver)
  546. // remove network and exercise remote ipam driver
  547. dockerCmd(c, "network", "rm", "br0")
  548. assertNwNotAvailable(c, "br0")
  549. }
  550. func (s *DockerNetworkSuite) TestDockerNetworkIPAMOptions(c *check.C) {
  551. // Create a bridge network using custom ipam driver and options
  552. dockerCmd(c, "network", "create", "--ipam-driver", dummyIPAMDriver, "--ipam-opt", "opt1=drv1", "--ipam-opt", "opt2=drv2", "br0")
  553. assertNwIsAvailable(c, "br0")
  554. // Verify expected network ipam options
  555. nr := getNetworkResource(c, "br0")
  556. opts := nr.IPAM.Options
  557. c.Assert(opts["opt1"], checker.Equals, "drv1")
  558. c.Assert(opts["opt2"], checker.Equals, "drv2")
  559. }
  560. func (s *DockerNetworkSuite) TestDockerNetworkInspectDefault(c *check.C) {
  561. nr := getNetworkResource(c, "none")
  562. c.Assert(nr.Driver, checker.Equals, "null")
  563. c.Assert(nr.Scope, checker.Equals, "local")
  564. c.Assert(nr.Internal, checker.Equals, false)
  565. c.Assert(nr.EnableIPv6, checker.Equals, false)
  566. c.Assert(nr.IPAM.Driver, checker.Equals, "default")
  567. c.Assert(len(nr.IPAM.Config), checker.Equals, 0)
  568. nr = getNetworkResource(c, "host")
  569. c.Assert(nr.Driver, checker.Equals, "host")
  570. c.Assert(nr.Scope, checker.Equals, "local")
  571. c.Assert(nr.Internal, checker.Equals, false)
  572. c.Assert(nr.EnableIPv6, checker.Equals, false)
  573. c.Assert(nr.IPAM.Driver, checker.Equals, "default")
  574. c.Assert(len(nr.IPAM.Config), checker.Equals, 0)
  575. nr = getNetworkResource(c, "bridge")
  576. c.Assert(nr.Driver, checker.Equals, "bridge")
  577. c.Assert(nr.Scope, checker.Equals, "local")
  578. c.Assert(nr.Internal, checker.Equals, false)
  579. c.Assert(nr.EnableIPv6, checker.Equals, false)
  580. c.Assert(nr.IPAM.Driver, checker.Equals, "default")
  581. c.Assert(len(nr.IPAM.Config), checker.Equals, 1)
  582. c.Assert(nr.IPAM.Config[0].Subnet, checker.NotNil)
  583. c.Assert(nr.IPAM.Config[0].Gateway, checker.NotNil)
  584. }
  585. func (s *DockerNetworkSuite) TestDockerNetworkInspectCustomUnspecified(c *check.C) {
  586. // if unspecified, network subnet will be selected from inside preferred pool
  587. dockerCmd(c, "network", "create", "test01")
  588. assertNwIsAvailable(c, "test01")
  589. nr := getNetworkResource(c, "test01")
  590. c.Assert(nr.Driver, checker.Equals, "bridge")
  591. c.Assert(nr.Scope, checker.Equals, "local")
  592. c.Assert(nr.Internal, checker.Equals, false)
  593. c.Assert(nr.EnableIPv6, checker.Equals, false)
  594. c.Assert(nr.IPAM.Driver, checker.Equals, "default")
  595. c.Assert(len(nr.IPAM.Config), checker.Equals, 1)
  596. c.Assert(nr.IPAM.Config[0].Subnet, checker.NotNil)
  597. c.Assert(nr.IPAM.Config[0].Gateway, checker.NotNil)
  598. dockerCmd(c, "network", "rm", "test01")
  599. assertNwNotAvailable(c, "test01")
  600. }
  601. func (s *DockerNetworkSuite) TestDockerNetworkInspectCustomSpecified(c *check.C) {
  602. 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")
  603. assertNwIsAvailable(c, "br0")
  604. nr := getNetworkResource(c, "br0")
  605. c.Assert(nr.Driver, checker.Equals, "bridge")
  606. c.Assert(nr.Scope, checker.Equals, "local")
  607. c.Assert(nr.Internal, checker.Equals, false)
  608. c.Assert(nr.EnableIPv6, checker.Equals, true)
  609. c.Assert(nr.IPAM.Driver, checker.Equals, "default")
  610. c.Assert(len(nr.IPAM.Config), checker.Equals, 2)
  611. c.Assert(nr.IPAM.Config[0].Subnet, checker.Equals, "172.28.0.0/16")
  612. c.Assert(nr.IPAM.Config[0].IPRange, checker.Equals, "172.28.5.0/24")
  613. c.Assert(nr.IPAM.Config[0].Gateway, checker.Equals, "172.28.5.254")
  614. c.Assert(nr.Internal, checker.False)
  615. dockerCmd(c, "network", "rm", "br0")
  616. assertNwNotAvailable(c, "test01")
  617. }
  618. func (s *DockerNetworkSuite) TestDockerNetworkIPAMInvalidCombinations(c *check.C) {
  619. // network with ip-range out of subnet range
  620. _, _, err := dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--ip-range=192.170.0.0/16", "test")
  621. c.Assert(err, check.NotNil)
  622. // network with multiple gateways for a single subnet
  623. _, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--gateway=192.168.0.1", "--gateway=192.168.0.2", "test")
  624. c.Assert(err, check.NotNil)
  625. // Multiple overlapping subnets in the same network must fail
  626. _, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--subnet=192.168.1.0/16", "test")
  627. c.Assert(err, check.NotNil)
  628. // overlapping subnets across networks must fail
  629. // create a valid test0 network
  630. dockerCmd(c, "network", "create", "--subnet=192.168.0.0/16", "test0")
  631. assertNwIsAvailable(c, "test0")
  632. // create an overlapping test1 network
  633. _, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.128.0/17", "test1")
  634. c.Assert(err, check.NotNil)
  635. dockerCmd(c, "network", "rm", "test0")
  636. assertNwNotAvailable(c, "test0")
  637. }
  638. func (s *DockerNetworkSuite) TestDockerNetworkDriverOptions(c *check.C) {
  639. dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, "-o", "opt1=drv1", "-o", "opt2=drv2", "testopt")
  640. assertNwIsAvailable(c, "testopt")
  641. gopts := remoteDriverNetworkRequest.Options[netlabel.GenericData]
  642. c.Assert(gopts, checker.NotNil)
  643. opts, ok := gopts.(map[string]interface{})
  644. c.Assert(ok, checker.Equals, true)
  645. c.Assert(opts["opt1"], checker.Equals, "drv1")
  646. c.Assert(opts["opt2"], checker.Equals, "drv2")
  647. dockerCmd(c, "network", "rm", "testopt")
  648. assertNwNotAvailable(c, "testopt")
  649. }
  650. func (s *DockerNetworkSuite) TestDockerPluginV2NetworkDriver(c *check.C) {
  651. testRequires(c, DaemonIsLinux, Network, IsAmd64)
  652. var (
  653. npName = "tiborvass/test-docker-netplugin"
  654. npTag = "latest"
  655. npNameWithTag = npName + ":" + npTag
  656. )
  657. _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", npNameWithTag)
  658. c.Assert(err, checker.IsNil)
  659. out, _, err := dockerCmdWithError("plugin", "ls")
  660. c.Assert(err, checker.IsNil)
  661. c.Assert(out, checker.Contains, npName)
  662. c.Assert(out, checker.Contains, npTag)
  663. c.Assert(out, checker.Contains, "true")
  664. dockerCmd(c, "network", "create", "-d", npNameWithTag, "v2net")
  665. assertNwIsAvailable(c, "v2net")
  666. dockerCmd(c, "network", "rm", "v2net")
  667. assertNwNotAvailable(c, "v2net")
  668. }
  669. func (s *DockerDaemonSuite) TestDockerNetworkNoDiscoveryDefaultBridgeNetwork(c *check.C) {
  670. testRequires(c, ExecSupport)
  671. // On default bridge network built-in service discovery should not happen
  672. hostsFile := "/etc/hosts"
  673. bridgeName := "external-bridge"
  674. bridgeIP := "192.169.255.254/24"
  675. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  676. c.Assert(err, check.IsNil, check.Commentf(out))
  677. defer deleteInterface(c, bridgeName)
  678. err = s.d.StartWithBusybox("--bridge", bridgeName)
  679. c.Assert(err, check.IsNil)
  680. defer s.d.Restart()
  681. // run two containers and store first container's etc/hosts content
  682. out, err = s.d.Cmd("run", "-d", "busybox", "top")
  683. c.Assert(err, check.IsNil)
  684. cid1 := strings.TrimSpace(out)
  685. defer s.d.Cmd("stop", cid1)
  686. hosts, err := s.d.Cmd("exec", cid1, "cat", hostsFile)
  687. c.Assert(err, checker.IsNil)
  688. out, err = s.d.Cmd("run", "-d", "--name", "container2", "busybox", "top")
  689. c.Assert(err, check.IsNil)
  690. cid2 := strings.TrimSpace(out)
  691. // verify first container's etc/hosts file has not changed after spawning the second named container
  692. hostsPost, err := s.d.Cmd("exec", cid1, "cat", hostsFile)
  693. c.Assert(err, checker.IsNil)
  694. c.Assert(string(hosts), checker.Equals, string(hostsPost),
  695. check.Commentf("Unexpected %s change on second container creation", hostsFile))
  696. // stop container 2 and verify first container's etc/hosts has not changed
  697. _, err = s.d.Cmd("stop", cid2)
  698. c.Assert(err, check.IsNil)
  699. hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
  700. c.Assert(err, checker.IsNil)
  701. c.Assert(string(hosts), checker.Equals, string(hostsPost),
  702. check.Commentf("Unexpected %s change on second container creation", hostsFile))
  703. // but discovery is on when connecting to non default bridge network
  704. network := "anotherbridge"
  705. out, err = s.d.Cmd("network", "create", network)
  706. c.Assert(err, check.IsNil, check.Commentf(out))
  707. defer s.d.Cmd("network", "rm", network)
  708. out, err = s.d.Cmd("network", "connect", network, cid1)
  709. c.Assert(err, check.IsNil, check.Commentf(out))
  710. hosts, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
  711. c.Assert(err, checker.IsNil)
  712. hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
  713. c.Assert(err, checker.IsNil)
  714. c.Assert(string(hosts), checker.Equals, string(hostsPost),
  715. check.Commentf("Unexpected %s change on second network connection", hostsFile))
  716. }
  717. func (s *DockerNetworkSuite) TestDockerNetworkAnonymousEndpoint(c *check.C) {
  718. testRequires(c, ExecSupport, NotArm)
  719. hostsFile := "/etc/hosts"
  720. cstmBridgeNw := "custom-bridge-nw"
  721. cstmBridgeNw1 := "custom-bridge-nw1"
  722. dockerCmd(c, "network", "create", "-d", "bridge", cstmBridgeNw)
  723. assertNwIsAvailable(c, cstmBridgeNw)
  724. // run two anonymous containers and store their etc/hosts content
  725. out, _ := dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "busybox", "top")
  726. cid1 := strings.TrimSpace(out)
  727. hosts1, err := readContainerFileWithExec(cid1, hostsFile)
  728. c.Assert(err, checker.IsNil)
  729. out, _ = dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "busybox", "top")
  730. cid2 := strings.TrimSpace(out)
  731. hosts2, err := readContainerFileWithExec(cid2, hostsFile)
  732. c.Assert(err, checker.IsNil)
  733. // verify first container etc/hosts file has not changed
  734. hosts1post, err := readContainerFileWithExec(cid1, hostsFile)
  735. c.Assert(err, checker.IsNil)
  736. c.Assert(string(hosts1), checker.Equals, string(hosts1post),
  737. check.Commentf("Unexpected %s change on anonymous container creation", hostsFile))
  738. // Connect the 2nd container to a new network and verify the
  739. // first container /etc/hosts file still hasn't changed.
  740. dockerCmd(c, "network", "create", "-d", "bridge", cstmBridgeNw1)
  741. assertNwIsAvailable(c, cstmBridgeNw1)
  742. dockerCmd(c, "network", "connect", cstmBridgeNw1, cid2)
  743. hosts2, err = readContainerFileWithExec(cid2, hostsFile)
  744. c.Assert(err, checker.IsNil)
  745. hosts1post, err = readContainerFileWithExec(cid1, hostsFile)
  746. c.Assert(err, checker.IsNil)
  747. c.Assert(string(hosts1), checker.Equals, string(hosts1post),
  748. check.Commentf("Unexpected %s change on container connect", hostsFile))
  749. // start a named container
  750. cName := "AnyName"
  751. out, _ = dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "--name", cName, "busybox", "top")
  752. cid3 := strings.TrimSpace(out)
  753. // verify that container 1 and 2 can ping the named container
  754. dockerCmd(c, "exec", cid1, "ping", "-c", "1", cName)
  755. dockerCmd(c, "exec", cid2, "ping", "-c", "1", cName)
  756. // Stop named container and verify first two containers' etc/hosts file hasn't changed
  757. dockerCmd(c, "stop", cid3)
  758. hosts1post, err = readContainerFileWithExec(cid1, hostsFile)
  759. c.Assert(err, checker.IsNil)
  760. c.Assert(string(hosts1), checker.Equals, string(hosts1post),
  761. check.Commentf("Unexpected %s change on name container creation", hostsFile))
  762. hosts2post, err := readContainerFileWithExec(cid2, hostsFile)
  763. c.Assert(err, checker.IsNil)
  764. c.Assert(string(hosts2), checker.Equals, string(hosts2post),
  765. check.Commentf("Unexpected %s change on name container creation", hostsFile))
  766. // verify that container 1 and 2 can't ping the named container now
  767. _, _, err = dockerCmdWithError("exec", cid1, "ping", "-c", "1", cName)
  768. c.Assert(err, check.NotNil)
  769. _, _, err = dockerCmdWithError("exec", cid2, "ping", "-c", "1", cName)
  770. c.Assert(err, check.NotNil)
  771. }
  772. func (s *DockerNetworkSuite) TestDockerNetworkLinkOnDefaultNetworkOnly(c *check.C) {
  773. // Legacy Link feature must work only on default network, and not across networks
  774. cnt1 := "container1"
  775. cnt2 := "container2"
  776. network := "anotherbridge"
  777. // Run first container on default network
  778. dockerCmd(c, "run", "-d", "--name", cnt1, "busybox", "top")
  779. // Create another network and run the second container on it
  780. dockerCmd(c, "network", "create", network)
  781. assertNwIsAvailable(c, network)
  782. dockerCmd(c, "run", "-d", "--net", network, "--name", cnt2, "busybox", "top")
  783. // Try launching a container on default network, linking to the first container. Must succeed
  784. dockerCmd(c, "run", "-d", "--link", fmt.Sprintf("%s:%s", cnt1, cnt1), "busybox", "top")
  785. // Try launching a container on default network, linking to the second container. Must fail
  786. _, _, err := dockerCmdWithError("run", "-d", "--link", fmt.Sprintf("%s:%s", cnt2, cnt2), "busybox", "top")
  787. c.Assert(err, checker.NotNil)
  788. // Connect second container to default network. Now a container on default network can link to it
  789. dockerCmd(c, "network", "connect", "bridge", cnt2)
  790. dockerCmd(c, "run", "-d", "--link", fmt.Sprintf("%s:%s", cnt2, cnt2), "busybox", "top")
  791. }
  792. func (s *DockerNetworkSuite) TestDockerNetworkOverlayPortMapping(c *check.C) {
  793. // Verify exposed ports are present in ps output when running a container on
  794. // a network managed by a driver which does not provide the default gateway
  795. // for the container
  796. nwn := "ov"
  797. ctn := "bb"
  798. port1 := 80
  799. port2 := 443
  800. expose1 := fmt.Sprintf("--expose=%d", port1)
  801. expose2 := fmt.Sprintf("--expose=%d", port2)
  802. dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, nwn)
  803. assertNwIsAvailable(c, nwn)
  804. dockerCmd(c, "run", "-d", "--net", nwn, "--name", ctn, expose1, expose2, "busybox", "top")
  805. // Check docker ps o/p for last created container reports the unpublished ports
  806. unpPort1 := fmt.Sprintf("%d/tcp", port1)
  807. unpPort2 := fmt.Sprintf("%d/tcp", port2)
  808. out, _ := dockerCmd(c, "ps", "-n=1")
  809. // Missing unpublished ports in docker ps output
  810. c.Assert(out, checker.Contains, unpPort1)
  811. // Missing unpublished ports in docker ps output
  812. c.Assert(out, checker.Contains, unpPort2)
  813. }
  814. func (s *DockerNetworkSuite) TestDockerNetworkDriverUngracefulRestart(c *check.C) {
  815. testRequires(c, DaemonIsLinux, NotUserNamespace)
  816. dnd := "dnd"
  817. did := "did"
  818. mux := http.NewServeMux()
  819. server := httptest.NewServer(mux)
  820. setupRemoteNetworkDrivers(c, mux, server.URL, dnd, did)
  821. s.d.StartWithBusybox()
  822. _, err := s.d.Cmd("network", "create", "-d", dnd, "--subnet", "1.1.1.0/24", "net1")
  823. c.Assert(err, checker.IsNil)
  824. _, err = s.d.Cmd("run", "-itd", "--net", "net1", "--name", "foo", "--ip", "1.1.1.10", "busybox", "sh")
  825. c.Assert(err, checker.IsNil)
  826. // Kill daemon and restart
  827. c.Assert(s.d.Kill(), checker.IsNil)
  828. server.Close()
  829. startTime := time.Now().Unix()
  830. if err = s.d.Restart(); err != nil {
  831. c.Fatal(err)
  832. }
  833. lapse := time.Now().Unix() - startTime
  834. if lapse > 60 {
  835. // In normal scenarios, daemon restart takes ~1 second.
  836. // Plugin retry mechanism can delay the daemon start. systemd may not like it.
  837. // Avoid accessing plugins during daemon bootup
  838. c.Logf("daemon restart took too long : %d seconds", lapse)
  839. }
  840. // Restart the custom dummy plugin
  841. mux = http.NewServeMux()
  842. server = httptest.NewServer(mux)
  843. setupRemoteNetworkDrivers(c, mux, server.URL, dnd, did)
  844. // trying to reuse the same ip must succeed
  845. _, err = s.d.Cmd("run", "-itd", "--net", "net1", "--name", "bar", "--ip", "1.1.1.10", "busybox", "sh")
  846. c.Assert(err, checker.IsNil)
  847. }
  848. func (s *DockerNetworkSuite) TestDockerNetworkMacInspect(c *check.C) {
  849. // Verify endpoint MAC address is correctly populated in container's network settings
  850. nwn := "ov"
  851. ctn := "bb"
  852. dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, nwn)
  853. assertNwIsAvailable(c, nwn)
  854. dockerCmd(c, "run", "-d", "--net", nwn, "--name", ctn, "busybox", "top")
  855. mac := inspectField(c, ctn, "NetworkSettings.Networks."+nwn+".MacAddress")
  856. c.Assert(mac, checker.Equals, "a0:b1:c2:d3:e4:f5")
  857. }
  858. func (s *DockerSuite) TestInspectAPIMultipleNetworks(c *check.C) {
  859. dockerCmd(c, "network", "create", "mybridge1")
  860. dockerCmd(c, "network", "create", "mybridge2")
  861. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  862. id := strings.TrimSpace(out)
  863. c.Assert(waitRun(id), check.IsNil)
  864. dockerCmd(c, "network", "connect", "mybridge1", id)
  865. dockerCmd(c, "network", "connect", "mybridge2", id)
  866. body := getInspectBody(c, "v1.20", id)
  867. var inspect120 v1p20.ContainerJSON
  868. err := json.Unmarshal(body, &inspect120)
  869. c.Assert(err, checker.IsNil)
  870. versionedIP := inspect120.NetworkSettings.IPAddress
  871. body = getInspectBody(c, "v1.21", id)
  872. var inspect121 types.ContainerJSON
  873. err = json.Unmarshal(body, &inspect121)
  874. c.Assert(err, checker.IsNil)
  875. c.Assert(inspect121.NetworkSettings.Networks, checker.HasLen, 3)
  876. bridge := inspect121.NetworkSettings.Networks["bridge"]
  877. c.Assert(bridge.IPAddress, checker.Equals, versionedIP)
  878. c.Assert(bridge.IPAddress, checker.Equals, inspect121.NetworkSettings.IPAddress)
  879. }
  880. func connectContainerToNetworks(c *check.C, d *daemon.Daemon, cName string, nws []string) {
  881. // Run a container on the default network
  882. out, err := d.Cmd("run", "-d", "--name", cName, "busybox", "top")
  883. c.Assert(err, checker.IsNil, check.Commentf(out))
  884. // Attach the container to other networks
  885. for _, nw := range nws {
  886. out, err = d.Cmd("network", "create", nw)
  887. c.Assert(err, checker.IsNil, check.Commentf(out))
  888. out, err = d.Cmd("network", "connect", nw, cName)
  889. c.Assert(err, checker.IsNil, check.Commentf(out))
  890. }
  891. }
  892. func verifyContainerIsConnectedToNetworks(c *check.C, d *daemon.Daemon, cName string, nws []string) {
  893. // Verify container is connected to all the networks
  894. for _, nw := range nws {
  895. out, err := d.Cmd("inspect", "-f", fmt.Sprintf("{{.NetworkSettings.Networks.%s}}", nw), cName)
  896. c.Assert(err, checker.IsNil, check.Commentf(out))
  897. c.Assert(out, checker.Not(checker.Equals), "<no value>\n")
  898. }
  899. }
  900. func (s *DockerNetworkSuite) TestDockerNetworkMultipleNetworksGracefulDaemonRestart(c *check.C) {
  901. cName := "bb"
  902. nwList := []string{"nw1", "nw2", "nw3"}
  903. s.d.StartWithBusybox()
  904. connectContainerToNetworks(c, s.d, cName, nwList)
  905. verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
  906. // Reload daemon
  907. s.d.Restart()
  908. _, err := s.d.Cmd("start", cName)
  909. c.Assert(err, checker.IsNil)
  910. verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
  911. }
  912. func (s *DockerNetworkSuite) TestDockerNetworkMultipleNetworksUngracefulDaemonRestart(c *check.C) {
  913. cName := "cc"
  914. nwList := []string{"nw1", "nw2", "nw3"}
  915. s.d.StartWithBusybox()
  916. connectContainerToNetworks(c, s.d, cName, nwList)
  917. verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
  918. // Kill daemon and restart
  919. c.Assert(s.d.Kill(), checker.IsNil)
  920. c.Assert(s.d.Restart(), checker.IsNil)
  921. // Restart container
  922. _, err := s.d.Cmd("start", cName)
  923. c.Assert(err, checker.IsNil)
  924. verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
  925. }
  926. func (s *DockerNetworkSuite) TestDockerNetworkRunNetByID(c *check.C) {
  927. out, _ := dockerCmd(c, "network", "create", "one")
  928. containerOut, _, err := dockerCmdWithError("run", "-d", "--net", strings.TrimSpace(out), "busybox", "top")
  929. c.Assert(err, checker.IsNil, check.Commentf(containerOut))
  930. }
  931. func (s *DockerNetworkSuite) TestDockerNetworkHostModeUngracefulDaemonRestart(c *check.C) {
  932. testRequires(c, DaemonIsLinux, NotUserNamespace)
  933. s.d.StartWithBusybox()
  934. // Run a few containers on host network
  935. for i := 0; i < 10; i++ {
  936. cName := fmt.Sprintf("hostc-%d", i)
  937. out, err := s.d.Cmd("run", "-d", "--name", cName, "--net=host", "--restart=always", "busybox", "top")
  938. c.Assert(err, checker.IsNil, check.Commentf(out))
  939. // verfiy container has finished starting before killing daemon
  940. err = s.d.WaitRun(cName)
  941. c.Assert(err, checker.IsNil)
  942. }
  943. // Kill daemon ungracefully and restart
  944. c.Assert(s.d.Kill(), checker.IsNil)
  945. c.Assert(s.d.Restart(), checker.IsNil)
  946. // make sure all the containers are up and running
  947. for i := 0; i < 10; i++ {
  948. err := s.d.WaitRun(fmt.Sprintf("hostc-%d", i))
  949. c.Assert(err, checker.IsNil)
  950. }
  951. }
  952. func (s *DockerNetworkSuite) TestDockerNetworkConnectToHostFromOtherNetwork(c *check.C) {
  953. dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  954. c.Assert(waitRun("container1"), check.IsNil)
  955. dockerCmd(c, "network", "disconnect", "bridge", "container1")
  956. out, _, err := dockerCmdWithError("network", "connect", "host", "container1")
  957. c.Assert(err, checker.NotNil, check.Commentf(out))
  958. c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetwork.Error())
  959. }
  960. func (s *DockerNetworkSuite) TestDockerNetworkDisconnectFromHost(c *check.C) {
  961. dockerCmd(c, "run", "-d", "--name", "container1", "--net=host", "busybox", "top")
  962. c.Assert(waitRun("container1"), check.IsNil)
  963. out, _, err := dockerCmdWithError("network", "disconnect", "host", "container1")
  964. c.Assert(err, checker.NotNil, check.Commentf("Should err out disconnect from host"))
  965. c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetwork.Error())
  966. }
  967. func (s *DockerNetworkSuite) TestDockerNetworkConnectWithPortMapping(c *check.C) {
  968. testRequires(c, NotArm)
  969. dockerCmd(c, "network", "create", "test1")
  970. dockerCmd(c, "run", "-d", "--name", "c1", "-p", "5000:5000", "busybox", "top")
  971. c.Assert(waitRun("c1"), check.IsNil)
  972. dockerCmd(c, "network", "connect", "test1", "c1")
  973. }
  974. func verifyPortMap(c *check.C, container, port, originalMapping string, mustBeEqual bool) {
  975. chk := checker.Equals
  976. if !mustBeEqual {
  977. chk = checker.Not(checker.Equals)
  978. }
  979. currentMapping, _ := dockerCmd(c, "port", container, port)
  980. c.Assert(currentMapping, chk, originalMapping)
  981. }
  982. func (s *DockerNetworkSuite) TestDockerNetworkConnectDisconnectWithPortMapping(c *check.C) {
  983. // Connect and disconnect a container with explicit and non-explicit
  984. // host port mapping to/from networks which do cause and do not cause
  985. // the container default gateway to change, and verify docker port cmd
  986. // returns congruent information
  987. testRequires(c, NotArm)
  988. cnt := "c1"
  989. dockerCmd(c, "network", "create", "aaa")
  990. dockerCmd(c, "network", "create", "ccc")
  991. dockerCmd(c, "run", "-d", "--name", cnt, "-p", "9000:90", "-p", "70", "busybox", "top")
  992. c.Assert(waitRun(cnt), check.IsNil)
  993. curPortMap, _ := dockerCmd(c, "port", cnt, "70")
  994. curExplPortMap, _ := dockerCmd(c, "port", cnt, "90")
  995. // Connect to a network which causes the container's default gw switch
  996. dockerCmd(c, "network", "connect", "aaa", cnt)
  997. verifyPortMap(c, cnt, "70", curPortMap, false)
  998. verifyPortMap(c, cnt, "90", curExplPortMap, true)
  999. // Read current mapping
  1000. curPortMap, _ = dockerCmd(c, "port", cnt, "70")
  1001. // Disconnect from a network which causes the container's default gw switch
  1002. dockerCmd(c, "network", "disconnect", "aaa", cnt)
  1003. verifyPortMap(c, cnt, "70", curPortMap, false)
  1004. verifyPortMap(c, cnt, "90", curExplPortMap, true)
  1005. // Read current mapping
  1006. curPortMap, _ = dockerCmd(c, "port", cnt, "70")
  1007. // Connect to a network which does not cause the container's default gw switch
  1008. dockerCmd(c, "network", "connect", "ccc", cnt)
  1009. verifyPortMap(c, cnt, "70", curPortMap, true)
  1010. verifyPortMap(c, cnt, "90", curExplPortMap, true)
  1011. }
  1012. func (s *DockerNetworkSuite) TestDockerNetworkConnectWithMac(c *check.C) {
  1013. macAddress := "02:42:ac:11:00:02"
  1014. dockerCmd(c, "network", "create", "mynetwork")
  1015. dockerCmd(c, "run", "--name=test", "-d", "--mac-address", macAddress, "busybox", "top")
  1016. c.Assert(waitRun("test"), check.IsNil)
  1017. mac1 := inspectField(c, "test", "NetworkSettings.Networks.bridge.MacAddress")
  1018. c.Assert(strings.TrimSpace(mac1), checker.Equals, macAddress)
  1019. dockerCmd(c, "network", "connect", "mynetwork", "test")
  1020. mac2 := inspectField(c, "test", "NetworkSettings.Networks.mynetwork.MacAddress")
  1021. c.Assert(strings.TrimSpace(mac2), checker.Not(checker.Equals), strings.TrimSpace(mac1))
  1022. }
  1023. func (s *DockerNetworkSuite) TestDockerNetworkInspectCreatedContainer(c *check.C) {
  1024. dockerCmd(c, "create", "--name", "test", "busybox")
  1025. networks := inspectField(c, "test", "NetworkSettings.Networks")
  1026. c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should return 'bridge' network"))
  1027. }
  1028. func (s *DockerNetworkSuite) TestDockerNetworkRestartWithMultipleNetworks(c *check.C) {
  1029. dockerCmd(c, "network", "create", "test")
  1030. dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top")
  1031. c.Assert(waitRun("foo"), checker.IsNil)
  1032. dockerCmd(c, "network", "connect", "test", "foo")
  1033. dockerCmd(c, "restart", "foo")
  1034. networks := inspectField(c, "foo", "NetworkSettings.Networks")
  1035. c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should contain 'bridge' network"))
  1036. c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' network"))
  1037. }
  1038. func (s *DockerNetworkSuite) TestDockerNetworkConnectDisconnectToStoppedContainer(c *check.C) {
  1039. dockerCmd(c, "network", "create", "test")
  1040. dockerCmd(c, "create", "--name=foo", "busybox", "top")
  1041. dockerCmd(c, "network", "connect", "test", "foo")
  1042. networks := inspectField(c, "foo", "NetworkSettings.Networks")
  1043. c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' network"))
  1044. // Restart docker daemon to test the config has persisted to disk
  1045. s.d.Restart()
  1046. networks = inspectField(c, "foo", "NetworkSettings.Networks")
  1047. c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' network"))
  1048. // start the container and test if we can ping it from another container in the same network
  1049. dockerCmd(c, "start", "foo")
  1050. c.Assert(waitRun("foo"), checker.IsNil)
  1051. ip := inspectField(c, "foo", "NetworkSettings.Networks.test.IPAddress")
  1052. ip = strings.TrimSpace(ip)
  1053. dockerCmd(c, "run", "--net=test", "busybox", "sh", "-c", fmt.Sprintf("ping -c 1 %s", ip))
  1054. dockerCmd(c, "stop", "foo")
  1055. // Test disconnect
  1056. dockerCmd(c, "network", "disconnect", "test", "foo")
  1057. networks = inspectField(c, "foo", "NetworkSettings.Networks")
  1058. c.Assert(networks, checker.Not(checker.Contains), "test", check.Commentf("Should not contain 'test' network"))
  1059. // Restart docker daemon to test the config has persisted to disk
  1060. s.d.Restart()
  1061. networks = inspectField(c, "foo", "NetworkSettings.Networks")
  1062. c.Assert(networks, checker.Not(checker.Contains), "test", check.Commentf("Should not contain 'test' network"))
  1063. }
  1064. func (s *DockerNetworkSuite) TestDockerNetworkDisconnectContainerNonexistingNetwork(c *check.C) {
  1065. dockerCmd(c, "network", "create", "test")
  1066. dockerCmd(c, "run", "--net=test", "-d", "--name=foo", "busybox", "top")
  1067. networks := inspectField(c, "foo", "NetworkSettings.Networks")
  1068. c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' network"))
  1069. // Stop container and remove network
  1070. dockerCmd(c, "stop", "foo")
  1071. dockerCmd(c, "network", "rm", "test")
  1072. // Test disconnecting stopped container from nonexisting network
  1073. dockerCmd(c, "network", "disconnect", "-f", "test", "foo")
  1074. networks = inspectField(c, "foo", "NetworkSettings.Networks")
  1075. c.Assert(networks, checker.Not(checker.Contains), "test", check.Commentf("Should not contain 'test' network"))
  1076. }
  1077. func (s *DockerNetworkSuite) TestDockerNetworkConnectPreferredIP(c *check.C) {
  1078. // create two networks
  1079. dockerCmd(c, "network", "create", "--ipv6", "--subnet=172.28.0.0/16", "--subnet=2001:db8:1234::/64", "n0")
  1080. assertNwIsAvailable(c, "n0")
  1081. 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")
  1082. assertNwIsAvailable(c, "n1")
  1083. // run a container on first network specifying the ip addresses
  1084. dockerCmd(c, "run", "-d", "--name", "c0", "--net=n0", "--ip", "172.28.99.88", "--ip6", "2001:db8:1234::9988", "busybox", "top")
  1085. c.Assert(waitRun("c0"), check.IsNil)
  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. // connect the container to the second network specifying an ip addresses
  1089. dockerCmd(c, "network", "connect", "--ip", "172.30.55.44", "--ip6", "2001:db8:abcd::5544", "n1", "c0")
  1090. verifyIPAddressConfig(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
  1091. verifyIPAddresses(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
  1092. // Stop and restart the container
  1093. dockerCmd(c, "stop", "c0")
  1094. dockerCmd(c, "start", "c0")
  1095. // verify requested addresses are applied and configs are still there
  1096. verifyIPAddressConfig(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
  1097. verifyIPAddresses(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
  1098. verifyIPAddressConfig(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
  1099. verifyIPAddresses(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
  1100. // Still it should fail to connect to the default network with a specified IP (whatever ip)
  1101. out, _, err := dockerCmdWithError("network", "connect", "--ip", "172.21.55.44", "bridge", "c0")
  1102. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  1103. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndIP.Error())
  1104. }
  1105. func (s *DockerNetworkSuite) TestDockerNetworkConnectPreferredIPStoppedContainer(c *check.C) {
  1106. // create a container
  1107. dockerCmd(c, "create", "--name", "c0", "busybox", "top")
  1108. // create a network
  1109. dockerCmd(c, "network", "create", "--ipv6", "--subnet=172.30.0.0/16", "--subnet=2001:db8:abcd::/64", "n0")
  1110. assertNwIsAvailable(c, "n0")
  1111. // connect the container to the network specifying an ip addresses
  1112. dockerCmd(c, "network", "connect", "--ip", "172.30.55.44", "--ip6", "2001:db8:abcd::5544", "n0", "c0")
  1113. verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
  1114. // start the container, verify config has not changed and ip addresses are assigned
  1115. dockerCmd(c, "start", "c0")
  1116. c.Assert(waitRun("c0"), check.IsNil)
  1117. verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
  1118. verifyIPAddresses(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
  1119. // stop the container and check ip config has not changed
  1120. dockerCmd(c, "stop", "c0")
  1121. verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
  1122. }
  1123. func (s *DockerNetworkSuite) TestDockerNetworkUnsupportedRequiredIP(c *check.C) {
  1124. // requested IP is not supported on predefined networks
  1125. for _, mode := range []string{"none", "host", "bridge", "default"} {
  1126. checkUnsupportedNetworkAndIP(c, mode)
  1127. }
  1128. // requested IP is not supported on networks with no user defined subnets
  1129. dockerCmd(c, "network", "create", "n0")
  1130. assertNwIsAvailable(c, "n0")
  1131. out, _, err := dockerCmdWithError("run", "-d", "--ip", "172.28.99.88", "--net", "n0", "busybox", "top")
  1132. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  1133. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkNoSubnetAndIP.Error())
  1134. out, _, err = dockerCmdWithError("run", "-d", "--ip6", "2001:db8:1234::9988", "--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. dockerCmd(c, "network", "rm", "n0")
  1138. assertNwNotAvailable(c, "n0")
  1139. }
  1140. func checkUnsupportedNetworkAndIP(c *check.C, nwMode string) {
  1141. out, _, err := dockerCmdWithError("run", "-d", "--net", nwMode, "--ip", "172.28.99.88", "--ip6", "2001:db8:1234::9988", "busybox", "top")
  1142. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  1143. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndIP.Error())
  1144. }
  1145. func verifyIPAddressConfig(c *check.C, cName, nwname, ipv4, ipv6 string) {
  1146. if ipv4 != "" {
  1147. out := inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAMConfig.IPv4Address", nwname))
  1148. c.Assert(strings.TrimSpace(out), check.Equals, ipv4)
  1149. }
  1150. if ipv6 != "" {
  1151. out := inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAMConfig.IPv6Address", nwname))
  1152. c.Assert(strings.TrimSpace(out), check.Equals, ipv6)
  1153. }
  1154. }
  1155. func verifyIPAddresses(c *check.C, cName, nwname, ipv4, ipv6 string) {
  1156. out := inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAddress", nwname))
  1157. c.Assert(strings.TrimSpace(out), check.Equals, ipv4)
  1158. out = inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.GlobalIPv6Address", nwname))
  1159. c.Assert(strings.TrimSpace(out), check.Equals, ipv6)
  1160. }
  1161. func (s *DockerNetworkSuite) TestDockerNetworkConnectLinkLocalIP(c *check.C) {
  1162. // create one test network
  1163. dockerCmd(c, "network", "create", "n0")
  1164. assertNwIsAvailable(c, "n0")
  1165. // run a container with incorrect link-local address
  1166. _, _, err := dockerCmdWithError("run", "--link-local-ip", "169.253.5.5", "busybox", "top")
  1167. c.Assert(err, check.NotNil)
  1168. _, _, err = dockerCmdWithError("run", "--link-local-ip", "2001:db8::89", "busybox", "top")
  1169. c.Assert(err, check.NotNil)
  1170. // run two containers with link-local ip on the test network
  1171. dockerCmd(c, "run", "-d", "--name", "c0", "--net=n0", "--link-local-ip", "169.254.7.7", "--link-local-ip", "fe80::254:77", "busybox", "top")
  1172. c.Assert(waitRun("c0"), check.IsNil)
  1173. dockerCmd(c, "run", "-d", "--name", "c1", "--net=n0", "--link-local-ip", "169.254.8.8", "--link-local-ip", "fe80::254:88", "busybox", "top")
  1174. c.Assert(waitRun("c1"), check.IsNil)
  1175. // run a container on the default network and connect it to the test network specifying a link-local address
  1176. dockerCmd(c, "run", "-d", "--name", "c2", "busybox", "top")
  1177. c.Assert(waitRun("c2"), check.IsNil)
  1178. dockerCmd(c, "network", "connect", "--link-local-ip", "169.254.9.9", "n0", "c2")
  1179. // verify the three containers can ping each other via the link-local addresses
  1180. _, _, err = dockerCmdWithError("exec", "c0", "ping", "-c", "1", "169.254.8.8")
  1181. c.Assert(err, check.IsNil)
  1182. _, _, err = dockerCmdWithError("exec", "c1", "ping", "-c", "1", "169.254.9.9")
  1183. c.Assert(err, check.IsNil)
  1184. _, _, err = dockerCmdWithError("exec", "c2", "ping", "-c", "1", "169.254.7.7")
  1185. c.Assert(err, check.IsNil)
  1186. // Stop and restart the three containers
  1187. dockerCmd(c, "stop", "c0")
  1188. dockerCmd(c, "stop", "c1")
  1189. dockerCmd(c, "stop", "c2")
  1190. dockerCmd(c, "start", "c0")
  1191. dockerCmd(c, "start", "c1")
  1192. dockerCmd(c, "start", "c2")
  1193. // verify the ping again
  1194. _, _, err = dockerCmdWithError("exec", "c0", "ping", "-c", "1", "169.254.8.8")
  1195. c.Assert(err, check.IsNil)
  1196. _, _, err = dockerCmdWithError("exec", "c1", "ping", "-c", "1", "169.254.9.9")
  1197. c.Assert(err, check.IsNil)
  1198. _, _, err = dockerCmdWithError("exec", "c2", "ping", "-c", "1", "169.254.7.7")
  1199. c.Assert(err, check.IsNil)
  1200. }
  1201. func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectLink(c *check.C) {
  1202. testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
  1203. dockerCmd(c, "network", "create", "-d", "bridge", "foo1")
  1204. dockerCmd(c, "network", "create", "-d", "bridge", "foo2")
  1205. dockerCmd(c, "run", "-d", "--net=foo1", "--name=first", "busybox", "top")
  1206. c.Assert(waitRun("first"), check.IsNil)
  1207. // run a container in a user-defined network with a link for an existing container
  1208. // and a link for a container that doesn't exist
  1209. dockerCmd(c, "run", "-d", "--net=foo1", "--name=second", "--link=first:FirstInFoo1",
  1210. "--link=third:bar", "busybox", "top")
  1211. c.Assert(waitRun("second"), check.IsNil)
  1212. // ping to first and its alias FirstInFoo1 must succeed
  1213. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  1214. c.Assert(err, check.IsNil)
  1215. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo1")
  1216. c.Assert(err, check.IsNil)
  1217. // connect first container to foo2 network
  1218. dockerCmd(c, "network", "connect", "foo2", "first")
  1219. // connect second container to foo2 network with a different alias for first container
  1220. dockerCmd(c, "network", "connect", "--link=first:FirstInFoo2", "foo2", "second")
  1221. // ping the new alias in network foo2
  1222. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo2")
  1223. c.Assert(err, check.IsNil)
  1224. // disconnect first container from foo1 network
  1225. dockerCmd(c, "network", "disconnect", "foo1", "first")
  1226. // link in foo1 network must fail
  1227. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo1")
  1228. c.Assert(err, check.NotNil)
  1229. // link in foo2 network must succeed
  1230. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo2")
  1231. c.Assert(err, check.IsNil)
  1232. }
  1233. func (s *DockerNetworkSuite) TestDockerNetworkDisconnectDefault(c *check.C) {
  1234. netWorkName1 := "test1"
  1235. netWorkName2 := "test2"
  1236. containerName := "foo"
  1237. dockerCmd(c, "network", "create", netWorkName1)
  1238. dockerCmd(c, "network", "create", netWorkName2)
  1239. dockerCmd(c, "create", "--name", containerName, "busybox", "top")
  1240. dockerCmd(c, "network", "connect", netWorkName1, containerName)
  1241. dockerCmd(c, "network", "connect", netWorkName2, containerName)
  1242. dockerCmd(c, "network", "disconnect", "bridge", containerName)
  1243. dockerCmd(c, "start", containerName)
  1244. c.Assert(waitRun(containerName), checker.IsNil)
  1245. networks := inspectField(c, containerName, "NetworkSettings.Networks")
  1246. c.Assert(networks, checker.Contains, netWorkName1, check.Commentf(fmt.Sprintf("Should contain '%s' network", netWorkName1)))
  1247. c.Assert(networks, checker.Contains, netWorkName2, check.Commentf(fmt.Sprintf("Should contain '%s' network", netWorkName2)))
  1248. c.Assert(networks, checker.Not(checker.Contains), "bridge", check.Commentf("Should not contain 'bridge' network"))
  1249. }
  1250. func (s *DockerNetworkSuite) TestDockerNetworkConnectWithAliasOnDefaultNetworks(c *check.C) {
  1251. testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
  1252. defaults := []string{"bridge", "host", "none"}
  1253. out, _ := dockerCmd(c, "run", "-d", "--net=none", "busybox", "top")
  1254. containerID := strings.TrimSpace(out)
  1255. for _, net := range defaults {
  1256. res, _, err := dockerCmdWithError("network", "connect", "--alias", "alias"+net, net, containerID)
  1257. c.Assert(err, checker.NotNil)
  1258. c.Assert(res, checker.Contains, runconfig.ErrUnsupportedNetworkAndAlias.Error())
  1259. }
  1260. }
  1261. func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectAlias(c *check.C) {
  1262. testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
  1263. dockerCmd(c, "network", "create", "-d", "bridge", "net1")
  1264. dockerCmd(c, "network", "create", "-d", "bridge", "net2")
  1265. cid, _ := dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo", "busybox", "top")
  1266. c.Assert(waitRun("first"), check.IsNil)
  1267. dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top")
  1268. c.Assert(waitRun("second"), check.IsNil)
  1269. // ping first container and its alias
  1270. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  1271. c.Assert(err, check.IsNil)
  1272. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  1273. c.Assert(err, check.IsNil)
  1274. // ping first container's short-id alias
  1275. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid))
  1276. c.Assert(err, check.IsNil)
  1277. // connect first container to net2 network
  1278. dockerCmd(c, "network", "connect", "--alias=bar", "net2", "first")
  1279. // connect second container to foo2 network with a different alias for first container
  1280. dockerCmd(c, "network", "connect", "net2", "second")
  1281. // ping the new alias in network foo2
  1282. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "bar")
  1283. c.Assert(err, check.IsNil)
  1284. // disconnect first container from net1 network
  1285. dockerCmd(c, "network", "disconnect", "net1", "first")
  1286. // ping to net1 scoped alias "foo" must fail
  1287. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  1288. c.Assert(err, check.NotNil)
  1289. // ping to net2 scoped alias "bar" must still succeed
  1290. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "bar")
  1291. c.Assert(err, check.IsNil)
  1292. // ping to net2 scoped alias short-id must still succeed
  1293. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid))
  1294. c.Assert(err, check.IsNil)
  1295. // verify the alias option is rejected when running on predefined network
  1296. out, _, err := dockerCmdWithError("run", "--rm", "--name=any", "--net-alias=any", "busybox", "top")
  1297. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  1298. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndAlias.Error())
  1299. // verify the alias option is rejected when connecting to predefined network
  1300. out, _, err = dockerCmdWithError("network", "connect", "--alias=any", "bridge", "first")
  1301. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  1302. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndAlias.Error())
  1303. }
  1304. func (s *DockerSuite) TestUserDefinedNetworkConnectivity(c *check.C) {
  1305. testRequires(c, DaemonIsLinux, NotUserNamespace)
  1306. dockerCmd(c, "network", "create", "-d", "bridge", "br.net1")
  1307. dockerCmd(c, "run", "-d", "--net=br.net1", "--name=c1.net1", "busybox", "top")
  1308. c.Assert(waitRun("c1.net1"), check.IsNil)
  1309. dockerCmd(c, "run", "-d", "--net=br.net1", "--name=c2.net1", "busybox", "top")
  1310. c.Assert(waitRun("c2.net1"), check.IsNil)
  1311. // ping first container by its unqualified name
  1312. _, _, err := dockerCmdWithError("exec", "c2.net1", "ping", "-c", "1", "c1.net1")
  1313. c.Assert(err, check.IsNil)
  1314. // ping first container by its qualified name
  1315. _, _, err = dockerCmdWithError("exec", "c2.net1", "ping", "-c", "1", "c1.net1.br.net1")
  1316. c.Assert(err, check.IsNil)
  1317. // ping with first qualified name masked by an additional domain. should fail
  1318. _, _, err = dockerCmdWithError("exec", "c2.net1", "ping", "-c", "1", "c1.net1.br.net1.google.com")
  1319. c.Assert(err, check.NotNil)
  1320. }
  1321. func (s *DockerSuite) TestEmbeddedDNSInvalidInput(c *check.C) {
  1322. testRequires(c, DaemonIsLinux, NotUserNamespace)
  1323. dockerCmd(c, "network", "create", "-d", "bridge", "nw1")
  1324. // Sending garbage to embedded DNS shouldn't crash the daemon
  1325. dockerCmd(c, "run", "-i", "--net=nw1", "--name=c1", "debian:jessie", "bash", "-c", "echo InvalidQuery > /dev/udp/127.0.0.11/53")
  1326. }
  1327. func (s *DockerSuite) TestDockerNetworkConnectFailsNoInspectChange(c *check.C) {
  1328. dockerCmd(c, "run", "-d", "--name=bb", "busybox", "top")
  1329. c.Assert(waitRun("bb"), check.IsNil)
  1330. ns0 := inspectField(c, "bb", "NetworkSettings.Networks.bridge")
  1331. // A failing redundant network connect should not alter current container's endpoint settings
  1332. _, _, err := dockerCmdWithError("network", "connect", "bridge", "bb")
  1333. c.Assert(err, check.NotNil)
  1334. ns1 := inspectField(c, "bb", "NetworkSettings.Networks.bridge")
  1335. c.Assert(ns1, check.Equals, ns0)
  1336. }
  1337. func (s *DockerSuite) TestDockerNetworkInternalMode(c *check.C) {
  1338. dockerCmd(c, "network", "create", "--driver=bridge", "--internal", "internal")
  1339. assertNwIsAvailable(c, "internal")
  1340. nr := getNetworkResource(c, "internal")
  1341. c.Assert(nr.Internal, checker.True)
  1342. dockerCmd(c, "run", "-d", "--net=internal", "--name=first", "busybox", "top")
  1343. c.Assert(waitRun("first"), check.IsNil)
  1344. dockerCmd(c, "run", "-d", "--net=internal", "--name=second", "busybox", "top")
  1345. c.Assert(waitRun("second"), check.IsNil)
  1346. out, _, err := dockerCmdWithError("exec", "first", "ping", "-W", "4", "-c", "1", "www.google.com")
  1347. c.Assert(err, check.NotNil)
  1348. c.Assert(out, checker.Contains, "ping: bad address")
  1349. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  1350. c.Assert(err, check.IsNil)
  1351. }
  1352. // Test for special characters in network names. only [a-zA-Z0-9][a-zA-Z0-9_.-] are
  1353. // valid characters
  1354. func (s *DockerNetworkSuite) TestDockerNetworkCreateDeleteSpecialCharacters(c *check.C) {
  1355. _, _, err := dockerCmdWithError("network", "create", "test@#$")
  1356. c.Assert(err, check.NotNil)
  1357. dockerCmd(c, "network", "create", "test-1_0.net")
  1358. assertNwIsAvailable(c, "test-1_0.net")
  1359. dockerCmd(c, "network", "rm", "test-1_0.net")
  1360. assertNwNotAvailable(c, "test-1_0.net")
  1361. }
  1362. func (s *DockerDaemonSuite) TestDaemonRestartRestoreBridgeNetwork(t *check.C) {
  1363. testRequires(t, DaemonIsLinux)
  1364. if err := s.d.StartWithBusybox("--live-restore"); err != nil {
  1365. t.Fatal(err)
  1366. }
  1367. defer s.d.Stop()
  1368. oldCon := "old"
  1369. _, err := s.d.Cmd("run", "-d", "--name", oldCon, "-p", "80:80", "busybox", "top")
  1370. if err != nil {
  1371. t.Fatal(err)
  1372. }
  1373. oldContainerIP, err := s.d.Cmd("inspect", "-f", "{{ .NetworkSettings.Networks.bridge.IPAddress }}", oldCon)
  1374. if err != nil {
  1375. t.Fatal(err)
  1376. }
  1377. // Kill the daemon
  1378. if err := s.d.Kill(); err != nil {
  1379. t.Fatal(err)
  1380. }
  1381. // restart the daemon
  1382. if err := s.d.Start("--live-restore"); err != nil {
  1383. t.Fatal(err)
  1384. }
  1385. // start a new container, the new container's ip should not be the same with
  1386. // old running container.
  1387. newCon := "new"
  1388. _, err = s.d.Cmd("run", "-d", "--name", newCon, "busybox", "top")
  1389. if err != nil {
  1390. t.Fatal(err)
  1391. }
  1392. newContainerIP, err := s.d.Cmd("inspect", "-f", "{{ .NetworkSettings.Networks.bridge.IPAddress }}", newCon)
  1393. if err != nil {
  1394. t.Fatal(err)
  1395. }
  1396. if strings.Compare(strings.TrimSpace(oldContainerIP), strings.TrimSpace(newContainerIP)) == 0 {
  1397. t.Fatalf("new container ip should not equal to old running container ip")
  1398. }
  1399. // start a new container, the new container should ping old running container
  1400. _, err = s.d.Cmd("run", "-t", "busybox", "ping", "-c", "1", oldContainerIP)
  1401. if err != nil {
  1402. t.Fatal(err)
  1403. }
  1404. // start a new container, trying to publish port 80:80 should fail
  1405. out, err := s.d.Cmd("run", "-p", "80:80", "-d", "busybox", "top")
  1406. if err == nil || !strings.Contains(out, "Bind for 0.0.0.0:80 failed: port is already allocated") {
  1407. t.Fatalf("80 port is allocated to old running container, it should failed on allocating to new container")
  1408. }
  1409. // kill old running container and try to allocate again
  1410. _, err = s.d.Cmd("kill", oldCon)
  1411. if err != nil {
  1412. t.Fatal(err)
  1413. }
  1414. id, err := s.d.Cmd("run", "-p", "80:80", "-d", "busybox", "top")
  1415. if err != nil {
  1416. t.Fatal(err)
  1417. }
  1418. // Cleanup because these containers will not be shut down by daemon
  1419. out, err = s.d.Cmd("stop", newCon)
  1420. if err != nil {
  1421. t.Fatalf("err: %v %v", err, string(out))
  1422. }
  1423. _, err = s.d.Cmd("stop", strings.TrimSpace(id))
  1424. if err != nil {
  1425. t.Fatal(err)
  1426. }
  1427. }
  1428. func (s *DockerNetworkSuite) TestDockerNetworkFlagAlias(c *check.C) {
  1429. dockerCmd(c, "network", "create", "user")
  1430. output, status := dockerCmd(c, "run", "--rm", "--network=user", "--network-alias=foo", "busybox", "true")
  1431. c.Assert(status, checker.Equals, 0, check.Commentf("unexpected status code %d (%s)", status, output))
  1432. output, status, _ = dockerCmdWithError("run", "--rm", "--net=user", "--network=user", "busybox", "true")
  1433. c.Assert(status, checker.Equals, 0, check.Commentf("unexpected status code %d (%s)", status, output))
  1434. output, status, _ = dockerCmdWithError("run", "--rm", "--network=user", "--net-alias=foo", "--network-alias=bar", "busybox", "true")
  1435. c.Assert(status, checker.Equals, 0, check.Commentf("unexpected status code %d (%s)", status, output))
  1436. }
  1437. func (s *DockerNetworkSuite) TestDockerNetworkValidateIP(c *check.C) {
  1438. _, _, err := dockerCmdWithError("network", "create", "--ipv6", "--subnet=172.28.0.0/16", "--subnet=2001:db8:1234::/64", "mynet")
  1439. c.Assert(err, check.IsNil)
  1440. assertNwIsAvailable(c, "mynet")
  1441. _, _, err = dockerCmdWithError("run", "-d", "--name", "mynet0", "--net=mynet", "--ip", "172.28.99.88", "--ip6", "2001:db8:1234::9988", "busybox", "top")
  1442. c.Assert(err, check.IsNil)
  1443. c.Assert(waitRun("mynet0"), check.IsNil)
  1444. verifyIPAddressConfig(c, "mynet0", "mynet", "172.28.99.88", "2001:db8:1234::9988")
  1445. verifyIPAddresses(c, "mynet0", "mynet", "172.28.99.88", "2001:db8:1234::9988")
  1446. _, _, err = dockerCmdWithError("run", "--net=mynet", "--ip", "mynet_ip", "--ip6", "2001:db8:1234::9999", "busybox", "top")
  1447. c.Assert(err.Error(), checker.Contains, "invalid IPv4 address")
  1448. _, _, err = dockerCmdWithError("run", "--net=mynet", "--ip", "172.28.99.99", "--ip6", "mynet_ip6", "busybox", "top")
  1449. c.Assert(err.Error(), checker.Contains, "invalid IPv6 address")
  1450. // This is a case of IPv4 address to `--ip6`
  1451. _, _, err = dockerCmdWithError("run", "--net=mynet", "--ip6", "172.28.99.99", "busybox", "top")
  1452. c.Assert(err.Error(), checker.Contains, "invalid IPv6 address")
  1453. // This is a special case of an IPv4-mapped IPv6 address
  1454. _, _, err = dockerCmdWithError("run", "--net=mynet", "--ip6", "::ffff:172.28.99.99", "busybox", "top")
  1455. c.Assert(err.Error(), checker.Contains, "invalid IPv6 address")
  1456. }
  1457. // Test case for 26220
  1458. func (s *DockerNetworkSuite) TestDockerNetworkDisconnectFromBridge(c *check.C) {
  1459. out, _ := dockerCmd(c, "network", "inspect", "--format", "{{.Id}}", "bridge")
  1460. network := strings.TrimSpace(out)
  1461. name := "test"
  1462. dockerCmd(c, "create", "--name", name, "busybox", "top")
  1463. _, _, err := dockerCmdWithError("network", "disconnect", network, name)
  1464. c.Assert(err, check.IsNil)
  1465. }