SPEC-3484: create new application tests using new wgt types
[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
87         export SERVICE_PLATFORM=0
88         export SERVICE_USER=0
89         export APPLICATION_USER=0
90
91         CURDIR="$(pwd)"
92         ZIPOUT="$(mktemp -d)"
93         cd $ZIPOUT
94
95         echo "DEBUG: analyse wgt file"
96         unzip $CURDIR/$wgtfile
97         if [ $? -ne 0 ];then
98                 # TODO Do not fail yet, busybox unzip seems to "fail with success" when checking CRC
99                 echo "ERROR: cannot unzip $wgtfile"
100         fi
101         if [ -f config.xml ];then
102                 grep hidden config.xml
103                 if [ $? -eq 0 ];then
104                         echo "DEBUG: hidden package"
105                 else
106                         echo "DEBUG: not hidden package"
107                 fi
108                 # a service sets urn:AGL:widget:provided-api
109                 grep "urn:AGL:widget:provided-api" config.xml
110                 if [ $? -eq 0 ] ; then
111                     # we are a service, now determine the scope ...
112                     grep "urn:AGL:permission::partner:scope-platform" config.xml
113                     if [ $? -eq 0 ];then
114                         export SERVICE_PLATFORM=1
115                     else
116                         export SERVICE_USER=1
117                     fi
118                 else
119                     # we are an application
120                     export APPLICATION_USER=1
121                     # no other type known (yet)
122                 fi
123         else
124                 echo "DEBUG: fail to unzip"
125         fi
126
127         cd $CURDIR
128         rm -r $ZIPOUT
129 }
130
131 # check if WGTNAME is running
132 check_service_running() {
133         WGTNAME=$1
134         RUNNING=0
135
136         echo "DEBUG: check_service_running with systemctl list-units -full"
137         systemctl list-units --full | grep "afm.*$WGTNAME--"
138         if [ $? -eq 0 ];then
139                 RUNNING=1
140         fi
141         echo "DEBUG: check_service_running with systemctl -a"
142         systemctl -a |grep "afm.*$WGTNAME--"
143         if [ $? -eq 0 ];then
144                 if [ $RUNNING -eq 0 ];then
145                         echo "ERROR: inconsistent results"
146                 fi
147                 RUNNING=1
148         fi
149         return $RUNNING
150 }
151
152 do_release_test() {
153         WGTNAME=$1
154         wgtfile=$2
155         # we need the full name (with -test, -debug etc..) for LAVA test case
156         WGTNAMEF=$(echo $2 | sed 's,.wgt,,')
157
158         echo "INFO: do_release_test $WGTNAME $wgtfile"
159
160         echo "DEBUG: list current pkgs"
161         # TODO mktemp
162         LIST='list'
163         afm-util list --all > $LIST
164         if [ $? -ne 0 ];then
165                 echo "ERROR: afm-util list exit with error"
166                 continue
167         fi
168         if [ ! -s "$LIST" ];then
169                 echo "ERROR: afm-util list is empty"
170                 continue
171         fi
172
173         echo "DEBUG: check presence of $WGTNAME"
174         NAMEID=$(grep id\\\":\\\"${WGTNAME}\" $LIST | cut -d\" -f4 | cut -d\\ -f1)
175         if [ ! -z "$NAMEID" ];then
176                 echo "DEBUG: $WGTNAME already installed as $NAMEID"
177                 # need to kill then deinstall
178                 do_afm_util ps --all | grep -q $WGTNAME
179                 if [ $? -eq 0 ];then
180                         echo "DEBUG: kill $WGTNAME"
181                         do_afm_util kill $WGTNAME
182                         if [ $? -ne 0 ];then
183                                 echo "ERROR: afm-util kill"
184                                 #lava-test-case afm-util-pre-kill-$WGTNAME --result fail
185                                 #continue
186                         #else
187                         #       lava-test-case afm-util-pre-kill-$WGTNAME --result pass
188                         fi
189                 else
190                         echo "DEBUG: no need to kill $WGTNAME"
191                 fi
192
193                 echo "DEBUG: deinstall $WGTNAME"
194                 afm-util remove $NAMEID
195                 if [ $? -ne 0 ];then
196                         echo "ERROR: afm-util remove"
197                         #lava-test-case afm-util-remove-$WGTNAMEF --result fail
198                         journalctl -b | tail -40
199                         #continue
200                 else
201                         lava-test-case afm-util-remove-$WGTNAMEF --result pass
202                 fi
203         else
204                 echo "DEBUG: $WGTNAME not installed"
205         fi
206         grep id $LIST
207
208         echo "DEBUG: install $wgtfile"
209         OUT="out"
210         afm-util install $wgtfile > $OUT
211         if [ $? -ne 0 ];then
212                 echo "ERROR: afm-util install"
213                 lava-test-case afm-util-install-$WGTNAMEF --result fail
214                 continue
215         else
216                 lava-test-case afm-util-install-$WGTNAMEF --result pass
217         fi
218         # message is like \"added\":\"mediaplayer@0.1\"
219         NAMEID=$(grep d\\\":\\\"${WGTNAME}\" $OUT | cut -d\" -f4 | cut -d\\ -f1)
220         if [ -z "$NAMEID" ];then
221                 echo "ERROR: Cannot get nameid"
222                 echo "DEBUG: ========== DUMPING output =========="
223                 cat $OUT
224                 echo "DEBUG: ========== END DUMP =========="
225                 continue
226         fi
227         echo "DEBUG: $WGTNAME is installed as $NAMEID"
228
229         afm-util list --all > $LIST
230         if [ $? -ne 0 ];then
231                 echo "ERROR: afm-util list exit with error"
232                 continue
233         fi
234         if [ ! -s "$LIST" ];then
235                 echo "ERROR: afm-util list is empty"
236                 continue
237         fi
238         echo "DEBUG: Verify that $WGTNAME is installed"
239         grep -q $NAMEID $LIST
240         if [ $? -ne 0 ];then
241                 echo "ERROR: $WGTNAME is not installed"
242                 # for debugging, give full output
243                 echo "DEBUG: start of list"
244                 cat $LIST
245                 echo "DEBUG: end of list"
246         fi
247
248         do_afm_util info $NAMEID
249         if [ $? -ne 0 ];then
250                 echo "ERROR: afm-util info"
251                 lava-test-case afm-util-info-$WGTNAMEF --result fail
252         else
253                 lava-test-case afm-util-info-$WGTNAMEF --result pass
254         fi
255
256         echo "DEBUG: check if we see the package with systemctl list-units (before start)"
257         systemctl list-units --full | grep "afm.*$WGTNAME--"
258         echo "DEBUG: check if we see the package with systemctl -a (before start)"
259         systemctl -a |grep "afm.*$WGTNAME--"
260
261         echo "DEBUG: start $NAMEID"
262         do_afm_util start $NAMEID > "rid"
263         if [ $? -ne 0 ];then
264                 echo "ERROR: afm-util start"
265                 lava-test-case afm-util-start-$WGTNAMEF --result fail
266                 journalctl -an 200
267                 continue
268         else
269                 lava-test-case afm-util-start-$WGTNAMEF --result pass
270         fi
271
272         check_service_running $WGTNAME
273
274         echo "DEBUG: Get RID for $NAMEID"
275         PSLIST="pslist"
276         afm-util ps --all > $PSLIST
277         if [ $? -ne 0 ];then
278                 echo "ERROR: afm-util ps"
279                 lava-test-case afm-util-ps-$WGTNAMEF --result fail
280                 continue
281         else
282                 cat $PSLIST
283                 lava-test-case afm-util-ps-$WGTNAMEF --result pass
284         fi
285         # TODO, compare RID with the list in $PSLIST"
286         RID="$(cat rid)"
287         if [ "$RID" == 'null' ];then
288                 sleep 20
289                 echo "DEBUG: retry start $NAMEID"
290                 do_afm_util start $NAMEID > "rid"
291                 if [ $? -ne 0 ];then
292                         echo "ERROR: afm-util start"
293                         lava-test-case afm-util-start-$WGTNAMEF --result fail
294                         continue
295                 fi
296                 RID="$(cat rid)"
297         fi
298
299         if [ "$RID" == 'null' ];then
300                 echo "ERROR: RID is null, service fail to start"
301                 lava-test-case afm-util-status-$WGTNAMEF --result fail
302                 continue
303         fi
304
305         echo "DEBUG: status $NAMEID ($RID)"
306         do_afm_util status $RID
307         if [ $? -ne 0 ];then
308                 echo "ERROR: afm-util status"
309                 lava-test-case afm-util-status-$WGTNAMEF --result fail
310                 continue
311         else
312                 lava-test-case afm-util-status-$WGTNAMEF --result pass
313         fi
314
315         echo "DEBUG: kill $NAMEID ($RID)"
316         do_afm_util kill $NAMEID
317         if [ $? -ne 0 ];then
318                 echo "ERROR: afm-util kill"
319                 lava-test-case afm-util-kill-$WGTNAMEF --result fail
320                 continue
321         else
322                 lava-test-case afm-util-kill-$WGTNAMEF --result pass
323         fi
324
325         echo "DEBUG: start2 $NAMEID"
326         do_afm_util start $NAMEID > rid
327         if [ $? -ne 0 ];then
328                 echo "ERROR: afm-util start2"
329                 lava-test-case afm-util-start2-$WGTNAMEF --result fail
330                 journalctl -an 200
331                 continue
332         else
333                 lava-test-case afm-util-start2-$WGTNAMEF --result pass
334         fi
335         RID="$(cat rid)"
336         if [ "$RID" == 'null' ];then
337                 echo "ERROR: RID is null"
338                 continue
339         fi
340         sleep 10
341         echo "DEBUG: status2 $NAMEID ($RID)"
342         do_afm_util status $RID
343         if [ $? -ne 0 ];then
344                 echo "ERROR: afm-util status2"
345                 lava-test-case afm-util-status2-$WGTNAMEF --result fail
346                 continue
347         else
348                 lava-test-case afm-util-status2-$WGTNAMEF --result pass
349         fi
350 }
351
352 WGTNAMES=$(grep -o '[a-z-]*.wgt' index.html | sed 's,.wgt$,,' | sed 's,-debug$,,' | sed 's,-test$,,' | sed 's,-coverage$,,' | sort | uniq)
353 for WGTNAME in $WGTNAMES
354 do
355         if [ -e $WGTNAME.wgt ];then
356                 inspect_wgt $WGTNAME.wgt
357                 do_release_test $WGTNAME $WGTNAME.wgt
358         else
359                 echo "WARN: cannot find $WGTNAME.wgt"
360         fi
361         if [ -e $WGTNAME-test.wgt ];then
362                 # wgt-test do not have the same permissions in the config.xml as the parent wgt
363                 # so keep the value from last run
364                 #inspect_wgt $WGTNAME-test.wgt
365                 check_service_running $WGTNAME
366                 if [ $? -eq 1 ];then
367                         do_afm_test $TOPDIR/$WGTNAME-test.wgt
368                         if [ $? -eq 0 ];then
369                                 lava-test-case run-test-$WGTNAME --result pass
370                         else
371                                 lava-test-case run-test-$WGTNAME --result fail
372                         fi
373                 else
374                         echo "DEBUG: $WGTNAME is not running, skipping test"
375                         lava-test-case run-test-$WGTNAME --result skip
376                 fi
377         else
378                 echo "WARN: cannot find $WGTNAME.wgt"
379         fi
380         if [ -e $WGTNAME-debug.wgt ];then
381                 inspect_wgt $WGTNAME-debug.wgt
382                 do_release_test $WGTNAME $WGTNAME-debug.wgt
383         fi
384         if [ -e $WGTNAME-coverage.wgt ];then
385                 inspect_wgt $WGTNAME-coverage.wgt
386                 echo "DEBUG: coverage not handled yet"
387         fi
388 done
389