build_traces_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package build
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/client/buildkit"
  8. "github.com/docker/docker/testutil"
  9. moby_buildkit_v1 "github.com/moby/buildkit/api/services/control"
  10. "github.com/moby/buildkit/client"
  11. "github.com/moby/buildkit/client/llb"
  12. "github.com/moby/buildkit/util/progress/progressui"
  13. "go.opentelemetry.io/otel"
  14. "golang.org/x/sync/errgroup"
  15. "gotest.tools/v3/assert"
  16. "gotest.tools/v3/poll"
  17. "gotest.tools/v3/skip"
  18. )
  19. type testWriter struct {
  20. *testing.T
  21. }
  22. func (t *testWriter) Write(p []byte) (int, error) {
  23. t.Log(string(p))
  24. return len(p), nil
  25. }
  26. func TestBuildkitHistoryTracePropagation(t *testing.T) {
  27. skip.If(t, testEnv.DaemonInfo.OSType == "windows", "buildkit is not supported on Windows")
  28. ctx := testutil.StartSpan(baseContext, t)
  29. opts := buildkit.ClientOpts(testEnv.APIClient())
  30. bc, err := client.New(ctx, "", opts...)
  31. assert.NilError(t, err)
  32. defer bc.Close()
  33. def, err := llb.Scratch().Marshal(ctx)
  34. assert.NilError(t, err)
  35. eg, ctxGo := errgroup.WithContext(ctx)
  36. ch := make(chan *client.SolveStatus)
  37. ctxHistory, cancel := context.WithCancel(ctx)
  38. defer cancel()
  39. sub, err := bc.ControlClient().ListenBuildHistory(ctxHistory, &moby_buildkit_v1.BuildHistoryRequest{ActiveOnly: true})
  40. assert.NilError(t, err)
  41. sub.CloseSend()
  42. defer func() {
  43. cancel()
  44. <-sub.Context().Done()
  45. }()
  46. eg.Go(func() error {
  47. _, err := progressui.DisplaySolveStatus(ctxGo, nil, &testWriter{t}, ch, progressui.WithPhase("test"))
  48. return err
  49. })
  50. eg.Go(func() error {
  51. _, err := bc.Solve(ctxGo, def, client.SolveOpt{}, ch)
  52. return err
  53. })
  54. assert.NilError(t, eg.Wait())
  55. he, err := sub.Recv()
  56. assert.NilError(t, err)
  57. assert.Assert(t, he != nil)
  58. cancel()
  59. // Traces for history records are recorded asynchronously, so we need to wait for it to be available.
  60. if he.Record.Trace != nil {
  61. return
  62. }
  63. // Split this into a new span so it doesn't clutter up the trace reporting GUI.
  64. ctx, span := otel.Tracer("").Start(ctx, "Wait for trace to propagate to history record")
  65. defer span.End()
  66. t.Log("Waiting for trace to be available")
  67. poll.WaitOn(t, func(logger poll.LogT) poll.Result {
  68. ctx, cancel := context.WithCancel(ctx)
  69. defer cancel()
  70. sub, err := bc.ControlClient().ListenBuildHistory(ctx, &moby_buildkit_v1.BuildHistoryRequest{Ref: he.Record.Ref})
  71. if err != nil {
  72. return poll.Error(err)
  73. }
  74. sub.CloseSend()
  75. defer func() {
  76. cancel()
  77. <-sub.Context().Done()
  78. }()
  79. msg, err := sub.Recv()
  80. if err != nil {
  81. return poll.Error(err)
  82. }
  83. if msg.Record.Ref != he.Record.Ref {
  84. return poll.Error(fmt.Errorf("got incorrect history record"))
  85. }
  86. if msg.Record.Trace != nil {
  87. return poll.Success()
  88. }
  89. return poll.Continue("trace not available yet")
  90. }, poll.WithDelay(time.Second), poll.WithTimeout(30*time.Second))
  91. }