Run the normal and the test wgt
[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 "aft-test $*"
47         else
48                 afm-test -l $*
49         fi
50         return $?
51 }
52
53 if [ ! -f index.html ] ; then
54         wget -q $BASEURL -O index.html
55         if [ $? -ne 0 ];then
56             echo "ERROR: Cannot wget $BASEURL"
57                 exit 1
58         fi
59 fi
60
61 WGTNAME="$(grep -o '[a-z-]*.wgt' index.html | sed 's,.wgt$,,' | sed 's,-debug$,,' | sed 's,-coverage$,,' | sed 's,-test$,,' | uniq)"
62
63 grep -o '[a-z-]*.wgt' index.html | sort | uniq |
64 while read wgtfile
65 do
66         # remove extension and the debug state
67         #export WGTNAME=$(echo $wgtfile | sed 's,.wgt$,,' | sed 's,-debug$,,' | sed 's,-coverage$,,' | sed 's,-test$,,')
68         echo "DEBUG: fetch $wgtfile"
69
70         if [ ! -f $wgtfile ] ; then
71                 wget -q $BASEURL/$wgtfile
72                 if [ $? -ne 0 ];then
73                         echo "ERROR: wget from $BASEURL/$wgtfile"
74                         continue
75                 fi
76         fi
77 done
78
79 echo "$WGTNAME"
80
81 inspectwgt() 
82 {
83
84         wgtfile=$1
85         CURDIR="$(pwd)"
86         ZIPOUT="$(mktemp -d)"
87         cd $ZIPOUT
88
89         echo "DEBUG: analyse wgt file"
90         unzip $CURDIR/$wgtfile
91         if [ $? -ne 0 ];then
92                 # TODO Do not fail yet, busybox unzip seems to "fail with success" when checking CRC
93                 echo "ERROR: cannot unzip $wgtfile"
94         fi
95         if [ -f config.xml ];then
96                 grep hidden config.xml
97                 if [ $? -eq 0 ];then
98                         echo "DEBUG: hidden package"
99                 else
100                         echo "DEBUG: not hidden package"
101                 fi
102                 # a service sets urn:AGL:widget:provided-api
103                 grep "urn:AGL:widget:provided-api" config.xml
104                 if [ $? -eq 0 ] ; then
105                     # we are a service, now determine the scope ...
106                     grep "urn:AGL:permission::partner:scope-platform" config.xml
107                     if [ $? -eq 0 ];then
108                         export SERVICE_PLATFORM=1
109                     else
110                         export SERVICE_USER=1
111                     fi
112                 else
113                     # we are an application
114                     export APPLICATION_USER=1
115                     # no other type known (yet)
116                 fi
117         else
118                 echo "DEBUG: fail to unzip"
119         fi
120
121         cd $CURDIR
122         rm -r $ZIPOUT
123
124 }
125
126 # cases:
127 # a) (release).wgt -> lifecycle
128 # b) -test.wgt -> run afm-test $wgt
129 # later: c) -coverage wgt -> install coverage AND run afm-test $wgt
130 for RUNIT in runrelease runtest ; do
131
132     SERVICE_PLATFORM=0
133     SERVICE_USER=0
134     APPLICATION_USER=0
135
136     if [ x"runrelease" = x"$RUNIT" ] ; then
137         eval wgtfile="${WGTNAME}.wgt"
138         inspectwgt $wgtfile
139
140         echo "DEBUG: list current pkgs"
141         # TODO mktemp
142         LIST='list'
143         afm-util list --all > $LIST
144         if [ $? -ne 0 ];then
145                 echo "ERROR: afm-util list exit with error"
146                 continue
147         fi
148         if [ ! -s "$LIST" ];then
149                 echo "ERROR: afm-util list is empty"
150                 continue
151         fi
152
153         echo "DEBUG: check presence of $WGTNAME"
154         NAMEID=$(grep id\\\":\\\"${WGTNAME}\" $LIST | cut -d\" -f4 | cut -d\\ -f1)
155         if [ ! -z "$NAMEID" ];then
156                 echo "DEBUG: $WGTNAME already installed as $NAMEID"
157                 # need to kill then deinstall
158                 do_afm_util ps --all | grep -q $WGTNAME
159                 if [ $? -eq 0 ];then
160                         echo "DEBUG: kill $WGTNAME"
161                         do_afm_util kill $WGTNAME
162                         if [ $? -ne 0 ];then
163                                 echo "ERROR: afm-util kill"
164                                 #lava-test-case afm-util-pre-kill-$WGTNAME --result fail
165                                 #continue
166                         #else
167                         #       lava-test-case afm-util-pre-kill-$WGTNAME --result pass
168                         fi
169                 else
170                         echo "DEBUG: no need to kill $WGTNAME"
171                 fi
172
173                 echo "DEBUG: deinstall $WGTNAME"
174                 afm-util remove $NAMEID
175                 if [ $? -ne 0 ];then
176                         echo "ERROR: afm-util remove"
177                         #lava-test-case afm-util-remove-$WGTNAME --result fail
178                         journalctl -b | tail -40
179                         #continue
180                 else
181                         lava-test-case afm-util-remove-$WGTNAME --result pass
182                 fi
183         else
184                 echo "DEBUG: $WGTNAME not installed"
185         fi
186         grep id $LIST
187
188         echo "DEBUG: install $wgtfile"
189         OUT="out"
190         afm-util install $wgtfile > $OUT
191         if [ $? -ne 0 ];then
192                 echo "ERROR: afm-util install"
193                 lava-test-case afm-util-install-$WGTNAME --result fail
194                 continue
195         else
196                 lava-test-case afm-util-install-$WGTNAME --result pass
197         fi
198         # message is like \"added\":\"mediaplayer@0.1\"
199         NAMEID=$(grep d\\\":\\\"${WGTNAME}\" $OUT | cut -d\" -f4 | cut -d\\ -f1)
200         if [ -z "$NAMEID" ];then
201                 echo "ERROR: Cannot get nameid"
202                 echo "DEBUG: ========== DUMPING output =========="
203                 cat $OUT
204                 echo "DEBUG: ========== END DUMP =========="
205                 continue
206         fi
207         echo "DEBUG: $WGTNAME is installed as $NAMEID"
208
209         afm-util list --all > $LIST
210         if [ $? -ne 0 ];then
211                 echo "ERROR: afm-util list exit with error"
212                 continue
213         fi
214         if [ ! -s "$LIST" ];then
215                 echo "ERROR: afm-util list is empty"
216                 continue
217         fi
218         echo "DEBUG: Verify that $WGTNAME is installed"
219         grep -q $NAMEID $LIST
220         if [ $? -ne 0 ];then
221                 echo "ERROR: $WGTNAME is not installed"
222                 # for debugging, give full output
223                 echo "DEBUG: start of list"
224                 cat $LIST
225                 echo "DEBUG: end of list"
226         fi
227
228         do_afm_util info $NAMEID
229         if [ $? -ne 0 ];then
230                 echo "ERROR: afm-util info"
231                 lava-test-case afm-util-info-$WGTNAME --result fail
232         else
233                 lava-test-case afm-util-info-$WGTNAME --result pass
234         fi
235
236         echo "DEBUG: check if we see the package with systemctl list-units (before start)"
237         systemctl list-units --full | grep "afm.*$WGTNAME--"
238         echo "DEBUG: check if we see the package with systemctl -a (before start)"
239         systemctl -a |grep "afm.*$WGTNAME--"
240
241         echo "DEBUG: start $NAMEID"
242         do_afm_util start $NAMEID > "rid"
243         if [ $? -ne 0 ];then
244                 echo "ERROR: afm-util start"
245                 lava-test-case afm-util-start-$WGTNAME --result fail
246                 journalctl -an 200
247                 continue
248         else
249                 lava-test-case afm-util-start-$WGTNAME --result pass
250         fi
251
252         echo "DEBUG: check if we see the package with systemctl list-units (after start)"
253         systemctl list-units --full | grep "afm.*$WGTNAME--"
254         echo "DEBUG: check if we see the package with systemctl -a (after start)"
255         systemctl -a |grep "afm.*$WGTNAME--"
256
257         echo "DEBUG: Get RID for $NAMEID"
258         PSLIST="pslist"
259         afm-util ps --all > $PSLIST
260         if [ $? -ne 0 ];then
261                 echo "ERROR: afm-util ps"
262                 lava-test-case afm-util-ps-$WGTNAME --result fail
263                 continue
264         else
265                 cat $PSLIST
266                 lava-test-case afm-util-ps-$WGTNAME --result pass
267         fi
268         # TODO, compare RID with the list in $PSLIST"
269         RID="$(cat rid)"
270         if [ "$RID" == 'null' ];then
271                 sleep 20
272                 echo "DEBUG: retry start $NAMEID"
273                 do_afm_util start $NAMEID > "rid"
274                 if [ $? -ne 0 ];then
275                         echo "ERROR: afm-util start"
276                         lava-test-case afm-util-start-$WGTNAME --result fail
277                         continue
278                 fi
279                 RID="$(cat rid)"
280         fi
281
282         if [ "$RID" == 'null' ];then
283                 echo "ERROR: RID is null, service fail to start"
284                 lava-test-case afm-util-status-$WGTNAME --result fail
285                 continue
286         fi
287
288         echo "DEBUG: status $NAMEID ($RID)"
289         do_afm_util status $RID
290         if [ $? -ne 0 ];then
291                 echo "ERROR: afm-util status"
292                 lava-test-case afm-util-status-$WGTNAME --result fail
293                 continue
294         else
295                 lava-test-case afm-util-status-$WGTNAME --result pass
296         fi
297
298         echo "DEBUG: kill $NAMEID ($RID)"
299         do_afm_util kill $NAMEID
300         if [ $? -ne 0 ];then
301                 echo "ERROR: afm-util kill"
302                 lava-test-case afm-util-kill-$WGTNAME --result fail
303                 continue
304         else
305                 lava-test-case afm-util-kill-$WGTNAME --result pass
306         fi
307
308         echo "DEBUG: start2 $NAMEID"
309         do_afm_util start $NAMEID > rid
310         if [ $? -ne 0 ];then
311                 echo "ERROR: afm-util start2"
312                 lava-test-case afm-util-start2-$WGTNAME --result fail
313                 journalctl -an 200
314                 continue
315         else
316                 lava-test-case afm-util-start2-$WGTNAME --result pass
317         fi
318         RID="$(cat rid)"
319         if [ "$RID" == 'null' ];then
320                 echo "ERROR: RID is null"
321                 continue
322         fi
323         sleep 120
324         echo "DEBUG: status2 $NAMEID ($RID)"
325         do_afm_util status $RID
326         if [ $? -ne 0 ];then
327                 echo "ERROR: afm-util status2"
328                 lava-test-case afm-util-status2-$WGTNAME --result fail
329                 continue
330         else
331                 lava-test-case afm-util-status2-$WGTNAME --result pass
332         fi
333     fi
334     if [ x"runtest" = x"$RUNIT" ] ; then
335         eval wgtfile="${WGTNAME}-test.wgt"
336         inspectwgt $wgtfile
337         afm-test -l $wgtfile
338         journalctl -b | grep ${WGTNAME}-test
339         ( journalctl -b | grep ${WGTNAME}-test | grep ERROR ) || true   
340     fi
341
342 done