Skip to content

Commit c0eb5f0

Browse files
authored
Merge pull request #3838 from tonistiigi/policy-progress-test-fix
policy: preserve late progress errors
2 parents 46f0853 + 0c04ba8 commit c0eb5f0

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

build/opt.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ func (l *policyProgressLogger) Close(err error) {
188188
shouldComplete = true
189189
started = l.started
190190
l.open = false
191+
} else if err != nil && !l.started.IsZero() {
192+
shouldComplete = true
193+
started = l.started
191194
}
192195
l.window++
193196
if l.timer != nil {

build/opt_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package build
22

33
import (
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+
240266
type testProgressWriter struct{}
241267

242268
func (testProgressWriter) Write(*client.SolveStatus) {}
@@ -248,3 +274,28 @@ func (testProgressWriter) ValidateLogSource(digest.Digest, any) bool { return tr
248274
func (testProgressWriter) ClearLogSource(any) {}
249275

250276
var _ 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

Comments
 (0)