executor.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package main
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "os"
  8. "strings"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/api/types/container"
  11. "github.com/docker/docker/api/types/mount"
  12. "github.com/docker/docker/client"
  13. "github.com/docker/docker/pkg/stdcopy"
  14. )
  15. // testChunkExecutor executes integration-cli binary.
  16. // image needs to be the worker image itself. testFlags are OR-set of regexp for filtering tests.
  17. type testChunkExecutor func(image string, tests []string) (int64, string, error)
  18. func dryTestChunkExecutor(image string, tests []string) (int64, string, error) {
  19. return 0, fmt.Sprintf("DRY RUN (image=%q, tests=%v)", image, tests), nil
  20. }
  21. // privilegedTestChunkExecutor invokes a privileged container from the worker
  22. // service via bind-mounted API socket so as to execute the test chunk
  23. func privilegedTestChunkExecutor(image string, tests []string) (int64, string, error) {
  24. cli, err := client.NewEnvClient()
  25. if err != nil {
  26. return 0, "", err
  27. }
  28. // propagate variables from the host (needs to be defined in the compose file)
  29. experimental := os.Getenv("DOCKER_EXPERIMENTAL")
  30. graphdriver := os.Getenv("DOCKER_GRAPHDRIVER")
  31. if graphdriver == "" {
  32. info, err := cli.Info(context.Background())
  33. if err != nil {
  34. return 0, "", err
  35. }
  36. graphdriver = info.Driver
  37. }
  38. // `daemon_dest` is similar to `$DEST` (e.g. `bundles/VERSION/test-integration-cli`)
  39. // but it exists outside of `bundles` so as to make `$DOCKER_GRAPHDRIVER` work.
  40. //
  41. // Without this hack, `$DOCKER_GRAPHDRIVER` fails because of (e.g.) `overlay2 is not supported over overlayfs`
  42. //
  43. // see integration-cli/daemon/daemon.go
  44. daemonDest := "/daemon_dest"
  45. config := container.Config{
  46. Image: image,
  47. Env: []string{
  48. "TESTFLAGS=-check.f " + strings.Join(tests, "|"),
  49. "KEEPBUNDLE=1",
  50. "DOCKER_INTEGRATION_TESTS_VERIFIED=1", // for avoiding rebuilding integration-cli
  51. "DOCKER_EXPERIMENTAL=" + experimental,
  52. "DOCKER_GRAPHDRIVER=" + graphdriver,
  53. "DOCKER_INTEGRATION_DAEMON_DEST=" + daemonDest,
  54. },
  55. // TODO: set label?
  56. Entrypoint: []string{"hack/dind"},
  57. Cmd: []string{"hack/make.sh", "test-integration-cli"},
  58. }
  59. hostConfig := container.HostConfig{
  60. AutoRemove: true,
  61. Privileged: true,
  62. Mounts: []mount.Mount{
  63. {
  64. Type: mount.TypeVolume,
  65. Target: daemonDest,
  66. },
  67. },
  68. }
  69. id, stream, err := runContainer(context.Background(), cli, config, hostConfig)
  70. if err != nil {
  71. return 0, "", err
  72. }
  73. var b bytes.Buffer
  74. teeContainerStream(&b, os.Stdout, os.Stderr, stream)
  75. rc, err := cli.ContainerWait(context.Background(), id)
  76. if err != nil {
  77. return 0, "", err
  78. }
  79. return rc, b.String(), nil
  80. }
  81. func runContainer(ctx context.Context, cli *client.Client, config container.Config, hostConfig container.HostConfig) (string, io.ReadCloser, error) {
  82. created, err := cli.ContainerCreate(context.Background(),
  83. &config, &hostConfig, nil, "")
  84. if err != nil {
  85. return "", nil, err
  86. }
  87. if err = cli.ContainerStart(ctx, created.ID, types.ContainerStartOptions{}); err != nil {
  88. return "", nil, err
  89. }
  90. stream, err := cli.ContainerLogs(ctx,
  91. created.ID,
  92. types.ContainerLogsOptions{
  93. ShowStdout: true,
  94. ShowStderr: true,
  95. Follow: true,
  96. })
  97. return created.ID, stream, err
  98. }
  99. func teeContainerStream(w, stdout, stderr io.Writer, stream io.ReadCloser) {
  100. stdcopy.StdCopy(io.MultiWriter(w, stdout), io.MultiWriter(w, stderr), stream)
  101. stream.Close()
  102. }