scan_message_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "io/ioutil"
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. "testing"
  20. "github.com/docker/compose/v2/pkg/utils"
  21. "gotest.tools/v3/assert"
  22. "gotest.tools/v3/icmd"
  23. )
  24. func TestDisplayScanMessageAfterBuild(t *testing.T) {
  25. c := NewParallelE2eCLI(t, binDir)
  26. // assert docker scan plugin is available
  27. c.RunDockerOrExitError("scan", "--help")
  28. t.Run("display on compose build", func(t *testing.T) {
  29. res := c.RunDockerCmd("compose", "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test-compose-build", "build")
  30. defer c.RunDockerOrExitError("rmi", "-f", "scan-msg-test-compose-build_nginx")
  31. res.Assert(t, icmd.Expected{Err: utils.ScanSuggestMsg})
  32. })
  33. t.Run("do not display on compose build with quiet flag", func(t *testing.T) {
  34. res := c.RunDockerCmd("compose", "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test-quiet", "build", "--quiet")
  35. assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
  36. res = c.RunDockerCmd("rmi", "-f", "scan-msg-test-quiet_nginx")
  37. assert.Assert(t, !strings.Contains(res.Combined(), "No such image"))
  38. res = c.RunDockerCmd("compose", "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test-q", "build", "-q")
  39. defer c.RunDockerOrExitError("rmi", "-f", "scan-msg-test-q_nginx")
  40. assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
  41. })
  42. _ = c.RunDockerOrExitError("rmi", "scan-msg-test_nginx")
  43. t.Run("display on compose up if image is built", func(t *testing.T) {
  44. res := c.RunDockerCmd("compose", "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test", "up", "-d")
  45. defer c.RunDockerOrExitError("compose", "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test", "down")
  46. res.Assert(t, icmd.Expected{Err: utils.ScanSuggestMsg})
  47. })
  48. t.Run("do not display on compose up if no image built", func(t *testing.T) { // re-run the same Compose aproject
  49. res := c.RunDockerCmd("compose", "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test", "up", "-d")
  50. defer c.RunDockerOrExitError("compose", "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test", "down", "--rmi", "all")
  51. assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
  52. })
  53. t.Run("do not display if scan already invoked", func(t *testing.T) {
  54. _ = os.MkdirAll(filepath.Join(c.ConfigDir, "scan"), 0755)
  55. scanConfigFile := filepath.Join(c.ConfigDir, "scan", "config.json")
  56. err := ioutil.WriteFile(scanConfigFile, []byte(`{"optin":true}`), 0644)
  57. assert.NilError(t, err)
  58. res := c.RunDockerCmd("build", "-t", "test-image-scan-msg", "fixtures/simple-build-test/nginx-build")
  59. assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
  60. })
  61. }