scan_message_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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"
  16. "io/ioutil"
  17. "net/http"
  18. "os"
  19. "path/filepath"
  20. "runtime"
  21. "strings"
  22. "testing"
  23. "gotest.tools/v3/assert"
  24. "gotest.tools/v3/icmd"
  25. . "github.com/docker/compose-cli/utils/e2e"
  26. )
  27. func TestDisplayScanMessageAfterBuild(t *testing.T) {
  28. c := NewParallelE2eCLI(t, binDir)
  29. setupScanPlugin(t, c)
  30. res := c.RunDockerCmd("info")
  31. res.Assert(t, icmd.Expected{Out: "scan: Docker Scan"})
  32. t.Run("display when docker build", func(t *testing.T) {
  33. res := c.RunDockerCmd("build", "-t", "test-image-scan-msg", "./fixtures/simple-build-test/nginx-build")
  34. defer c.RunDockerOrExitError("rmi", "-f", "test-image-scan-msg")
  35. res.Assert(t, icmd.Expected{Err: "Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them"})
  36. })
  37. t.Run("do not display with docker build and quiet flag", func(t *testing.T) {
  38. res := c.RunDockerCmd("build", "-t", "test-image-scan-msg-quiet", "--quiet", "./fixtures/simple-build-test/nginx-build")
  39. defer c.RunDockerOrExitError("rmi", "-f", "test-image-scan-msg-quiet")
  40. assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"))
  41. res = c.RunDockerCmd("build", "-t", "test-image-scan-msg-q", "-q", "./fixtures/simple-build-test/nginx-build")
  42. defer c.RunDockerOrExitError("rmi", "-f", "test-image-scan-msg-q")
  43. assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"))
  44. })
  45. t.Run("do not display if envvar DOCKER_SCAN_SUGGEST=false", func(t *testing.T) {
  46. cmd := c.NewDockerCmd("build", "-t", "test-image-scan-msg", "./fixtures/build-test/nginx-build")
  47. defer c.RunDockerOrExitError("rmi", "-f", "test-image-scan-msg")
  48. cmd.Env = append(cmd.Env, "DOCKER_SCAN_SUGGEST=false")
  49. res := icmd.StartCmd(cmd)
  50. assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
  51. })
  52. t.Run("display on compose build", func(t *testing.T) {
  53. res := c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test-compose-build", "build")
  54. defer c.RunDockerOrExitError("rmi", "-f", "scan-msg-test-compose-build_nginx")
  55. res.Assert(t, icmd.Expected{Err: "Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them"})
  56. })
  57. t.Run("do not display on compose build with quiet flag", func(t *testing.T) {
  58. res := c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test-quiet", "build", "--quiet")
  59. assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
  60. res = c.RunDockerCmd("rmi", "-f", "scan-msg-test-quiet_nginx")
  61. assert.Assert(t, !strings.Contains(res.Combined(), "No such image"))
  62. res = c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test-q", "build", "-q")
  63. defer c.RunDockerOrExitError("rmi", "-f", "scan-msg-test-q_nginx")
  64. assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
  65. })
  66. _ = c.RunDockerOrExitError("rmi", "scan-msg-test_nginx")
  67. t.Run("display on compose up if image is built", func(t *testing.T) {
  68. res := c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test", "up", "-d")
  69. defer c.RunDockerOrExitError("compose", "-f", "./fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test", "down")
  70. res.Assert(t, icmd.Expected{Err: "Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them"})
  71. })
  72. t.Run("do not display on compose up if no image built", func(t *testing.T) { // re-run the same Compose aproject
  73. res := c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test", "up", "-d")
  74. defer c.RunDockerOrExitError("compose", "-f", "./fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test", "down", "--rmi", "all")
  75. assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
  76. })
  77. t.Run("do not display if scan already invoked", func(t *testing.T) {
  78. _ = os.MkdirAll(filepath.Join(c.ConfigDir, "scan"), 0755)
  79. scanConfigFile := filepath.Join(c.ConfigDir, "scan", "config.json")
  80. err := ioutil.WriteFile(scanConfigFile, []byte(`{"optin":true}`), 0644)
  81. assert.NilError(t, err)
  82. res := c.RunDockerCmd("build", "-t", "test-image-scan-msg", "./fixtures/simple-build-test/nginx-build")
  83. assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
  84. })
  85. }
  86. func setupScanPlugin(t *testing.T, c *E2eCLI) {
  87. _ = os.MkdirAll(filepath.Join(c.ConfigDir, "cli-plugins"), 0755)
  88. scanPluginFile := "docker-scan"
  89. scanPluginURL := "https://github.com/docker/scan-cli-plugin/releases/download/v0.7.0/docker-scan_linux_amd64"
  90. if runtime.GOOS == "windows" {
  91. scanPluginFile += ".exe"
  92. scanPluginURL = "https://github.com/docker/scan-cli-plugin/releases/download/v0.7.0/docker-scan_windows_amd64.exe"
  93. }
  94. if runtime.GOOS == "darwin" {
  95. scanPluginURL = "https://github.com/docker/scan-cli-plugin/releases/download/v0.7.0/docker-scan_darwin_amd64"
  96. }
  97. localScanBinary := filepath.Join("..", "..", "..", "bin", scanPluginFile)
  98. if _, err := os.Stat(localScanBinary); os.IsNotExist(err) {
  99. out, err := os.Create(localScanBinary)
  100. assert.NilError(t, err)
  101. defer out.Close() //nolint:errcheck
  102. resp, err := http.Get(scanPluginURL)
  103. assert.NilError(t, err)
  104. defer resp.Body.Close() //nolint:errcheck
  105. _, err = io.Copy(out, resp.Body)
  106. assert.NilError(t, err)
  107. }
  108. finalScanBinaryFile := filepath.Join(c.ConfigDir, "cli-plugins", scanPluginFile)
  109. out, err := os.Create(finalScanBinaryFile)
  110. assert.NilError(t, err)
  111. defer out.Close() //nolint:errcheck
  112. in, err := os.Open(localScanBinary)
  113. assert.NilError(t, err)
  114. defer in.Close() //nolint:errcheck
  115. _, err = io.Copy(out, in)
  116. assert.NilError(t, err)
  117. err = os.Chmod(finalScanBinaryFile, 7777)
  118. assert.NilError(t, err)
  119. }