Skip to content

Commit f8ae80f

Browse files
committed
tests: cover debug build missing mount result IDs
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 0a0ef95 commit f8ae80f

2 files changed

Lines changed: 132 additions & 0 deletions

File tree

tests/debug.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package tests
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"os/exec"
7+
"strings"
8+
"sync"
9+
"testing"
10+
"time"
11+
12+
"github.com/containerd/continuity/fs/fstest"
13+
"github.com/creack/pty"
14+
"github.com/moby/buildkit/util/testutil/integration"
15+
"github.com/stretchr/testify/require"
16+
)
17+
18+
var debugTests = []func(t *testing.T, sb integration.Sandbox){
19+
testDebugBuildMissingBindMountSource,
20+
}
21+
22+
func testDebugBuildMissingBindMountSource(t *testing.T, sb integration.Sandbox) {
23+
if !isExperimental() {
24+
t.Skip("debug command is experimental")
25+
}
26+
if !isDockerWorker(sb) {
27+
t.Skip("debug monitor test only needs a docker worker")
28+
}
29+
30+
dockerfile := []byte(`
31+
FROM busybox:latest
32+
RUN --mount=type=bind,source=missing,target=/src true
33+
`)
34+
dir := tmpdir(t,
35+
fstest.CreateFile("Dockerfile", dockerfile, 0o600),
36+
)
37+
38+
cmd := buildxCmd(sb, withArgs("debug", "--on=error", "build", "--progress=plain", "--output=type=cacheonly", dir))
39+
f, err := pty.StartWithSize(cmd, &pty.Winsize{
40+
Cols: 120,
41+
Rows: 24,
42+
})
43+
require.NoError(t, err)
44+
defer f.Close()
45+
46+
var output debugOutput
47+
copyDone := make(chan struct{})
48+
go func() {
49+
_, _ = io.Copy(&output, f)
50+
close(copyDone)
51+
}()
52+
53+
waitCh := make(chan error, 1)
54+
go func() {
55+
waitCh <- cmd.Wait()
56+
}()
57+
58+
waitErr, exited := waitForDebugPromptOrExit(t, &output, waitCh)
59+
if !exited {
60+
_, err = f.Write([]byte("exit\r"))
61+
require.NoError(t, err)
62+
waitErr = waitForDebugCommandExit(t, cmd, waitCh, &output)
63+
}
64+
65+
_ = f.Close()
66+
select {
67+
case <-copyDone:
68+
case <-time.After(time.Second):
69+
}
70+
71+
out := output.String()
72+
require.Error(t, waitErr, out)
73+
require.Contains(t, out, "failed to calculate checksum")
74+
require.Contains(t, out, "failed to create container")
75+
require.NotContains(t, out, "panic:")
76+
require.NotContains(t, out, "index out of range")
77+
}
78+
79+
func waitForDebugPromptOrExit(t *testing.T, output *debugOutput, waitCh <-chan error) (error, bool) {
80+
t.Helper()
81+
82+
timeout := time.NewTimer(time.Minute)
83+
defer timeout.Stop()
84+
ticker := time.NewTicker(100 * time.Millisecond)
85+
defer ticker.Stop()
86+
87+
for {
88+
select {
89+
case err := <-waitCh:
90+
return err, true
91+
case <-ticker.C:
92+
if strings.Contains(output.String(), "(buildx) ") {
93+
return nil, false
94+
}
95+
case <-timeout.C:
96+
require.FailNow(t, "timeout waiting for debug monitor", output.String())
97+
}
98+
}
99+
}
100+
101+
func waitForDebugCommandExit(t *testing.T, cmd *exec.Cmd, waitCh <-chan error, output *debugOutput) error {
102+
t.Helper()
103+
104+
select {
105+
case err := <-waitCh:
106+
return err
107+
case <-time.After(30 * time.Second):
108+
if cmd.Process != nil {
109+
_ = cmd.Process.Kill()
110+
}
111+
require.FailNow(t, "timeout waiting for debug command to exit", output.String())
112+
return nil
113+
}
114+
}
115+
116+
type debugOutput struct {
117+
mu sync.Mutex
118+
buf bytes.Buffer
119+
}
120+
121+
func (b *debugOutput) Write(p []byte) (int, error) {
122+
b.mu.Lock()
123+
defer b.mu.Unlock()
124+
return b.buf.Write(p)
125+
}
126+
127+
func (b *debugOutput) String() string {
128+
b.mu.Lock()
129+
defer b.mu.Unlock()
130+
return b.buf.String()
131+
}

tests/integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func TestIntegration(t *testing.T) {
2424
var tests []func(t *testing.T, sb integration.Sandbox)
2525
tests = append(tests, commonTests...)
2626
tests = append(tests, buildTests...)
27+
tests = append(tests, debugTests...)
2728
tests = append(tests, policyBuildTests...)
2829
tests = append(tests, policyEvalTests...)
2930
tests = append(tests, policyBakeTests...)

0 commit comments

Comments
 (0)