From 553df8748bd7936ba09c836d54525e45d80c121e Mon Sep 17 00:00:00 2001 From: Aditya R Date: Mon, 12 Dec 2022 11:28:43 +0530 Subject: [PATCH] podman-remote,bindings: trim context path correctly when its emptydir podman-remote converts and sends absolute path as context when its an emptydir by adding additional seperator however it should correctly trim the path and not add additional seperator for such use cases. Closes: BZ#2145054 Signed-off-by: Aditya R --- pkg/bindings/images/build.go | 30 +++++++++++++++++------------- test/e2e/build_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/pkg/bindings/images/build.go b/pkg/bindings/images/build.go index d8ddeb6a84..4dcfb8e167 100644 --- a/pkg/bindings/images/build.go +++ b/pkg/bindings/images/build.go @@ -647,23 +647,27 @@ func nTar(excludes []string, sources ...string) (io.ReadCloser, error) { return err } + separator := string(filepath.Separator) // check if what we are given is an empty dir, if so then continue w/ it. Else return. // if we are given a file or a symlink, we do not want to exclude it. - if d.IsDir() && s == path { - var p *os.File - p, err = os.Open(path) - if err != nil { - return err - } - defer p.Close() - _, err = p.Readdir(1) - if err != io.EOF { - return nil // non empty root dir, need to return - } else if err != nil { - logrus.Errorf("While reading directory %v: %v", path, err) + if s == path { + separator = "" + if d.IsDir() { + var p *os.File + p, err = os.Open(path) + if err != nil { + return err + } + defer p.Close() + _, err = p.Readdir(1) + if err != io.EOF { + return nil // non empty root dir, need to return + } else if err != nil { + logrus.Errorf("While reading directory %v: %v", path, err) + } } } - name := filepath.ToSlash(strings.TrimPrefix(path, s+string(filepath.Separator))) + name := filepath.ToSlash(strings.TrimPrefix(path, s+separator)) excluded, err := pm.Matches(name) //nolint:staticcheck if err != nil { diff --git a/test/e2e/build_test.go b/test/e2e/build_test.go index 5d2674cf07..fb75ad1b94 100644 --- a/test/e2e/build_test.go +++ b/test/e2e/build_test.go @@ -386,6 +386,41 @@ RUN exit 5`, ALPINE) Expect(data).To(ContainSubstring(buildah.Version)) }) + It("podman-remote send correct path to copier", func() { + if IsRemote() { + podmanTest.StopRemoteService() + podmanTest.StartRemoteService() + } else { + Skip("Only valid at remote test, case works fine for regular podman and buildah") + } + cwd, err := os.Getwd() + Expect(err).ToNot(HaveOccurred()) + + // Write target and fake files + targetSubPath := filepath.Join(cwd, "emptydir") + if _, err = os.Stat(targetSubPath); err != nil { + if os.IsNotExist(err) { + err = os.Mkdir(targetSubPath, 0755) + Expect(err).ToNot(HaveOccurred()) + } + } + + containerfile := fmt.Sprintf(`FROM %s +COPY /* /dir`, ALPINE) + + containerfilePath := filepath.Join(cwd, "ContainerfilePathToCopier") + err = os.WriteFile(containerfilePath, []byte(containerfile), 0644) + Expect(err).ToNot(HaveOccurred()) + + session := podmanTest.Podman([]string{"build", "--pull-never", "-t", "test", "-f", "ContainerfilePathToCopier", targetSubPath}) + session.WaitWithDefaultTimeout() + // NOTE: Docker and buildah both should error when `COPY /* /dir` is done on emptydir + // as context. However buildkit simply ignores this so when buildah also starts ignoring + // for such case edit this test to return 0 and check that no `/dir` should be in the result. + Expect(session).Should(Exit(125)) + Expect(session.ErrorToString()).To(ContainSubstring("can't make relative to")) + }) + It("podman remote test container/docker file is not inside context dir", func() { // Given // Switch to temp dir and restore it afterwards