transient-services integration test: Create the right directory
[platform/upstream/dbus.git] / test / integration / transient-services.sh
1 #!/bin/sh
2
3 # Copyright © 2017 Collabora Ltd.
4 #
5 # Permission is hereby granted, free of charge, to any person
6 # obtaining a copy of this software and associated documentation files
7 # (the "Software"), to deal in the Software without restriction,
8 # including without limitation the rights to use, copy, modify, merge,
9 # publish, distribute, sublicense, and/or sell copies of the Software,
10 # and to permit persons to whom the Software is furnished to do so,
11 # subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 # SOFTWARE.
24
25 set -e
26
27 if test -z "$XDG_RUNTIME_DIR"; then
28     echo "1..0 # SKIP - $XDG_RUNTIME_DIR is empty or unset"
29     exit 0
30 fi
31
32 # test -O is non-POSIX, but bash and dash have it. If it doesn't work,
33 # we're probably not on Linux and so would likely be skipping this test
34 # anyway.
35 if ! test -O "$XDG_RUNTIME_DIR"; then
36     echo "1..0 # SKIP - $XDG_RUNTIME_DIR is not ours or test -O does not work"
37     exit 0
38 fi
39
40 if ! test -S "$XDG_RUNTIME_DIR/bus"; then
41     echo "1..0 # SKIP - $XDG_RUNTIME_DIR/bus is not a socket"
42     exit 0
43 fi
44
45 if ! test -O "$XDG_RUNTIME_DIR/bus"; then
46     echo "1..0 # SKIP - $XDG_RUNTIME_DIR/bus is not ours"
47     exit 0
48 fi
49
50 if test -n "$DBUS_SESSION_BUS_ADDRESS" && \
51         ! test "unix:path=$XDG_RUNTIME_DIR/bus" = "$DBUS_SESSION_BUS_ADDRESS"; then
52     echo "1..0 # SKIP - DBUS_SESSION_BUS_ADDRESS does not point to $XDG_RUNTIME_DIR/bus"
53     exit 0
54 fi
55
56 # Any unique token that won't collide would do here. It must start with
57 # a letter or underscore.
58 unique="t$(dbus-uuidgen)"
59
60 if ! workdir="$(mktemp -d)"; then
61     echo "1..0 # SKIP - mktemp -d doesn't work"
62     exit 0
63 fi
64
65 cleanup () {
66     rm -fr "$workdir"
67     rm -f "$XDG_RUNTIME_DIR/dbus-1/services/com.example.DBusTests.$unique.tmp"
68     rm -f "$XDG_RUNTIME_DIR/dbus-1/services/com.example.DBusTests.$unique.service"
69     rm -f "$XDG_RUNTIME_DIR/dbus-1/services/com.example.DBusTests.Systemd.$unique.tmp"
70     rm -f "$XDG_RUNTIME_DIR/dbus-1/services/com.example.DBusTests.Systemd.$unique.service"
71     rm -f "$XDG_RUNTIME_DIR/systemd/user/dbus-com.example.DBusTests.Systemd.$unique.service"
72 }
73 trap cleanup EXIT
74
75 echo "1..2"
76
77 # This is an integration test, so we expect the dbus-daemon to already be
78 # running
79 if ! test -d "$XDG_RUNTIME_DIR/dbus-1/services"; then
80     echo "Bail out! $XDG_RUNTIME_DIR/dbus-1/services is not a directory"
81     exit 1
82 fi
83
84 cd "$XDG_RUNTIME_DIR/dbus-1/services"
85
86 sed -e 's/^ *//' > "com.example.DBusTests.$unique.tmp" <<EOF
87     [D-BUS Service]
88     Name=com.example.DBusTests.$unique
89     Exec=/bin/sh -c 'touch "\$1"; exit 1' sh '$workdir/ran-$unique'
90 EOF
91 mv "com.example.DBusTests.$unique.tmp" "com.example.DBusTests.$unique.service"
92
93 (
94 dbus-send --session --dest="org.freedesktop.DBus" \
95     --type=method_call --print-reply /org/freedesktop/DBus \
96     org.freedesktop.DBus.ReloadConfig || touch "$workdir/failed" \
97 ) 2>&1 | sed -e 's/^/# /'
98
99 if [ -e "$workdir/failed" ]; then
100     echo "Bail out! Unable to tell dbus-daemon to reload"
101     exit 1
102 fi
103
104 # Because the service never actually takes a bus name, we expect this to fail;
105 # but its exit status is ignored because it's in a pipeline.
106 dbus-send --session --dest="com.example.DBusTests.$unique" \
107     --type=method_call --print-reply / com.example.DBusTests.ThisWillFail \
108     2>&1 | sed -e 's/^/# /'
109
110 if [ -e "$workdir/ran-$unique" ]; then
111     echo "ok 1 - attempted to run transient service directly"
112 else
113     echo "not ok 1 - no sign of having run transient service"
114 fi
115
116 # See whether systemd is on the session bus.
117 (
118 dbus-send --session --dest="org.freedesktop.systemd1" \
119     --type=method_call --print-reply /org/freedesktop/systemd1/Manager \
120     org.freedesktop.DBus.Peer.Ping || touch "$workdir/no-systemd" \
121 ) 2>&1 | sed -e 's/^/# /'
122
123 if [ -d "$XDG_RUNTIME_DIR/systemd" ] && ! [ -e "$workdir/no-systemd" ]; then
124
125     # systemd is Linux-specific, so we can assume GNU mkdir.
126     mkdir -p -m700 "$XDG_RUNTIME_DIR/systemd/user"
127     cd "$XDG_RUNTIME_DIR/systemd/user"
128
129     sed -e 's/^ *//' > "dbus-com.example.DBusTests.Systemd.$unique.tmp" <<EOF
130         [Unit]
131         Description=Non-functional service in the dbus integration tests
132         [Service]
133         ExecStart=/bin/sh -c 'touch "\$1"; exit 1' sh '$workdir/ran-$unique-via-systemd'
134         Type=forking
135 EOF
136     mv "dbus-com.example.DBusTests.Systemd.$unique.tmp" \
137         "dbus-com.example.DBusTests.Systemd.$unique.service"
138
139     if ! systemctl --user daemon-reload; then
140         echo "Bail out! Unable to tell systemd to reload"
141         exit 1
142     fi
143
144     cd "$XDG_RUNTIME_DIR/dbus-1/services"
145
146     sed -e 's/^ *//' > "com.example.DBusTests.Systemd.$unique.tmp" <<EOF
147         [D-BUS Service]
148         Name=com.example.DBusTests.Systemd.$unique
149         SystemdService=dbus-com.example.DBusTests.Systemd.$unique.service
150         Exec=/bin/sh -c 'touch "\$1"; exit 1' sh '$workdir/ran-$unique-wrong'
151 EOF
152     mv "com.example.DBusTests.Systemd.$unique.tmp" \
153         "com.example.DBusTests.Systemd.$unique.service"
154
155     (
156     dbus-send --session --dest="org.freedesktop.DBus" \
157         --type=method_call --print-reply /org/freedesktop/DBus \
158         org.freedesktop.DBus.ReloadConfig || touch "$workdir/failed" \
159     ) 2>&1 | sed -e 's/^/# /'
160
161     if [ -e "$workdir/failed" ]; then
162         echo "Bail out! Unable to tell dbus-daemon to reload"
163         exit 1
164     fi
165
166     # Because the service never actually takes a bus name, we expect this to fail;
167     # but its exit status is ignored because it's in a pipeline.
168     #
169     # systemd never sends back an ActivationFailure message for a service
170     # if it starts the job but the service subsequently doesn't behave as
171     # intended, so use a relatively short timeout (3000ms).
172     dbus-send --session --dest="com.example.DBusTests.Systemd.$unique" \
173         --reply-timeout=3000 --type=method_call --print-reply \
174         / com.example.DBusTests.ThisWillFail \
175         2>&1 | sed -e 's/^/# /'
176
177     if [ -e "$workdir/ran-$unique-via-systemd" ]; then
178         echo "ok 2 - attempted to run transient service via systemd"
179     elif [ -e "$workdir/ran-$unique-wrong" ]; then
180         echo "not ok 2 - ran transient service incorrectly"
181     else
182         echo "not ok 2 - no sign of having run transient service"
183     fi
184 else
185     echo "ok 2 # SKIP session bus does not appear to be managed by systemd"
186 fi
187
188 echo "# Done."
189 cleanup
190 trap '' EXIT
191 exit 0