docker_cli_network_unix_test.go 70 KB

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