From 1857d7e50f71c3676f1a1709ae951fc1cdc2907a Mon Sep 17 00:00:00 2001 From: Mario Loriedo Date: Fri, 13 Sep 2024 11:44:48 +0200 Subject: [PATCH] Convert windows paths in volume arg of the build command Signed-off-by: Mario Loriedo --- pkg/bindings/images/build.go | 23 ++++++++++++++++++++++- pkg/machine/e2e/basic_test.go | 8 ++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pkg/bindings/images/build.go b/pkg/bindings/images/build.go index 7756731182..76fc452054 100644 --- a/pkg/bindings/images/build.go +++ b/pkg/bindings/images/build.go @@ -65,6 +65,27 @@ func convertAdditionalBuildContexts(additionalBuildContexts map[string]*define.A } } +// convertVolumeSrcPath converts windows paths in the HOST-DIR part of a volume +// into the corresponding path in the default Windows machine. +// (e.g. C:\test:/src/docs ==> /mnt/c/test:/src/docs). +// If any error occurs while parsing the volume string, the original volume +// string is returned. +func convertVolumeSrcPath(volume string) string { + splitVol := specgen.SplitVolumeString(volume) + if len(splitVol) < 2 || len(splitVol) > 3 { + return volume + } + convertedSrcPath, err := specgen.ConvertWinMountPath(splitVol[0]) + if err != nil { + return volume + } + if len(splitVol) == 2 { + return convertedSrcPath + ":" + splitVol[1] + } else { + return convertedSrcPath + ":" + splitVol[1] + ":" + splitVol[2] + } +} + // Build creates an image using a containerfile reference func Build(ctx context.Context, containerFiles []string, options types.BuildOptions) (*types.BuildReport, error) { if options.CommonBuildOpts == nil { @@ -352,7 +373,7 @@ func Build(ctx context.Context, containerFiles []string, options types.BuildOpti } for _, volume := range options.CommonBuildOpts.Volumes { - params.Add("volume", volume) + params.Add("volume", convertVolumeSrcPath(volume)) } for _, group := range options.GroupAdd { diff --git a/pkg/machine/e2e/basic_test.go b/pkg/machine/e2e/basic_test.go index dffae8bac6..e0585e2752 100644 --- a/pkg/machine/e2e/basic_test.go +++ b/pkg/machine/e2e/basic_test.go @@ -88,6 +88,14 @@ var _ = Describe("run basic podman commands", func() { runAlp, err := mb.setCmd(bm.withPodmanCommand([]string{"run", "-v", tDir + ":/test:Z", "quay.io/libpod/alpine_nginx", "ls", "/test/attr-test-file"})).run() Expect(err).ToNot(HaveOccurred()) Expect(runAlp).To(Exit(0)) + + // Test build with --volume option + cf := filepath.Join(tDir, "Containerfile") + err = os.WriteFile(cf, []byte("FROM quay.io/libpod/alpine_nginx\nRUN ls /test/attr-test-file\n"), 0o644) + Expect(err).ToNot(HaveOccurred()) + build, err := mb.setCmd(bm.withPodmanCommand([]string{"build", "-t", name, "-v", tDir + ":/test", tDir})).run() + Expect(err).ToNot(HaveOccurred()) + Expect(build).To(Exit(0)) }) It("Volume should be virtiofs", func() {