fixtures_linux_daemon_test.go 4.0 KB

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