scan_message_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. res.Assert(t, icmd.Expected{Out: "Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them"})
  35. })
  36. t.Run("do not display if envvar DOCKER_SCAN_SUGGEST=false", func(t *testing.T) {
  37. cmd := c.NewDockerCmd("build", "-t", "test-image-scan-msg", "./fixtures/build-test/nginx-build")
  38. cmd.Env = append(cmd.Env, "DOCKER_SCAN_SUGGEST=false")
  39. res := icmd.StartCmd(cmd)
  40. assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
  41. })
  42. t.Run("display on compose build", func(t *testing.T) {
  43. res := c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yml", "-p", "scan-msg-test", "build")
  44. res.Assert(t, icmd.Expected{Out: "Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them"})
  45. })
  46. _ = c.RunDockerOrExitError("rmi", "scan-msg-test_nginx")
  47. t.Run("display on compose up if image is built", func(t *testing.T) {
  48. res := c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yml", "-p", "scan-msg-test", "up", "-d")
  49. defer c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yml", "-p", "scan-msg-test", "down")
  50. res.Assert(t, icmd.Expected{Out: "Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them"})
  51. })
  52. t.Run("do not display on compose up if no image built", func(t *testing.T) { // re-run the same Compose aproject
  53. res := c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yml", "-p", "scan-msg-test", "up", "-d")
  54. defer c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yml", "-p", "scan-msg-test", "down")
  55. assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
  56. })
  57. t.Run("do not display if scan already invoked", func(t *testing.T) {
  58. _ = os.MkdirAll(filepath.Join(c.ConfigDir, "scan"), 0755)
  59. scanConfigFile := filepath.Join(c.ConfigDir, "scan", "config.json")
  60. err := ioutil.WriteFile(scanConfigFile, []byte(`{"optin":true}`), 0644)
  61. assert.NilError(t, err)
  62. res := c.RunDockerCmd("build", "-t", "test-image-scan-msg", "./fixtures/simple-build-test/nginx-build")
  63. assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
  64. })
  65. }
  66. func setupScanPlugin(t *testing.T, c *E2eCLI) {
  67. _ = os.MkdirAll(filepath.Join(c.ConfigDir, "cli-plugins"), 0755)
  68. scanPluginFile := "docker-scan"
  69. scanPluginURL := "https://github.com/docker/scan-cli-plugin/releases/download/v0.7.0/docker-scan_linux_amd64"
  70. if runtime.GOOS == "windows" {
  71. scanPluginFile += ".exe"
  72. scanPluginURL = "https://github.com/docker/scan-cli-plugin/releases/download/v0.7.0/docker-scan_windows_amd64.exe"
  73. }
  74. if runtime.GOOS == "darwin" {
  75. scanPluginURL = "https://github.com/docker/scan-cli-plugin/releases/download/v0.7.0/docker-scan_darwin_amd64"
  76. }
  77. localScanBinary := filepath.Join("..", "..", "..", "bin", scanPluginFile)
  78. if _, err := os.Stat(localScanBinary); os.IsNotExist(err) {
  79. out, err := os.Create(localScanBinary)
  80. assert.NilError(t, err)
  81. defer out.Close() //nolint:errcheck
  82. resp, err := http.Get(scanPluginURL)
  83. assert.NilError(t, err)
  84. defer resp.Body.Close() //nolint:errcheck
  85. _, err = io.Copy(out, resp.Body)
  86. assert.NilError(t, err)
  87. }
  88. finalScanBinaryFile := filepath.Join(c.ConfigDir, "cli-plugins", scanPluginFile)
  89. out, err := os.Create(finalScanBinaryFile)
  90. assert.NilError(t, err)
  91. defer out.Close() //nolint:errcheck
  92. in, err := os.Open(localScanBinary)
  93. assert.NilError(t, err)
  94. defer in.Close() //nolint:errcheck
  95. _, err = io.Copy(out, in)
  96. assert.NilError(t, err)
  97. err = os.Chmod(finalScanBinaryFile, 7777)
  98. assert.NilError(t, err)
  99. }