Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ func (l *policyProgressLogger) Close(err error) {
shouldComplete = true
started = l.started
l.open = false
} else if err != nil && !l.started.IsZero() {
shouldComplete = true
started = l.started
}
l.window++
if l.timer != nil {
Expand Down
51 changes: 51 additions & 0 deletions build/opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package build

import (
"context"
"sync"
"testing"

"github.com/docker/buildx/util/buildflags"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/moby/buildkit/client/ociindex"
"github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -237,6 +239,30 @@ func TestLoadInputsOCILayoutNamedContext(t *testing.T) {
}
}

func TestPolicyProgressLoggerCloseWithErrorAfterCompletedWindow(t *testing.T) {
pw := &captureProgressWriter{}
logger := newPolicyProgressLogger(pw, "loading policies policy.rego")

logger.Log("policy decision: DENY")
logger.completeWindow(1, nil)
logger.Close(errors.New("source not allowed by policy"))

var completedErrors []string
for _, st := range pw.Statuses() {
for _, v := range st.Vertexes {
if v == nil || v.Completed == nil {
continue
}
if v.Error == "" {
continue
}
completedErrors = append(completedErrors, v.Error)
}
}

require.Equal(t, []string{"source not allowed by policy"}, completedErrors)
}

type testProgressWriter struct{}

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

var _ progress.Writer = testProgressWriter{}

type captureProgressWriter struct {
mu sync.Mutex
statuses []*client.SolveStatus
}

func (w *captureProgressWriter) Write(st *client.SolveStatus) {
w.mu.Lock()
defer w.mu.Unlock()
w.statuses = append(w.statuses, st)
}

func (w *captureProgressWriter) Statuses() []*client.SolveStatus {
w.mu.Lock()
defer w.mu.Unlock()
return append([]*client.SolveStatus(nil), w.statuses...)
}

func (w *captureProgressWriter) WriteBuildRef(string, string) {}

func (w *captureProgressWriter) ValidateLogSource(digest.Digest, any) bool { return true }

func (w *captureProgressWriter) ClearLogSource(any) {}

var _ progress.Writer = (*captureProgressWriter)(nil)
Loading