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