]>
Commit | Line | Data |
---|---|---|
378db9e2 FS |
1 | #!/usr/bin/env bash |
2 | # SPDX-License-Identifier: LGPL-2.1-or-later | |
3 | # shellcheck disable=SC2016 | |
4 | set -eux | |
5 | set -o pipefail | |
6 | ||
5656759d FS |
7 | # shellcheck source=test/units/util.sh |
8 | . "$(dirname "$0")"/util.sh | |
378db9e2 | 9 | |
5656759d | 10 | export PAGER= |
378db9e2 FS |
11 | |
12 | at_exit() { | |
13 | set +e | |
14 | ||
15 | machinectl status long-running >/dev/null && machinectl kill --signal=KILL long-running | |
16 | mountpoint -q /var/lib/machines && timeout 10 sh -c "while ! umount /var/lib/machines; do sleep .5; done" | |
a1ff1ac2 | 17 | [[ -n "${NSPAWN_FRAGMENT:-}" ]] && rm -f "/etc/systemd/nspawn/$NSPAWN_FRAGMENT" "/var/lib/machines/$NSPAWN_FRAGMENT" |
28ed2326 | 18 | rm -f /run/systemd/nspawn/*.nspawn |
378db9e2 FS |
19 | } |
20 | ||
21 | trap at_exit EXIT | |
22 | ||
23 | # Mount tmpfs over /var/lib/machines to not pollute the image | |
24 | mkdir -p /var/lib/machines | |
25 | mount -t tmpfs tmpfs /var/lib/machines | |
26 | ||
27 | # Create a couple of containers we can refer to in tests | |
28 | for i in {0..4}; do | |
5656759d | 29 | create_dummy_container "/var/lib/machines/container$i" |
378db9e2 FS |
30 | machinectl start "container$i" |
31 | done | |
32 | # Create one "long running" container with some basic signal handling | |
5656759d | 33 | create_dummy_container /var/lib/machines/long-running |
378db9e2 | 34 | cat >/var/lib/machines/long-running/sbin/init <<\EOF |
5656759d FS |
35 | #!/usr/bin/bash -x |
36 | ||
378db9e2 FS |
37 | PID=0 |
38 | ||
5656759d | 39 | trap "touch /poweroff" RTMIN+4 |
378db9e2 FS |
40 | trap "touch /reboot" INT |
41 | trap "touch /trap" TRAP | |
42 | trap 'kill $PID' EXIT | |
43 | ||
44 | # We need to wait for the sleep process asynchronously in order to allow | |
45 | # bash to process signals | |
46 | sleep infinity & | |
47 | PID=$! | |
48 | while :; do | |
49 | wait || : | |
50 | done | |
51 | EOF | |
52 | machinectl start long-running | |
53 | ||
54 | machinectl | |
55 | machinectl --no-pager --help | |
56 | machinectl --version | |
57 | machinectl list | |
58 | machinectl list --no-legend --no-ask-password | |
59 | ||
60 | machinectl status long-running long-running long-running | |
61 | machinectl status --full long-running | |
62 | machinectl status --quiet --lines=1 long-running | |
63 | machinectl status --lines=0 --max-addresses=0 long-running | |
64 | machinectl status --machine=testuser@.host long-running | |
65 | machinectl status --output=help long-running | |
66 | while read -r output; do | |
67 | machinectl status --output="$output" long-running | |
68 | done < <(machinectl --output=help) | |
69 | ||
70 | machinectl show | |
71 | machinectl show --all | |
72 | machinectl show --all --machine=root@ | |
73 | machinectl show --all --machine=testuser@ | |
74 | [[ "$(machinectl show --property=PoolPath --value)" == "/var/lib/machines" ]] | |
75 | machinectl show long-running | |
76 | machinectl show long-running long-running long-running --all | |
77 | [[ "$(machinectl show --property=RootDirectory --value long-running)" == "/var/lib/machines/long-running" ]] | |
78 | ||
79 | machinectl enable long-running | |
80 | test -L /etc/systemd/system/machines.target.wants/systemd-nspawn@long-running.service | |
81 | machinectl enable long-running long-running long-running container1 | |
82 | machinectl disable long-running | |
83 | test ! -L /etc/systemd/system/machines.target.wants/systemd-nspawn@long-running.service | |
84 | machinectl disable long-running long-running long-running container1 | |
85 | ||
5656759d FS |
86 | [[ "$(machinectl shell testuser@ /usr/bin/bash -c 'echo -ne $FOO')" == "" ]] |
87 | [[ "$(machinectl shell --setenv=FOO=bar testuser@ /usr/bin/bash -c 'echo -ne $FOO')" == "bar" ]] | |
378db9e2 FS |
88 | |
89 | [[ "$(machinectl show --property=State --value long-running)" == "running" ]] | |
90 | # Equivalent to machinectl kill --signal=SIGRTMIN+4 --kill-whom=leader | |
91 | rm -f /var/lib/machines/long-running/poweroff | |
92 | machinectl poweroff long-running | |
5656759d | 93 | timeout 10 bash -c "while ! test -e /var/lib/machines/long-running/poweroff; do sleep .5; done" |
378db9e2 FS |
94 | machinectl poweroff long-running long-running long-running |
95 | # Equivalent to machinectl kill --signal=SIGINT --kill-whom=leader | |
96 | rm -f /var/lib/machines/long-running/reboot | |
97 | machinectl reboot long-running | |
5656759d | 98 | timeout 10 bash -c "while ! test -e /var/lib/machines/long-running/reboot; do sleep .5; done" |
378db9e2 FS |
99 | machinectl reboot long-running long-running long-running |
100 | # Skip machinectl terminate for now, as it doesn't play well with our "init" | |
101 | rm -f /var/lib/machines/long-running/trap | |
102 | machinectl kill --signal=SIGTRAP --kill-whom=leader long-running | |
5656759d | 103 | timeout 10 bash -c "while ! test -e /var/lib/machines/long-running/trap; do sleep .5; done" |
378db9e2 FS |
104 | machinectl kill --signal=SIGTRAP --kill-whom=leader long-running long-running long-running |
105 | # All used signals should've been caught by a handler | |
106 | [[ "$(machinectl show --property=State --value long-running)" == "running" ]] | |
107 | ||
108 | cp /etc/machine-id /tmp/foo | |
109 | machinectl copy-to long-running /tmp/foo /root/foo | |
110 | test -f /var/lib/machines/long-running/root/foo | |
111 | machinectl copy-from long-running /root/foo /tmp/bar | |
112 | diff /tmp/foo /tmp/bar | |
113 | rm -f /tmp/{foo,bar} | |
114 | ||
115 | # machinectl bind is covered by testcase_check_machinectl_bind() in nspawn tests | |
116 | ||
117 | machinectl list-images | |
118 | machinectl list-images --no-legend | |
119 | machinectl image-status | |
120 | machinectl image-status container1 | |
121 | machinectl image-status container1 container1 container{0..4} | |
122 | machinectl show-image | |
123 | machinectl show-image container1 | |
124 | machinectl show-image container1 container1 container{0..4} | |
125 | ||
126 | machinectl clone container1 clone1 | |
127 | machinectl show-image clone1 | |
128 | machinectl rename clone1 clone2 | |
129 | (! machinectl show-image clone1) | |
130 | machinectl show-image clone2 | |
131 | if lsattr -d /var/lib/machines >/dev/null; then | |
132 | [[ "$(machinectl show-image --property=ReadOnly --value clone2)" == no ]] | |
133 | machinectl read-only clone2 yes | |
134 | [[ "$(machinectl show-image --property=ReadOnly --value clone2)" == yes ]] | |
135 | machinectl read-only clone2 no | |
136 | [[ "$(machinectl show-image --property=ReadOnly --value clone2)" == no ]] | |
137 | fi | |
138 | machinectl remove clone2 | |
139 | for i in {0..4}; do | |
140 | machinectl clone container1 "clone$i" | |
141 | done | |
142 | machinectl remove clone{0..4} | |
143 | for i in {0..4}; do | |
144 | machinectl clone container1 ".hidden$i" | |
145 | done | |
146 | machinectl list-images --all | |
147 | test -d /var/lib/machines/.hidden1 | |
148 | machinectl clean | |
149 | test ! -d /var/lib/machines/.hidden1 | |
150 | ||
151 | # Prepare a simple raw container | |
152 | mkdir -p /tmp/mnt | |
153 | dd if=/dev/zero of=/tmp/container.raw bs=1M count=32 | |
154 | mkfs.ext4 /tmp/container.raw | |
155 | mount -o loop /tmp/container.raw /tmp/mnt | |
156 | cp -r /var/lib/machines/container1/* /tmp/mnt | |
157 | umount /tmp/mnt | |
158 | # Try to import it, run it, export it, and re-import it | |
159 | machinectl import-raw /tmp/container.raw container-raw | |
160 | [[ "$(machinectl show-image --property=Type --value container-raw)" == "raw" ]] | |
161 | machinectl start container-raw | |
162 | machinectl export-raw container-raw /tmp/container-export.raw | |
163 | machinectl import-raw /tmp/container-export.raw container-raw-reimport | |
164 | [[ "$(machinectl show-image --property=Type --value container-raw-reimport)" == "raw" ]] | |
165 | rm -f /tmp/container{,-export}.raw | |
166 | ||
167 | # Prepare a simple tar.gz container | |
168 | tar -pczf /tmp/container.tar.gz -C /var/lib/machines/container1 . | |
169 | # Try to import it, run it, export it, and re-import it | |
170 | machinectl import-tar /tmp/container.tar.gz container-tar | |
171 | [[ "$(machinectl show-image --property=Type --value container-tar)" == "directory" ]] | |
172 | machinectl start container-tar | |
173 | machinectl export-tar container-tar /tmp/container-export.tar.gz | |
174 | machinectl import-tar /tmp/container-export.tar.gz container-tar-reimport | |
175 | [[ "$(machinectl show-image --property=Type --value container-tar-reimport)" == "directory" ]] | |
176 | rm -f /tmp/container{,-export}.tar.gz | |
177 | ||
178 | # Try to import a container directory & run it | |
179 | cp -r /var/lib/machines/container1 /tmp/container.dir | |
180 | machinectl import-fs /tmp/container.dir container-dir | |
181 | [[ "$(machinectl show-image --property=Type --value container-dir)" == "directory" ]] | |
182 | machinectl start container-dir | |
183 | rm -fr /tmp/container.dir | |
184 | ||
5656759d | 185 | timeout 10 bash -c "while ! machinectl clean --all; do sleep .5; done" |
378db9e2 | 186 | |
a1ff1ac2 FS |
187 | NSPAWN_FRAGMENT="machinectl-test-$RANDOM.nspawn" |
188 | cat >"/var/lib/machines/$NSPAWN_FRAGMENT" <<EOF | |
189 | [Exec] | |
190 | Boot=true | |
191 | EOF | |
192 | machinectl cat "$NSPAWN_FRAGMENT" | |
193 | EDITOR=true script -qec "machinectl edit $NSPAWN_FRAGMENT" /dev/null | |
194 | test -f "/etc/systemd/nspawn/$NSPAWN_FRAGMENT" | |
195 | diff "/var/lib/machines/$NSPAWN_FRAGMENT" "/etc/systemd/nspawn/$NSPAWN_FRAGMENT" | |
196 | ||
197 | cat >/tmp/fragment.nspawn <<EOF | |
198 | [Exec] | |
199 | Boot=false | |
200 | EOF | |
201 | machinectl cat /tmp/fragment.nspawn | |
202 | EDITOR="cp /tmp/fragment.nspawn" script -qec "machinectl edit $NSPAWN_FRAGMENT" /dev/null | |
203 | diff /tmp/fragment.nspawn "/etc/systemd/nspawn/$NSPAWN_FRAGMENT" | |
204 | ||
378db9e2 FS |
205 | for opt in format lines machine max-addresses output setenv verify; do |
206 | (! machinectl status "--$opt=" long-running) | |
207 | (! machinectl status "--$opt=-1" long-running) | |
208 | (! machinectl status "--$opt=''" long-running) | |
209 | done | |
210 | (! machinectl show "") | |
211 | (! machinectl enable) | |
212 | (! machinectl enable "") | |
213 | (! machinectl disable) | |
214 | (! machinectl disable "") | |
215 | (! machinectl read-only container1 "") | |
216 | (! machinectl read-only container1 foo) | |
217 | (! machinectl read-only container1 -- -1) |