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