Skip to content

Commit 7c0d261

Browse files
authored
Merge pull request #4583 from leandrosansilva/refactoring/do_not_use_deprecated_local_dirs_in_tests
Refactoring: Remove usage of deprecated LocalDirs field in SolveOpts
2 parents db3aaa3 + 704268a commit 7c0d261

22 files changed

Lines changed: 351 additions & 249 deletions

client/build_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
digest "github.com/opencontainers/go-digest"
3232
"github.com/pkg/errors"
3333
"github.com/stretchr/testify/require"
34+
"github.com/tonistiigi/fsutil"
3435
"golang.org/x/crypto/ssh/agent"
3536
"google.golang.org/grpc/codes"
3637
)
@@ -691,9 +692,9 @@ func testClientGatewayContainerMounts(t *testing.T, sb integration.Sandbox) {
691692
require.NoError(t, err)
692693
defer c.Close()
693694

694-
tmpdir := t.TempDir()
695+
tmpdir := integration.Tmpdir(t)
695696

696-
err = os.WriteFile(filepath.Join(tmpdir, "local-file"), []byte("local"), 0644)
697+
err = os.WriteFile(filepath.Join(tmpdir.Name, "local-file"), []byte("local"), 0644)
697698
require.NoError(t, err)
698699

699700
a := agent.NewKeyring()
@@ -834,7 +835,7 @@ func testClientGatewayContainerMounts(t *testing.T, sb integration.Sandbox) {
834835
}
835836

836837
_, err = c.Build(ctx, SolveOpt{
837-
LocalDirs: map[string]string{
838+
LocalMounts: map[string]fsutil.FS{
838839
"mylocal": tmpdir,
839840
},
840841
Session: []session.Attachable{

client/client_test.go

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import (
6969
"github.com/pkg/errors"
7070
"github.com/spdx/tools-golang/spdx"
7171
"github.com/stretchr/testify/require"
72+
"github.com/tonistiigi/fsutil"
7273
"golang.org/x/crypto/ssh/agent"
7374
"golang.org/x/sync/errgroup"
7475
"google.golang.org/grpc"
@@ -210,6 +211,7 @@ var allTests = []func(t *testing.T, sb integration.Sandbox){
210211
testSnapshotWithMultipleBlobs,
211212
testExportLocalNoPlatformSplit,
212213
testExportLocalNoPlatformSplitOverwrite,
214+
testSolverOptLocalDirsStillWorks,
213215
}
214216

215217
func TestIntegration(t *testing.T) {
@@ -272,9 +274,9 @@ func testCacheExportCacheKeyLoop(t *testing.T, sb integration.Sandbox) {
272274
require.NoError(t, err)
273275
defer c.Close()
274276

275-
tmpdir := t.TempDir()
277+
tmpdir := integration.Tmpdir(t)
276278

277-
err = os.WriteFile(filepath.Join(tmpdir, "foo"), []byte("foodata"), 0600)
279+
err = os.WriteFile(filepath.Join(tmpdir.Name, "foo"), []byte("foodata"), 0600)
278280
require.NoError(t, err)
279281

280282
for _, mode := range []bool{false, true} {
@@ -295,11 +297,11 @@ func testCacheExportCacheKeyLoop(t *testing.T, sb integration.Sandbox) {
295297
{
296298
Type: "local",
297299
Attrs: map[string]string{
298-
"dest": filepath.Join(tmpdir, "cache"),
300+
"dest": filepath.Join(tmpdir.Name, "cache"),
299301
},
300302
},
301303
},
302-
LocalDirs: map[string]string{
304+
LocalMounts: map[string]fsutil.FS{
303305
"mylocal": tmpdir,
304306
},
305307
}, nil)
@@ -1515,7 +1517,7 @@ func testLocalSymlinkEscape(t *testing.T, sb integration.Sandbox) {
15151517
require.NoError(t, err)
15161518

15171519
_, err = c.Solve(sb.Context(), def, SolveOpt{
1518-
LocalDirs: map[string]string{
1520+
LocalMounts: map[string]fsutil.FS{
15191521
"mylocal": dir,
15201522
},
15211523
}, nil)
@@ -1554,6 +1556,49 @@ func testRelativeWorkDir(t *testing.T, sb integration.Sandbox) {
15541556
require.Equal(t, []byte("/test1/test2\n"), dt)
15551557
}
15561558

1559+
// TODO: remove this test once `client.SolveOpt.LocalDirs`, now marked as deprecated, is removed.
1560+
// For more context on this test, please check:
1561+
// https://github.com/moby/buildkit/pull/4583#pullrequestreview-1847043452
1562+
func testSolverOptLocalDirsStillWorks(t *testing.T, sb integration.Sandbox) {
1563+
integration.SkipOnPlatform(t, "windows")
1564+
1565+
c, err := New(sb.Context(), sb.Address())
1566+
require.NoError(t, err)
1567+
defer c.Close()
1568+
1569+
out := llb.Image("docker.io/library/busybox:latest").
1570+
File(llb.Copy(llb.Local("mylocal"), "input.txt", "input.txt")).
1571+
Run(llb.Shlex(`sh -c "/bin/rev < input.txt > /out/output.txt"`)).
1572+
AddMount(`/out`, llb.Scratch())
1573+
1574+
def, err := out.Marshal(sb.Context())
1575+
require.NoError(t, err)
1576+
1577+
srcDir := integration.Tmpdir(t,
1578+
fstest.CreateFile("input.txt", []byte("Hello World"), 0600),
1579+
)
1580+
1581+
destDir := integration.Tmpdir(t)
1582+
1583+
_, err = c.Solve(sb.Context(), def, SolveOpt{
1584+
LocalDirs: map[string]string{
1585+
"mylocal": srcDir.Name,
1586+
},
1587+
Exports: []ExportEntry{
1588+
{
1589+
Type: ExporterLocal,
1590+
OutputDir: destDir.Name,
1591+
},
1592+
},
1593+
}, nil)
1594+
1595+
require.NoError(t, err)
1596+
1597+
dt, err := os.ReadFile(filepath.Join(destDir.Name, "output.txt"))
1598+
require.NoError(t, err)
1599+
require.Equal(t, []byte("dlroW olleH"), dt)
1600+
}
1601+
15571602
func testFileOpMkdirMkfile(t *testing.T, sb integration.Sandbox) {
15581603
requiresLinux(t)
15591604
c, err := New(sb.Context(), sb.Address())
@@ -1624,7 +1669,7 @@ func testFileOpCopyRm(t *testing.T, sb integration.Sandbox) {
16241669
OutputDir: destDir,
16251670
},
16261671
},
1627-
LocalDirs: map[string]string{
1672+
LocalMounts: map[string]fsutil.FS{
16281673
"mylocal": dir,
16291674
"mylocal2": dir2,
16301675
},
@@ -1751,7 +1796,7 @@ func testFileOpCopyIncludeExclude(t *testing.T, sb integration.Sandbox) {
17511796
OutputDir: destDir,
17521797
},
17531798
},
1754-
LocalDirs: map[string]string{
1799+
LocalMounts: map[string]fsutil.FS{
17551800
"mylocal": dir,
17561801
},
17571802
}, nil)
@@ -1772,7 +1817,7 @@ func testFileOpCopyIncludeExclude(t *testing.T, sb integration.Sandbox) {
17721817
// Create additional file which doesn't match the include pattern, and make
17731818
// sure this doesn't invalidate the cache.
17741819

1775-
err = fstest.Apply(fstest.CreateFile("unmatchedfile", []byte("data1"), 0600)).Apply(dir)
1820+
err = fstest.Apply(fstest.CreateFile("unmatchedfile", []byte("data1"), 0600)).Apply(dir.Name)
17761821
require.NoError(t, err)
17771822

17781823
st = llb.Scratch().File(
@@ -1798,7 +1843,7 @@ func testFileOpCopyIncludeExclude(t *testing.T, sb integration.Sandbox) {
17981843
OutputDir: destDir,
17991844
},
18001845
},
1801-
LocalDirs: map[string]string{
1846+
LocalMounts: map[string]fsutil.FS{
18021847
"mylocal": dir,
18031848
},
18041849
}, nil)
@@ -1861,7 +1906,7 @@ func testLocalSourceWithDiffer(t *testing.T, sb integration.Sandbox, d llb.DiffT
18611906

18621907
tv := syscall.NsecToTimespec(time.Now().UnixNano())
18631908

1864-
err = syscall.UtimesNano(filepath.Join(dir, "foo"), []syscall.Timespec{tv, tv})
1909+
err = syscall.UtimesNano(filepath.Join(dir.Name, "foo"), []syscall.Timespec{tv, tv})
18651910
require.NoError(t, err)
18661911

18671912
st := llb.Local("mylocal"+string(d), llb.Differ(d, false))
@@ -1878,7 +1923,7 @@ func testLocalSourceWithDiffer(t *testing.T, sb integration.Sandbox, d llb.DiffT
18781923
OutputDir: destDir,
18791924
},
18801925
},
1881-
LocalDirs: map[string]string{
1926+
LocalMounts: map[string]fsutil.FS{
18821927
"mylocal" + string(d): dir,
18831928
},
18841929
}, nil)
@@ -1888,10 +1933,10 @@ func testLocalSourceWithDiffer(t *testing.T, sb integration.Sandbox, d llb.DiffT
18881933
require.NoError(t, err)
18891934
require.Equal(t, []byte("foo"), dt)
18901935

1891-
err = os.WriteFile(filepath.Join(dir, "foo"), []byte("bar"), 0600)
1936+
err = os.WriteFile(filepath.Join(dir.Name, "foo"), []byte("bar"), 0600)
18921937
require.NoError(t, err)
18931938

1894-
err = syscall.UtimesNano(filepath.Join(dir, "foo"), []syscall.Timespec{tv, tv})
1939+
err = syscall.UtimesNano(filepath.Join(dir.Name, "foo"), []syscall.Timespec{tv, tv})
18951940
require.NoError(t, err)
18961941

18971942
_, err = c.Solve(context.TODO(), def, SolveOpt{
@@ -1901,7 +1946,7 @@ func testLocalSourceWithDiffer(t *testing.T, sb integration.Sandbox, d llb.DiffT
19011946
OutputDir: destDir,
19021947
},
19031948
},
1904-
LocalDirs: map[string]string{
1949+
LocalMounts: map[string]fsutil.FS{
19051950
"mylocal" + string(d): dir,
19061951
},
19071952
}, nil)
@@ -2211,7 +2256,7 @@ func testFileOpRmWildcard(t *testing.T, sb integration.Sandbox) {
22112256
OutputDir: destDir,
22122257
},
22132258
},
2214-
LocalDirs: map[string]string{
2259+
LocalMounts: map[string]fsutil.FS{
22152260
"mylocal": dir,
22162261
},
22172262
}, nil)
@@ -7658,7 +7703,7 @@ func testParallelLocalBuilds(t *testing.T, sb integration.Sandbox) {
76587703
OutputDir: destDir,
76597704
},
76607705
},
7661-
LocalDirs: map[string]string{
7706+
LocalMounts: map[string]fsutil.FS{
76627707
"source": srcDir,
76637708
},
76647709
}, nil)
@@ -9713,7 +9758,7 @@ func ensureFileContents(t *testing.T, path, expectedContents string) {
97139758

97149759
func makeSSHAgentSock(t *testing.T, agent agent.Agent) (p string, err error) {
97159760
tmpDir := integration.Tmpdir(t)
9716-
sockPath := filepath.Join(tmpDir, "ssh_auth_sock")
9761+
sockPath := filepath.Join(tmpDir.Name, "ssh_auth_sock")
97179762

97189763
l, err := net.Listen("unix", sockPath)
97199764
if err != nil {

cmd/buildctl/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func buildAction(clicontext *cli.Context) error {
250250

251251
solveOpt := client.SolveOpt{
252252
Exports: exports,
253-
// LocalDirs is set later
253+
// LocalMounts is set later
254254
Frontend: clicontext.String("frontend"),
255255
// FrontendAttrs is set later
256256
// OCILayouts is set later
@@ -267,7 +267,7 @@ func buildAction(clicontext *cli.Context) error {
267267
return errors.Wrap(err, "invalid opt")
268268
}
269269

270-
solveOpt.LocalDirs, err = build.ParseLocal(clicontext.StringSlice("local"))
270+
solveOpt.LocalMounts, err = build.ParseLocal(clicontext.StringSlice("local"))
271271
if err != nil {
272272
return errors.Wrap(err, "invalid local")
273273
}

cmd/buildctl/build/local.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
package build
22

3+
import (
4+
"github.com/pkg/errors"
5+
"github.com/tonistiigi/fsutil"
6+
)
7+
38
// ParseLocal parses --local
4-
func ParseLocal(locals []string) (map[string]string, error) {
5-
return attrMap(locals)
9+
func ParseLocal(locals []string) (map[string]fsutil.FS, error) {
10+
localDirs, err := attrMap(locals)
11+
if err != nil {
12+
return nil, errors.WithStack(err)
13+
}
14+
15+
mounts := make(map[string]fsutil.FS, len(localDirs))
16+
17+
for k, v := range localDirs {
18+
mounts[k], err = fsutil.NewFS(v)
19+
if err != nil {
20+
return nil, errors.WithStack(err)
21+
}
22+
}
23+
24+
return mounts, nil
625
}

examples/build-using-dockerfile/main.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/moby/buildkit/util/progress/progressui"
1717
"github.com/pkg/errors"
1818
"github.com/sirupsen/logrus"
19+
"github.com/tonistiigi/fsutil"
1920
"github.com/urfave/cli"
2021
"golang.org/x/sync/errgroup"
2122
)
@@ -136,9 +137,15 @@ func newSolveOpt(clicontext *cli.Context, w io.WriteCloser) (*client.SolveOpt, e
136137
if file == "" {
137138
file = filepath.Join(buildCtx, "Dockerfile")
138139
}
139-
localDirs := map[string]string{
140-
"context": buildCtx,
141-
"dockerfile": filepath.Dir(file),
140+
141+
cxtLocalMount, err := fsutil.NewFS(buildCtx)
142+
if err != nil {
143+
return nil, errors.New("invalid buildCtx local mount dir")
144+
}
145+
146+
dockerfileLocalMount, err := fsutil.NewFS(filepath.Dir(file))
147+
if err != nil {
148+
return nil, errors.New("invalid dockerfile local mount dir")
142149
}
143150

144151
frontend := "dockerfile.v0" // TODO: use gateway
@@ -173,7 +180,10 @@ func newSolveOpt(clicontext *cli.Context, w io.WriteCloser) (*client.SolveOpt, e
173180
},
174181
},
175182
},
176-
LocalDirs: localDirs,
183+
LocalMounts: map[string]fsutil.FS{
184+
"context": cxtLocalMount,
185+
"dockerfile": dockerfileLocalMount,
186+
},
177187
Frontend: frontend,
178188
FrontendAttrs: frontendAttrs,
179189
}, nil

frontend/dockerfile/dockerfile_addchecksum_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/moby/buildkit/util/testutil/integration"
1313
digest "github.com/opencontainers/go-digest"
1414
"github.com/stretchr/testify/require"
15+
"github.com/tonistiigi/fsutil"
1516
)
1617

1718
var addChecksumTests = integration.TestFuncs(
@@ -50,7 +51,7 @@ ADD --checksum=%s %s /tmp/foo
5051
fstest.CreateFile("Dockerfile", dockerfile, 0600),
5152
)
5253
_, err := f.Solve(sb.Context(), c, client.SolveOpt{
53-
LocalDirs: map[string]string{
54+
LocalMounts: map[string]fsutil.FS{
5455
dockerui.DefaultLocalNameDockerfile: dir,
5556
dockerui.DefaultLocalNameContext: dir,
5657
},
@@ -69,7 +70,7 @@ ADD --checksum=${DIGEST} ${LINK} /tmp/foo
6970
fstest.CreateFile("Dockerfile", dockerfile, 0600),
7071
)
7172
_, err := f.Solve(sb.Context(), c, client.SolveOpt{
72-
LocalDirs: map[string]string{
73+
LocalMounts: map[string]fsutil.FS{
7374
dockerui.DefaultLocalNameDockerfile: dir,
7475
dockerui.DefaultLocalNameContext: dir,
7576
},
@@ -86,7 +87,7 @@ ADD --checksum=%s %s /tmp/foo
8687
fstest.CreateFile("Dockerfile", dockerfile, 0600),
8788
)
8889
_, err := f.Solve(sb.Context(), c, client.SolveOpt{
89-
LocalDirs: map[string]string{
90+
LocalMounts: map[string]fsutil.FS{
9091
dockerui.DefaultLocalNameDockerfile: dir,
9192
dockerui.DefaultLocalNameContext: dir,
9293
},
@@ -103,7 +104,7 @@ ADD --checksum=md5:7e55db001d319a94b0b713529a756623 %s /tmp/foo
103104
fstest.CreateFile("Dockerfile", dockerfile, 0600),
104105
)
105106
_, err := f.Solve(sb.Context(), c, client.SolveOpt{
106-
LocalDirs: map[string]string{
107+
LocalMounts: map[string]fsutil.FS{
107108
dockerui.DefaultLocalNameDockerfile: dir,
108109
dockerui.DefaultLocalNameContext: dir,
109110
},
@@ -120,7 +121,7 @@ ADD --checksum=unknown:%s %s /tmp/foo
120121
fstest.CreateFile("Dockerfile", dockerfile, 0600),
121122
)
122123
_, err := f.Solve(sb.Context(), c, client.SolveOpt{
123-
LocalDirs: map[string]string{
124+
LocalMounts: map[string]fsutil.FS{
124125
dockerui.DefaultLocalNameDockerfile: dir,
125126
dockerui.DefaultLocalNameContext: dir,
126127
},
@@ -137,7 +138,7 @@ ADD --checksum=%s %s /tmp/foo
137138
fstest.CreateFile("Dockerfile", dockerfile, 0600),
138139
)
139140
_, err := f.Solve(sb.Context(), c, client.SolveOpt{
140-
LocalDirs: map[string]string{
141+
LocalMounts: map[string]fsutil.FS{
141142
dockerui.DefaultLocalNameDockerfile: dir,
142143
dockerui.DefaultLocalNameContext: dir,
143144
},
@@ -156,7 +157,7 @@ ADD --checksum=%s foo /tmp/foo
156157
fstest.CreateFile("Dockerfile", dockerfile, 0600),
157158
)
158159
_, err := f.Solve(sb.Context(), c, client.SolveOpt{
159-
LocalDirs: map[string]string{
160+
LocalMounts: map[string]fsutil.FS{
160161
dockerui.DefaultLocalNameDockerfile: dir,
161162
dockerui.DefaultLocalNameContext: dir,
162163
},

0 commit comments

Comments
 (0)