docker_cli_network_unix_test.go 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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 retrieved 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. c.Assert(nr.Internal, checker.False)
  529. dockerCmd(c, "network", "rm", "br0")
  530. assertNwNotAvailable(c, "test01")
  531. }
  532. func (s *DockerNetworkSuite) TestDockerNetworkIpamInvalidCombinations(c *check.C) {
  533. // network with ip-range out of subnet range
  534. _, _, err := dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--ip-range=192.170.0.0/16", "test")
  535. c.Assert(err, check.NotNil)
  536. // network with multiple gateways for a single subnet
  537. _, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--gateway=192.168.0.1", "--gateway=192.168.0.2", "test")
  538. c.Assert(err, check.NotNil)
  539. // Multiple overlapping subnets in the same network must fail
  540. _, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.0.0/16", "--subnet=192.168.1.0/16", "test")
  541. c.Assert(err, check.NotNil)
  542. // overlapping subnets across networks must fail
  543. // create a valid test0 network
  544. dockerCmd(c, "network", "create", "--subnet=192.168.0.0/16", "test0")
  545. assertNwIsAvailable(c, "test0")
  546. // create an overlapping test1 network
  547. _, _, err = dockerCmdWithError("network", "create", "--subnet=192.168.128.0/17", "test1")
  548. c.Assert(err, check.NotNil)
  549. dockerCmd(c, "network", "rm", "test0")
  550. assertNwNotAvailable(c, "test0")
  551. }
  552. func (s *DockerNetworkSuite) TestDockerNetworkDriverOptions(c *check.C) {
  553. dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, "-o", "opt1=drv1", "-o", "opt2=drv2", "testopt")
  554. assertNwIsAvailable(c, "testopt")
  555. gopts := remoteDriverNetworkRequest.Options[netlabel.GenericData]
  556. c.Assert(gopts, checker.NotNil)
  557. opts, ok := gopts.(map[string]interface{})
  558. c.Assert(ok, checker.Equals, true)
  559. c.Assert(opts["opt1"], checker.Equals, "drv1")
  560. c.Assert(opts["opt2"], checker.Equals, "drv2")
  561. dockerCmd(c, "network", "rm", "testopt")
  562. assertNwNotAvailable(c, "testopt")
  563. }
  564. func (s *DockerDaemonSuite) TestDockerNetworkNoDiscoveryDefaultBridgeNetwork(c *check.C) {
  565. testRequires(c, ExecSupport)
  566. // On default bridge network built-in service discovery should not happen
  567. hostsFile := "/etc/hosts"
  568. bridgeName := "external-bridge"
  569. bridgeIP := "192.169.255.254/24"
  570. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  571. c.Assert(err, check.IsNil, check.Commentf(out))
  572. defer deleteInterface(c, bridgeName)
  573. err = s.d.StartWithBusybox("--bridge", bridgeName)
  574. c.Assert(err, check.IsNil)
  575. defer s.d.Restart()
  576. // run two containers and store first container's etc/hosts content
  577. out, err = s.d.Cmd("run", "-d", "busybox", "top")
  578. c.Assert(err, check.IsNil)
  579. cid1 := strings.TrimSpace(out)
  580. defer s.d.Cmd("stop", cid1)
  581. hosts, err := s.d.Cmd("exec", cid1, "cat", hostsFile)
  582. c.Assert(err, checker.IsNil)
  583. out, err = s.d.Cmd("run", "-d", "--name", "container2", "busybox", "top")
  584. c.Assert(err, check.IsNil)
  585. cid2 := strings.TrimSpace(out)
  586. // verify first container's etc/hosts file has not changed after spawning the second named container
  587. hostsPost, err := s.d.Cmd("exec", cid1, "cat", hostsFile)
  588. c.Assert(err, checker.IsNil)
  589. c.Assert(string(hosts), checker.Equals, string(hostsPost),
  590. check.Commentf("Unexpected %s change on second container creation", hostsFile))
  591. // stop container 2 and verify first container's etc/hosts has not changed
  592. _, err = s.d.Cmd("stop", cid2)
  593. c.Assert(err, check.IsNil)
  594. hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
  595. c.Assert(err, checker.IsNil)
  596. c.Assert(string(hosts), checker.Equals, string(hostsPost),
  597. check.Commentf("Unexpected %s change on second container creation", hostsFile))
  598. // but discovery is on when connecting to non default bridge network
  599. network := "anotherbridge"
  600. out, err = s.d.Cmd("network", "create", network)
  601. c.Assert(err, check.IsNil, check.Commentf(out))
  602. defer s.d.Cmd("network", "rm", network)
  603. out, err = s.d.Cmd("network", "connect", network, cid1)
  604. c.Assert(err, check.IsNil, check.Commentf(out))
  605. hosts, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
  606. c.Assert(err, checker.IsNil)
  607. hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
  608. c.Assert(err, checker.IsNil)
  609. c.Assert(string(hosts), checker.Equals, string(hostsPost),
  610. check.Commentf("Unexpected %s change on second network connection", hostsFile))
  611. }
  612. func (s *DockerNetworkSuite) TestDockerNetworkAnonymousEndpoint(c *check.C) {
  613. testRequires(c, ExecSupport, NotArm)
  614. hostsFile := "/etc/hosts"
  615. cstmBridgeNw := "custom-bridge-nw"
  616. cstmBridgeNw1 := "custom-bridge-nw1"
  617. dockerCmd(c, "network", "create", "-d", "bridge", cstmBridgeNw)
  618. assertNwIsAvailable(c, cstmBridgeNw)
  619. // run two anonymous containers and store their etc/hosts content
  620. out, _ := dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "busybox", "top")
  621. cid1 := strings.TrimSpace(out)
  622. hosts1, err := readContainerFileWithExec(cid1, hostsFile)
  623. c.Assert(err, checker.IsNil)
  624. out, _ = dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "busybox", "top")
  625. cid2 := strings.TrimSpace(out)
  626. hosts2, err := readContainerFileWithExec(cid2, hostsFile)
  627. c.Assert(err, checker.IsNil)
  628. // verify first container etc/hosts file has not changed
  629. hosts1post, err := readContainerFileWithExec(cid1, hostsFile)
  630. c.Assert(err, checker.IsNil)
  631. c.Assert(string(hosts1), checker.Equals, string(hosts1post),
  632. check.Commentf("Unexpected %s change on anonymous container creation", hostsFile))
  633. // Connect the 2nd container to a new network and verify the
  634. // first container /etc/hosts file still hasn't changed.
  635. dockerCmd(c, "network", "create", "-d", "bridge", cstmBridgeNw1)
  636. assertNwIsAvailable(c, cstmBridgeNw1)
  637. dockerCmd(c, "network", "connect", cstmBridgeNw1, cid2)
  638. hosts2, err = readContainerFileWithExec(cid2, hostsFile)
  639. c.Assert(err, checker.IsNil)
  640. hosts1post, err = readContainerFileWithExec(cid1, hostsFile)
  641. c.Assert(err, checker.IsNil)
  642. c.Assert(string(hosts1), checker.Equals, string(hosts1post),
  643. check.Commentf("Unexpected %s change on container connect", hostsFile))
  644. // start a named container
  645. cName := "AnyName"
  646. out, _ = dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "--name", cName, "busybox", "top")
  647. cid3 := strings.TrimSpace(out)
  648. // verify that container 1 and 2 can ping the named container
  649. dockerCmd(c, "exec", cid1, "ping", "-c", "1", cName)
  650. dockerCmd(c, "exec", cid2, "ping", "-c", "1", cName)
  651. // Stop named container and verify first two containers' etc/hosts file hasn't changed
  652. dockerCmd(c, "stop", cid3)
  653. hosts1post, err = readContainerFileWithExec(cid1, hostsFile)
  654. c.Assert(err, checker.IsNil)
  655. c.Assert(string(hosts1), checker.Equals, string(hosts1post),
  656. check.Commentf("Unexpected %s change on name container creation", hostsFile))
  657. hosts2post, err := readContainerFileWithExec(cid2, hostsFile)
  658. c.Assert(err, checker.IsNil)
  659. c.Assert(string(hosts2), checker.Equals, string(hosts2post),
  660. check.Commentf("Unexpected %s change on name container creation", hostsFile))
  661. // verify that container 1 and 2 can't ping the named container now
  662. _, _, err = dockerCmdWithError("exec", cid1, "ping", "-c", "1", cName)
  663. c.Assert(err, check.NotNil)
  664. _, _, err = dockerCmdWithError("exec", cid2, "ping", "-c", "1", cName)
  665. c.Assert(err, check.NotNil)
  666. }
  667. func (s *DockerNetworkSuite) TestDockerNetworkLinkOndefaultNetworkOnly(c *check.C) {
  668. // Link feature must work only on default network, and not across networks
  669. cnt1 := "container1"
  670. cnt2 := "container2"
  671. network := "anotherbridge"
  672. // Run first container on default network
  673. dockerCmd(c, "run", "-d", "--name", cnt1, "busybox", "top")
  674. // Create another network and run the second container on it
  675. dockerCmd(c, "network", "create", network)
  676. assertNwIsAvailable(c, network)
  677. dockerCmd(c, "run", "-d", "--net", network, "--name", cnt2, "busybox", "top")
  678. // Try launching a container on default network, linking to the first container. Must succeed
  679. dockerCmd(c, "run", "-d", "--link", fmt.Sprintf("%s:%s", cnt1, cnt1), "busybox", "top")
  680. // Try launching a container on default network, linking to the second container. Must fail
  681. _, _, err := dockerCmdWithError("run", "-d", "--link", fmt.Sprintf("%s:%s", cnt2, cnt2), "busybox", "top")
  682. c.Assert(err, checker.NotNil)
  683. // Connect second container to default network. Now a container on default network can link to it
  684. dockerCmd(c, "network", "connect", "bridge", cnt2)
  685. dockerCmd(c, "run", "-d", "--link", fmt.Sprintf("%s:%s", cnt2, cnt2), "busybox", "top")
  686. }
  687. func (s *DockerNetworkSuite) TestDockerNetworkOverlayPortMapping(c *check.C) {
  688. // Verify exposed ports are present in ps output when running a container on
  689. // a network managed by a driver which does not provide the default gateway
  690. // for the container
  691. nwn := "ov"
  692. ctn := "bb"
  693. port1 := 80
  694. port2 := 443
  695. expose1 := fmt.Sprintf("--expose=%d", port1)
  696. expose2 := fmt.Sprintf("--expose=%d", port2)
  697. dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, nwn)
  698. assertNwIsAvailable(c, nwn)
  699. dockerCmd(c, "run", "-d", "--net", nwn, "--name", ctn, expose1, expose2, "busybox", "top")
  700. // Check docker ps o/p for last created container reports the unpublished ports
  701. unpPort1 := fmt.Sprintf("%d/tcp", port1)
  702. unpPort2 := fmt.Sprintf("%d/tcp", port2)
  703. out, _ := dockerCmd(c, "ps", "-n=1")
  704. // Missing unpublished ports in docker ps output
  705. c.Assert(out, checker.Contains, unpPort1)
  706. // Missing unpublished ports in docker ps output
  707. c.Assert(out, checker.Contains, unpPort2)
  708. }
  709. func (s *DockerNetworkSuite) TestDockerNetworkDriverUngracefulRestart(c *check.C) {
  710. testRequires(c, DaemonIsLinux, NotUserNamespace)
  711. dnd := "dnd"
  712. did := "did"
  713. mux := http.NewServeMux()
  714. server := httptest.NewServer(mux)
  715. setupRemoteNetworkDrivers(c, mux, server.URL, dnd, did)
  716. s.d.StartWithBusybox()
  717. _, err := s.d.Cmd("network", "create", "-d", dnd, "--subnet", "1.1.1.0/24", "net1")
  718. c.Assert(err, checker.IsNil)
  719. _, err = s.d.Cmd("run", "-itd", "--net", "net1", "--name", "foo", "--ip", "1.1.1.10", "busybox", "sh")
  720. c.Assert(err, checker.IsNil)
  721. // Kill daemon and restart
  722. if err = s.d.cmd.Process.Kill(); err != nil {
  723. c.Fatal(err)
  724. }
  725. server.Close()
  726. startTime := time.Now().Unix()
  727. if err = s.d.Restart(); err != nil {
  728. c.Fatal(err)
  729. }
  730. lapse := time.Now().Unix() - startTime
  731. if lapse > 60 {
  732. // In normal scenarios, daemon restart takes ~1 second.
  733. // Plugin retry mechanism can delay the daemon start. systemd may not like it.
  734. // Avoid accessing plugins during daemon bootup
  735. c.Logf("daemon restart took too long : %d seconds", lapse)
  736. }
  737. // Restart the custom dummy plugin
  738. mux = http.NewServeMux()
  739. server = httptest.NewServer(mux)
  740. setupRemoteNetworkDrivers(c, mux, server.URL, dnd, did)
  741. // trying to reuse the same ip must succeed
  742. _, err = s.d.Cmd("run", "-itd", "--net", "net1", "--name", "bar", "--ip", "1.1.1.10", "busybox", "sh")
  743. c.Assert(err, checker.IsNil)
  744. }
  745. func (s *DockerNetworkSuite) TestDockerNetworkMacInspect(c *check.C) {
  746. // Verify endpoint MAC address is correctly populated in container's network settings
  747. nwn := "ov"
  748. ctn := "bb"
  749. dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, nwn)
  750. assertNwIsAvailable(c, nwn)
  751. dockerCmd(c, "run", "-d", "--net", nwn, "--name", ctn, "busybox", "top")
  752. mac := inspectField(c, ctn, "NetworkSettings.Networks."+nwn+".MacAddress")
  753. c.Assert(mac, checker.Equals, "a0:b1:c2:d3:e4:f5")
  754. }
  755. func (s *DockerSuite) TestInspectApiMultipleNetworks(c *check.C) {
  756. dockerCmd(c, "network", "create", "mybridge1")
  757. dockerCmd(c, "network", "create", "mybridge2")
  758. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  759. id := strings.TrimSpace(out)
  760. c.Assert(waitRun(id), check.IsNil)
  761. dockerCmd(c, "network", "connect", "mybridge1", id)
  762. dockerCmd(c, "network", "connect", "mybridge2", id)
  763. body := getInspectBody(c, "v1.20", id)
  764. var inspect120 v1p20.ContainerJSON
  765. err := json.Unmarshal(body, &inspect120)
  766. c.Assert(err, checker.IsNil)
  767. versionedIP := inspect120.NetworkSettings.IPAddress
  768. body = getInspectBody(c, "v1.21", id)
  769. var inspect121 types.ContainerJSON
  770. err = json.Unmarshal(body, &inspect121)
  771. c.Assert(err, checker.IsNil)
  772. c.Assert(inspect121.NetworkSettings.Networks, checker.HasLen, 3)
  773. bridge := inspect121.NetworkSettings.Networks["bridge"]
  774. c.Assert(bridge.IPAddress, checker.Equals, versionedIP)
  775. c.Assert(bridge.IPAddress, checker.Equals, inspect121.NetworkSettings.IPAddress)
  776. }
  777. func connectContainerToNetworks(c *check.C, d *Daemon, cName string, nws []string) {
  778. // Run a container on the default network
  779. out, err := d.Cmd("run", "-d", "--name", cName, "busybox", "top")
  780. c.Assert(err, checker.IsNil, check.Commentf(out))
  781. // Attach the container to other networks
  782. for _, nw := range nws {
  783. out, err = d.Cmd("network", "create", nw)
  784. c.Assert(err, checker.IsNil, check.Commentf(out))
  785. out, err = d.Cmd("network", "connect", nw, cName)
  786. c.Assert(err, checker.IsNil, check.Commentf(out))
  787. }
  788. }
  789. func verifyContainerIsConnectedToNetworks(c *check.C, d *Daemon, cName string, nws []string) {
  790. // Verify container is connected to all the networks
  791. for _, nw := range nws {
  792. out, err := d.Cmd("inspect", "-f", fmt.Sprintf("{{.NetworkSettings.Networks.%s}}", nw), cName)
  793. c.Assert(err, checker.IsNil, check.Commentf(out))
  794. c.Assert(out, checker.Not(checker.Equals), "<no value>\n")
  795. }
  796. }
  797. func (s *DockerNetworkSuite) TestDockerNetworkMultipleNetworksGracefulDaemonRestart(c *check.C) {
  798. cName := "bb"
  799. nwList := []string{"nw1", "nw2", "nw3"}
  800. s.d.StartWithBusybox()
  801. connectContainerToNetworks(c, s.d, cName, nwList)
  802. verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
  803. // Reload daemon
  804. s.d.Restart()
  805. _, err := s.d.Cmd("start", cName)
  806. c.Assert(err, checker.IsNil)
  807. verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
  808. }
  809. func (s *DockerNetworkSuite) TestDockerNetworkMultipleNetworksUngracefulDaemonRestart(c *check.C) {
  810. cName := "cc"
  811. nwList := []string{"nw1", "nw2", "nw3"}
  812. s.d.StartWithBusybox()
  813. connectContainerToNetworks(c, s.d, cName, nwList)
  814. verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
  815. // Kill daemon and restart
  816. if err := s.d.cmd.Process.Kill(); err != nil {
  817. c.Fatal(err)
  818. }
  819. s.d.Restart()
  820. // Restart container
  821. _, err := s.d.Cmd("start", cName)
  822. c.Assert(err, checker.IsNil)
  823. verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
  824. }
  825. func (s *DockerNetworkSuite) TestDockerNetworkRunNetByID(c *check.C) {
  826. out, _ := dockerCmd(c, "network", "create", "one")
  827. containerOut, _, err := dockerCmdWithError("run", "-d", "--net", strings.TrimSpace(out), "busybox", "top")
  828. c.Assert(err, checker.IsNil, check.Commentf(containerOut))
  829. }
  830. func (s *DockerNetworkSuite) TestDockerNetworkHostModeUngracefulDaemonRestart(c *check.C) {
  831. testRequires(c, DaemonIsLinux, NotUserNamespace)
  832. s.d.StartWithBusybox()
  833. // Run a few containers on host network
  834. for i := 0; i < 10; i++ {
  835. cName := fmt.Sprintf("hostc-%d", i)
  836. out, err := s.d.Cmd("run", "-d", "--name", cName, "--net=host", "--restart=always", "busybox", "top")
  837. c.Assert(err, checker.IsNil, check.Commentf(out))
  838. // verfiy container has finished starting before killing daemon
  839. err = s.d.waitRun(cName)
  840. c.Assert(err, checker.IsNil)
  841. }
  842. // Kill daemon ungracefully and restart
  843. if err := s.d.cmd.Process.Kill(); err != nil {
  844. c.Fatal(err)
  845. }
  846. if err := s.d.Restart(); err != nil {
  847. c.Fatal(err)
  848. }
  849. // make sure all the containers are up and running
  850. for i := 0; i < 10; i++ {
  851. err := s.d.waitRun(fmt.Sprintf("hostc-%d", i))
  852. c.Assert(err, checker.IsNil)
  853. }
  854. }
  855. func (s *DockerNetworkSuite) TestDockerNetworkConnectToHostFromOtherNetwork(c *check.C) {
  856. dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  857. c.Assert(waitRun("container1"), check.IsNil)
  858. dockerCmd(c, "network", "disconnect", "bridge", "container1")
  859. out, _, err := dockerCmdWithError("network", "connect", "host", "container1")
  860. c.Assert(err, checker.NotNil, check.Commentf(out))
  861. c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetwork.Error())
  862. }
  863. func (s *DockerNetworkSuite) TestDockerNetworkDisconnectFromHost(c *check.C) {
  864. dockerCmd(c, "run", "-d", "--name", "container1", "--net=host", "busybox", "top")
  865. c.Assert(waitRun("container1"), check.IsNil)
  866. out, _, err := dockerCmdWithError("network", "disconnect", "host", "container1")
  867. c.Assert(err, checker.NotNil, check.Commentf("Should err out disconnect from host"))
  868. c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetwork.Error())
  869. }
  870. func (s *DockerNetworkSuite) TestDockerNetworkConnectWithPortMapping(c *check.C) {
  871. testRequires(c, NotArm)
  872. dockerCmd(c, "network", "create", "test1")
  873. dockerCmd(c, "run", "-d", "--name", "c1", "-p", "5000:5000", "busybox", "top")
  874. c.Assert(waitRun("c1"), check.IsNil)
  875. dockerCmd(c, "network", "connect", "test1", "c1")
  876. }
  877. func (s *DockerNetworkSuite) TestDockerNetworkConnectWithMac(c *check.C) {
  878. macAddress := "02:42:ac:11:00:02"
  879. dockerCmd(c, "network", "create", "mynetwork")
  880. dockerCmd(c, "run", "--name=test", "-d", "--mac-address", macAddress, "busybox", "top")
  881. c.Assert(waitRun("test"), check.IsNil)
  882. mac1 := inspectField(c, "test", "NetworkSettings.Networks.bridge.MacAddress")
  883. c.Assert(strings.TrimSpace(mac1), checker.Equals, macAddress)
  884. dockerCmd(c, "network", "connect", "mynetwork", "test")
  885. mac2 := inspectField(c, "test", "NetworkSettings.Networks.mynetwork.MacAddress")
  886. c.Assert(strings.TrimSpace(mac2), checker.Not(checker.Equals), strings.TrimSpace(mac1))
  887. }
  888. func (s *DockerNetworkSuite) TestDockerNetworkInspectCreatedContainer(c *check.C) {
  889. dockerCmd(c, "create", "--name", "test", "busybox")
  890. networks := inspectField(c, "test", "NetworkSettings.Networks")
  891. c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should return 'bridge' network"))
  892. }
  893. func (s *DockerNetworkSuite) TestDockerNetworkRestartWithMultipleNetworks(c *check.C) {
  894. dockerCmd(c, "network", "create", "test")
  895. dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top")
  896. c.Assert(waitRun("foo"), checker.IsNil)
  897. dockerCmd(c, "network", "connect", "test", "foo")
  898. dockerCmd(c, "restart", "foo")
  899. networks := inspectField(c, "foo", "NetworkSettings.Networks")
  900. c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should contain 'bridge' network"))
  901. c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' network"))
  902. }
  903. func (s *DockerNetworkSuite) TestDockerNetworkConnectDisconnectToStoppedContainer(c *check.C) {
  904. dockerCmd(c, "network", "create", "test")
  905. dockerCmd(c, "create", "--name=foo", "busybox", "top")
  906. dockerCmd(c, "network", "connect", "test", "foo")
  907. networks := inspectField(c, "foo", "NetworkSettings.Networks")
  908. c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' network"))
  909. // Restart docker daemon to test the config has persisted to disk
  910. s.d.Restart()
  911. networks = inspectField(c, "foo", "NetworkSettings.Networks")
  912. c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' network"))
  913. // start the container and test if we can ping it from another container in the same network
  914. dockerCmd(c, "start", "foo")
  915. c.Assert(waitRun("foo"), checker.IsNil)
  916. ip := inspectField(c, "foo", "NetworkSettings.Networks.test.IPAddress")
  917. ip = strings.TrimSpace(ip)
  918. dockerCmd(c, "run", "--net=test", "busybox", "sh", "-c", fmt.Sprintf("ping -c 1 %s", ip))
  919. dockerCmd(c, "stop", "foo")
  920. // Test disconnect
  921. dockerCmd(c, "network", "disconnect", "test", "foo")
  922. networks = inspectField(c, "foo", "NetworkSettings.Networks")
  923. c.Assert(networks, checker.Not(checker.Contains), "test", check.Commentf("Should not contain 'test' network"))
  924. // Restart docker daemon to test the config has persisted to disk
  925. s.d.Restart()
  926. networks = inspectField(c, "foo", "NetworkSettings.Networks")
  927. c.Assert(networks, checker.Not(checker.Contains), "test", check.Commentf("Should not contain 'test' network"))
  928. }
  929. func (s *DockerNetworkSuite) TestDockerNetworkConnectPreferredIP(c *check.C) {
  930. // create two networks
  931. dockerCmd(c, "network", "create", "--subnet=172.28.0.0/16", "--subnet=2001:db8:1234::/64", "n0")
  932. assertNwIsAvailable(c, "n0")
  933. 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")
  934. assertNwIsAvailable(c, "n1")
  935. // run a container on first network specifying the ip addresses
  936. dockerCmd(c, "run", "-d", "--name", "c0", "--net=n0", "--ip", "172.28.99.88", "--ip6", "2001:db8:1234::9988", "busybox", "top")
  937. c.Assert(waitRun("c0"), check.IsNil)
  938. verifyIPAddressConfig(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
  939. verifyIPAddresses(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
  940. // connect the container to the second network specifying an ip addresses
  941. dockerCmd(c, "network", "connect", "--ip", "172.30.55.44", "--ip6", "2001:db8:abcd::5544", "n1", "c0")
  942. verifyIPAddressConfig(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
  943. verifyIPAddresses(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
  944. // Stop and restart the container
  945. dockerCmd(c, "stop", "c0")
  946. dockerCmd(c, "start", "c0")
  947. // verify requested addresses are applied and configs are still there
  948. verifyIPAddressConfig(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
  949. verifyIPAddresses(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
  950. verifyIPAddressConfig(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
  951. verifyIPAddresses(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
  952. // Still it should fail to connect to the default network with a specified IP (whatever ip)
  953. out, _, err := dockerCmdWithError("network", "connect", "--ip", "172.21.55.44", "bridge", "c0")
  954. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  955. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndIP.Error())
  956. }
  957. func (s *DockerNetworkSuite) TestDockerNetworkConnectPreferredIPStoppedContainer(c *check.C) {
  958. // create a container
  959. dockerCmd(c, "create", "--name", "c0", "busybox", "top")
  960. // create a network
  961. dockerCmd(c, "network", "create", "--subnet=172.30.0.0/16", "--subnet=2001:db8:abcd::/64", "n0")
  962. assertNwIsAvailable(c, "n0")
  963. // connect the container to the network specifying an ip addresses
  964. dockerCmd(c, "network", "connect", "--ip", "172.30.55.44", "--ip6", "2001:db8:abcd::5544", "n0", "c0")
  965. verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
  966. // start the container, verify config has not changed and ip addresses are assigned
  967. dockerCmd(c, "start", "c0")
  968. c.Assert(waitRun("c0"), check.IsNil)
  969. verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
  970. verifyIPAddresses(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
  971. // stop the container and check ip config has not changed
  972. dockerCmd(c, "stop", "c0")
  973. verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
  974. }
  975. func (s *DockerNetworkSuite) TestDockerNetworkUnsupportedRequiredIP(c *check.C) {
  976. // requested IP is not supported on predefined networks
  977. for _, mode := range []string{"none", "host", "bridge", "default"} {
  978. checkUnsupportedNetworkAndIP(c, mode)
  979. }
  980. // requested IP is not supported on networks with no user defined subnets
  981. dockerCmd(c, "network", "create", "n0")
  982. assertNwIsAvailable(c, "n0")
  983. out, _, err := dockerCmdWithError("run", "-d", "--ip", "172.28.99.88", "--net", "n0", "busybox", "top")
  984. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  985. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkNoSubnetAndIP.Error())
  986. out, _, err = dockerCmdWithError("run", "-d", "--ip6", "2001:db8:1234::9988", "--net", "n0", "busybox", "top")
  987. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  988. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkNoSubnetAndIP.Error())
  989. dockerCmd(c, "network", "rm", "n0")
  990. assertNwNotAvailable(c, "n0")
  991. }
  992. func checkUnsupportedNetworkAndIP(c *check.C, nwMode string) {
  993. out, _, err := dockerCmdWithError("run", "-d", "--net", nwMode, "--ip", "172.28.99.88", "--ip6", "2001:db8:1234::9988", "busybox", "top")
  994. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  995. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndIP.Error())
  996. }
  997. func verifyIPAddressConfig(c *check.C, cName, nwname, ipv4, ipv6 string) {
  998. if ipv4 != "" {
  999. out := inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAMConfig.IPv4Address", nwname))
  1000. c.Assert(strings.TrimSpace(out), check.Equals, ipv4)
  1001. }
  1002. if ipv6 != "" {
  1003. out := inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAMConfig.IPv6Address", nwname))
  1004. c.Assert(strings.TrimSpace(out), check.Equals, ipv6)
  1005. }
  1006. }
  1007. func verifyIPAddresses(c *check.C, cName, nwname, ipv4, ipv6 string) {
  1008. out := inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAddress", nwname))
  1009. c.Assert(strings.TrimSpace(out), check.Equals, ipv4)
  1010. out = inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.GlobalIPv6Address", nwname))
  1011. c.Assert(strings.TrimSpace(out), check.Equals, ipv6)
  1012. }
  1013. func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectLink(c *check.C) {
  1014. testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
  1015. dockerCmd(c, "network", "create", "-d", "bridge", "foo1")
  1016. dockerCmd(c, "network", "create", "-d", "bridge", "foo2")
  1017. dockerCmd(c, "run", "-d", "--net=foo1", "--name=first", "busybox", "top")
  1018. c.Assert(waitRun("first"), check.IsNil)
  1019. // run a container in a user-defined network with a link for an existing container
  1020. // and a link for a container that doesn't exist
  1021. dockerCmd(c, "run", "-d", "--net=foo1", "--name=second", "--link=first:FirstInFoo1",
  1022. "--link=third:bar", "busybox", "top")
  1023. c.Assert(waitRun("second"), check.IsNil)
  1024. // ping to first and its alias FirstInFoo1 must succeed
  1025. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  1026. c.Assert(err, check.IsNil)
  1027. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo1")
  1028. c.Assert(err, check.IsNil)
  1029. // connect first container to foo2 network
  1030. dockerCmd(c, "network", "connect", "foo2", "first")
  1031. // connect second container to foo2 network with a different alias for first container
  1032. dockerCmd(c, "network", "connect", "--link=first:FirstInFoo2", "foo2", "second")
  1033. // ping the new alias in network foo2
  1034. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo2")
  1035. c.Assert(err, check.IsNil)
  1036. // disconnect first container from foo1 network
  1037. dockerCmd(c, "network", "disconnect", "foo1", "first")
  1038. // link in foo1 network must fail
  1039. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo1")
  1040. c.Assert(err, check.NotNil)
  1041. // link in foo2 network must succeed
  1042. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo2")
  1043. c.Assert(err, check.IsNil)
  1044. }
  1045. // #19100 This is a deprecated feature test, it should be removed in Docker 1.12
  1046. func (s *DockerNetworkSuite) TestDockerNetworkStartAPIWithHostconfig(c *check.C) {
  1047. netName := "test"
  1048. conName := "foo"
  1049. dockerCmd(c, "network", "create", netName)
  1050. dockerCmd(c, "create", "--name", conName, "busybox", "top")
  1051. config := map[string]interface{}{
  1052. "HostConfig": map[string]interface{}{
  1053. "NetworkMode": netName,
  1054. },
  1055. }
  1056. _, _, err := sockRequest("POST", "/containers/"+conName+"/start", config)
  1057. c.Assert(err, checker.IsNil)
  1058. c.Assert(waitRun(conName), checker.IsNil)
  1059. networks := inspectField(c, conName, "NetworkSettings.Networks")
  1060. c.Assert(networks, checker.Contains, netName, check.Commentf(fmt.Sprintf("Should contain '%s' network", netName)))
  1061. c.Assert(networks, checker.Not(checker.Contains), "bridge", check.Commentf("Should not contain 'bridge' network"))
  1062. }
  1063. func (s *DockerNetworkSuite) TestDockerNetworkDisconnectDefault(c *check.C) {
  1064. netWorkName1 := "test1"
  1065. netWorkName2 := "test2"
  1066. containerName := "foo"
  1067. dockerCmd(c, "network", "create", netWorkName1)
  1068. dockerCmd(c, "network", "create", netWorkName2)
  1069. dockerCmd(c, "create", "--name", containerName, "busybox", "top")
  1070. dockerCmd(c, "network", "connect", netWorkName1, containerName)
  1071. dockerCmd(c, "network", "connect", netWorkName2, containerName)
  1072. dockerCmd(c, "network", "disconnect", "bridge", containerName)
  1073. dockerCmd(c, "start", containerName)
  1074. c.Assert(waitRun(containerName), checker.IsNil)
  1075. networks := inspectField(c, containerName, "NetworkSettings.Networks")
  1076. c.Assert(networks, checker.Contains, netWorkName1, check.Commentf(fmt.Sprintf("Should contain '%s' network", netWorkName1)))
  1077. c.Assert(networks, checker.Contains, netWorkName2, check.Commentf(fmt.Sprintf("Should contain '%s' network", netWorkName2)))
  1078. c.Assert(networks, checker.Not(checker.Contains), "bridge", check.Commentf("Should not contain 'bridge' network"))
  1079. }
  1080. func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectAlias(c *check.C) {
  1081. testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
  1082. dockerCmd(c, "network", "create", "-d", "bridge", "net1")
  1083. dockerCmd(c, "network", "create", "-d", "bridge", "net2")
  1084. dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo", "busybox", "top")
  1085. c.Assert(waitRun("first"), check.IsNil)
  1086. dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top")
  1087. c.Assert(waitRun("second"), check.IsNil)
  1088. // ping first container and its alias
  1089. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  1090. c.Assert(err, check.IsNil)
  1091. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  1092. c.Assert(err, check.IsNil)
  1093. // connect first container to net2 network
  1094. dockerCmd(c, "network", "connect", "--alias=bar", "net2", "first")
  1095. // connect second container to foo2 network with a different alias for first container
  1096. dockerCmd(c, "network", "connect", "net2", "second")
  1097. // ping the new alias in network foo2
  1098. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "bar")
  1099. c.Assert(err, check.IsNil)
  1100. // disconnect first container from net1 network
  1101. dockerCmd(c, "network", "disconnect", "net1", "first")
  1102. // ping to net1 scoped alias "foo" must fail
  1103. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  1104. c.Assert(err, check.NotNil)
  1105. // ping to net2 scoped alias "bar" must still succeed
  1106. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "bar")
  1107. c.Assert(err, check.IsNil)
  1108. // verify the alias option is rejected when running on predefined network
  1109. out, _, err := dockerCmdWithError("run", "--rm", "--name=any", "--net-alias=any", "busybox", "top")
  1110. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  1111. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndAlias.Error())
  1112. // verify the alias option is rejected when connecting to predefined network
  1113. out, _, err = dockerCmdWithError("network", "connect", "--alias=any", "bridge", "first")
  1114. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  1115. c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndAlias.Error())
  1116. }
  1117. func (s *DockerSuite) TestUserDefinedNetworkConnectivity(c *check.C) {
  1118. testRequires(c, DaemonIsLinux, NotUserNamespace)
  1119. dockerCmd(c, "network", "create", "-d", "bridge", "br.net1")
  1120. dockerCmd(c, "run", "-d", "--net=br.net1", "--name=c1.net1", "busybox", "top")
  1121. c.Assert(waitRun("c1.net1"), check.IsNil)
  1122. dockerCmd(c, "run", "-d", "--net=br.net1", "--name=c2.net1", "busybox", "top")
  1123. c.Assert(waitRun("c2.net1"), check.IsNil)
  1124. // ping first container by its unqualified name
  1125. _, _, err := dockerCmdWithError("exec", "c2.net1", "ping", "-c", "1", "c1.net1")
  1126. c.Assert(err, check.IsNil)
  1127. // ping first container by its qualified name
  1128. _, _, err = dockerCmdWithError("exec", "c2.net1", "ping", "-c", "1", "c1.net1.br.net1")
  1129. c.Assert(err, check.IsNil)
  1130. // ping with first qualified name masked by an additional domain. should fail
  1131. _, _, err = dockerCmdWithError("exec", "c2.net1", "ping", "-c", "1", "c1.net1.br.net1.google.com")
  1132. c.Assert(err, check.NotNil)
  1133. }
  1134. func (s *DockerSuite) TestDockerNetworkConnectFailsNoInspectChange(c *check.C) {
  1135. dockerCmd(c, "run", "-d", "--name=bb", "busybox", "top")
  1136. c.Assert(waitRun("bb"), check.IsNil)
  1137. ns0 := inspectField(c, "bb", "NetworkSettings.Networks.bridge")
  1138. // A failing redundant network connect should not alter current container's endpoint settings
  1139. _, _, err := dockerCmdWithError("network", "connect", "bridge", "bb")
  1140. c.Assert(err, check.NotNil)
  1141. ns1 := inspectField(c, "bb", "NetworkSettings.Networks.bridge")
  1142. c.Assert(ns1, check.Equals, ns0)
  1143. }
  1144. func (s *DockerNetworkSuite) TestDockerNetworkInternalMode(c *check.C) {
  1145. dockerCmd(c, "network", "create", "--driver=bridge", "--internal", "internal")
  1146. assertNwIsAvailable(c, "internal")
  1147. nr := getNetworkResource(c, "internal")
  1148. c.Assert(nr.Internal, checker.True)
  1149. dockerCmd(c, "run", "-d", "--net=internal", "--name=first", "busybox", "top")
  1150. c.Assert(waitRun("first"), check.IsNil)
  1151. dockerCmd(c, "run", "-d", "--net=internal", "--name=second", "busybox", "top")
  1152. c.Assert(waitRun("second"), check.IsNil)
  1153. _, _, err := dockerCmdWithError("exec", "first", "ping", "-c", "1", "www.google.com")
  1154. c.Assert(err, check.NotNil)
  1155. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  1156. c.Assert(err, check.IsNil)
  1157. }