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