publish_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "strings"
  16. "testing"
  17. "gotest.tools/v3/assert"
  18. "gotest.tools/v3/icmd"
  19. )
  20. func TestPublishChecks(t *testing.T) {
  21. c := NewParallelCLI(t)
  22. const projectName = "compose-e2e-explicit-profiles"
  23. t.Run("publish error environment", func(t *testing.T) {
  24. res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/publish/compose-environment.yml",
  25. "-p", projectName, "alpha", "publish", "test/test")
  26. res.Assert(t, icmd.Expected{ExitCode: 1, Err: `service "serviceA" has environment variable(s) declared.
  27. To avoid leaking sensitive data,`})
  28. })
  29. t.Run("publish error env_file", func(t *testing.T) {
  30. res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/publish/compose-env-file.yml",
  31. "-p", projectName, "alpha", "publish", "test/test")
  32. res.Assert(t, icmd.Expected{ExitCode: 1, Err: `service "serviceA" has env_file declared.
  33. service "serviceA" has environment variable(s) declared.
  34. To avoid leaking sensitive data,`})
  35. })
  36. t.Run("publish multiple errors env_file and environment", func(t *testing.T) {
  37. res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/publish/compose-multi-env-config.yml",
  38. "-p", projectName, "alpha", "publish", "test/test")
  39. // we don't in which order the services will be loaded, so we can't predict the order of the error messages
  40. assert.Assert(t, strings.Contains(res.Combined(), `service "serviceB" has env_file declared.`), res.Combined())
  41. assert.Assert(t, strings.Contains(res.Combined(), `service "serviceB" has environment variable(s) declared.`), res.Combined())
  42. assert.Assert(t, strings.Contains(res.Combined(), `service "serviceA" has environment variable(s) declared.`), res.Combined())
  43. assert.Assert(t, strings.Contains(res.Combined(), `To avoid leaking sensitive data, you must either explicitly allow the sending of environment variables by using the --with-env flag,
  44. or remove sensitive data from your Compose configuration
  45. `), res.Combined())
  46. })
  47. t.Run("publish success environment", func(t *testing.T) {
  48. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/publish/compose-environment.yml",
  49. "-p", projectName, "alpha", "publish", "test/test", "--with-env", "-y", "--dry-run")
  50. assert.Assert(t, strings.Contains(res.Combined(), "test/test publishing"), res.Combined())
  51. assert.Assert(t, strings.Contains(res.Combined(), "test/test published"), res.Combined())
  52. })
  53. t.Run("publish success env_file", func(t *testing.T) {
  54. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/publish/compose-env-file.yml",
  55. "-p", projectName, "alpha", "publish", "test/test", "--with-env", "-y", "--dry-run")
  56. assert.Assert(t, strings.Contains(res.Combined(), "test/test publishing"), res.Combined())
  57. assert.Assert(t, strings.Contains(res.Combined(), "test/test published"), res.Combined())
  58. })
  59. t.Run("publish approve validation message", func(t *testing.T) {
  60. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/publish/compose-env-file.yml",
  61. "-p", projectName, "alpha", "publish", "test/test", "--with-env", "--dry-run")
  62. cmd.Stdin = strings.NewReader("y\n")
  63. res := icmd.RunCmd(cmd)
  64. res.Assert(t, icmd.Expected{ExitCode: 0})
  65. assert.Assert(t, strings.Contains(res.Combined(), "Are you ok to publish these environment variables? [y/N]:"), res.Combined())
  66. assert.Assert(t, strings.Contains(res.Combined(), "test/test publishing"), res.Combined())
  67. assert.Assert(t, strings.Contains(res.Combined(), "test/test published"), res.Combined())
  68. })
  69. t.Run("publish refuse validation message", func(t *testing.T) {
  70. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/publish/compose-env-file.yml",
  71. "-p", projectName, "alpha", "publish", "test/test", "--with-env", "--dry-run")
  72. cmd.Stdin = strings.NewReader("n\n")
  73. res := icmd.RunCmd(cmd)
  74. res.Assert(t, icmd.Expected{ExitCode: 0})
  75. assert.Assert(t, strings.Contains(res.Combined(), "Are you ok to publish these environment variables? [y/N]:"), res.Combined())
  76. assert.Assert(t, !strings.Contains(res.Combined(), "test/test publishing"), res.Combined())
  77. assert.Assert(t, !strings.Contains(res.Combined(), "test/test published"), res.Combined())
  78. })
  79. t.Run("publish list env variables", func(t *testing.T) {
  80. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/publish/compose-multi-env-config.yml",
  81. "-p", projectName, "alpha", "publish", "test/test", "--with-env", "--dry-run")
  82. cmd.Stdin = strings.NewReader("n\n")
  83. res := icmd.RunCmd(cmd)
  84. res.Assert(t, icmd.Expected{ExitCode: 0})
  85. assert.Assert(t, strings.Contains(res.Combined(), `you are about to publish environment variables within your OCI artifact.
  86. please double check that you are not leaking sensitive data`), res.Combined())
  87. assert.Assert(t, strings.Contains(res.Combined(), `Service/Config serviceA
  88. FOO=bar`), res.Combined())
  89. assert.Assert(t, strings.Contains(res.Combined(), `Service/Config serviceB`), res.Combined())
  90. // we don't know in which order the env variables will be loaded
  91. assert.Assert(t, strings.Contains(res.Combined(), `FOO=bar`), res.Combined())
  92. assert.Assert(t, strings.Contains(res.Combined(), `BAR=baz`), res.Combined())
  93. assert.Assert(t, strings.Contains(res.Combined(), `QUIX=`), res.Combined())
  94. })
  95. }