mirror of
https://github.com/containers/podman.git
synced 2025-10-18 03:33:32 +08:00
Turn PodmanAsUserBase into PodmanExecBaseWithOptions
... replacing the many parameters with a struct with named fields. This makes the meaning of parameters more explicit, and more importantly it makes it easier to just edit _one_ of the parameters without requiring specialized wrappers for every single case. Should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
@ -1060,7 +1060,12 @@ func SkipIfNetavark(p *PodmanTestIntegration) {
|
|||||||
|
|
||||||
// PodmanAsUser is the exec call to podman on the filesystem with the specified uid/gid and environment
|
// 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 {
|
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}
|
return &PodmanSessionIntegration{podmanSession}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
. "github.com/containers/podman/v5/test/utils"
|
||||||
. "github.com/onsi/ginkgo/v2"
|
. "github.com/onsi/ginkgo/v2"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
)
|
)
|
||||||
@ -37,14 +38,18 @@ func (p *PodmanTestIntegration) PodmanSystemdScope(args []string) *PodmanSession
|
|||||||
wrapper = []string{"systemd-run", "--scope", "--user"}
|
wrapper = []string{"systemd-run", "--scope", "--user"}
|
||||||
}
|
}
|
||||||
|
|
||||||
podmanSession := p.PodmanAsUserBase(args, 0, 0, "", nil, false, false, wrapper, nil)
|
podmanSession := p.PodmanExecBaseWithOptions(args, PodmanExecOptions{
|
||||||
|
Wrapper: wrapper,
|
||||||
|
})
|
||||||
return &PodmanSessionIntegration{podmanSession}
|
return &PodmanSessionIntegration{podmanSession}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PodmanExtraFiles is the exec call to podman on the filesystem and passes down extra files
|
// 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 {
|
func (p *PodmanTestIntegration) PodmanExtraFiles(args []string, extraFiles []*os.File) *PodmanSessionIntegration {
|
||||||
args = p.makeOptions(args, false, false)
|
args = p.makeOptions(args, false, false)
|
||||||
podmanSession := p.PodmanAsUserBase(args, 0, 0, "", nil, false, false, nil, extraFiles)
|
podmanSession := p.PodmanExecBaseWithOptions(args, PodmanExecOptions{
|
||||||
|
ExtraFiles: extraFiles,
|
||||||
|
})
|
||||||
return &PodmanSessionIntegration{podmanSession}
|
return &PodmanSessionIntegration{podmanSession}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
. "github.com/containers/podman/v5/test/utils"
|
||||||
. "github.com/onsi/ginkgo/v2"
|
. "github.com/onsi/ginkgo/v2"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
)
|
)
|
||||||
@ -26,13 +27,17 @@ func (p *PodmanTestIntegration) PodmanSystemdScope(args []string) *PodmanSession
|
|||||||
if isRootless() {
|
if isRootless() {
|
||||||
wrapper = []string{"systemd-run", "--scope", "--user"}
|
wrapper = []string{"systemd-run", "--scope", "--user"}
|
||||||
}
|
}
|
||||||
podmanSession := p.PodmanAsUserBase(args, 0, 0, "", nil, false, false, wrapper, nil)
|
podmanSession := p.PodmanExecBaseWithOptions(args, PodmanExecOptions{
|
||||||
|
Wrapper: wrapper,
|
||||||
|
})
|
||||||
return &PodmanSessionIntegration{podmanSession}
|
return &PodmanSessionIntegration{podmanSession}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PodmanExtraFiles is the exec call to podman on the filesystem and passes down extra files
|
// 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 {
|
func (p *PodmanTestIntegration) PodmanExtraFiles(args []string, extraFiles []*os.File) *PodmanSessionIntegration {
|
||||||
podmanSession := p.PodmanAsUserBase(args, 0, 0, "", nil, false, false, nil, extraFiles)
|
podmanSession := p.PodmanExecBaseWithOptions(args, PodmanExecOptions{
|
||||||
|
ExtraFiles: extraFiles,
|
||||||
|
})
|
||||||
return &PodmanSessionIntegration{podmanSession}
|
return &PodmanSessionIntegration{podmanSession}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,16 +94,28 @@ func (p *PodmanTest) MakeOptions(args []string, noEvents, noCache bool) []string
|
|||||||
return p.PodmanMakeOptions(args, noEvents, noCache)
|
return p.PodmanMakeOptions(args, noEvents, noCache)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PodmanAsUserBase exec podman as user. uid and gid is set for credentials usage. env is used
|
// PodmanExecOptions modify behavior of PodmanTest.PodmanExecBaseWithOptions and its callers.
|
||||||
// to record the env for debugging
|
// Users should typically leave most fields default-initialized, and only set those that are relevant to them.
|
||||||
func (p *PodmanTest) PodmanAsUserBase(args []string, uid, gid uint32, cwd string, env []string, noEvents, noCache bool, wrapper []string, extraFiles []*os.File) *PodmanSession {
|
type PodmanExecOptions struct {
|
||||||
|
UID, GID uint32 // default: inherited form the current process
|
||||||
|
CWD string // default: inherited form the current process
|
||||||
|
Env []string // default: inherited form the current process
|
||||||
|
NoEvents bool
|
||||||
|
NoCache bool
|
||||||
|
Wrapper []string // A command to run, receiving the Podman command line. default: none
|
||||||
|
ExtraFiles []*os.File
|
||||||
|
}
|
||||||
|
|
||||||
|
// PodmanExecBaseWithOptions execs podman with the specified args, and in an environment defined by options
|
||||||
|
func (p *PodmanTest) PodmanExecBaseWithOptions(args []string, options PodmanExecOptions) *PodmanSession {
|
||||||
var command *exec.Cmd
|
var command *exec.Cmd
|
||||||
podmanOptions := p.MakeOptions(args, noEvents, noCache)
|
podmanOptions := p.MakeOptions(args, options.NoEvents, options.NoCache)
|
||||||
podmanBinary := p.PodmanBinary
|
podmanBinary := p.PodmanBinary
|
||||||
if p.RemoteTest {
|
if p.RemoteTest {
|
||||||
podmanBinary = p.RemotePodmanBinary
|
podmanBinary = p.RemotePodmanBinary
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runCmd := options.Wrapper
|
||||||
if timeDir := os.Getenv(EnvTimeDir); timeDir != "" {
|
if timeDir := os.Getenv(EnvTimeDir); timeDir != "" {
|
||||||
timeFile, err := os.CreateTemp(timeDir, ".time")
|
timeFile, err := os.CreateTemp(timeDir, ".time")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -111,18 +123,17 @@ func (p *PodmanTest) PodmanAsUserBase(args []string, uid, gid uint32, cwd string
|
|||||||
}
|
}
|
||||||
timeArgs := []string{"-f", "%M", "-o", timeFile.Name()}
|
timeArgs := []string{"-f", "%M", "-o", timeFile.Name()}
|
||||||
timeCmd := append([]string{"/usr/bin/time"}, timeArgs...)
|
timeCmd := append([]string{"/usr/bin/time"}, timeArgs...)
|
||||||
wrapper = append(timeCmd, wrapper...)
|
runCmd = append(timeCmd, runCmd...)
|
||||||
}
|
}
|
||||||
runCmd := wrapper
|
|
||||||
runCmd = append(runCmd, podmanBinary)
|
runCmd = append(runCmd, podmanBinary)
|
||||||
|
|
||||||
if env == nil {
|
if options.Env == nil {
|
||||||
GinkgoWriter.Printf("Running: %s %s\n", strings.Join(runCmd, " "), strings.Join(podmanOptions, " "))
|
GinkgoWriter.Printf("Running: %s %s\n", strings.Join(runCmd, " "), strings.Join(podmanOptions, " "))
|
||||||
} else {
|
} else {
|
||||||
GinkgoWriter.Printf("Running: (env: %v) %s %s\n", env, strings.Join(runCmd, " "), strings.Join(podmanOptions, " "))
|
GinkgoWriter.Printf("Running: (env: %v) %s %s\n", options.Env, strings.Join(runCmd, " "), strings.Join(podmanOptions, " "))
|
||||||
}
|
}
|
||||||
if uid != 0 || gid != 0 {
|
if options.UID != 0 || options.GID != 0 {
|
||||||
pythonCmd := fmt.Sprintf("import os; import sys; uid = %d; gid = %d; cwd = '%s'; os.setgid(gid); os.setuid(uid); os.chdir(cwd) if len(cwd)>0 else True; os.execv(sys.argv[1], sys.argv[1:])", gid, uid, cwd)
|
pythonCmd := fmt.Sprintf("import os; import sys; uid = %d; gid = %d; cwd = '%s'; os.setgid(gid); os.setuid(uid); os.chdir(cwd) if len(cwd)>0 else True; os.execv(sys.argv[1], sys.argv[1:])", options.GID, options.UID, options.CWD)
|
||||||
runCmd = append(runCmd, podmanOptions...)
|
runCmd = append(runCmd, podmanOptions...)
|
||||||
nsEnterOpts := append([]string{"-c", pythonCmd}, runCmd...)
|
nsEnterOpts := append([]string{"-c", pythonCmd}, runCmd...)
|
||||||
command = exec.Command("python", nsEnterOpts...)
|
command = exec.Command("python", nsEnterOpts...)
|
||||||
@ -130,14 +141,14 @@ func (p *PodmanTest) PodmanAsUserBase(args []string, uid, gid uint32, cwd string
|
|||||||
runCmd = append(runCmd, podmanOptions...)
|
runCmd = append(runCmd, podmanOptions...)
|
||||||
command = exec.Command(runCmd[0], runCmd[1:]...)
|
command = exec.Command(runCmd[0], runCmd[1:]...)
|
||||||
}
|
}
|
||||||
if env != nil {
|
if options.Env != nil {
|
||||||
command.Env = env
|
command.Env = options.Env
|
||||||
}
|
}
|
||||||
if cwd != "" {
|
if options.CWD != "" {
|
||||||
command.Dir = cwd
|
command.Dir = options.CWD
|
||||||
}
|
}
|
||||||
|
|
||||||
command.ExtraFiles = extraFiles
|
command.ExtraFiles = options.ExtraFiles
|
||||||
|
|
||||||
session, err := Start(command, GinkgoWriter, GinkgoWriter)
|
session, err := Start(command, GinkgoWriter, GinkgoWriter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -148,7 +159,10 @@ func (p *PodmanTest) PodmanAsUserBase(args []string, uid, gid uint32, cwd string
|
|||||||
|
|
||||||
// PodmanBase exec podman with default env.
|
// PodmanBase exec podman with default env.
|
||||||
func (p *PodmanTest) PodmanBase(args []string, noEvents, noCache bool) *PodmanSession {
|
func (p *PodmanTest) PodmanBase(args []string, noEvents, noCache bool) *PodmanSession {
|
||||||
return p.PodmanAsUserBase(args, 0, 0, "", nil, noEvents, noCache, nil, nil)
|
return p.PodmanExecBaseWithOptions(args, PodmanExecOptions{
|
||||||
|
NoEvents: noEvents,
|
||||||
|
NoCache: noCache,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitForContainer waits on a started container
|
// WaitForContainer waits on a started container
|
||||||
|
Reference in New Issue
Block a user