networks_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. c := NewParallelE2eCLI(t, binDir)
  24. const projectName = "network_e2e"
  25. t.Run("ensure we do not reuse previous networks", func(t *testing.T) {
  26. c.RunDockerOrExitError("network", "rm", projectName+"_dbnet")
  27. c.RunDockerOrExitError("network", "rm", "microservices")
  28. })
  29. t.Run("up", func(t *testing.T) {
  30. c.RunDockerCmd("compose", "-f", "./fixtures/network-test/compose.yaml", "--project-name", projectName, "up", "-d")
  31. })
  32. t.Run("check running project", func(t *testing.T) {
  33. res := c.RunDockerCmd("compose", "-p", projectName, "ps")
  34. res.Assert(t, icmd.Expected{Out: `web`})
  35. endpoint := "http://localhost:80"
  36. output := HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
  37. assert.Assert(t, strings.Contains(output, `"word":`))
  38. res = c.RunDockerCmd("network", "ls")
  39. res.Assert(t, icmd.Expected{Out: projectName + "_dbnet"})
  40. res.Assert(t, icmd.Expected{Out: "microservices"})
  41. })
  42. t.Run("port", func(t *testing.T) {
  43. res := c.RunDockerCmd("compose", "--project-name", projectName, "port", "words", "8080")
  44. res.Assert(t, icmd.Expected{Out: `0.0.0.0:8080`})
  45. })
  46. t.Run("down", func(t *testing.T) {
  47. _ = c.RunDockerCmd("compose", "--project-name", projectName, "down")
  48. })
  49. t.Run("check networks after down", func(t *testing.T) {
  50. res := c.RunDockerCmd("network", "ls")
  51. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  52. assert.Assert(t, !strings.Contains(res.Combined(), "microservices"), res.Combined())
  53. })
  54. }
  55. func TestNetworkAliassesAndLinks(t *testing.T) {
  56. c := NewParallelE2eCLI(t, binDir)
  57. const projectName = "network_alias_e2e"
  58. t.Run("up", func(t *testing.T) {
  59. c.RunDockerCmd("compose", "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName, "up", "-d")
  60. })
  61. t.Run("curl alias", func(t *testing.T) {
  62. res := c.RunDockerCmd("compose", "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName, "exec", "-T", "container1", "curl", "http://alias-of-container2/")
  63. assert.Assert(t, strings.Contains(res.Stdout(), "Welcome to nginx!"), res.Stdout())
  64. })
  65. t.Run("curl links", func(t *testing.T) {
  66. res := c.RunDockerCmd("compose", "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName, "exec", "-T", "container1", "curl", "http://container/")
  67. assert.Assert(t, strings.Contains(res.Stdout(), "Welcome to nginx!"), res.Stdout())
  68. })
  69. t.Run("down", func(t *testing.T) {
  70. _ = c.RunDockerCmd("compose", "--project-name", projectName, "down")
  71. })
  72. }
  73. func TestIPAMConfig(t *testing.T) {
  74. c := NewParallelE2eCLI(t, binDir)
  75. const projectName = "ipam_e2e"
  76. t.Run("ensure we do not reuse previous networks", func(t *testing.T) {
  77. c.RunDockerOrExitError("network", "rm", projectName+"_default")
  78. })
  79. t.Run("up", func(t *testing.T) {
  80. c.RunDockerCmd("compose", "-f", "./fixtures/ipam/compose.yaml", "--project-name", projectName, "up", "-d")
  81. })
  82. t.Run("ensure service get fixed IP assigned", func(t *testing.T) {
  83. res := c.RunDockerCmd("inspect", projectName+"-foo-1", "-f", "{{ .NetworkSettings.Networks."+projectName+"_default.IPAddress }}")
  84. res.Assert(t, icmd.Expected{Out: "10.1.0.100"})
  85. })
  86. t.Run("down", func(t *testing.T) {
  87. _ = c.RunDockerCmd("compose", "--project-name", projectName, "down")
  88. })
  89. }
  90. func TestNetworkModes(t *testing.T) {
  91. c := NewParallelE2eCLI(t, binDir)
  92. const projectName = "network_mode_service_run"
  93. t.Run("run with service mode dependency", func(t *testing.T) {
  94. res := c.RunDockerOrExitError("compose", "-f", "./fixtures/network-test/compose.yaml", "--project-name", projectName, "run", "mydb", "echo", "success")
  95. res.Assert(t, icmd.Expected{Out: "success"})
  96. })
  97. t.Run("down", func(t *testing.T) {
  98. _ = c.RunDockerCmd("compose", "--project-name", projectName, "down")
  99. })
  100. }