From 036658b84472f08ffa26cf4ea5ebcc313107faa9 Mon Sep 17 00:00:00 2001 From: umohnani8 Date: Fri, 15 Sep 2017 15:17:36 -0400 Subject: [PATCH] Modified namespace field in json struct The namespace field was not being omitted when empty Signed-off-by: umohnani8 --- cmd/kpod/ps.go | 12 ++++++------ test/kpod_ps.bats | 25 +++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/cmd/kpod/ps.go b/cmd/kpod/ps.go index 3f08a4e7..b8bc7f0e 100644 --- a/cmd/kpod/ps.go +++ b/cmd/kpod/ps.go @@ -75,16 +75,16 @@ type psJSONParams struct { Labels fields.Set `json:"labels"` Mounts []specs.Mount `json:"mounts"` ContainerRunning bool `json:"ctrRunning"` - Namespaces namespace `json:"namespace,omitempty"` + Namespaces *namespace `json:"namespace,omitempty"` } type namespace struct { - PID string `json:"ctrPID,omitempty"` + PID string `json:"pid,omitempty"` Cgroup string `json:"cgroup,omitempty"` IPC string `json:"ipc,omitempty"` MNT string `json:"mnt,omitempty"` NET string `json:"net,omitempty"` - PIDNS string `json:"pid,omitempty"` + PIDNS string `json:"pidns,omitempty"` User string `json:"user,omitempty"` UTS string `json:"uts,omitempty"` } @@ -334,7 +334,7 @@ func getTemplateOutput(containers []*libkpod.ContainerData, opts psOptions) (psO return } -func getNamespaces(pid int) namespace { +func getNamespaces(pid int) *namespace { ctrPID := strconv.Itoa(pid) cgroup, _ := getNamespaceInfo(filepath.Join("/proc", ctrPID, "ns", "cgroup")) ipc, _ := getNamespaceInfo(filepath.Join("/proc", ctrPID, "ns", "ipc")) @@ -344,7 +344,7 @@ func getNamespaces(pid int) namespace { user, _ := getNamespaceInfo(filepath.Join("/proc", ctrPID, "ns", "user")) uts, _ := getNamespaceInfo(filepath.Join("/proc", ctrPID, "ns", "uts")) - return namespace{ + return &namespace{ PID: ctrPID, Cgroup: cgroup, IPC: ipc, @@ -366,7 +366,7 @@ func getNamespaceInfo(path string) (string, error) { // getJSONOutput returns the container info in its raw form func getJSONOutput(containers []*libkpod.ContainerData, nSpace bool) (psOutput []psJSONParams) { - var ns namespace + var ns *namespace for _, ctr := range containers { if nSpace { ns = getNamespaces(ctr.State.Pid) diff --git a/test/kpod_ps.bats b/test/kpod_ps.bats index bbc01d32..cd1be60c 100644 --- a/test/kpod_ps.bats +++ b/test/kpod_ps.bats @@ -203,7 +203,7 @@ KPOD_OPTIONS="--root $ROOT --runroot $RUNROOT ${STORAGE_OPTS}" [ "$status" -eq 0 ] } -@test "kpod ps format flag = json" { +@test "kpod ps namespace flag and format flag = json" { start_crio [ "$status" -eq 0 ] run crioctl pod run --config "$TESTDATA"/sandbox_config.json @@ -215,7 +215,7 @@ KPOD_OPTIONS="--root $ROOT --runroot $RUNROOT ${STORAGE_OPTS}" run crioctl ctr create --config "$TESTDATA"/container_config.json --pod "$pod_id" echo "$output" [ "$status" -eq 0 ] - run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} ps -a --ns --format json | python -m json.tool" + run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} ps -a --ns --format json | python -m json.tool | grep namespace" echo "$output" [ "$status" -eq 0 ] cleanup_ctrs @@ -224,6 +224,27 @@ KPOD_OPTIONS="--root $ROOT --runroot $RUNROOT ${STORAGE_OPTS}" [ "$status" -eq 0 ] } +@test "kpod ps without namespace flag and format flag = json" { + start_crio + [ "$status" -eq 0 ] + run crioctl pod run --config "$TESTDATA"/sandbox_config.json + echo "$output" + [ "$status" -eq 0 ] + pod_id="$output" + run crioctl image pull "$IMAGE" + [ "$status" -eq 0 ] + run crioctl ctr create --config "$TESTDATA"/container_config.json --pod "$pod_id" + echo "$output" + [ "$status" -eq 0 ] + run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} ps -a --format json | python -m json.tool | grep namespace" + echo "$output" + [ "$status" -eq 1 ] + cleanup_ctrs + cleanup_pods + stop_crio + [ "$status" -eq 0 ] +} + @test "kpod ps format flag = go template" { start_crio [ "$status" -eq 0 ]