fixtures.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package fakestorage // import "github.com/docker/docker/testutil/fakestorage"
  2. import (
  3. "context"
  4. "io"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "sync"
  9. "testing"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/pkg/archive"
  12. "gotest.tools/v3/assert"
  13. )
  14. var ensureHTTPServerOnce sync.Once
  15. func ensureHTTPServerImage(t testing.TB) {
  16. t.Helper()
  17. var doIt bool
  18. ensureHTTPServerOnce.Do(func() {
  19. doIt = true
  20. })
  21. if !doIt {
  22. return
  23. }
  24. defer testEnv.ProtectImage(t, "httpserver:latest")
  25. tmp, err := os.MkdirTemp("", "docker-http-server-test")
  26. if err != nil {
  27. t.Fatalf("could not build http server: %v", err)
  28. }
  29. defer os.RemoveAll(tmp)
  30. goos := testEnv.DaemonInfo.OSType
  31. if goos == "" {
  32. goos = "linux"
  33. }
  34. goarch := testEnv.DaemonVersion.Arch
  35. if goarch == "" {
  36. goarch = "amd64"
  37. }
  38. cpCmd, lookErr := exec.LookPath("cp")
  39. if lookErr != nil {
  40. t.Fatalf("could not build http server: %v", lookErr)
  41. }
  42. if _, err = os.Stat("../contrib/httpserver/httpserver"); os.IsNotExist(err) {
  43. goCmd, lookErr := exec.LookPath("go")
  44. if lookErr != nil {
  45. t.Fatalf("could not build http server: %v", lookErr)
  46. }
  47. cmd := exec.Command(goCmd, "build", "-o", filepath.Join(tmp, "httpserver"), "github.com/docker/docker/contrib/httpserver")
  48. cmd.Env = append(os.Environ(), []string{
  49. "CGO_ENABLED=0",
  50. "GOOS=" + goos,
  51. "GOARCH=" + goarch,
  52. }...)
  53. var out []byte
  54. if out, err = cmd.CombinedOutput(); err != nil {
  55. t.Fatalf("could not build http server: %s", string(out))
  56. }
  57. } else {
  58. if out, err := exec.Command(cpCmd, "../contrib/httpserver/httpserver", filepath.Join(tmp, "httpserver")).CombinedOutput(); err != nil {
  59. t.Fatalf("could not copy http server: %v", string(out))
  60. }
  61. }
  62. if out, err := exec.Command(cpCmd, "../contrib/httpserver/Dockerfile", filepath.Join(tmp, "Dockerfile")).CombinedOutput(); err != nil {
  63. t.Fatalf("could not build http server: %v", string(out))
  64. }
  65. c := testEnv.APIClient()
  66. reader, err := archive.TarWithOptions(tmp, &archive.TarOptions{})
  67. assert.NilError(t, err)
  68. resp, err := c.ImageBuild(context.Background(), reader, types.ImageBuildOptions{
  69. Remove: true,
  70. ForceRemove: true,
  71. Tags: []string{"httpserver"},
  72. })
  73. assert.NilError(t, err)
  74. _, err = io.Copy(io.Discard, resp.Body)
  75. assert.NilError(t, err)
  76. }