Skip to content

Commit

Permalink
package e2e tests in rpm, part 3 of 3
Browse files Browse the repository at this point in the history
Part 3: get tests passing. Mostly by fixing tests that assume
that cwd is writable. I, um, have taken some liberties in
fixing a couple of broken tests.

Not all tests pass rootless. In particular, these three still fail:

  Podman run with --cgroup-parent [It] no --cgroup-parent
  Podman systemd [It] podman run container with systemd PID1
     -- both with cgroup errors

  podman system connection sshd and API services required [It] add ssh:// socket path using connection heuristic
     -- assumes too much about rootless user's ssh setup

I call this good enough.

Signed-off-by: Ed Santiago <[email protected]>
  • Loading branch information
edsantiago committed May 9, 2024
1 parent b1627ff commit f17b6de
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 67 deletions.
96 changes: 48 additions & 48 deletions test/e2e/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import (
. "github.com/onsi/gomega/gexec"
)

// tempContextDirectory makes a writable copy of one of our test-data directories
func tempContextDirectory(podmanTest *PodmanTestIntegration, source string) string {
contextDir, err := os.MkdirTemp(podmanTest.TempDir, "context-")
Expect(err).ToNot(HaveOccurred())
Expect(CopyDirectory(filepath.Join("build", source), contextDir)).To(Succeed())
return contextDir
}

var _ = Describe("Podman build", func() {
// Let's first do the most simple build possible to make sure stuff is
// happy and then clean up after ourselves to make sure that works too.
Expand All @@ -40,6 +48,14 @@ var _ = Describe("Podman build", func() {
})

It("podman build and remove basic alpine with TMPDIR as relative", func() {
// We need to cd into a writable directory, but then cd back
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(tempContextDirectory(podmanTest, "basicrun"))).To(Succeed())
defer func() {
Expect(os.Chdir(cwd)).To(Succeed())
}()

// preserve TMPDIR if it was originally set
if cacheDir, found := os.LookupEnv("TMPDIR"); found {
defer os.Setenv("TMPDIR", cacheDir)
Expand All @@ -49,8 +65,9 @@ var _ = Describe("Podman build", func() {
}
// Test case described here: https://github.com/containers/buildah/pull/5084
os.Setenv("TMPDIR", ".")

podmanTest.AddImageToRWStore(ALPINE)
session := podmanTest.Podman([]string{"build", "--pull-never", "build/basicrun"})
session := podmanTest.Podman([]string{"build", "--pull-never", "."})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
})
Expand Down Expand Up @@ -199,55 +216,50 @@ var _ = Describe("Podman build", func() {
})

It("podman build Containerfile locations", func() {
// Given
// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
// Given a Containerfile in current directory, but an explicit
// command-line "-f OtherContainerfile", ignore the one in cwd.
cdDir := filepath.Join(podmanTest.TempDir, "dir-where-we-cd")
err := os.Mkdir(cdDir, 0755)
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(os.TempDir())).To(Succeed())
defer Expect(os.Chdir(cwd)).To(BeNil())

fakeFile := filepath.Join(os.TempDir(), "Containerfile")
fakeFile := filepath.Join(cdDir, "Containerfile")
Expect(os.WriteFile(fakeFile, []byte(fmt.Sprintf("FROM %s", CITEST_IMAGE)), 0755)).To(Succeed())

targetFile := filepath.Join(podmanTest.TempDir, "Containerfile")
Expect(os.WriteFile(targetFile, []byte("FROM scratch"), 0755)).To(Succeed())
ctrfileDir := filepath.Join(podmanTest.TempDir, "dir-with-containerfile")
err = os.Mkdir(ctrfileDir, 0755)
Expect(err).ToNot(HaveOccurred())
realFile := filepath.Join(ctrfileDir, "Containerfile")
Expect(os.WriteFile(realFile, []byte("FROM scratch"), 0755)).To(Succeed())

// cd to the dir with the to-be-ignored Containerfile; but cd back when test is done
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(cdDir)).To(Succeed())
defer func() {
Expect(os.RemoveAll(fakeFile)).To(Succeed())
Expect(os.RemoveAll(targetFile)).To(Succeed())
Expect(os.Chdir(cwd)).To(Succeed())
}()

// When
session := podmanTest.Podman([]string{
"build", "--pull-never", "-f", targetFile, "-t", "test-locations",
"build", "--pull-never", "-f", realFile, "-t", "test-locations",
})
session.WaitWithDefaultTimeout()

// Then
Expect(session).Should(ExitCleanly())
Expect(strings.Fields(session.OutputToString())).
To(ContainElement("scratch"))
Expect(session.OutputToStringArray()).To(ContainElement("STEP 1/1: FROM scratch"))
// Assumes CITEST_IMAGE repo is quay.io
Expect(session.OutputToString()).NotTo(ContainSubstring("quay"))
})

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())

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

session := podmanTest.Podman([]string{"build", "--pull-never", "build/basicalpine", "--iidfile", targetFile})
session := podmanTest.Podman([]string{"build", "-q", "--pull-never", "build/basicalpine", "--iidfile", targetFile})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
id, _ := os.ReadFile(targetFile)
idFromBuild := session.OutputToString()
idFromFile, _ := os.ReadFile(targetFile)

// Verify that id is correct
inspect := podmanTest.Podman([]string{"inspect", string(id)})
inspect.WaitWithDefaultTimeout()
data := inspect.InspectImageJSON()
Expect("sha256:" + data[0].ID).To(Equal(string(id)))
// Give or take the "sha256:" prefix
Expect(string(idFromFile)).To(Equal("sha256:"+idFromBuild), "iidfile contents match output of podman build")
})

It("podman Test PATH and reserved annotation in built image", func() {
Expand Down Expand Up @@ -439,33 +451,21 @@ RUN find /test`, CITEST_IMAGE)
})

It("podman remote build must not allow symlink for ignore files", func() {
// Create a random file where symlink must be resolved
// but build should not be able to access it.
// dockerignore file outside the context directory
privateFile := filepath.Join(podmanTest.TempDir, "private_file")
f, err := os.Create(privateFile)
Expect(err).ToNot(HaveOccurred())
// Mark hello to be ignored in outerfile, but it should not be ignored.
_, err = f.WriteString("hello\n")
Expect(err).ToNot(HaveOccurred())
defer f.Close()
f.Close()

// Create .dockerignore which is a symlink to /tmp/.../private_file outside of the context dir.
currentDir, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
ignoreFile := filepath.Join(currentDir, "build/containerignore-symlink/.dockerignore")
// Link to .dockerignore outside context dir
contextDir := tempContextDirectory(podmanTest, "containerignore-symlink")
ignoreFile := filepath.Join(contextDir, ".dockerignore")
err = os.Symlink(privateFile, ignoreFile)
Expect(err).ToNot(HaveOccurred())
// Remove created .dockerignore for this test when test ends.
defer os.Remove(ignoreFile)

if IsRemote() {
podmanTest.StopRemoteService()
podmanTest.StartRemoteService()
} else {
Skip("Only valid at remote test")
}

session := podmanTest.Podman([]string{"build", "--pull-never", "-t", "test", "build/containerignore-symlink/"})
session := podmanTest.Podman([]string{"build", "--pull-never", "-t", "test", contextDir})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())

Expand Down
10 changes: 0 additions & 10 deletions test/e2e/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"strconv"
"strings"
"sync"
"syscall"
"testing"
"time"

Expand Down Expand Up @@ -1430,11 +1429,6 @@ func CopyDirectory(srcDir, dest string) error {
return err
}

stat, ok := fileInfo.Sys().(*syscall.Stat_t)
if !ok {
return fmt.Errorf("failed to get raw syscall.Stat_t data for %q", sourcePath)
}

switch fileInfo.Mode() & os.ModeType {
case os.ModeDir:
if err := os.MkdirAll(destPath, 0755); err != nil {
Expand All @@ -1453,10 +1447,6 @@ func CopyDirectory(srcDir, dest string) error {
}
}

if err := os.Lchown(destPath, int(stat.Uid), int(stat.Gid)); err != nil {
return err
}

fInfo, err := entry.Info()
if err != nil {
return err
Expand Down
23 changes: 16 additions & 7 deletions test/e2e/generate_systemd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ var _ = Describe("Podman generate systemd", func() {
})

It("podman generate systemd --files --name", func() {
// We need to cd to a writable directory
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(podmanTest.TempDir)).To(Succeed())
defer func() {
Expect(os.Chdir(cwd)).To(Succeed())
}()

n := podmanTest.Podman([]string{"run", "--name", "nginx", "-dt", NGINX_IMAGE})
n.WaitWithDefaultTimeout()
Expect(n).Should(Exit(0))
Expand All @@ -105,9 +113,6 @@ var _ = Describe("Podman generate systemd", func() {
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

for _, file := range session.OutputToStringArray() {
os.Remove(file)
}
Expect(session.OutputToString()).To(ContainSubstring("/container-nginx.service"))
})

Expand Down Expand Up @@ -228,6 +233,14 @@ var _ = Describe("Podman generate systemd", func() {
})

It("podman generate systemd pod --name --files", func() {
// We need to cd to a writable directory
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expect(os.Chdir(podmanTest.TempDir)).To(Succeed())
defer func() {
Expect(os.Chdir(cwd)).To(Succeed())
}()

n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"})
n.WaitWithDefaultTimeout()
Expect(n).Should(Exit(0))
Expand All @@ -240,10 +253,6 @@ var _ = Describe("Podman generate systemd", func() {
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

for _, file := range session.OutputToStringArray() {
os.Remove(file)
}

Expect(session.OutputToString()).To(ContainSubstring("/pod-foo.service"))
Expect(session.OutputToString()).To(ContainSubstring("/container-foo-1.service"))
})
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/login_logout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var _ = Describe("Podman login and logout", func() {
"-e", strings.Join([]string{"REGISTRY_HTTP_ADDR=0.0.0.0", strconv.Itoa(port)}, ":"), "--name", "registry", "-v",
strings.Join([]string{authPath, "/auth:Z"}, ":"), "-e", "REGISTRY_AUTH=htpasswd", "-e",
"REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm", "-e", "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd",
"-v", strings.Join([]string{certPath, "/certs:Z"}, ":"), "-e", "REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt",
"-v", strings.Join([]string{certDirPath, "/certs:Z"}, ":"), "-e", "REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt",
"-e", "REGISTRY_HTTP_TLS_KEY=/certs/domain.key", REGISTRY_IMAGE})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/run_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ VOLUME /test/`, ALPINE)

session = podmanTest.Podman([]string{"run", "--rm", "-v", ".:/app:O", ALPINE, "ls", "/app"})
session.WaitWithDefaultTimeout()
Expect(session.OutputToString()).To(ContainSubstring(filepath.Base(CurrentSpecReport().FileName())))
Expect(session.OutputToString()).To(ContainSubstring(" quadlet "))
Expect(session).Should(ExitCleanly())

// Make sure modifications in container do not show up on host
Expand Down

0 comments on commit f17b6de

Please sign in to comment.