fixtures_linux_daemon_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/testutil/fixtures/load"
  12. "gotest.tools/v3/assert"
  13. )
  14. func ensureSyscallTest(c *testing.T) {
  15. defer testEnv.ProtectImage(c, "syscall-test:latest")
  16. // If the image already exists, there's nothing left to do.
  17. if testEnv.HasExistingImage(c, "syscall-test:latest") {
  18. return
  19. }
  20. // if no match, must build in docker, which is significantly slower
  21. // (slower mostly because of the vfs graphdriver)
  22. if testEnv.OSType != runtime.GOOS {
  23. ensureSyscallTestBuild(c)
  24. return
  25. }
  26. tmp, err := ioutil.TempDir("", "syscall-test-build")
  27. assert.NilError(c, err, "couldn't create temp dir")
  28. defer os.RemoveAll(tmp)
  29. gcc, err := exec.LookPath("gcc")
  30. assert.NilError(c, err, "could not find gcc")
  31. tests := []string{"userns", "ns", "acct", "setuid", "setgid", "socket", "raw"}
  32. for _, test := range tests {
  33. out, err := exec.Command(gcc, "-g", "-Wall", "-static", fmt.Sprintf("../contrib/syscall-test/%s.c", test), "-o", fmt.Sprintf("%s/%s-test", tmp, test)).CombinedOutput()
  34. assert.NilError(c, err, string(out))
  35. }
  36. if runtime.GOOS == "linux" && runtime.GOARCH == "amd64" {
  37. out, err := exec.Command(gcc, "-s", "-m32", "-nostdlib", "-static", "../contrib/syscall-test/exit32.s", "-o", tmp+"/"+"exit32-test").CombinedOutput()
  38. assert.NilError(c, err, string(out))
  39. }
  40. dockerFile := filepath.Join(tmp, "Dockerfile")
  41. content := []byte(`
  42. FROM debian:bullseye-slim
  43. COPY . /usr/bin/
  44. `)
  45. err = ioutil.WriteFile(dockerFile, content, 0600)
  46. assert.NilError(c, err)
  47. var buildArgs []string
  48. if arg := os.Getenv("DOCKER_BUILD_ARGS"); strings.TrimSpace(arg) != "" {
  49. buildArgs = strings.Split(arg, " ")
  50. }
  51. buildArgs = append(buildArgs, []string{"-q", "-t", "syscall-test", tmp}...)
  52. buildArgs = append([]string{"build"}, buildArgs...)
  53. dockerCmd(c, buildArgs...)
  54. }
  55. func ensureSyscallTestBuild(c *testing.T) {
  56. err := load.FrozenImagesLinux(testEnv.APIClient(), "debian:bullseye-slim")
  57. assert.NilError(c, err)
  58. var buildArgs []string
  59. if arg := os.Getenv("DOCKER_BUILD_ARGS"); strings.TrimSpace(arg) != "" {
  60. buildArgs = strings.Split(arg, " ")
  61. }
  62. buildArgs = append(buildArgs, []string{"-q", "-t", "syscall-test", "../contrib/syscall-test"}...)
  63. buildArgs = append([]string{"build"}, buildArgs...)
  64. dockerCmd(c, buildArgs...)
  65. }
  66. func ensureNNPTest(c *testing.T) {
  67. defer testEnv.ProtectImage(c, "nnp-test:latest")
  68. // If the image already exists, there's nothing left to do.
  69. if testEnv.HasExistingImage(c, "nnp-test:latest") {
  70. return
  71. }
  72. // if no match, must build in docker, which is significantly slower
  73. // (slower mostly because of the vfs graphdriver)
  74. if testEnv.OSType != runtime.GOOS {
  75. ensureNNPTestBuild(c)
  76. return
  77. }
  78. tmp, err := ioutil.TempDir("", "docker-nnp-test")
  79. assert.NilError(c, err)
  80. gcc, err := exec.LookPath("gcc")
  81. assert.NilError(c, err, "could not find gcc")
  82. out, err := exec.Command(gcc, "-g", "-Wall", "-static", "../contrib/nnp-test/nnp-test.c", "-o", filepath.Join(tmp, "nnp-test")).CombinedOutput()
  83. assert.NilError(c, err, string(out))
  84. dockerfile := filepath.Join(tmp, "Dockerfile")
  85. content := `
  86. FROM debian:bullseye-slim
  87. COPY . /usr/bin
  88. RUN chmod +s /usr/bin/nnp-test
  89. `
  90. err = ioutil.WriteFile(dockerfile, []byte(content), 0600)
  91. assert.NilError(c, err, "could not write Dockerfile for nnp-test image")
  92. var buildArgs []string
  93. if arg := os.Getenv("DOCKER_BUILD_ARGS"); strings.TrimSpace(arg) != "" {
  94. buildArgs = strings.Split(arg, " ")
  95. }
  96. buildArgs = append(buildArgs, []string{"-q", "-t", "nnp-test", tmp}...)
  97. buildArgs = append([]string{"build"}, buildArgs...)
  98. dockerCmd(c, buildArgs...)
  99. }
  100. func ensureNNPTestBuild(c *testing.T) {
  101. err := load.FrozenImagesLinux(testEnv.APIClient(), "debian:bullseye-slim")
  102. assert.NilError(c, err)
  103. var buildArgs []string
  104. if arg := os.Getenv("DOCKER_BUILD_ARGS"); strings.TrimSpace(arg) != "" {
  105. buildArgs = strings.Split(arg, " ")
  106. }
  107. buildArgs = append(buildArgs, []string{"-q", "-t", "npp-test", "../contrib/nnp-test"}...)
  108. buildArgs = append([]string{"build"}, buildArgs...)
  109. dockerCmd(c, buildArgs...)
  110. }