docker_cli_network_unix_test.go 54 KB

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