networks_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package e2e
  14. import (
  15. "net/http"
  16. "strings"
  17. "testing"
  18. "time"
  19. "gotest.tools/v3/assert"
  20. "gotest.tools/v3/icmd"
  21. )
  22. func TestNetworks(t *testing.T) {
  23. // fixture is shared with TestNetworkModes and is not safe to run concurrently
  24. c := NewCLI(t)
  25. const projectName = "network-e2e"
  26. t.Run("ensure we do not reuse previous networks", func(t *testing.T) {
  27. c.RunDockerOrExitError(t, "network", "rm", projectName+"-dbnet")
  28. c.RunDockerOrExitError(t, "network", "rm", "microservices")
  29. })
  30. t.Run("up", func(t *testing.T) {
  31. c.RunDockerComposeCmd(t, "-f", "./fixtures/network-test/compose.yaml", "--project-name", projectName, "up",
  32. "-d")
  33. })
  34. t.Run("check running project", func(t *testing.T) {
  35. res := c.RunDockerComposeCmd(t, "-p", projectName, "ps")
  36. res.Assert(t, icmd.Expected{Out: `web`})
  37. endpoint := "http://localhost:80"
  38. output := HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
  39. assert.Assert(t, strings.Contains(output, `"word":`))
  40. res = c.RunDockerCmd(t, "network", "ls")
  41. res.Assert(t, icmd.Expected{Out: projectName + "_dbnet"})
  42. res.Assert(t, icmd.Expected{Out: "microservices"})
  43. })
  44. t.Run("port", func(t *testing.T) {
  45. res := c.RunDockerComposeCmd(t, "--project-name", projectName, "port", "words", "8080")
  46. res.Assert(t, icmd.Expected{Out: `0.0.0.0:8080`})
  47. })
  48. t.Run("down", func(t *testing.T) {
  49. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  50. })
  51. t.Run("check networks after down", func(t *testing.T) {
  52. res := c.RunDockerCmd(t, "network", "ls")
  53. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  54. assert.Assert(t, !strings.Contains(res.Combined(), "microservices"), res.Combined())
  55. })
  56. }
  57. func TestNetworkAliases(t *testing.T) {
  58. c := NewParallelCLI(t)
  59. const projectName = "network_alias_e2e"
  60. t.Run("up", func(t *testing.T) {
  61. c.RunDockerComposeCmd(t, "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName, "up",
  62. "-d")
  63. })
  64. t.Run("curl alias", func(t *testing.T) {
  65. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName,
  66. "exec", "-T", "container1", "curl", "http://alias-of-container2/")
  67. assert.Assert(t, strings.Contains(res.Stdout(), "Welcome to nginx!"), res.Stdout())
  68. })
  69. t.Run("curl links", func(t *testing.T) {
  70. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName,
  71. "exec", "-T", "container1", "curl", "http://container/")
  72. assert.Assert(t, strings.Contains(res.Stdout(), "Welcome to nginx!"), res.Stdout())
  73. })
  74. t.Run("down", func(t *testing.T) {
  75. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  76. })
  77. }
  78. func TestNetworkLinks(t *testing.T) {
  79. c := NewParallelCLI(t)
  80. const projectName = "network_link_e2e"
  81. t.Run("up", func(t *testing.T) {
  82. c.RunDockerComposeCmd(t, "-f", "./fixtures/network-links/compose.yaml", "--project-name", projectName, "up",
  83. "-d")
  84. })
  85. t.Run("curl links in default bridge network", func(t *testing.T) {
  86. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/network-links/compose.yaml", "--project-name", projectName,
  87. "exec", "-T", "container2", "curl", "http://container1/")
  88. assert.Assert(t, strings.Contains(res.Stdout(), "Welcome to nginx!"), res.Stdout())
  89. })
  90. t.Run("down", func(t *testing.T) {
  91. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  92. })
  93. }
  94. func TestIPAMConfig(t *testing.T) {
  95. c := NewParallelCLI(t)
  96. const projectName = "ipam_e2e"
  97. t.Run("ensure we do not reuse previous networks", func(t *testing.T) {
  98. c.RunDockerOrExitError(t, "network", "rm", projectName+"-default")
  99. })
  100. t.Run("up", func(t *testing.T) {
  101. c.RunDockerComposeCmd(t, "-f", "./fixtures/ipam/compose.yaml", "--project-name", projectName, "up", "-d")
  102. })
  103. t.Run("ensure service get fixed IP assigned", func(t *testing.T) {
  104. res := c.RunDockerCmd(t, "inspect", projectName+"-foo-1", "-f",
  105. "{{ .NetworkSettings.Networks."+projectName+"_default.IPAddress }}")
  106. res.Assert(t, icmd.Expected{Out: "10.1.0.100"})
  107. })
  108. t.Run("down", func(t *testing.T) {
  109. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  110. })
  111. }
  112. func TestNetworkModes(t *testing.T) {
  113. // fixture is shared with TestNetworks and is not safe to run concurrently
  114. c := NewCLI(t)
  115. const projectName = "network_mode_service_run"
  116. t.Run("run with service mode dependency", func(t *testing.T) {
  117. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/network-test/compose.yaml", "--project-name", projectName, "run", "-T", "mydb", "echo", "success")
  118. res.Assert(t, icmd.Expected{Out: "success"})
  119. })
  120. t.Run("down", func(t *testing.T) {
  121. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  122. })
  123. }