-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_test
executable file
·237 lines (188 loc) · 4.63 KB
/
package_test
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/bash
# Tests for the hg-agent packaging lifecycle.
## Constants
readonly PACKAGENAME=hg-agent
readonly SUPERVISORD_LINK="/opt/hg-agent/bin/supervisord"
readonly PERIODIC_LINK="/opt/hg-agent/bin/periodic"
readonly SUPERVISORD_PROCESS="supervisord"
readonly PERIODIC_PROCESS="periodic"
## Messages
fail() {
echo "FAILURE ($1) $2"
exit 1
}
succeed() {
echo "SUCCESS ($1) $2"
}
waiting() {
echo "WAITING ($1)"
sleep "$2"
}
## Package / service actions
do_install() {
local pkgtype=$1
local svctype=$2
local packageroot="/hg-agent/out/$pkgtype/$svctype"
case "$pkgtype" in
rpm)
local tool="rpm --install"
;;
deb)
local tool="dpkg --install"
;;
esac
local package="$packageroot/${PACKAGENAME}"*
$tool $package \
|| fail do_install "installing $package"
}
do_remove() {
local pkgtype=$1
local svctype=$2
case "$pkgtype" in
rpm)
local tool="rpm --erase"
;;
deb)
local tool="dpkg --remove"
;;
esac
$tool $PACKAGENAME \
|| fail do_remove "removing $PACKAGENAME"
}
do_stop() {
local pkgtype=$1
local svctype=$2
case "$svctype" in
sysvinit|upstart)
service $PACKAGENAME stop \
|| fail do_stop "stopping $PACKAGENAME"
;;
systemd)
systemctl stop $PACKAGENAME \
|| fail do_stop "stopping $PACKAGENAME"
;;
*)
fail do_stop "unsupported service type $svctype"
;;
esac
}
do_start() {
local pkgtype=$1
local svctype=$2
case "$svctype" in
sysvinit|upstart)
service $PACKAGENAME start \
|| fail do_start "starting $PACKAGENAME"
;;
systemd)
systemctl start $PACKAGENAME \
|| fail do_start "starting $PACKAGENAME"
;;
*)
fail do_start "unsupported service type $svctype"
;;
esac
}
## Tests
is_symlink() {
[[ -h $1 ]]
}
is_running() {
pgrep -u hg-agent -f "$1"
}
test_postinstall() {
waiting postinstall 10
is_symlink "$SUPERVISORD_LINK" \
|| fail postinstall "expected symlink $SSUPERVISORD_LINK"
is_running "$SUPERVISORD_PROCESS" \
|| fail postinstall "expected process for $SUPERVISORD_PROCESS"
is_running "$PERIODIC_PROCESS" \
|| fail postinstall "expected process for $PERIODIC_PROCESS"
succeed postinstall
}
test_postboot() {
is_running "$SUPERVISORD_PROCESS" \
|| fail postboot "expected process for $SUPERVISORD_PROCESS"
succeed postboot
}
test_poststop() {
waiting poststop 10
! is_running "$SUPERVISORD_PROCESS" \
|| fail poststop "unexpected process for $SUPERVISORD_PROCESS"
succeed poststop
}
test_poststart() {
waiting poststart 10
is_running "$SUPERVISORD_PROCESS" \
|| fail poststart "expected process for $SUPERVISORD_PROCESS"
succeed poststart
}
test_postremove() {
waiting postremove 10
! is_symlink "$SUPERVISORD_LINK" \
|| fail postremove "unexpected symlink $SUPERVISORD_LINK"
! is_running "$SUPERVISORD_PROCESS" \
|| fail postremove "unexpected process for $SUPERVISORD_PROCESS"
! is_running "$PERIODIC_PROCESS" \
|| fail postremove "unexpected process for $PERIODIC_PROCESS"
succeed postremove
}
main() {
local usage="Usage: package_test {deb,rpm} {sysvinit,upstart,systemd} {install, boot, stop, start, remove}"
local pkgtype="${1:?$usage}"
local svctype="${2:?$usage}"
local testtype="${3:?$usage}"
case "$pkgtype" in
rpm)
true
;;
deb)
true
;;
*)
echo "$usage"
exit 1
;;
esac
case "$svctype" in
sysvinit)
true
;;
upstart)
true
;;
systemd)
true
;;
*)
echo "$usage"
exit 1
;;
esac
case "$testtype" in
install)
do_install "$pkgtype" "$svctype"
test_postinstall
;;
boot)
test_postboot
;;
stop)
do_stop "$pkgtype" "$svctype"
test_poststop
;;
start)
do_start "$pkgtype" "$svctype"
test_poststart
;;
remove)
do_remove "$pkgtype" "$svctype"
test_postremove
;;
*)
echo "$usage"
exit 1
;;
esac
}
main "$@"