@@ -2,6 +2,7 @@ package build
22
33import (
44 "context"
5+ "sync"
56 "testing"
67
78 "github.com/docker/buildx/util/buildflags"
@@ -11,6 +12,7 @@ import (
1112 "github.com/moby/buildkit/client/ociindex"
1213 "github.com/opencontainers/go-digest"
1314 ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
15+ "github.com/pkg/errors"
1416 "github.com/stretchr/testify/assert"
1517 "github.com/stretchr/testify/require"
1618)
@@ -237,6 +239,30 @@ func TestLoadInputsOCILayoutNamedContext(t *testing.T) {
237239 }
238240}
239241
242+ func TestPolicyProgressLoggerCloseWithErrorAfterCompletedWindow (t * testing.T ) {
243+ pw := & captureProgressWriter {}
244+ logger := newPolicyProgressLogger (pw , "loading policies policy.rego" )
245+
246+ logger .Log ("policy decision: DENY" )
247+ logger .completeWindow (1 , nil )
248+ logger .Close (errors .New ("source not allowed by policy" ))
249+
250+ var completedErrors []string
251+ for _ , st := range pw .Statuses () {
252+ for _ , v := range st .Vertexes {
253+ if v == nil || v .Completed == nil {
254+ continue
255+ }
256+ if v .Error == "" {
257+ continue
258+ }
259+ completedErrors = append (completedErrors , v .Error )
260+ }
261+ }
262+
263+ require .Equal (t , []string {"source not allowed by policy" }, completedErrors )
264+ }
265+
240266type testProgressWriter struct {}
241267
242268func (testProgressWriter ) Write (* client.SolveStatus ) {}
@@ -248,3 +274,28 @@ func (testProgressWriter) ValidateLogSource(digest.Digest, any) bool { return tr
248274func (testProgressWriter ) ClearLogSource (any ) {}
249275
250276var _ progress.Writer = testProgressWriter {}
277+
278+ type captureProgressWriter struct {
279+ mu sync.Mutex
280+ statuses []* client.SolveStatus
281+ }
282+
283+ func (w * captureProgressWriter ) Write (st * client.SolveStatus ) {
284+ w .mu .Lock ()
285+ defer w .mu .Unlock ()
286+ w .statuses = append (w .statuses , st )
287+ }
288+
289+ func (w * captureProgressWriter ) Statuses () []* client.SolveStatus {
290+ w .mu .Lock ()
291+ defer w .mu .Unlock ()
292+ return append ([]* client.SolveStatus (nil ), w .statuses ... )
293+ }
294+
295+ func (w * captureProgressWriter ) WriteBuildRef (string , string ) {}
296+
297+ func (w * captureProgressWriter ) ValidateLogSource (digest.Digest , any ) bool { return true }
298+
299+ func (w * captureProgressWriter ) ClearLogSource (any ) {}
300+
301+ var _ progress.Writer = (* captureProgressWriter )(nil )
0 commit comments