Skip to content

Commit

Permalink
Merge pull request #25097 from mtrmac/PodmanOptions
Browse files Browse the repository at this point in the history
Refactor Podman E2E helpers to allow passing/adding more options to the low-level executor
  • Loading branch information
openshift-merge-bot[bot] authored Jan 23, 2025
2 parents c0ec44f + 4bdb947 commit c8fc73e
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 90 deletions.
4 changes: 2 additions & 2 deletions test/e2e/checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1559,8 +1559,8 @@ var _ = Describe("Podman checkpoint", func() {
// Prevent --runtime arg from being set to force using default
// runtime unless explicitly set through passed args.
preservedMakeOptions := podmanTest.PodmanMakeOptions
podmanTest.PodmanMakeOptions = func(args []string, noEvents, noCache bool) []string {
defaultArgs := preservedMakeOptions(args, noEvents, noCache)
podmanTest.PodmanMakeOptions = func(args []string, options PodmanExecOptions) []string {
defaultArgs := preservedMakeOptions(args, options)
for i := range args {
// Runtime is set explicitly, so we should keep --runtime arg.
if args[i] == "--runtime" {
Expand Down
38 changes: 29 additions & 9 deletions test/e2e/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,10 @@ func (p *PodmanTestIntegration) pullImage(image string, toCache bool) {
}()
}
for try := 0; try < 3; try++ {
podmanSession := p.PodmanBase([]string{"pull", image}, toCache, true)
podmanSession := p.PodmanExecBaseWithOptions([]string{"pull", image}, PodmanExecOptions{
NoEvents: toCache,
NoCache: true,
})
pull := PodmanSessionIntegration{podmanSession}
pull.Wait(440)
if pull.ExitCode() == 0 {
Expand Down Expand Up @@ -489,7 +492,14 @@ func (s *PodmanSessionIntegration) InspectImageJSON() []inspect.ImageData {
// It returns the session (to allow consuming output if desired).
func (p *PodmanTestIntegration) PodmanExitCleanly(args ...string) *PodmanSessionIntegration {
GinkgoHelper()
session := p.Podman(args)
return p.PodmanExitCleanlyWithOptions(PodmanExecOptions{}, args...)
}

// PodmanExitCleanlyWithOptions runs a podman command with (optinos, args), and expects it to ExitCleanly within the default timeout.
// It returns the session (to allow consuming output if desired).
func (p *PodmanTestIntegration) PodmanExitCleanlyWithOptions(options PodmanExecOptions, args ...string) *PodmanSessionIntegration {
GinkgoHelper()
session := p.PodmanWithOptions(options, args...)
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
return session
Expand Down Expand Up @@ -679,7 +689,7 @@ func (p *PodmanTestIntegration) BuildImageWithLabel(dockerfile, imageName string

// PodmanPID execs podman and returns its PID
func (p *PodmanTestIntegration) PodmanPID(args []string) (*PodmanSessionIntegration, int) {
podmanOptions := p.MakeOptions(args, false, false)
podmanOptions := p.MakeOptions(args, PodmanExecOptions{})
GinkgoWriter.Printf("Running: %s %s\n", p.PodmanBinary, strings.Join(podmanOptions, " "))

command := exec.Command(p.PodmanBinary, podmanOptions...)
Expand Down Expand Up @@ -1060,7 +1070,12 @@ func SkipIfNetavark(p *PodmanTestIntegration) {

// PodmanAsUser is the exec call to podman on the filesystem with the specified uid/gid and environment
func (p *PodmanTestIntegration) PodmanAsUser(args []string, uid, gid uint32, cwd string, env []string) *PodmanSessionIntegration {
podmanSession := p.PodmanAsUserBase(args, uid, gid, cwd, env, false, false, nil, nil)
podmanSession := p.PodmanExecBaseWithOptions(args, PodmanExecOptions{
UID: uid,
GID: gid,
CWD: cwd,
Env: env,
})
return &PodmanSessionIntegration{podmanSession}
}

Expand Down Expand Up @@ -1119,7 +1134,9 @@ func rmAll(podmanBin string, path string) {

// PodmanNoCache calls the podman command with no configured imagecache
func (p *PodmanTestIntegration) PodmanNoCache(args []string) *PodmanSessionIntegration {
podmanSession := p.PodmanBase(args, false, true)
podmanSession := p.PodmanExecBaseWithOptions(args, PodmanExecOptions{
NoCache: true,
})
return &PodmanSessionIntegration{podmanSession}
}

Expand All @@ -1130,12 +1147,15 @@ func PodmanTestSetup(tempDir string) *PodmanTestIntegration {
// PodmanNoEvents calls the Podman command without an imagecache and without an
// events backend. It is used mostly for caching and uncaching images.
func (p *PodmanTestIntegration) PodmanNoEvents(args []string) *PodmanSessionIntegration {
podmanSession := p.PodmanBase(args, true, true)
podmanSession := p.PodmanExecBaseWithOptions(args, PodmanExecOptions{
NoEvents: true,
NoCache: true,
})
return &PodmanSessionIntegration{podmanSession}
}

// MakeOptions assembles all the podman main options
func (p *PodmanTestIntegration) makeOptions(args []string, noEvents, noCache bool) []string {
func (p *PodmanTestIntegration) makeOptions(args []string, options PodmanExecOptions) []string {
if p.RemoteTest {
if !slices.Contains(args, "--remote") {
return append([]string{"--remote", "--url", p.RemoteSocket}, args...)
Expand All @@ -1149,15 +1169,15 @@ func (p *PodmanTestIntegration) makeOptions(args []string, noEvents, noCache boo
}

eventsType := "file"
if noEvents {
if options.NoEvents {
eventsType = "none"
}

podmanOptions := strings.Split(fmt.Sprintf("%s--root %s --runroot %s --runtime %s --conmon %s --network-config-dir %s --network-backend %s --cgroup-manager %s --tmpdir %s --events-backend %s --db-backend %s",
debug, p.Root, p.RunRoot, p.OCIRuntime, p.ConmonBinary, p.NetworkConfigDir, p.NetworkBackend.ToString(), p.CgroupManager, p.TmpDir, eventsType, p.DatabaseBackend), " ")

podmanOptions = append(podmanOptions, strings.Split(p.StorageOptions, " ")...)
if !noCache {
if !options.NoCache {
cacheOptions := []string{"--storage-opt",
fmt.Sprintf("%s.imagestore=%s", p.PodmanTest.ImageCacheFS, p.PodmanTest.ImageCacheDir)}
podmanOptions = append(cacheOptions, podmanOptions...)
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,9 @@ var _ = Describe("Podman exec", func() {
files := []*os.File{
devNull,
}
session := podmanTest.PodmanExtraFiles([]string{"exec", "--preserve-fds", "1", "test1", "ls"}, files)
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
podmanTest.PodmanExitCleanlyWithOptions(PodmanExecOptions{
ExtraFiles: files,
}, "exec", "--preserve-fds", "1", "test1", "ls")
})

It("podman exec preserves --group-add groups", func() {
Expand Down
28 changes: 7 additions & 21 deletions test/e2e/libpod_suite_remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"syscall"
"time"

. "github.com/containers/podman/v5/test/utils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand All @@ -21,30 +22,15 @@ func IsRemote() bool {
return true
}

// Podman is the exec call to podman on the filesystem
// Podman executes podman on the filesystem with default options.
func (p *PodmanTestIntegration) Podman(args []string) *PodmanSessionIntegration {
args = p.makeOptions(args, false, false)
podmanSession := p.PodmanBase(args, false, false)
return &PodmanSessionIntegration{podmanSession}
}

// PodmanSystemdScope runs the podman command in a new systemd scope
func (p *PodmanTestIntegration) PodmanSystemdScope(args []string) *PodmanSessionIntegration {
args = p.makeOptions(args, false, false)

wrapper := []string{"systemd-run", "--scope"}
if isRootless() {
wrapper = []string{"systemd-run", "--scope", "--user"}
}

podmanSession := p.PodmanAsUserBase(args, 0, 0, "", nil, false, false, wrapper, nil)
return &PodmanSessionIntegration{podmanSession}
return p.PodmanWithOptions(PodmanExecOptions{}, args...)
}

// PodmanExtraFiles is the exec call to podman on the filesystem and passes down extra files
func (p *PodmanTestIntegration) PodmanExtraFiles(args []string, extraFiles []*os.File) *PodmanSessionIntegration {
args = p.makeOptions(args, false, false)
podmanSession := p.PodmanAsUserBase(args, 0, 0, "", nil, false, false, nil, extraFiles)
// PodmanWithOptions executes podman on the filesystem with the supplied options.
func (p *PodmanTestIntegration) PodmanWithOptions(options PodmanExecOptions, args ...string) *PodmanSessionIntegration {
args = p.makeOptions(args, options)
podmanSession := p.PodmanExecBaseWithOptions(args, options)
return &PodmanSessionIntegration{podmanSession}
}

Expand Down
22 changes: 6 additions & 16 deletions test/e2e/libpod_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"

. "github.com/containers/podman/v5/test/utils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand All @@ -14,25 +15,14 @@ func IsRemote() bool {
return false
}

// Podman is the exec call to podman on the filesystem
// Podman executes podman on the filesystem with default options.
func (p *PodmanTestIntegration) Podman(args []string) *PodmanSessionIntegration {
podmanSession := p.PodmanBase(args, false, false)
return &PodmanSessionIntegration{podmanSession}
}

// PodmanSystemdScope runs the podman command in a new systemd scope
func (p *PodmanTestIntegration) PodmanSystemdScope(args []string) *PodmanSessionIntegration {
wrapper := []string{"systemd-run", "--scope"}
if isRootless() {
wrapper = []string{"systemd-run", "--scope", "--user"}
}
podmanSession := p.PodmanAsUserBase(args, 0, 0, "", nil, false, false, wrapper, nil)
return &PodmanSessionIntegration{podmanSession}
return p.PodmanWithOptions(PodmanExecOptions{}, args...)
}

// PodmanExtraFiles is the exec call to podman on the filesystem and passes down extra files
func (p *PodmanTestIntegration) PodmanExtraFiles(args []string, extraFiles []*os.File) *PodmanSessionIntegration {
podmanSession := p.PodmanAsUserBase(args, 0, 0, "", nil, false, false, nil, extraFiles)
// PodmanWithOptions executes podman on the filesystem with the supplied options.
func (p *PodmanTestIntegration) PodmanWithOptions(options PodmanExecOptions, args ...string) *PodmanSessionIntegration {
podmanSession := p.PodmanExecBaseWithOptions(args, options)
return &PodmanSessionIntegration{podmanSession}
}

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ RUN touch /file
It("authenticated push", func() {
registryOptions := &podmanRegistry.Options{
PodmanPath: podmanTest.PodmanBinary,
PodmanArgs: podmanTest.MakeOptions(nil, false, false),
PodmanArgs: podmanTest.MakeOptions(nil, PodmanExecOptions{}),
Image: "docker-archive:" + imageTarPath(REGISTRY_IMAGE),
}

Expand Down
4 changes: 2 additions & 2 deletions test/e2e/mount_rootless_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var _ = Describe("Podman mount", func() {

// command: podman <options> unshare podman <options> mount cid
args := []string{"unshare", podmanTest.PodmanBinary}
opts := podmanTest.PodmanMakeOptions([]string{"mount", cid}, false, false)
opts := podmanTest.PodmanMakeOptions([]string{"mount", cid}, PodmanExecOptions{})
args = append(args, opts...)

// container root file system location is podmanTest.TempDir/...
Expand All @@ -59,7 +59,7 @@ var _ = Describe("Podman mount", func() {

// command: podman <options> unshare podman <options> image mount IMAGE
args := []string{"unshare", podmanTest.PodmanBinary}
opts := podmanTest.PodmanMakeOptions([]string{"image", "mount", CITEST_IMAGE}, false, false)
opts := podmanTest.PodmanMakeOptions([]string{"image", "mount", CITEST_IMAGE}, PodmanExecOptions{})
args = append(args, opts...)

// image location is podmanTest.TempDir/... because "--root podmanTest.TempDir/..."
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/run_cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var _ = Describe("Podman run exit", func() {

// command: podman <options> unshare podman <options> image mount ALPINE
args := []string{"unshare", podmanTest.PodmanBinary}
opts := podmanTest.PodmanMakeOptions([]string{"mount", "--no-trunc"}, false, false)
opts := podmanTest.PodmanMakeOptions([]string{"mount", "--no-trunc"}, PodmanExecOptions{})
args = append(args, opts...)

pmount := podmanTest.Podman(args)
Expand Down
14 changes: 11 additions & 3 deletions test/e2e/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,13 @@ VOLUME %s`, ALPINE, volPath, volPath)
}
}

container := podmanTest.PodmanSystemdScope([]string{"run", "--rm", "--cgroups=split", ALPINE, "cat", "/proc/self/cgroup"})
scopeOptions := PodmanExecOptions{
Wrapper: []string{"systemd-run", "--scope"},
}
if isRootless() {
scopeOptions.Wrapper = append(scopeOptions.Wrapper, "--user")
}
container := podmanTest.PodmanWithOptions(scopeOptions, "run", "--rm", "--cgroups=split", ALPINE, "cat", "/proc/self/cgroup")
container.WaitWithDefaultTimeout()
Expect(container).Should(Exit(0))
checkLines(container.OutputToStringArray())
Expand All @@ -1713,7 +1719,7 @@ VOLUME %s`, ALPINE, volPath, volPath)
ContainSubstring("Running as unit: "))) // systemd >= 255

// check that --cgroups=split is honored also when a container runs in a pod
container = podmanTest.PodmanSystemdScope([]string{"run", "--rm", "--pod", "new:split-test-pod", "--cgroups=split", ALPINE, "cat", "/proc/self/cgroup"})
container = podmanTest.PodmanWithOptions(scopeOptions, "run", "--rm", "--pod", "new:split-test-pod", "--cgroups=split", ALPINE, "cat", "/proc/self/cgroup")
container.WaitWithDefaultTimeout()
Expect(container).Should(Exit(0))
checkLines(container.OutputToStringArray())
Expand Down Expand Up @@ -1849,7 +1855,9 @@ VOLUME %s`, ALPINE, volPath, volPath)
files := []*os.File{
devNull,
}
session := podmanTest.PodmanExtraFiles([]string{"run", "--preserve-fds", "1", ALPINE, "ls"}, files)
session := podmanTest.PodmanWithOptions(PodmanExecOptions{
ExtraFiles: files,
}, "run", "--preserve-fds", "1", ALPINE, "ls")
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
})
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/systemd_activate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var _ = Describe("Systemd activate", func() {
Expect(err).ToNot(HaveOccurred())
addr := net.JoinHostPort(host, strconv.Itoa(port))

podmanOptions := podmanTest.makeOptions(nil, false, false)
podmanOptions := podmanTest.makeOptions(nil, testUtils.PodmanExecOptions{})

systemdArgs := []string{
"-E", "http_proxy", "-E", "https_proxy", "-E", "no_proxy",
Expand Down
Loading

0 comments on commit c8fc73e

Please sign in to comment.