ee3d790616cd2a1b10386937d31d362d3876a605
[src/qa-testdefinitions.git] / common / scripts / application-lifecycle.sh
1 #!/bin/sh
2
3 set -x
4
5 export TERM=dumb
6 export COLUMNS=1000
7
8 AGLDRIVER=agl-driver
9
10 while [ $# -ge 1 ]
11 do
12         case $1 in
13         -b)
14                 shift
15                 BASEURL=$1
16                 shift
17         ;;
18         *)
19                 echo "Unknown argument $1"
20                 exit 1
21         ;;
22         esac
23 done
24
25 if [ -z "$BASEURL" ]; then
26         echo "$0: missing BASEURL"
27         echo "Usage: $0 -b BASEURL"
28         exit 1
29 fi
30
31 do_afm_util()
32 {
33 set -x
34         if [ $SERVICE_USER -eq 1 -o $APPLICATION_USER -eq 1 ];then
35                 su - $AGLDRIVER -c "afm-util $*"
36         else
37                 afm-util $*
38         fi
39         return $?
40 }
41
42 do_afm_test()
43 {
44         set -x
45         if [ $SERVICE_USER -eq 1 -o $APPLICATION_USER -eq 1 ];then
46                 su - $AGLDRIVER -c "afm-test -l $*"
47         else
48                 afm-test -l $*
49         fi
50         return $?
51 }
52
53 # work in tmp folder to allow different users to access files (smack)
54 TOPDIR=$(mktemp -d)
55 cd $TOPDIR
56
57 if [ ! -f index.html ] ; then
58         wget -q $BASEURL -O index.html
59         if [ $? -ne 0 ];then
60             echo "ERROR: Cannot wget $BASEURL"
61                 exit 1
62         fi
63 fi
64
65 # first download all files
66 grep -o '[a-z-]*.wgt' index.html | sort | uniq |
67 while read wgtfile
68 do
69         # remove extension and the debug state
70         echo "DEBUG: fetch $wgtfile"
71
72         if [ ! -f $wgtfile ] ; then
73                 wget -q $BASEURL/$wgtfile
74                 if [ $? -ne 0 ];then
75                         echo "ERROR: wget from $BASEURL/$wgtfile"
76                         continue
77                 fi
78         fi
79         # do adapt security
80         chmod -R a+rwx ${TOPDIR}
81         chsmack -a "*" ${TOPDIR}/*
82 done
83
84 inspect_wgt() {
85         wgtfile=$1
86         WGTNAME=$2
87
88         export SERVICE_PLATFORM=0
89         export SERVICE_USER=0
90         export APPLICATION_USER=0
91
92         CURDIR="$(pwd)"
93         ZIPOUT="$(mktemp -d)"
94         cd $ZIPOUT
95
96         echo "DEBUG: analyse wgt file"
97         unzip $CURDIR/$wgtfile
98         if [ $? -ne 0 ];then
99                 # TODO Do not fail yet, busybox unzip seems to "fail with success" when checking CRC
100                 echo "ERROR: cannot unzip $wgtfile"
101         fi
102         if [ -f config.xml ];then
103                 grep hidden config.xml
104                 if [ $? -eq 0 ];then
105                         echo "DEBUG: hidden package"
106                 else
107                         echo "DEBUG: not hidden package"
108                 fi
109                 # a service sets urn:AGL:widget:provided-api
110                 grep "urn:AGL:widget:provided-api" config.xml
111                 if [ $? -eq 0 ] ; then
112                     # we are a service, now determine the scope ...
113                     grep "urn:AGL:permission::partner:scope-platform" config.xml
114                     if [ $? -eq 0 ];then
115                         export SERVICE_PLATFORM=1
116                     else
117                         export SERVICE_USER=1
118                     fi
119                 else
120                     # we are an application
121                     export APPLICATION_USER=1
122                     # no other type known (yet)
123                 fi
124                 # the file naming convention is servicename.wgt
125                 # but some didnt respect it
126                 export WGTSERVICENAME=$(grep '<name>' config.xml | cut -d'>' -f2 | cut -d'<' -f1)
127                 if [ -z "$WGTSERVICENAME" ];then
128                         echo "WARN: failed to find name in config.xml, fallback to filename"
129                         export WGTSERVICENAME="$WGTNAME"
130                 else
131                         echo "DEBUG: detected service name as $WGTSERVICENAME"
132                 fi
133         else
134                 echo "DEBUG: fail to unzip"
135         fi
136
137         cd $CURDIR
138         rm -r $ZIPOUT
139 }
140
141 # check if WGTNAME is running
142 check_service_running() {
143         WGTNAME=$1
144         RUNNING=0
145
146         echo "DEBUG: check_service_running with systemctl list-units -full"
147         systemctl list-units --full | grep "afm.*$WGTNAME--"
148         if [ $? -eq 0 ];then
149                 RUNNING=1
150         fi
151         echo "DEBUG: check_service_running with systemctl -a"
152         systemctl -a |grep "afm.*$WGTNAME--"
153         if [ $? -eq 0 ];then
154                 if [ $RUNNING -eq 0 ];then
155                         echo "ERROR: inconsistent results"
156                 fi
157                 RUNNING=1
158         fi
159         return $RUNNING
160 }
161
162 do_release_test() {
163         WGTNAME=$1
164         wgtfile=$2
165         # we need the full name (with -test, -debug etc..) for LAVA test case
166         WGTNAMEF=$(echo $2 | sed 's,.wgt,,')
167
168         echo "INFO: do_release_test $WGTNAME $wgtfile"
169
170         echo "DEBUG: list current pkgs"
171         # TODO mktemp
172         LIST='list'
173         afm-util list --all > $LIST
174         if [ $? -ne 0 ];then
175                 echo "ERROR: afm-util list exit with error"
176                 continue
177         fi
178         if [ ! -s "$LIST" ];then
179                 echo "ERROR: afm-util list is empty"
180                 continue
181         fi
182
183         echo "DEBUG: check presence of $WGTNAME"
184         NAMEID=$(grep id\\\":\\\"${WGTSERVICENAME}\" $LIST | cut -d\" -f4 | cut -d\\ -f1)
185         if [ ! -z "$NAMEID" ];then
186                 echo "DEBUG: $WGTNAME already installed as $NAMEID"
187                 # need to kill then deinstall
188                 do_afm_util ps --all | grep -q $WGTNAME
189                 if [ $? -eq 0 ];then
190                         echo "DEBUG: kill $WGTNAME"
191                         do_afm_util kill $WGTNAME
192                         if [ $? -ne 0 ];then
193                                 echo "ERROR: afm-util kill"
194                                 #lava-test-case afm-util-pre-kill-$WGTNAME --result fail
195                                 #continue
196                         #else
197                         #       lava-test-case afm-util-pre-kill-$WGTNAME --result pass
198                         fi
199                 else
200                         echo "DEBUG: no need to kill $WGTNAME"
201                 fi
202
203                 echo "DEBUG: deinstall $WGTNAME"
204                 afm-util remove $NAMEID
205                 if [ $? -ne 0 ];then
206                         echo "ERROR: afm-util remove"
207                         #lava-test-case afm-util-remove-$WGTNAMEF --result fail
208                         journalctl -b | tail -40
209                         #continue
210                 else
211                         lava-test-case afm-util-remove-$WGTNAMEF --result pass
212                 fi
213         else
214                 echo "DEBUG: $WGTNAME not installed"
215         fi
216         grep id $LIST
217
218         echo "DEBUG: install $wgtfile"
219         OUT="out"
220         afm-util install $wgtfile > $OUT
221         if [ $? -ne 0 ];then
222                 echo "ERROR: afm-util install"
223                 lava-test-case afm-util-install-$WGTNAMEF --result fail
224                 continue
225         else
226                 lava-test-case afm-util-install-$WGTNAMEF --result pass
227         fi
228         # message is like \"added\":\"mediaplayer@0.1\"
229         NAMEID=$(grep d\\\":\\\"${WGTSERVICENAME}\" $OUT | cut -d\" -f4 | cut -d\\ -f1)
230         if [ -z "$NAMEID" ];then
231                 echo "ERROR: Cannot get nameid"
232                 echo "DEBUG: ========== DUMPING output =========="
233                 cat $OUT
234                 echo "DEBUG: ========== END DUMP =========="
235                 continue
236         fi
237         echo "DEBUG: $WGTNAME is installed as $NAMEID"
238
239         afm-util list --all > $LIST
240         if [ $? -ne 0 ];then
241                 echo "ERROR: afm-util list exit with error"
242                 continue
243         fi
244         if [ ! -s "$LIST" ];then
245                 echo "ERROR: afm-util list is empty"
246                 continue
247         fi
248         echo "DEBUG: Verify that $WGTNAME is installed"
249         grep -q $NAMEID $LIST
250         if [ $? -ne 0 ];then
251                 echo "ERROR: $WGTNAME is not installed"
252                 # for debugging, give full output
253                 echo "DEBUG: start of list"
254                 cat $LIST
255                 echo "DEBUG: end of list"
256         fi
257
258         do_afm_util info $NAMEID
259         if [ $? -ne 0 ];then
260                 echo "ERROR: afm-util info"
261                 lava-test-case afm-util-info-$WGTNAMEF --result fail
262         else
263                 lava-test-case afm-util-info-$WGTNAMEF --result pass
264         fi
265
266         echo "DEBUG: check if we see the package with systemctl list-units (before start)"
267         systemctl list-units --full | grep "afm.*$WGTNAME--"
268         echo "DEBUG: check if we see the package with systemctl -a (before start)"
269         systemctl -a |grep "afm.*$WGTNAME--"
270
271         echo "DEBUG: start $NAMEID"
272         do_afm_util start $NAMEID > "rid"
273         if [ $? -ne 0 ];then
274                 echo "ERROR: afm-util start"
275                 lava-test-case afm-util-start-$WGTNAMEF --result fail
276                 journalctl -an 200
277                 continue
278         else
279                 lava-test-case afm-util-start-$WGTNAMEF --result pass
280         fi
281
282         check_service_running $WGTNAME
283
284         echo "DEBUG: Get RID for $NAMEID"
285         PSLIST="pslist"
286         afm-util ps --all > $PSLIST
287         if [ $? -ne 0 ];then
288                 echo "ERROR: afm-util ps"
289                 lava-test-case afm-util-ps-$WGTNAMEF --result fail
290                 continue
291         else
292                 cat $PSLIST
293                 lava-test-case afm-util-ps-$WGTNAMEF --result pass
294         fi
295         # TODO, compare RID with the list in $PSLIST"
296         RID="$(cat rid)"
297         if [ "$RID" == 'null' ];then
298                 sleep 20
299                 echo "DEBUG: retry start $NAMEID"
300                 do_afm_util start $NAMEID > "rid"
301                 if [ $? -ne 0 ];then
302                         echo "ERROR: afm-util start"
303                         lava-test-case afm-util-start-$WGTNAMEF --result fail
304                         continue
305                 fi
306                 RID="$(cat rid)"
307         fi
308
309         if [ "$RID" == 'null' ];then
310                 echo "ERROR: RID is null, service fail to start"
311                 lava-test-case afm-util-status-$WGTNAMEF --result fail
312                 continue
313         fi
314
315         echo "DEBUG: status $NAMEID ($RID)"
316         do_afm_util status $RID
317         if [ $? -ne 0 ];then
318                 echo "ERROR: afm-util status"
319                 lava-test-case afm-util-status-$WGTNAMEF --result fail
320                 continue
321         else
322                 lava-test-case afm-util-status-$WGTNAMEF --result pass
323         fi
324
325         echo "DEBUG: kill $NAMEID ($RID)"
326         do_afm_util kill $NAMEID
327         if [ $? -ne 0 ];then
328                 echo "ERROR: afm-util kill"
329                 lava-test-case afm-util-kill-$WGTNAMEF --result fail
330                 continue
331         else
332                 lava-test-case afm-util-kill-$WGTNAMEF --result pass
333         fi
334
335         echo "DEBUG: start2 $NAMEID"
336         do_afm_util start $NAMEID > rid
337         if [ $? -ne 0 ];then
338                 echo "ERROR: afm-util start2"
339                 lava-test-case afm-util-start2-$WGTNAMEF --result fail
340                 journalctl -an 200
341                 continue
342         else
343                 lava-test-case afm-util-start2-$WGTNAMEF --result pass
344         fi
345         RID="$(cat rid)"
346         if [ "$RID" == 'null' ];then
347                 echo "ERROR: RID is null"
348                 continue
349         fi
350         sleep 10
351         echo "DEBUG: status2 $NAMEID ($RID)"
352         do_afm_util status $RID
353         if [ $? -ne 0 ];then
354                 echo "ERROR: afm-util status2"
355                 lava-test-case afm-util-status2-$WGTNAMEF --result fail
356                 continue
357         else
358                 lava-test-case afm-util-status2-$WGTNAMEF --result pass
359         fi
360 }
361
362 WGTNAMES=$(grep -o '[a-z-]*.wgt' index.html | sed 's,.wgt$,,' | sed 's,-debug$,,' | sed 's,-test$,,' | sed 's,-coverage$,,' | sort | uniq)
363 for WGTNAME in $WGTNAMES
364 do
365         if [ -e $WGTNAME.wgt ];then
366                 inspect_wgt $WGTNAME.wgt $WGTNAME
367                 do_release_test $WGTNAME $WGTNAME.wgt
368         else
369                 echo "WARN: cannot find $WGTNAME.wgt"
370         fi
371         if [ -e $WGTNAME-test.wgt ];then
372                 # wgt-test do not have the same permissions in the config.xml as the parent wgt
373                 # so keep the value from last run
374                 #inspect_wgt $WGTNAME-test.wgt
375                 check_service_running $WGTNAME
376                 if [ $? -eq 1 ];then
377                         do_afm_test $TOPDIR/$WGTNAME-test.wgt
378                         if [ $? -eq 0 ];then
379                                 lava-test-case run-test-$WGTNAME --result pass
380                         else
381                                 lava-test-case run-test-$WGTNAME --result fail
382                         fi
383                 else
384                         echo "DEBUG: $WGTNAME is not running, skipping test"
385                         lava-test-case run-test-$WGTNAME --result skip
386                 fi
387         else
388                 echo "WARN: cannot find $WGTNAME.wgt"
389         fi
390         if [ -e $WGTNAME-debug.wgt ];then
391                 inspect_wgt $WGTNAME-debug.wgt $WGTNAME
392                 do_release_test $WGTNAME $WGTNAME-debug.wgt
393         fi
394         if [ -e $WGTNAME-coverage.wgt ];then
395                 inspect_wgt $WGTNAME-coverage.wgt $WGTNAME
396                 echo "DEBUG: coverage not handled yet"
397         fi
398 done
399