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