| 1 |
#!/usr/bin/env bats -t |
| 2 |
|
| 3 |
function is_podman_available() { |
| 4 |
if podman help >> /dev/null; then |
| 5 |
echo 1 |
| 6 |
return |
| 7 |
fi |
| 8 |
echo 0 |
| 9 |
} |
| 10 |
|
| 11 |
@test "Join namespace of a Docker container" { |
| 12 |
ID="$(docker run -d alpine sleep 100)" |
| 13 |
PID="$(docker inspect --format '{{.State.Pid}}' $ID)" |
| 14 |
|
| 15 |
run sudo ./bin/psgo -pids $PID -join |
| 16 |
[ "$status" -eq 0 ] |
| 17 |
[[ ${lines[1]} =~ "sleep" ]] |
| 18 |
|
| 19 |
docker rm -f $ID |
| 20 |
} |
| 21 |
|
| 22 |
@test "Join namespace of a Docker container and format" { |
| 23 |
ID="$(docker run -d alpine sleep 100)" |
| 24 |
PID="$(docker inspect --format '{{.State.Pid}}' $ID)" |
| 25 |
|
| 26 |
run sudo ./bin/psgo -pids $PID -join -format "pid, group, args" |
| 27 |
[ "$status" -eq 0 ] |
| 28 |
[[ ${lines[0]} == "PID GROUP COMMAND" ]] |
| 29 |
[[ ${lines[1]} =~ "1 root sleep 100" ]] |
| 30 |
|
| 31 |
docker rm -f $ID |
| 32 |
} |
| 33 |
|
| •34 |
# in Debian, we don't get "full", but instead (not sure why): |
| 35 |
# PID EFFECTIVE CAPS |
| 36 |
# 1 AUDIT_CONTROL,AUDIT_READ,AUDIT_WRITE,BLOCK_SUSPEND,CHOWN,DAC_OVERRIDE,DAC_READ_SEARCH,FOWNER,FSETID,IPC_LOCK,IPC_OWNER,KILL,LEASE,LINUX_IMMUTABLE,MAC_ADMIN,MAC_OVERRIDE,MKNOD,NET_ADMIN,NET_BIND_SERVICE,NET_BROADCAST,NET_RAW,SETFCAP,SETGID,SETPCAP,SETUID,SYSLOG,SYS_ADMIN,SYS_BOOT,SYS_CHROOT,SYS_MODULE,SYS_NICE,SYS_PACCT,SYS_PTRACE,SYS_RAWIO,SYS_RESOURCE,SYS_TIME,SYS_TTY_CONFIG,WAKE_ALARM,unknown,unknown,unknown |
| 37 |
# bats test_tags=distro-integration, debian_ignore |
| 38 |
@test "Join namespace of a Docker container and check capabilities" { |
| 39 |
ID="$(docker run --privileged -d alpine sleep 100)" |
| 40 |
PID="$(docker inspect --format '{{.State.Pid}}' $ID)" |
| 41 |
|
| 42 |
run sudo ./bin/psgo -pids $PID -join -format "pid, capeff" |
| 43 |
[ "$status" -eq 0 ] |
| 44 |
[[ ${lines[0]} == "PID EFFECTIVE CAPS" ]] |
| 45 |
# FIXME: we don't get "full" anymore running against Docker and Podman |
| 46 |
# seems to report some unknown ones. I didn't see new capabilities in |
| 47 |
# the kernel source, so there may be more to it. |
| 48 |
# [[ ${lines[1]} =~ "1 full" ]] |
| 49 |
|
| 50 |
docker rm -f $ID |
| 51 |
} |
| 52 |
|
| 53 |
@test "Join namespace of a Docker container and check seccomp mode" { |
| 54 |
# Run a privileged container to force seecomp to "disabled" to avoid |
| 55 |
# hiccups in Travis. |
| 56 |
ID="$(docker run -d --privileged alpine sleep 100)" |
| 57 |
PID="$(docker inspect --format '{{.State.Pid}}' $ID)" |
| 58 |
|
| 59 |
run sudo ./bin/psgo -pids $PID --join -format "pid, seccomp" |
| 60 |
[ "$status" -eq 0 ] |
| 61 |
[[ ${lines[0]} == "PID SECCOMP" ]] |
| 62 |
[[ ${lines[1]} =~ "1 disabled" ]] |
| 63 |
|
| 64 |
docker rm -f $ID |
| 65 |
} |
| 66 |
|
| 67 |
@test "Join namespace of a Docker container and extract host PID" { |
| 68 |
ID="$(docker run -d alpine sleep 100)" |
| 69 |
PID="$(docker inspect --format '{{.State.Pid}}' $ID)" |
| 70 |
|
| 71 |
run sudo ./bin/psgo -pids $PID -join -format "pid, hpid" |
| 72 |
[ "$status" -eq 0 ] |
| 73 |
[[ ${lines[0]} == "PID HPID" ]] |
| 74 |
[[ ${lines[1]} =~ "1 $PID" ]] |
| 75 |
|
| 76 |
docker rm -f $ID |
| 77 |
} |
| 78 |
|
| 79 |
@test "Join namespace of a Docker container and extract effective host user ID" { |
| 80 |
ID="$(docker run -d alpine sleep 100)" |
| 81 |
PID="$(docker inspect --format '{{.State.Pid}}' $ID)" |
| 82 |
|
| 83 |
run sudo ./bin/psgo -pids $PID -join -format "pid, huser" |
| 84 |
[ "$status" -eq 0 ] |
| 85 |
[[ ${lines[0]} == "PID HUSER" ]] |
| 86 |
[[ ${lines[1]} =~ "1 root" ]] |
| 87 |
|
| 88 |
docker rm -f $ID |
| 89 |
} |
| 90 |
|
| 91 |
@test "Join namespace of a Podman container and extract pid, {host,}user and group with {g,u}idmap" { |
| 92 |
enabled=$(is_podman_available) |
| 93 |
if [[ "$enabled" -eq 0 ]]; then |
| 94 |
skip "skip this test since Podman is not available." |
| 95 |
fi |
| 96 |
|
| 97 |
ID="$(sudo podman run -d --uidmap=0:300000:70000 --gidmap=0:100000:70000 alpine sleep 100)" |
| 98 |
PID="$(sudo podman inspect --format '{{.State.Pid}}' $ID)" |
| 99 |
|
| 100 |
run sudo ./bin/psgo -pids $PID -join -format "pid, user, huser, group, hgroup" |
| 101 |
[ "$status" -eq 0 ] |
| 102 |
[[ ${lines[0]} == "PID USER HUSER GROUP HGROUP" ]] |
| 103 |
[[ ${lines[1]} =~ "1 root 300000 root 100000" ]] |
| 104 |
|
| 105 |
sudo podman rm -f $ID |
| 106 |
} |
| 107 |
|
| 108 |
@test "Join namespace of a Docker container and extract effective host group ID" { |
| 109 |
ID="$(docker run -d alpine sleep 100)" |
| 110 |
PID="$(docker inspect --format '{{.State.Pid}}' $ID)" |
| 111 |
|
| 112 |
run sudo ./bin/psgo -pids $PID -join -format "pid, hgroup" |
| 113 |
[ "$status" -eq 0 ] |
| 114 |
[[ ${lines[0]} == "PID HGROUP" ]] |
| 115 |
[[ ${lines[1]} =~ "1 root" ]] |
| 116 |
|
| 117 |
docker rm -f $ID |
| 118 |
} |
| 119 |
|
| 120 |
@test "Join namespace of a Docker container and check the process state" { |
| 121 |
ID="$(docker run -d alpine sleep 100)" |
| 122 |
PID="$(docker inspect --format '{{.State.Pid}}' $ID)" |
| 123 |
|
| 124 |
run sudo ./bin/psgo -pids $PID -join -format "pid, state" |
| 125 |
[ "$status" -eq 0 ] |
| 126 |
[[ ${lines[0]} == "PID STATE" ]] |
| 127 |
[[ ${lines[1]} =~ "1 S" ]] |
| 128 |
|
| 129 |
docker rm -f $ID |
| 130 |
} |
| 131 |
|
| 132 |
@test "Run Podman pod and check for redundant entries" { |
| 133 |
enabled=$(is_podman_available) |
| 134 |
if [[ "$enabled" -eq 0 ]]; then |
| 135 |
skip "skip this test since Podman is not available." |
| 136 |
fi |
| 137 |
|
| 138 |
POD_ID="$(sudo podman pod create)" |
| 139 |
ID_1="$(sudo podman run --pod $POD_ID -d alpine sleep 111)" |
| 140 |
PID_1="$(sudo podman inspect --format '{{.State.Pid}}' $ID_1)" |
| 141 |
ID_2="$(sudo podman run --pod $POD_ID -d alpine sleep 222)" |
| 142 |
PID_2="$(sudo podman inspect --format '{{.State.Pid}}' $ID_2)" |
| 143 |
|
| 144 |
# The underlying idea is that is that we had redundant entries if |
| 145 |
# the detection of PID namespaces wouldn't work correctly. |
| 146 |
run sudo ./bin/psgo -pids "$PID_1,$PID_2" -join -format "pid, args" |
| 147 |
[ "$status" -eq 0 ] |
| 148 |
[[ ${lines[0]} == "PID COMMAND" ]] |
| 149 |
[[ ${lines[1]} =~ "1 sleep 111" ]] |
| 150 |
[[ ${lines[2]} =~ "1 sleep 222" ]] |
| 151 |
[[ ${lines[3]} = "" ]] |
| 152 |
|
| 153 |
sudo podman rm -f $ID_1 $ID_2 |
| 154 |
sudo podman pod rm $POD_ID |
| 155 |
} |
| 156 |
|
| 157 |
@test "Test fill-mappings" { |
| 158 |
if [[ ! -z "$TRAVIS" ]]; then |
| 159 |
skip "Travis doesn't like this test" |
| 160 |
fi |
| 161 |
|
| 162 |
run unshare -muinpfr --mount-proc true |
| 163 |
if [[ "$status" -ne 0 ]]; then |
| 164 |
skip "unshare doesn't support all the needed options" |
| 165 |
fi |
| 166 |
|
| 167 |
unshare -muinpfr --mount-proc sleep 20 & |
| 168 |
|
| 169 |
PID=$(echo $!) |
| 170 |
run nsenter --preserve-credentials -U -t $PID ./bin/psgo -pids $PID -join -fill-mappings -format huser |
| 171 |
kill -9 $PID |
| 172 |
[ "$status" -eq 0 ] |
| 173 |
[[ ${lines[0]} != "root" ]] |
| 174 |
} |