compose_build_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. "os"
  17. "strings"
  18. "testing"
  19. "time"
  20. "gotest.tools/v3/assert"
  21. "gotest.tools/v3/icmd"
  22. )
  23. func TestLocalComposeBuild(t *testing.T) {
  24. c := NewParallelCLI(t)
  25. t.Run("build named and unnamed images", func(t *testing.T) {
  26. // ensure local test run does not reuse previously build image
  27. c.RunDockerOrExitError(t, "rmi", "build-test_nginx")
  28. c.RunDockerOrExitError(t, "rmi", "custom-nginx")
  29. res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "build")
  30. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  31. c.RunDockerCmd(t, "image", "inspect", "build-test_nginx")
  32. c.RunDockerCmd(t, "image", "inspect", "custom-nginx")
  33. })
  34. t.Run("build with build-arg", func(t *testing.T) {
  35. // ensure local test run does not reuse previously build image
  36. c.RunDockerOrExitError(t, "rmi", "build-test_nginx")
  37. c.RunDockerOrExitError(t, "rmi", "custom-nginx")
  38. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "build", "--build-arg", "FOO=BAR")
  39. res := c.RunDockerCmd(t, "image", "inspect", "build-test_nginx")
  40. res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
  41. })
  42. t.Run("build with build-arg set by env", func(t *testing.T) {
  43. // ensure local test run does not reuse previously build image
  44. c.RunDockerOrExitError(t, "rmi", "build-test_nginx")
  45. c.RunDockerOrExitError(t, "rmi", "custom-nginx")
  46. icmd.RunCmd(c.NewDockerCmd("compose", "--project-directory", "fixtures/build-test", "build", "--build-arg", "FOO"),
  47. func(cmd *icmd.Cmd) {
  48. cmd.Env = append(cmd.Env, "FOO=BAR")
  49. })
  50. res := c.RunDockerCmd(t, "image", "inspect", "build-test_nginx")
  51. res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
  52. })
  53. t.Run("build with multiple build-args ", func(t *testing.T) {
  54. // ensure local test run does not reuse previously build image
  55. c.RunDockerOrExitError(t, "rmi", "-f", "multi-args_multiargs")
  56. cmd := c.NewDockerCmd("compose", "--project-directory", "fixtures/build-test/multi-args", "build")
  57. icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
  58. cmd.Env = append(cmd.Env, "DOCKER_BUILDKIT=0")
  59. })
  60. res := c.RunDockerCmd(t, "image", "inspect", "multi-args_multiargs")
  61. res.Assert(t, icmd.Expected{Out: `"RESULT": "SUCCESS"`})
  62. })
  63. t.Run("build failed with ssh default value", func(t *testing.T) {
  64. //unset SSH_AUTH_SOCK to be sure we don't have a default value for the SSH Agent
  65. defaultSSHAUTHSOCK := os.Getenv("SSH_AUTH_SOCK")
  66. os.Unsetenv("SSH_AUTH_SOCK") //nolint:errcheck
  67. defer os.Setenv("SSH_AUTH_SOCK", defaultSSHAUTHSOCK) //nolint:errcheck
  68. res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test", "build", "--ssh", "")
  69. res.Assert(t, icmd.Expected{
  70. ExitCode: 1,
  71. Err: "invalid empty ssh agent socket: make sure SSH_AUTH_SOCK is set",
  72. })
  73. })
  74. t.Run("build succeed with ssh from Compose file", func(t *testing.T) {
  75. c.RunDockerOrExitError(t, "rmi", "build-test-ssh")
  76. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test/ssh", "build")
  77. c.RunDockerCmd(t, "image", "inspect", "build-test-ssh")
  78. })
  79. t.Run("build succeed with ssh from CLI", func(t *testing.T) {
  80. c.RunDockerOrExitError(t, "rmi", "build-test-ssh")
  81. c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/ssh/compose-without-ssh.yaml", "--project-directory",
  82. "fixtures/build-test/ssh", "build", "--no-cache", "--ssh", "fake-ssh=./fixtures/build-test/ssh/fake_rsa")
  83. c.RunDockerCmd(t, "image", "inspect", "build-test-ssh")
  84. })
  85. t.Run("build failed with wrong ssh key id from CLI", func(t *testing.T) {
  86. c.RunDockerOrExitError(t, "rmi", "build-test-ssh")
  87. res := c.RunDockerComposeCmdNoCheck(t, "-f", "fixtures/build-test/ssh/compose-without-ssh.yaml",
  88. "--project-directory", "fixtures/build-test/ssh", "build", "--no-cache", "--ssh",
  89. "wrong-ssh=./fixtures/build-test/ssh/fake_rsa")
  90. res.Assert(t, icmd.Expected{
  91. ExitCode: 17,
  92. Err: "failed to solve: rpc error: code = Unknown desc = unset ssh forward key fake-ssh",
  93. })
  94. })
  95. t.Run("build succeed as part of up with ssh from Compose file", func(t *testing.T) {
  96. c.RunDockerOrExitError(t, "rmi", "build-test-ssh")
  97. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test/ssh", "up", "-d", "--build")
  98. t.Cleanup(func() {
  99. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test/ssh", "down")
  100. })
  101. c.RunDockerCmd(t, "image", "inspect", "build-test-ssh")
  102. })
  103. t.Run("build as part of up", func(t *testing.T) {
  104. c.RunDockerOrExitError(t, "rmi", "build-test_nginx")
  105. c.RunDockerOrExitError(t, "rmi", "custom-nginx")
  106. res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "up", "-d")
  107. t.Cleanup(func() {
  108. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "down")
  109. })
  110. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  111. res.Assert(t, icmd.Expected{Out: "COPY static2 /usr/share/nginx/html"})
  112. output := HTTPGetWithRetry(t, "http://localhost:8070", http.StatusOK, 2*time.Second, 20*time.Second)
  113. assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
  114. c.RunDockerCmd(t, "image", "inspect", "build-test_nginx")
  115. c.RunDockerCmd(t, "image", "inspect", "custom-nginx")
  116. })
  117. t.Run("no rebuild when up again", func(t *testing.T) {
  118. res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "up", "-d")
  119. assert.Assert(t, !strings.Contains(res.Stdout(), "COPY static"), res.Stdout())
  120. })
  121. t.Run("rebuild when up --build", func(t *testing.T) {
  122. res := c.RunDockerComposeCmd(t, "--workdir", "fixtures/build-test", "up", "-d", "--build")
  123. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  124. res.Assert(t, icmd.Expected{Out: "COPY static2 /usr/share/nginx/html"})
  125. })
  126. t.Run("cleanup build project", func(t *testing.T) {
  127. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "down")
  128. c.RunDockerCmd(t, "rmi", "build-test_nginx")
  129. c.RunDockerCmd(t, "rmi", "custom-nginx")
  130. })
  131. }
  132. func TestBuildSecrets(t *testing.T) {
  133. c := NewParallelCLI(t)
  134. t.Run("build with secrets", func(t *testing.T) {
  135. // ensure local test run does not reuse previously build image
  136. c.RunDockerOrExitError(t, "rmi", "build-test-secret")
  137. res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test/secrets", "build")
  138. res.Assert(t, icmd.Success)
  139. })
  140. }
  141. func TestBuildTags(t *testing.T) {
  142. c := NewParallelCLI(t)
  143. t.Run("build with tags", func(t *testing.T) {
  144. // ensure local test run does not reuse previously build image
  145. c.RunDockerOrExitError(t, "rmi", "build-test-tags")
  146. c.RunDockerComposeCmd(t, "--project-directory", "./fixtures/build-test/tags", "build", "--no-cache")
  147. res := c.RunDockerCmd(t, "image", "inspect", "build-test-tags")
  148. expectedOutput := `"RepoTags": [
  149. "docker/build-test-tags:1.0.0",
  150. "build-test-tags:latest",
  151. "other-image-name:v1.0.0"
  152. ],
  153. `
  154. res.Assert(t, icmd.Expected{Out: expectedOutput})
  155. })
  156. }