test/e2e: close and remove temp file before cp in cp_test

Close the temporary destination file and remove it before copying
to avoid issues with copying to an existing open file. Store the
filename in a variable for reuse and defer cleanup.

Signed-off-by: Lokesh Mandvekar <lsm5@redhat.com>
Signed-off-by: Chris Evich <cevich@redhat.com>
This commit is contained in:
Lokesh Mandvekar 2026-02-04 16:31:08 +05:30 • committed by Chris Evich
parent 75c7a0dd27
commit 07637f101b
No known key found for this signature in database
GPG key ID: 03EDC70FD578067F

View file

@ -72,12 +72,14 @@ var _ = Describe("Podman cp", func() {
// Copy FROM the container.
destFile, err := ioutil.TempFile("", "")
Expect(err).To(BeNil())
defer destFile.Close()
defer os.Remove(destFile.Name())
destFile, err := os.CreateTemp("", "")
Expect(err).ToNot(HaveOccurred())
destFileName := destFile.Name()
destFile.Close()
os.Remove(destFileName)
defer os.Remove(destFileName)
session = podmanTest.Podman([]string{"cp", name + ":foo", destFile.Name()})
session = podmanTest.Podman([]string{"cp", name + ":foo", destFileName})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
@ -86,8 +88,8 @@ var _ = Describe("Podman cp", func() {
Expect(session).Should(Exit(0))
// Now make sure the content matches.
roundtripContent, err := ioutil.ReadFile(destFile.Name())
Expect(err).To(BeNil())
roundtripContent, err := os.ReadFile(destFileName)
Expect(err).ToNot(HaveOccurred())
Expect(roundtripContent).To(Equal(originalContent))
})
@ -121,18 +123,20 @@ var _ = Describe("Podman cp", func() {
// Copy FROM the container.
destFile, err := ioutil.TempFile("", "")
Expect(err).To(BeNil())
defer destFile.Close()
defer os.Remove(destFile.Name())
destFile, err := os.CreateTemp("", "")
Expect(err).ToNot(HaveOccurred())
destFileName := destFile.Name()
destFile.Close()
os.Remove(destFileName)
defer os.Remove(destFileName)
session = podmanTest.Podman([]string{"cp", name + ":foo", destFile.Name()})
session = podmanTest.Podman([]string{"cp", name + ":foo", destFileName})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
// Now make sure the content matches.
roundtripContent, err := ioutil.ReadFile(destFile.Name())
Expect(err).To(BeNil())
roundtripContent, err := os.ReadFile(destFileName)
Expect(err).ToNot(HaveOccurred())
Expect(roundtripContent).To(Equal(originalContent))
})