Skip to content

test/e2e: simplify directory change handling #26768

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
61 changes: 8 additions & 53 deletions test/e2e/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,7 @@ var _ = Describe("Podman build", func() {

It("podman build Containerfile locations", func() {
// Given
// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(os.TempDir())).To(Succeed())
defer Expect(os.Chdir(cwd)).To(BeNil())
GinkgoT().Chdir(os.TempDir())

fakeFile := filepath.Join(os.TempDir(), "Containerfile")
Expect(os.WriteFile(fakeFile, []byte(fmt.Sprintf("FROM %s", CITEST_IMAGE)), 0755)).To(Succeed())
Expand All @@ -281,11 +277,7 @@ var _ = Describe("Podman build", func() {
})

It("podman build basic alpine and print id to external file", func() {
// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(os.TempDir())).To(Succeed())
defer Expect(os.Chdir(cwd)).To(BeNil())
GinkgoT().Chdir(os.TempDir())

targetFile := filepath.Join(podmanTest.TempDir, "idFile")

Expand Down Expand Up @@ -453,10 +445,6 @@ COPY /emptydir/* /dir`, CITEST_IMAGE)

It("podman remote test container/docker file is not inside context dir", func() {
// Given
// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())

// Write target and fake files
targetPath := podmanTest.TempDir
targetSubPath := filepath.Join(targetPath, "subdir")
Expand All @@ -474,12 +462,8 @@ RUN find /test`, CITEST_IMAGE)
err = os.WriteFile(containerfilePath, []byte(containerfile), 0644)
Expect(err).ToNot(HaveOccurred())

defer func() {
Expect(os.Chdir(cwd)).To(Succeed())
}()

// make cwd as context root path
Expect(os.Chdir(targetPath)).To(Succeed())
GinkgoT().Chdir(targetPath)

session := podmanTest.Podman([]string{"build", "--pull-never", "-t", "test", "-f", "Containerfile", targetSubPath})
session.WaitWithDefaultTimeout()
Expand Down Expand Up @@ -532,10 +516,6 @@ RUN find /test`, CITEST_IMAGE)
Skip("Only valid at remote test")
}
// Given
// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())

podmanTest.AddImageToRWStore(CITEST_IMAGE)

// Write target and fake files
Expand All @@ -550,12 +530,8 @@ RUN find /test`, CITEST_IMAGE)
err = os.WriteFile(containerfilePath, []byte(containerfile), 0644)
Expect(err).ToNot(HaveOccurred())

defer func() {
Expect(os.Chdir(cwd)).To(Succeed())
}()

// make cwd as context root path
Expect(os.Chdir(targetPath)).To(Succeed())
GinkgoT().Chdir(targetPath)

session := podmanTest.Podman([]string{"build", "--pull-never", "-t", "test", "-f", "subdir/Containerfile", "."})
session.WaitWithDefaultTimeout()
Expand All @@ -570,10 +546,6 @@ RUN find /test`, CITEST_IMAGE)
Skip("Only valid at remote test")
}
// Given
// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())

podmanTest.AddImageToRWStore(CITEST_IMAGE)

// Write target and fake files
Expand Down Expand Up @@ -606,12 +578,11 @@ RUN find /testfilter/`, CITEST_IMAGE)
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())
GinkgoT().Chdir(targetPath)

dockerignoreContent := `dummy1
subdir**`
Expand All @@ -636,10 +607,6 @@ subdir**`
SkipIfNotRemote("Testing remote .containerignore file filtering")
podmanTest.RestartRemoteService()

// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())

podmanTest.AddImageToRWStore(CITEST_IMAGE)

contents := bytes.Buffer{}
Expand Down Expand Up @@ -668,11 +635,7 @@ subdir**`
Eventually(ddSession, "30s", "1s").Should(Exit(0))

// make cwd as context root path
Expect(os.Chdir(contextDir)).ToNot(HaveOccurred())
defer func() {
err := os.Chdir(cwd)
Expect(err).ToNot(HaveOccurred())
}()
GinkgoT().Chdir(contextDir)

By("Test .containerignore filtering subdirectory")
err = os.WriteFile(filepath.Join(contextDir, ".containerignore"), []byte(`subdir/`), 0644)
Expand All @@ -691,10 +654,6 @@ subdir**`
SkipIfNotRemote("Testing remote contextDir empty")
podmanTest.RestartRemoteService()

// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())

podmanTest.AddImageToRWStore(CITEST_IMAGE)

// Write target and fake files
Expand All @@ -709,7 +668,7 @@ subdir**`
emptyDir := filepath.Join(targetSubPath, "emptyDir")
err = os.Mkdir(emptyDir, 0755)
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(targetSubPath)).To(Succeed())
GinkgoT().Chdir(targetSubPath)
Expect(os.Symlink("dummy", "dummy-symlink")).To(Succeed())

containerfile := fmt.Sprintf(`FROM %s
Expand All @@ -721,12 +680,8 @@ RUN [[ -L /test/dummy-symlink ]] && echo SYMLNKOK || echo SYMLNKERR`, CITEST_IMA
err = os.WriteFile(containerfilePath, []byte(containerfile), 0644)
Expect(err).ToNot(HaveOccurred())

defer func() {
Expect(os.Chdir(cwd)).To(Succeed())
}()

// make cwd as context root path
Expect(os.Chdir(targetPath)).To(Succeed())
GinkgoT().Chdir(targetPath)

session := podmanTest.Podman([]string{"build", "--pull-never", "-t", "test", targetSubPath})
session.WaitWithDefaultTimeout()
Expand Down
6 changes: 1 addition & 5 deletions test/e2e/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,9 @@ var _ = Describe("Podman commit", func() {
})

It("podman commit container and print id to external file", func() {
// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(os.TempDir())).To(Succeed())
GinkgoT().Chdir(os.TempDir())
targetPath := podmanTest.TempDir
targetFile := filepath.Join(targetPath, "idFile")
defer Expect(os.Chdir(cwd)).To(BeNil())

_, ec, _ := podmanTest.RunLsContainer("test1")
Expect(ec).To(Equal(0))
Expand Down
5 changes: 1 addition & 4 deletions test/e2e/healthcheck_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,9 @@ HEALTHCHECK CMD ls -l / 2>&1`, ALPINE)
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())
}()

// make cwd as context root path
Expect(os.Chdir(podmanTest.TempDir)).To(Succeed())
GinkgoT().Chdir(podmanTest.TempDir)

session := podmanTest.Podman([]string{"build", "--format", "docker", "-t", "test", "."})
session.WaitWithDefaultTimeout()
Expand Down
38 changes: 8 additions & 30 deletions test/e2e/play_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ LABEL marge=mom
// Write a file to be copied
err = writeYaml(copyFile, filepath.Join(app1Dir, "copyfile"))
Expect(err).ToNot(HaveOccurred())
// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(yamlDir)).To(Succeed())
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()

GinkgoT().Chdir(yamlDir)

session := podmanTest.Podman([]string{"kube", "play", "top.yaml"})
session.WaitWithDefaultTimeout()
Expand Down Expand Up @@ -117,11 +114,8 @@ LABEL marge=mom
// Write a file to be copied
err = writeYaml(copyFile, filepath.Join(app1Dir, "copyfile"))
Expect(err).ToNot(HaveOccurred())
// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(yamlDir)).To(Succeed())
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()

GinkgoT().Chdir(yamlDir)

session := podmanTest.Podman([]string{"kube", "play", "top.yaml"})
session.WaitWithDefaultTimeout()
Expand Down Expand Up @@ -165,11 +159,7 @@ LABEL marge=mom
err = writeYaml(copyFile, filepath.Join(app1Dir, "copyfile"))
Expect(err).ToNot(HaveOccurred())

// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(yamlDir)).To(Succeed())
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
GinkgoT().Chdir(yamlDir)

// Build the image into the local store
build := podmanTest.Podman([]string{"build", "-t", "foobar", "-f", "Containerfile"})
Expand Down Expand Up @@ -212,11 +202,7 @@ LABEL marge=mom
err = writeYaml(copyFile, filepath.Join(app1Dir, "copyfile"))
Expect(err).ToNot(HaveOccurred())

// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(yamlDir)).To(Succeed())
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
GinkgoT().Chdir(yamlDir)

// Build the image into the local store
build := podmanTest.Podman([]string{"build", "-t", "foobar", "-f", "Containerfile"})
Expand Down Expand Up @@ -259,11 +245,7 @@ LABEL marge=mom
err = writeYaml(copyFile, filepath.Join(app1Dir, "copyfile"))
Expect(err).ToNot(HaveOccurred())

// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(yamlDir)).To(Succeed())
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
GinkgoT().Chdir(yamlDir)

// Build the image into the local store
build := podmanTest.Podman([]string{"build", "-t", "foobar", "-f", "Containerfile"})
Expand Down Expand Up @@ -352,11 +334,7 @@ echo GOT-HERE
os.Setenv("FOO", "make sure we use FOO from kube file, not env")
defer os.Unsetenv("FOO")

// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(yamlDir)).To(Succeed())
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
GinkgoT().Chdir(yamlDir)

session := podmanTest.Podman([]string{"kube", "play", "echo.yaml"})
session.WaitWithDefaultTimeout()
Expand Down
6 changes: 1 addition & 5 deletions test/e2e/pod_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,10 @@ var _ = Describe("Podman pod create", func() {
})

It("podman create pod and print id to external file", func() {
// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(os.TempDir())).To(Succeed())
GinkgoT().Chdir(os.TempDir())

targetFile := filepath.Join(podmanTest.TempDir, "idFile")
defer Expect(os.RemoveAll(targetFile)).To(BeNil())
defer Expect(os.Chdir(cwd)).To(BeNil())

session := podmanTest.Podman([]string{"pod", "create", "--name=abc", "--pod-id-file", targetFile})
session.WaitWithDefaultTimeout()
Expand Down
10 changes: 1 addition & 9 deletions test/e2e/run_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,15 +965,7 @@ USER testuser`, CITEST_IMAGE)
f, err := os.CreateTemp(mountPath, "podman")
Expect(err).ToNot(HaveOccurred())

cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())

err = os.Chdir(mountPath)
Expect(err).ToNot(HaveOccurred())
defer func() {
err := os.Chdir(cwd)
Expect(err).ToNot(HaveOccurred())
}()
GinkgoT().Chdir(mountPath)

run := podmanTest.Podman([]string{"run", "--security-opt", "label=disable", "-v", "./:" + dest, ALPINE, "ls", dest})
run.WaitWithDefaultTimeout()
Expand Down