-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-tests.sh
executable file
·138 lines (115 loc) · 3.56 KB
/
run-tests.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
set -e
PS4='$LINENO: '
red() {
printf "\033[31m%s\033[0m\n" "$1"
}
green() {
printf "\033[32m%s\033[0m\n" "$1"
}
expect() {
{ set +x; } 2>/dev/null
cmd=""
for arg in "$@"; do
cmd="${cmd}'${arg}' "
done
if ! "$@"; then
red "Test failed: $cmd"
exit 1
fi
set -x
}
is_equal() {
test "$1" = "$2"
}
parse() {
input=$(cat)
echo "$input" | tail -n +2 | sed 's/^ *//g' | tr -d '\n\r'
}
systemd_method() {
{ set +x; } 2>/dev/null
method=$1
shift
dbus-send --system --print-reply \
--dest=org.freedesktop.systemd1 \
/org/freedesktop/systemd1 \
"org.freedesktop.systemd1.Manager.$method" "$@" | parse
set -x
}
systemd_unit_prop() {
{ set +x; } 2>/dev/null
dbus-send --system --print-reply \
--dest=org.freedesktop.systemd1 \
"$1" \
org.freedesktop.DBus.Properties.Get string:org.freedesktop.systemd1.Unit "string:$2" | parse
set -x
}
login_method() {
{ set +x; } 2>/dev/null
method=$1
shift
dbus-send --system --print-reply \
--dest=org.freedesktop.login1 \
/org/freedesktop/login1 \
"org.freedesktop.login1.Manager.$method" "$@" | parse
set -x
}
login_prop() {
{ set +x; } 2>/dev/null
dbus-send --system --print-reply \
--dest=org.freedesktop.login1 \
/org/freedesktop/login1 \
org.freedesktop.DBus.Properties.Get string:org.freedesktop.login1.Manager "string:$1" | parse
set -x
}
cleanup() {
login_method MockReset >/dev/null
}
set_abort_timer() {
sleep "$1"
# Send a USR2 signal to the given pid after the timeout happens
kill -USR2 "$2"
}
timeout=30
abort_if_not_ready() {
# If the timeout is reached and the required services are not ready, it probably
# means something went wrong so we terminate the program with an error
echo "Something happened, failed to start in ${timeout}s" >&2
exit 1
}
trap 'abort_if_not_ready' USR2
set_abort_timer "$timeout" $$ &
timer_pid=$!
dbus_healthy() {
dbus-send --system --print-reply \
--dest=org.freedesktop.login1 \
/org/freedesktop/login1 \
org.freedesktop.DBus.Properties.Get string:org.freedesktop.login1.Manager string:MockState
}
# Wait for dbus
echo "Waiting for dbus"
until dbus_healthy >/dev/null; do
sleep 1
done
kill $timer_pid
echo "D-Bus socket ready"
# Cleanup if any of the tests fail
trap 'cleanup' EXIT
# Start tracing here
set -x
expect is_equal "$(systemd_method GetUnit string:dummy.service)" 'object path "/org/freedesktop/systemd1/unit/dummy_2eservice"'
expect is_equal "$(systemd_unit_prop /org/freedesktop/systemd1/unit/dummy_2eservice ActiveState)" 'variant string "active"'
expect systemd_method StopUnit string:dummy.service string:fail
expect is_equal "$(systemd_unit_prop /org/freedesktop/systemd1/unit/dummy_2eservice ActiveState)" 'variant string "inactive"'
expect systemd_method StartUnit string:dummy.service string:fail
expect is_equal "$(systemd_unit_prop /org/freedesktop/systemd1/unit/dummy_2eservice ActiveState)" 'variant string "active"'
expect systemd_method RestartUnit string:dummy.service string:fail
expect is_equal "$(systemd_unit_prop /org/freedesktop/systemd1/unit/dummy_2eservice ActiveState)" 'variant string "active"'
expect is_equal "$(systemd_unit_prop /org/freedesktop/systemd1/unit/dummy_2eservice PartOf)" 'variant array []'
expect is_equal "$(login_prop MockState)" 'variant string "ready"'
expect is_equal "$(login_method PowerOff boolean:false)" ''
expect is_equal "$(login_prop MockState)" 'variant string "off"'
expect is_equal "$(login_method Reboot boolean:false)" ''
expect is_equal "$(login_prop MockState)" 'variant string "rebooting"'
{ set +x; } 2>/dev/null
green "All tests passed!"