Skip to content

Commit 29bcd2e

Browse files
authored
Merge pull request #726 from adelton/systemd_in_container
Test running systemd in container.
2 parents 8f76c15 + 3b72a8e commit 29bcd2e

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
r"""
2+
Summary
3+
---------
4+
5+
Verify running systemd as ENTRYPOINT in container.
6+
7+
Operational Summary
8+
----------------------
9+
10+
#. Execute bundled test.sh script to perform label checking operations.
11+
#. Fail subtest if script returns non-zero.
12+
13+
Prerequisites
14+
---------------
15+
16+
Commands contained w/in test script are available/functional on system.
17+
Specifically docker 1.12 is needed for dockerd and containerd.
18+
"""
19+
20+
from os.path import join
21+
from autotest.client.utils import run
22+
from dockertest import subtest
23+
from dockertest.images import DockerImages
24+
from dockertest.output.validate import mustpass
25+
from dockertest.output.dockerversion import DockerVersion
26+
27+
28+
class systemd_in_container(subtest.Subtest):
29+
30+
def initialize(self):
31+
# See Prerequisites (above)
32+
DockerVersion().require_server("1.12")
33+
self.stuff['result'] = None
34+
self.stuff['di'] = DockerImages(self)
35+
super(systemd_in_container, self).initialize()
36+
37+
def run_once(self):
38+
super(systemd_in_container, self).run_once()
39+
# Assumes script exits non-zero on test-failure and
40+
# cleans up any/all containers/images it created
41+
result = run("%s %s"
42+
% (join(self.bindir, 'test.sh'),
43+
self.stuff['di'].default_image),
44+
ignore_status=True)
45+
self.logdebug(str(result))
46+
self.stuff['result'] = result
47+
48+
def postprocess(self):
49+
super(systemd_in_container, self).postprocess()
50+
mustpass(self.stuff['result'])
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
#
3+
# Test systemd in containers
4+
#
5+
6+
if [ $# -ne 1 ]; then
7+
echo "Usage: $0 IMAGE-NAME"
8+
exit 1
9+
fi
10+
image="$1"
11+
timeout=10
12+
13+
# Exit status. Default 0 (success), but set to 1 on any subtest failure
14+
rc=0
15+
16+
# Check that when container is run, its output contains the expected
17+
# string(s). With systemd, the container might be running bug systemd
18+
# freezing execution so we cannot really expect docker run to fail.
19+
check_output() {
20+
local container="$1"; shift
21+
local name="$1"; shift
22+
23+
local lrc=0
24+
for expect in "$@"; do
25+
echo "Checking for $expect"
26+
i=0
27+
while [ $i -lt $timeout ] ; do
28+
if docker logs "$container" | grep -q "$expect" ; then
29+
echo "PASS $name = $expect"
30+
break
31+
fi
32+
sleep 1
33+
i=$(expr $i + 1)
34+
done
35+
if [ $i -eq $timeout ] ; then
36+
echo "FAILED $name, expecting $expect"
37+
docker logs "$container"
38+
lrc=1
39+
rc=1
40+
break
41+
fi
42+
done
43+
44+
return $lrc
45+
}
46+
47+
# Actual checks
48+
49+
if docker run -d -ti --name systemd$$ $image /usr/sbin/init ; then
50+
container_id=$( docker ps -q -f name=systemd$$ )
51+
check_output "systemd$$" \
52+
"systemd in container" \
53+
"Set hostname to <$container_id>\." \
54+
"Started Journal Service"
55+
docker rm -f systemd$$
56+
else
57+
echo "FAILED running systemd in $image"
58+
fi
59+
60+
if docker run -d -ti --name systemd$$ -h www.example.test $image /usr/sbin/init ; then
61+
check_output "systemd$$" \
62+
"systemd with hostname set" \
63+
"Set hostname to <www\.example\.test>"
64+
docker rm -f systemd$$
65+
else
66+
echo "FAILED running systemd in $image"
67+
fi
68+
69+
exit $rc

0 commit comments

Comments
 (0)