Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

podman-remote,bindings: trim context path correctly when its emptydir #16810

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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