integration/internal: Print Buildkit logs

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
(cherry picked from commit ce338dec81)
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2025-09-01 18:23:46 +02:00
parent 687b206c6b
commit d70382e442

View File

@@ -1,16 +1,19 @@
package build package build
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"io" "io"
"testing" "testing"
"github.com/containerd/containerd/v2/pkg/protobuf/proto"
"github.com/docker/docker/api/types/build" "github.com/docker/docker/api/types/build"
"github.com/docker/docker/api/types/image" "github.com/docker/docker/api/types/image"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/testutil/fakecontext" "github.com/docker/docker/testutil/fakecontext"
controlapi "github.com/moby/buildkit/api/services/control"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
) )
@@ -31,6 +34,7 @@ func Do(ctx context.Context, t *testing.T, client client.APIClient, buildCtx *fa
// GetImageIDFromBody reads the image ID from the build response body. // GetImageIDFromBody reads the image ID from the build response body.
func GetImageIDFromBody(t *testing.T, body io.Reader) string { func GetImageIDFromBody(t *testing.T, body io.Reader) string {
var id string var id string
buf := bytes.NewBuffer(nil)
dec := json.NewDecoder(body) dec := json.NewDecoder(body)
for { for {
var jm jsonmessage.JSONMessage var jm jsonmessage.JSONMessage
@@ -39,6 +43,19 @@ func GetImageIDFromBody(t *testing.T, body io.Reader) string {
break break
} }
assert.NilError(t, err) assert.NilError(t, err)
if handled := processBuildkitAux(t, &jm, &id); handled {
continue
}
buf.Reset()
jm.Display(buf, false)
if buf.Len() == 0 {
continue
}
t.Log(buf.String())
if jm.Aux == nil { if jm.Aux == nil {
continue continue
} }
@@ -49,11 +66,49 @@ func GetImageIDFromBody(t *testing.T, body io.Reader) string {
continue continue
} }
id = br.ID id = br.ID
break continue
} }
t.Log("Raw Aux", string(*jm.Aux))
} }
_, _ = io.Copy(io.Discard, body) _, _ = io.Copy(io.Discard, body)
assert.Assert(t, id != "", "could not read image ID from build output") assert.Assert(t, id != "", "could not read image ID from build output")
return id return id
} }
func processBuildkitAux(t *testing.T, jm *jsonmessage.JSONMessage, id *string) bool {
if jm.ID == "moby.buildkit.trace" {
var dt []byte
if err := json.Unmarshal(*jm.Aux, &dt); err != nil {
t.Log("Error unmarshalling buildkit trace", err)
return true
}
var sr controlapi.StatusResponse
if err := proto.Unmarshal(dt, &sr); err != nil {
t.Log("Error unmarshalling buildkit trace proto", err)
return true
}
for _, vtx := range sr.GetVertexes() {
t.Log(vtx.String())
}
for _, vtx := range sr.GetStatuses() {
t.Log(vtx.String())
}
for _, vtx := range sr.GetLogs() {
t.Log(vtx.String())
}
for _, vtx := range sr.GetWarnings() {
t.Log(vtx.String())
}
return true
}
if jm.ID == "moby.image.id" {
var br build.Result
if err := json.Unmarshal(*jm.Aux, &br); err == nil {
*id = br.ID
return true
}
}
return false
}