Merge pull request #16810 from flouthoc/trim-path-correctly

podman-remote,bindings: trim context path correctly when its `emptydir`
This commit is contained in:
OpenShift Merge Robot
2023-01-02 05:07:31 -05:00
committed by GitHub
2 changed files with 52 additions and 13 deletions

View File

@ -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 {

View File

@ -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