test/e2e: fix incorrect usage of CreateTempDirInTempDir()

Creating a new diretory results in the test leaking it when it is not
removed via a defer call. All tests have already access to
`podmanTest.TempDir` which will be automatically removed in the
`AfterEach()` block.

While some test were fine other forgot the defer call. To keep the test
consitent and prevent other from making the same mistake convert all
users to `podmanTest.TempDir`. `CreateTempDirInTempDir()` is only used
for the `podmanTest.Setup()` call.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2023-04-17 12:16:20 +02:00
parent 601d228cae
commit 1f45c715df
10 changed files with 34 additions and 74 deletions

View File

@ -211,16 +211,10 @@ var _ = Describe("Podman build", func() {
Expect(os.Chdir(os.TempDir())).To(Succeed())
defer Expect(os.Chdir(cwd)).To(BeNil())
// Write target and fake files
targetPath, err := CreateTempDirInTempDir()
if err != nil {
os.Exit(1)
}
fakeFile := filepath.Join(os.TempDir(), "Containerfile")
Expect(os.WriteFile(fakeFile, []byte(fmt.Sprintf("FROM %s", ALPINE)), 0755)).To(Succeed())
targetFile := filepath.Join(targetPath, "Containerfile")
targetFile := filepath.Join(podmanTest.TempDir, "Containerfile")
Expect(os.WriteFile(targetFile, []byte("FROM scratch"), 0755)).To(Succeed())
defer func() {
@ -247,11 +241,7 @@ var _ = Describe("Podman build", func() {
Expect(os.Chdir(os.TempDir())).To(Succeed())
defer Expect(os.Chdir(cwd)).To(BeNil())
targetPath, err := CreateTempDirInTempDir()
if err != nil {
os.Exit(1)
}
targetFile := filepath.Join(targetPath, "idFile")
targetFile := filepath.Join(podmanTest.TempDir, "idFile")
session := podmanTest.Podman([]string{"build", "--pull-never", "build/basicalpine", "--iidfile", targetFile})
session.WaitWithDefaultTimeout()
@ -431,8 +421,7 @@ COPY /* /dir`, ALPINE)
podmanTest.AddImageToRWStore(ALPINE)
// Write target and fake files
targetPath, err := CreateTempDirInTempDir()
Expect(err).ToNot(HaveOccurred())
targetPath := podmanTest.TempDir
targetSubPath := filepath.Join(targetPath, "subdir")
err = os.Mkdir(targetSubPath, 0755)
Expect(err).ToNot(HaveOccurred())
@ -450,7 +439,6 @@ RUN find /test`, ALPINE)
defer func() {
Expect(os.Chdir(cwd)).To(Succeed())
Expect(os.RemoveAll(targetPath)).To(Succeed())
}()
// make cwd as context root path
@ -504,8 +492,7 @@ RUN find /test`, ALPINE)
podmanTest.AddImageToRWStore(ALPINE)
// Write target and fake files
targetPath, err := CreateTempDirInTempDir()
Expect(err).ToNot(HaveOccurred())
targetPath := podmanTest.TempDir
targetSubPath := filepath.Join(targetPath, "subdir")
err = os.Mkdir(targetSubPath, 0755)
Expect(err).ToNot(HaveOccurred())
@ -518,7 +505,6 @@ RUN find /test`, ALPINE)
defer func() {
Expect(os.Chdir(cwd)).To(Succeed())
Expect(os.RemoveAll(targetPath)).To(Succeed())
}()
// make cwd as context root path
@ -544,7 +530,8 @@ RUN find /test`, ALPINE)
podmanTest.AddImageToRWStore(ALPINE)
// Write target and fake files
targetPath, err := CreateTempDirInTempDir()
targetPath := filepath.Join(podmanTest.TempDir, "build")
err = os.Mkdir(targetPath, 0755)
Expect(err).ToNot(HaveOccurred())
containerfile := fmt.Sprintf(`FROM %s
@ -616,9 +603,9 @@ subdir**`
containerfile := filepath.Join(tempdir, "Containerfile")
Expect(os.WriteFile(containerfile, contents.Bytes(), 0644)).ToNot(HaveOccurred())
contextDir, err := CreateTempDirInTempDir()
contextDir := filepath.Join(podmanTest.TempDir, "context")
err = os.MkdirAll(contextDir, os.ModePerm)
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(contextDir)
Expect(os.WriteFile(filepath.Join(contextDir, "expected"), contents.Bytes(), 0644)).
ToNot(HaveOccurred())
@ -665,8 +652,7 @@ subdir**`
podmanTest.AddImageToRWStore(ALPINE)
// Write target and fake files
targetPath, err := CreateTempDirInTempDir()
Expect(err).ToNot(HaveOccurred())
targetPath := podmanTest.TempDir
targetSubPath := filepath.Join(targetPath, "subdir")
err = os.Mkdir(targetSubPath, 0755)
Expect(err).ToNot(HaveOccurred())
@ -691,7 +677,6 @@ RUN [[ -L /test/dummy-symlink ]] && echo SYMLNKOK || echo SYMLNKERR`, ALPINE)
defer func() {
Expect(os.Chdir(cwd)).To(Succeed())
Expect(os.RemoveAll(targetPath)).To(Succeed())
}()
// make cwd as context root path
@ -707,8 +692,7 @@ RUN [[ -L /test/dummy-symlink ]] && echo SYMLNKOK || echo SYMLNKERR`, ALPINE)
})
It("podman build --from, --add-host, --cap-drop, --cap-add", func() {
targetPath, err := CreateTempDirInTempDir()
Expect(err).ToNot(HaveOccurred())
targetPath := podmanTest.TempDir
containerFile := filepath.Join(targetPath, "Containerfile")
content := `FROM scratch
@ -738,9 +722,7 @@ RUN grep CapEff /proc/self/status`
})
It("podman build --isolation && --arch", func() {
targetPath, err := CreateTempDirInTempDir()
Expect(err).ToNot(HaveOccurred())
targetPath := podmanTest.TempDir
containerFile := filepath.Join(targetPath, "Containerfile")
Expect(os.WriteFile(containerFile, []byte(fmt.Sprintf("FROM %s", ALPINE)), 0755)).To(Succeed())
@ -798,8 +780,7 @@ RUN echo hello`, ALPINE)
})
It("podman build --log-rusage", func() {
targetPath, err := CreateTempDirInTempDir()
Expect(err).ToNot(HaveOccurred())
targetPath := podmanTest.TempDir
containerFile := filepath.Join(targetPath, "Containerfile")
content := `FROM scratch`

View File

@ -1327,6 +1327,7 @@ var _ = Describe("Podman checkpoint", func() {
Expect(podmanTest.NumberOfContainersRunning()).To(Equal(1))
cid := session.OutputToString()
fileName := filepath.Join(podmanTest.TempDir, "/checkpoint-"+cid+".tar.gz")
defer os.Remove(fileName)
result := podmanTest.Podman([]string{
"container",
@ -1343,8 +1344,9 @@ var _ = Describe("Podman checkpoint", func() {
Expect(podmanTest.NumberOfContainers()).To(Equal(0))
// Extract checkpoint archive
destinationDirectory, err := CreateTempDirInTempDir()
Expect(err).ShouldNot(HaveOccurred())
destinationDirectory := filepath.Join(podmanTest.TempDir, "dest")
err = os.MkdirAll(destinationDirectory, os.ModePerm)
Expect(err).ToNot(HaveOccurred())
tarsession := SystemExec(
"tar",
@ -1359,11 +1361,6 @@ var _ = Describe("Podman checkpoint", func() {
_, err = os.Stat(filepath.Join(destinationDirectory, stats.StatsDump))
Expect(err).ShouldNot(HaveOccurred())
Expect(os.RemoveAll(destinationDirectory)).To(Succeed())
// Remove exported checkpoint
os.Remove(fileName)
})
It("podman checkpoint and restore containers with --print-stats", func() {

View File

@ -270,12 +270,8 @@ var _ = Describe("Podman commit", func() {
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(os.TempDir())).To(Succeed())
targetPath, err := CreateTempDirInTempDir()
if err != nil {
os.Exit(1)
}
targetPath := podmanTest.TempDir
targetFile := filepath.Join(targetPath, "idFile")
defer Expect(os.RemoveAll(targetFile)).To(BeNil())
defer Expect(os.Chdir(cwd)).To(BeNil())
_, ec, _ := podmanTest.RunLsContainer("test1")

View File

@ -110,8 +110,6 @@ var _ = Describe("Verify podman containers.conf usage", func() {
// FIXME: Needs crun-1.8.2-2 to allow this with --cgroup-manager=cgroupfs, once this is available remove the skip below.
SkipIfRootless("--cgroup-manager=cgoupfs and --cgroup-conf not supported in rootless mode with crun")
conffile := filepath.Join(podmanTest.TempDir, "container.conf")
tempdir, err = CreateTempDirInTempDir()
Expect(err).ToNot(HaveOccurred())
err := os.WriteFile(conffile, []byte("[containers]\ncgroup_conf = [\"pids.max=1234\",]\n"), 0755)
Expect(err).ToNot(HaveOccurred())
@ -281,17 +279,18 @@ var _ = Describe("Verify podman containers.conf usage", func() {
It("add volumes", func() {
conffile := filepath.Join(podmanTest.TempDir, "container.conf")
tempdir, err = CreateTempDirInTempDir()
Expect(err).ToNot(HaveOccurred())
err := os.WriteFile(conffile, []byte(fmt.Sprintf("[containers]\nvolumes=[\"%s:%s:Z\",]\n", tempdir, tempdir)), 0755)
volume := filepath.Join(podmanTest.TempDir, "vol")
err = os.MkdirAll(volume, os.ModePerm)
Expect(err).ToNot(HaveOccurred())
err := os.WriteFile(conffile, []byte(fmt.Sprintf("[containers]\nvolumes=[\"%s:%s:Z\",]\n", volume, volume)), 0755)
Expect(err).ToNot(HaveOccurred())
os.Setenv("CONTAINERS_CONF", conffile)
if IsRemote() {
podmanTest.RestartRemoteService()
}
result := podmanTest.Podman([]string{"run", ALPINE, "ls", tempdir})
result := podmanTest.Podman([]string{"run", ALPINE, "ls", volume})
result.WaitWithDefaultTimeout()
Expect(result).Should(Exit(0))
})

View File

@ -1072,9 +1072,7 @@ var _ = Describe("Podman kube generate", func() {
containerfile := `FROM quay.io/libpod/alpine:latest
ENTRYPOINT ["sleep"]`
targetPath, err := CreateTempDirInTempDir()
Expect(err).ToNot(HaveOccurred())
containerfilePath := filepath.Join(targetPath, "Containerfile")
containerfilePath := filepath.Join(podmanTest.TempDir, "Containerfile")
err = os.WriteFile(containerfilePath, []byte(containerfile), 0644)
Expect(err).ToNot(HaveOccurred())
@ -1126,9 +1124,7 @@ ENTRYPOINT ["sleep"]`
containerfile := `FROM quay.io/libpod/alpine:latest
USER 1000`
targetPath, err := CreateTempDirInTempDir()
Expect(err).ToNot(HaveOccurred())
containerfilePath := filepath.Join(targetPath, "Containerfile")
containerfilePath := filepath.Join(podmanTest.TempDir, "Containerfile")
err = os.WriteFile(containerfilePath, []byte(containerfile), 0644)
Expect(err).ToNot(HaveOccurred())
@ -1197,9 +1193,7 @@ USER 1000`
RUN adduser -u 10001 -S test1
USER test1`
targetPath, err := CreateTempDirInTempDir()
Expect(err).ToNot(HaveOccurred())
containerfilePath := filepath.Join(targetPath, "Containerfile")
containerfilePath := filepath.Join(podmanTest.TempDir, "Containerfile")
err = os.WriteFile(containerfilePath, []byte(containerfile), 0644)
Expect(err).ToNot(HaveOccurred())

View File

@ -297,20 +297,17 @@ var _ = Describe("Podman healthcheck run", func() {
podmanTest.AddImageToRWStore(ALPINE)
// Write target and fake files
targetPath, err := CreateTempDirInTempDir()
Expect(err).ToNot(HaveOccurred())
containerfile := fmt.Sprintf(`FROM %s
HEALTHCHECK CMD ls -l / 2>&1`, ALPINE)
containerfilePath := filepath.Join(targetPath, "Containerfile")
containerfilePath := filepath.Join(podmanTest.TempDir, "Containerfile")
err = os.WriteFile(containerfilePath, []byte(containerfile), 0644)
Expect(err).ToNot(HaveOccurred())
defer func() {
Expect(os.Chdir(cwd)).To(Succeed())
Expect(os.RemoveAll(targetPath)).To(Succeed())
}()
// make cwd as context root path
Expect(os.Chdir(targetPath)).To(Succeed())
Expect(os.Chdir(podmanTest.TempDir)).To(Succeed())
session := podmanTest.Podman([]string{"build", "--format", "docker", "-t", "test", "."})
session.WaitWithDefaultTimeout()

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/containers/common/libnetwork/types"
@ -39,9 +40,7 @@ var _ = Describe("Podman network", func() {
It("podman --cni-config-dir backwards compat", func() {
SkipIfRemote("--cni-config-dir only works locally")
netDir, err := CreateTempDirInTempDir()
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(netDir)
netDir := filepath.Join(podmanTest.TempDir, "networks123")
session := podmanTest.Podman([]string{"--cni-config-dir", netDir, "network", "ls", "--noheading"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

View File

@ -2933,8 +2933,6 @@ spec:
Expect(c).Should(Exit(0))
conffile := filepath.Join(podmanTest.TempDir, "kube.yaml")
tempdir, err = CreateTempDirInTempDir()
Expect(err).ToNot(HaveOccurred())
err := os.WriteFile(conffile, []byte(testyaml), 0755)
Expect(err).ToNot(HaveOccurred())

View File

@ -304,11 +304,8 @@ var _ = Describe("Podman pod create", func() {
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(os.TempDir())).To(Succeed())
targetPath, err := CreateTempDirInTempDir()
if err != nil {
os.Exit(1)
}
targetFile := filepath.Join(targetPath, "idFile")
targetFile := filepath.Join(podmanTest.TempDir, "idFile")
defer Expect(os.RemoveAll(targetFile)).To(BeNil())
defer Expect(os.Chdir(cwd)).To(BeNil())

View File

@ -3,6 +3,7 @@ package integration
import (
"fmt"
"os"
"path/filepath"
. "github.com/containers/podman/v4/test/utils"
. "github.com/onsi/ginkgo"
@ -47,7 +48,8 @@ var _ = Describe("Podman run", func() {
})
It("podman run a container using a --workdir under a bind mount", func() {
volume, err := CreateTempDirInTempDir()
volume := filepath.Join(podmanTest.TempDir, "vol")
err = os.MkdirAll(volume, os.ModePerm)
Expect(err).ToNot(HaveOccurred())
session := podmanTest.Podman([]string{"run", "--volume", fmt.Sprintf("%s:/var_ovl/:O", volume), "--workdir", "/var_ovl/log", ALPINE, "true"})