Debian Codegraph @

test/join.bats

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 @test "Join namespace of a Docker container and check capabilities" {
35 ID="$(docker run --privileged -d alpine sleep 100)"
36 PID="$(docker inspect --format '{{.State.Pid}}' $ID)"
37
38 run sudo ./bin/psgo -pids $PID -join -format "pid, capeff"
39 [ "$status" -eq 0 ]
40 [[ ${lines[0]} == "PID EFFECTIVE CAPS" ]]
41 # FIXME: we don't get "full" anymore running against Docker and Podman
42 # seems to report some unknown ones. I didn't see new capabilities in
43 # the kernel source, so there may be more to it.
44 # [[ ${lines[1]} =~ "1 full" ]]
45
46 docker rm -f $ID
47 }
48
49 @test "Join namespace of a Docker container and check seccomp mode" {
50 # Run a privileged container to force seecomp to "disabled" to avoid
51 # hiccups in Travis.
52 ID="$(docker run -d --privileged alpine sleep 100)"
53 PID="$(docker inspect --format '{{.State.Pid}}' $ID)"
54
55 run sudo ./bin/psgo -pids $PID --join -format "pid, seccomp"
56 [ "$status" -eq 0 ]
57 [[ ${lines[0]} == "PID SECCOMP" ]]
58 [[ ${lines[1]} =~ "1 disabled" ]]
59
60 docker rm -f $ID
61 }
62
63 @test "Join namespace of a Docker container and extract host PID" {
64 ID="$(docker run -d alpine sleep 100)"
65 PID="$(docker inspect --format '{{.State.Pid}}' $ID)"
66
67 run sudo ./bin/psgo -pids $PID -join -format "pid, hpid"
68 [ "$status" -eq 0 ]
69 [[ ${lines[0]} == "PID HPID" ]]
70 [[ ${lines[1]} =~ "1 $PID" ]]
71
72 docker rm -f $ID
73 }
74
75 @test "Join namespace of a Docker container and extract effective host user ID" {
76 ID="$(docker run -d alpine sleep 100)"
77 PID="$(docker inspect --format '{{.State.Pid}}' $ID)"
78
79 run sudo ./bin/psgo -pids $PID -join -format "pid, huser"
80 [ "$status" -eq 0 ]
81 [[ ${lines[0]} == "PID HUSER" ]]
82 [[ ${lines[1]} =~ "1 root" ]]
83
84 docker rm -f $ID
85 }
86
87 @test "Join namespace of a Podman container and extract pid, {host,}user and group with {g,u}idmap" {
88 enabled=$(is_podman_available)
89 if [[ "$enabled" -eq 0 ]]; then
90 skip "skip this test since Podman is not available."
91 fi
92
93 ID="$(sudo podman run -d --uidmap=0:300000:70000 --gidmap=0:100000:70000 alpine sleep 100)"
94 PID="$(sudo podman inspect --format '{{.State.Pid}}' $ID)"
95
96 run sudo ./bin/psgo -pids $PID -join -format "pid, user, huser, group, hgroup"
97 [ "$status" -eq 0 ]
98 [[ ${lines[0]} == "PID USER HUSER GROUP HGROUP" ]]
99 [[ ${lines[1]} =~ "1 root 300000 root 100000" ]]
100
101 sudo podman rm -f $ID
102 }
103
104 @test "Join namespace of a Docker container and extract effective host group ID" {
105 ID="$(docker run -d alpine sleep 100)"
106 PID="$(docker inspect --format '{{.State.Pid}}' $ID)"
107
108 run sudo ./bin/psgo -pids $PID -join -format "pid, hgroup"
109 [ "$status" -eq 0 ]
110 [[ ${lines[0]} == "PID HGROUP" ]]
111 [[ ${lines[1]} =~ "1 root" ]]
112
113 docker rm -f $ID
114 }
115
116 @test "Join namespace of a Docker container and check the process state" {
117 ID="$(docker run -d alpine sleep 100)"
118 PID="$(docker inspect --format '{{.State.Pid}}' $ID)"
119
120 run sudo ./bin/psgo -pids $PID -join -format "pid, state"
121 [ "$status" -eq 0 ]
122 [[ ${lines[0]} == "PID STATE" ]]
123 [[ ${lines[1]} =~ "1 S" ]]
124
125 docker rm -f $ID
126 }
127
128 @test "Run Podman pod and check for redundant entries" {
129 enabled=$(is_podman_available)
130 if [[ "$enabled" -eq 0 ]]; then
131 skip "skip this test since Podman is not available."
132 fi
133
134 POD_ID="$(sudo podman pod create)"
135 ID_1="$(sudo podman run --pod $POD_ID -d alpine sleep 111)"
136 PID_1="$(sudo podman inspect --format '{{.State.Pid}}' $ID_1)"
137 ID_2="$(sudo podman run --pod $POD_ID -d alpine sleep 222)"
138 PID_2="$(sudo podman inspect --format '{{.State.Pid}}' $ID_2)"
139
140 # The underlying idea is that is that we had redundant entries if
141 # the detection of PID namespaces wouldn't work correctly.
142 run sudo ./bin/psgo -pids "$PID_1,$PID_2" -join -format "pid, args"
143 [ "$status" -eq 0 ]
144 [[ ${lines[0]} == "PID COMMAND" ]]
145 [[ ${lines[1]} =~ "1 sleep 111" ]]
146 [[ ${lines[2]} =~ "1 sleep 222" ]]
147 [[ ${lines[3]} = "" ]]
148
149 sudo podman rm -f $ID_1 $ID_2
150 sudo podman pod rm $POD_ID
151 }
152
153 @test "Test fill-mappings" {
154 if [[ ! -z "$TRAVIS" ]]; then
155 skip "Travis doesn't like this test"
156 fi
157
158 run unshare -muinpfr --mount-proc true
159 if [[ "$status" -ne 0 ]]; then
160 skip "unshare doesn't support all the needed options"
161 fi
162
163 unshare -muinpfr --mount-proc sleep 20 &
164
165 PID=$(echo $!)
166 run nsenter --preserve-credentials -U -t $PID ./bin/psgo -pids $PID -join -fill-mappings -format huser
167 kill -9 $PID
168 [ "$status" -eq 0 ]
169 [[ ${lines[0]} != "root" ]]
170 }