Skip to content

Commit

Permalink
printer: dedup warnings
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Jun 25, 2024
1 parent b7ac406 commit cc0a7be
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
67 changes: 67 additions & 0 deletions tests/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
testBakeUlimits,
testBakeMetadataProvenance,
testBakeMetadataWarnings,
testBakeMetadataWarningsDedup,
testBakeMultiExporters,
testBakeLoadPush,
}
Expand Down Expand Up @@ -784,6 +785,72 @@ target "default" {
require.Len(t, md.BuildWarnings, 3)
}

func testBakeMetadataWarningsDedup(t *testing.T, sb integration.Sandbox) {
dockerfile := []byte(`
frOM busybox as base
cOpy Dockerfile .
from scratch
COPy --from=base \
/Dockerfile \
/
`)
bakefile := []byte(`
group "default" {
targets = ["base", "def"]
}
target "base" {
target = "base"
}
target "def" {
}
`)
dir := tmpdir(
t,
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
fstest.CreateFile("Dockerfile", dockerfile, 0600),
)

dirDest := t.TempDir()

outFlag := "default.output=type=docker"
if sb.DockerAddress() == "" {
// there is no Docker atm to load the image
outFlag += ",dest=" + dirDest + "/image.tar"
}

cmd := buildxCmd(
sb,
withDir(dir),
withArgs("bake", "--metadata-file", filepath.Join(dirDest, "md.json"), "--set", outFlag),
withEnv("BUILDX_METADATA_WARNINGS=true"),
)
out, err := cmd.CombinedOutput()
require.NoError(t, err, string(out))

dt, err := os.ReadFile(filepath.Join(dirDest, "md.json"))
require.NoError(t, err)

type mdT struct {
BuildWarnings []client.VertexWarning `json:"buildx.build.warnings"`
Base struct {
BuildRef string `json:"buildx.build.ref"`
} `json:"base"`
Def struct {
BuildRef string `json:"buildx.build.ref"`
} `json:"def"`
}
var md mdT
err = json.Unmarshal(dt, &md)
require.NoError(t, err)

require.NotEmpty(t, md.Base.BuildRef)
require.NotEmpty(t, md.Def.BuildRef)
require.NotEmpty(t, md.BuildWarnings)

skipNoCompatBuildKit(t, sb, ">= 0.14.0-0", "lint")
require.Len(t, md.BuildWarnings, 3)
}

func testBakeMultiExporters(t *testing.T, sb integration.Sandbox) {
if !isDockerContainerWorker(sb) {
t.Skip("only testing with docker-container worker")
Expand Down
37 changes: 36 additions & 1 deletion util/progress/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package progress

import (
"context"
"crypto/sha256"
"encoding/hex"
"os"
"sync"

Expand Down Expand Up @@ -58,7 +60,7 @@ func (p *Printer) Write(s *client.SolveStatus) {
}

func (p *Printer) Warnings() []client.VertexWarning {
return p.warnings
return dedupWarnings(p.warnings)
}

func (p *Printer) ValidateLogSource(dgst digest.Digest, v interface{}) bool {
Expand Down Expand Up @@ -184,3 +186,36 @@ func WithOnClose(onclose func()) PrinterOpt {
opt.onclose = onclose
}
}

func dedupWarnings(inp []client.VertexWarning) []client.VertexWarning {
m := make(map[string]client.VertexWarning)
for _, w := range inp {
h := sha256.New()
h.Write(w.Short)
h.Write([]byte{0})
for _, d := range w.Detail {
h.Write(d)
h.Write([]byte{0})
}
h.Write([]byte(w.URL))
h.Write([]byte{0})
for _, r := range w.Range {
h.Write([]byte(r.String()))
h.Write([]byte{0})
}
if w.SourceInfo != nil {
h.Write([]byte(w.SourceInfo.Filename))
h.Write([]byte(w.SourceInfo.Data))
}
h.Write([]byte{0})
hkey := hex.EncodeToString(h.Sum(nil))
if _, ok := m[hkey]; !ok {
m[hkey] = w
}
}
res := make([]client.VertexWarning, 0, len(m))
for _, w := range m {
res = append(res, w)
}
return res
}

0 comments on commit cc0a7be

Please sign in to comment.