Skip to content

Commit 06cefd0

Browse files
committed
tests: Fix some issues with build output
Before this change `RunTest` may return before all messages are processed. This also moves a lot of the logic around to better separate responsibilities and provide a more flexible interface that is, hopefully, easier to reason about. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
1 parent 621a944 commit 06cefd0

File tree

5 files changed

+187
-97
lines changed

5 files changed

+187
-97
lines changed

test/gomod_git_auth_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import (
1414
"strconv"
1515
"testing"
1616

17+
"github.com/moby/buildkit/client/llb"
18+
gwclient "github.com/moby/buildkit/frontend/gateway/client"
19+
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
1720
"github.com/project-dalec/dalec"
1821
"github.com/project-dalec/dalec/test/cmd/git_repo/passwd"
1922
gitservices "github.com/project-dalec/dalec/test/git_services"
2023
"github.com/project-dalec/dalec/test/testenv"
21-
"github.com/moby/buildkit/client/llb"
22-
gwclient "github.com/moby/buildkit/frontend/gateway/client"
23-
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
2424
"golang.org/x/crypto/ssh"
2525
"golang.org/x/crypto/ssh/agent"
2626
)
@@ -109,7 +109,8 @@ go {{ .ModFileGoVersion }}
109109

110110
t.Run("HTTP", func(t *testing.T) {
111111
t.Parallel()
112-
netHostBuildxEnv.RunTestOptsFirst(ctx, t, []testenv.TestRunnerOpt{
112+
113+
opts := []testenv.TestRunnerOpt{
113114
// This gives buildkit access to a secret with the name
114115
// `secretName` and the value `passwd.Password`. On the worker that
115116
// fetches the gomod dependencies, a file will be mounted at the
@@ -120,7 +121,9 @@ go {{ .ModFileGoVersion }}
120121
// requests. It is necessary but not sufficient for the buildx
121122
// instance to have host networking enabled.
122123
testenv.WithHostNetworking,
123-
}, func(ctx context.Context, client gwclient.Client) {
124+
}
125+
126+
netHostBuildxEnv.RunTest(ctx, t, func(ctx context.Context, client gwclient.Client) {
124127
// This MUST be called at the start of each test. Because we
125128
// persist the go mod cache between runs, the git tag of the
126129
// private go module needs to be unique. Within a single run of the
@@ -167,7 +170,7 @@ go {{ .ModFileGoVersion }}
167170

168171
filename := calculateFilename(ctx, t, attr, res)
169172
checkFile(ctx, t, filename, res, []byte("bar\n"))
170-
})
173+
}, opts...)
171174
})
172175

173176
t.Run("SSH", func(t *testing.T) {
@@ -182,12 +185,13 @@ go {{ .ModFileGoVersion }}
182185
pubkey, privkey := generateKeyPair(t)
183186
agentErrChan := startSSHAgent(t, privkey, sockaddr)
184187

185-
netHostBuildxEnv.RunTestOptsFirst(ctx, t, []testenv.TestRunnerOpt{
188+
opts := []testenv.TestRunnerOpt{
186189
// This tells buildkit to forward the SSH Agent socket, giving the
187190
// gomod generator worker access to the private key
188191
testenv.WithSSHSocket(sshID, sockaddr),
189192
testenv.WithHostNetworking,
190-
}, func(ctx context.Context, client gwclient.Client) {
193+
}
194+
netHostBuildxEnv.RunTest(ctx, t, func(ctx context.Context, client gwclient.Client) {
191195
// This MUST be called at the start of each test. Because we
192196
// persist the go mod cache between runs, the git tag of the
193197
// private go module needs to be unique. Within a single run of the
@@ -245,7 +249,7 @@ go {{ .ModFileGoVersion }}
245249

246250
filename := calculateFilename(ctx, t, attr, res)
247251
checkFile(ctx, t, filename, res, []byte("bar\n"))
248-
})
252+
}, opts...)
249253
})
250254
}
251255

test/linux_target_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3224,7 +3224,7 @@ func testLinuxPackageTestsFail(ctx context.Context, t *testing.T, cfg testLinuxC
32243224

32253225
if !bytes.Contains(dt, []byte(tc.err.Error())) {
32263226
t.Errorf("expected error not found in logs")
3227-
t.Logf("Expected error:\n\t%v", err)
3227+
t.Logf("Expected error:\n\t%v", tc.err)
32283228
t.Log()
32293229
t.Log("---- Solve logs ----\n" + string(dt))
32303230
}

test/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
"testing"
1111
"time"
1212

13-
"github.com/project-dalec/dalec/test/fixtures"
14-
"github.com/project-dalec/dalec/test/testenv"
1513
"github.com/moby/buildkit/util/tracing/delegated"
1614
"github.com/moby/buildkit/util/tracing/detect"
15+
"github.com/project-dalec/dalec/test/fixtures"
16+
"github.com/project-dalec/dalec/test/testenv"
1717
"go.opentelemetry.io/otel"
1818
"go.opentelemetry.io/otel/propagation"
1919
"go.opentelemetry.io/otel/sdk/trace"

test/testenv/build.go

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
gwclient "github.com/moby/buildkit/frontend/gateway/client"
1919
"github.com/moby/buildkit/identity"
2020
"github.com/moby/buildkit/solver/pb"
21-
"github.com/moby/buildkit/util/progress/progressui"
2221
"github.com/pkg/errors"
2322
"go.opentelemetry.io/otel"
2423
"go.opentelemetry.io/otel/codes"
@@ -176,6 +175,40 @@ var logBufferPool = &sync.Pool{New: func() any {
176175
return bytes.NewBuffer(nil)
177176
}}
178177

178+
func outputStreamStatusFn(ctx context.Context, t *testing.T) (func(*client.SolveStatus), func()) {
179+
var (
180+
warnings []*client.VertexWarning
181+
errOnce sync.Once
182+
183+
done = make(chan struct{})
184+
w = getBuildOutputStream(ctx, t, done)
185+
)
186+
187+
fn := func(msg *client.SolveStatus) {
188+
for _, v := range msg.Warnings {
189+
warnings = append(warnings, v)
190+
}
191+
for _, l := range msg.Logs {
192+
if _, err := w.Write(l.Data); err != nil {
193+
errOnce.Do(func() {
194+
// Don't spam the logs with multiple errors
195+
t.Logf("error writing log data: %v", err)
196+
})
197+
}
198+
}
199+
}
200+
201+
return fn, func() {
202+
for _, v := range warnings {
203+
t.Logf("WARNING: %s", string(v.Short))
204+
}
205+
if closer, ok := w.(io.Closer); ok {
206+
closer.Close()
207+
}
208+
close(done)
209+
}
210+
}
211+
179212
func getBuildOutputStream(ctx context.Context, t *testing.T, done <-chan struct{}) io.Writer {
180213
t.Helper()
181214

@@ -226,47 +259,41 @@ func getBuildOutputStream(ctx context.Context, t *testing.T, done <-chan struct{
226259
return f
227260
}
228261

229-
func displaySolveStatus(ctx context.Context, t *testing.T) chan *client.SolveStatus {
230-
ch := make(chan *client.SolveStatus)
231-
done := make(chan struct{})
232-
233-
output := getBuildOutputStream(ctx, t, done)
234-
display, err := progressui.NewDisplay(output, progressui.AutoMode, progressui.WithPhase(t.Name()))
235-
if err != nil {
236-
t.Fatal(err)
237-
}
238-
239-
go func() {
240-
defer close(done)
241-
242-
_, err := display.UpdateFrom(ctx, ch)
243-
if err != nil {
244-
t.Log(err)
262+
func fowardToSolveStatusFn(ctx context.Context, ch <-chan *client.SolveStatus, f func(*client.SolveStatus)) {
263+
for {
264+
select {
265+
case <-ctx.Done():
266+
return
267+
case msg, ok := <-ch:
268+
if msg != nil {
269+
f(msg)
270+
continue
271+
}
272+
if !ok {
273+
return
274+
}
245275
}
246-
}()
247-
248-
return ch
276+
}
249277
}
250278

251279
// withProjectRoot adds the current project root as the build context for the solve request.
252-
func withProjectRoot(t *testing.T, opts *client.SolveOpt) {
253-
t.Helper()
254-
280+
func withProjectRoot(opts *client.SolveOpt) error {
255281
cwd, err := os.Getwd()
256282
if err != nil {
257-
t.Fatal(err)
283+
return err
258284
}
259285

260286
projectRoot, err := lookupProjectRoot(cwd)
261287
if err != nil {
262-
t.Fatal(err)
288+
return err
263289
}
264290

265291
if opts.LocalDirs == nil {
266292
opts.LocalDirs = make(map[string]string)
267293
}
268294
opts.LocalDirs[dockerui.DefaultLocalNameContext] = projectRoot
269295
opts.LocalDirs[dockerui.DefaultLocalNameDockerfile] = projectRoot
296+
return nil
270297
}
271298

272299
// lookupProjectRoot looks up the project root from the current working directory.

0 commit comments

Comments
 (0)