Skip to content

Commit

Permalink
podman-remote,bindings: trim context path correctly when its emptydir
Browse files Browse the repository at this point in the history
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 <arajan@redhat.com>
  • Loading branch information
flouthoc committed Dec 30, 2022
1 parent 3fbf62e commit 553df87
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
30 changes: 17 additions & 13 deletions pkg/bindings/images/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions test/e2e/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 553df87

Please sign in to comment.