Skip to content

Commit

Permalink
Merge pull request #19586 from rhatdan/ps
Browse files Browse the repository at this point in the history
Fix output of podman --remote top
  • Loading branch information
openshift-ci[bot] authored Oct 17, 2023
2 parents c909afb + 9637fed commit 91264e7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
9 changes: 5 additions & 4 deletions libpod/container_top_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,11 @@ func (c *Container) Top(descriptors []string) ([]string, error) {
return nil, psgoErr
}

// Note that the descriptors to ps(1) must be shlexed (see #12452).
psDescriptors := []string{}
for _, d := range descriptors {
shSplit, err := shlex.Split(d)
psDescriptors := descriptors
if len(descriptors) == 1 {
// Note that the descriptors to ps(1) must be shlexed (see #12452).
psDescriptors = make([]string, 0, len(descriptors))
shSplit, err := shlex.Split(descriptors[0])
if err != nil {
return nil, fmt.Errorf("parsing ps args: %w", err)
}
Expand Down
23 changes: 16 additions & 7 deletions pkg/api/handlers/compat/containers_top.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ func TopContainer(w http.ResponseWriter, r *http.Request) {
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
decoder := utils.GetDecoder(r)

psArgs := "-ef"
if utils.IsLibpodRequest(r) {
psArgs = ""
var psArgs []string
if !utils.IsLibpodRequest(r) {
psArgs = []string{"-ef"}
}
query := struct {
Delay int `schema:"delay"`
PsArgs string `schema:"ps_args"`
Stream bool `schema:"stream"`
Delay int `schema:"delay"`
PsArgs []string `schema:"ps_args"`
Stream bool `schema:"stream"`
}{
Delay: 5,
PsArgs: psArgs,
Expand All @@ -52,13 +52,22 @@ func TopContainer(w http.ResponseWriter, r *http.Request) {

encoder := json.NewEncoder(w)

args := query.PsArgs
if len(args) == 1 &&
utils.IsLibpodRequest(r) {
if _, err := utils.SupportedVersion(r, "< 4.8.0"); err == nil {
// Ugly workaround for older clients which used to send arguments comma separated.
args = strings.Split(args[0], ",")
}
}

loop: // break out of for/select infinite` loop
for {
select {
case <-r.Context().Done():
break loop
default:
output, err := c.Top(strings.Split(query.PsArgs, ","))
output, err := c.Top(args)
if err != nil {
if !statusWritten {
utils.InternalServerError(w, err)
Expand Down
5 changes: 3 additions & 2 deletions pkg/api/server/register_containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1181,8 +1181,9 @@ func (s *APIServer) registerContainersHandlers(r *mux.Router) error {
// default: 5
// - in: query
// name: ps_args
// type: string
// default:
// type: array
// items:
// type: string
// description: |
// arguments to pass to ps such as aux.
// produces:
Expand Down
6 changes: 4 additions & 2 deletions pkg/bindings/containers/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,10 @@ func Top(ctx context.Context, nameOrID string, options *TopOptions) ([]string, e
}
params := url.Values{}
if options.Changed("Descriptors") {
psArgs := strings.Join(options.GetDescriptors(), ",")
params.Add("ps_args", psArgs)
psArgs := options.GetDescriptors()
for _, arg := range psArgs {
params.Add("ps_args", arg)
}
}
response, err := conn.DoRequest(ctx, nil, http.MethodGet, "/containers/%s/top", params, nil, nameOrID)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/top_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ var _ = Describe("Podman top", func() {
result = podmanTest.Podman([]string{"top", session.OutputToString(), "ax -o args"})
result.WaitWithDefaultTimeout()
Expect(result).Should(ExitCleanly())

result = podmanTest.Podman([]string{"top", session.OutputToString(), "ax", "-o", "args"})
result.WaitWithDefaultTimeout()
Expect(result).Should(ExitCleanly())
Expect(result.OutputToStringArray()).To(Equal([]string{"COMMAND", "sleep inf"}))

// Now make sure we use ps in the container with CAP_SYS_PTRACE
Expand Down
25 changes: 25 additions & 0 deletions test/system/085-top.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bats

load helpers

@test "podman top - basic tests" {
run_podman run -d $IMAGE top
cid=$output
is "$cid" "[0-9a-f]\{64\}$"

run_podman top $cid
is "${lines[0]}" "USER[ \t]*PID[ \t]*PPID[ \t]*%CPU[ \t]*ELAPSED[ \t]*TTY[ \t]*TIME[ \t]*COMMAND" "podman top"
is "${lines[1]}" "root[ \t]*1[ \t]*0[ \t]*0.000[ \t]*" "podman top"

run_podman top $cid -eo pid,comm
is "${lines[0]}" "[ \t]*PID[ \t]*COMMAND" "podman top -eo pid,comm Heading"
is "${lines[1]}" "[ \t]*1[ \t]*top" "podman top -eo pid,comm processes"

run_podman top $cid -eo "pid comm"
is "${lines[0]}" "[ \t]*PID[ \t]*COMMAND" "podman top -eo "pid comm" Heading"
is "${lines[1]}" "[ \t]*1[ \t]*top" "podman top -eo "pid comm" processes"

run_podman rm -f $cid
}

# vim: filetype=sh

0 comments on commit 91264e7

Please sign in to comment.