fixtures_linux_daemon_test.go 4.2 KB

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