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