fixtures_linux_daemon_test.go 4.2 KB

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