64afdb613e7270787edc462f54bfb73347a51fd3
[src/qa-testdefinitions.git] / common / scripts / application-lifecycle.sh
1 #!/bin/sh
2
3 set -x
4
5 export TERM=dumb
6
7 AGLDRIVER=agl-driver
8
9 while [ $# -ge 1 ]
10 do
11         case $1 in
12         -b)
13                 shift
14                 BASEURL=$1
15                 shift
16         ;;
17         *)
18                 echo "Unknown argument $1"
19                 exit 1
20         ;;
21         esac
22 done
23
24 if [ -z "$BASEURL" ]; then
25         echo "$0: missing BASEURL"
26         echo "Usage: $0 -b BASEURL"
27         exit 1
28 fi
29
30 do_afm_util()
31 {
32         if [ $SERVICE_USER -eq 1 -o $APPLICATION_USER -eq 1 ];then
33                 su - $AGLDRIVER -c "afm-util $*"
34         else
35                 afm-util $*
36         fi
37         return $?
38 }
39
40 wget -q $BASEURL -O index.html
41 if [ $? -ne 0 ];then
42         echo "ERROR: Cannot wget $BASEURL"
43         exit 1
44 fi
45
46 grep -o '[a-z-]*.wgt' index.html | sort | uniq |
47 while read wgtfile
48 do
49         WGTNAME=$(echo $wgtfile | sed 's,.wgt$,,')
50         SERVICE_PLATFORM=0
51         SERVICE_USER=0
52         APPLICATION_USER=0
53         echo "DEBUG: fetch $wgtfile"
54         wget -q $BASEURL/$wgtfile
55         if [ $? -ne 0 ];then
56                 echo "ERROR: wget from $BASEURL/$wgtfile"
57                 continue
58         fi
59
60         echo "DEBUG: analyse wgt file"
61         unzip $wgtfile
62         if [ $? -ne 0 ];then
63                 # TODO Do not fail yet, busybox unzip seems to "fail with success" when checking CRC
64                 echo "ERROR: cannot unzip $wgtfile"
65         fi
66         if [ -f config.xml ];then
67                 grep hidden config.xml
68                 if [ $? -eq 0 ];then
69                         echo "DEBUG: hidden package"
70                 else
71                         echo "DEBUG: not hidden package"
72                 fi
73                 # a service sets urn:AGL:widget:provided-api
74                 if $(grep "urn:AGL:widget:provided-api" config.xml) ; then
75                     # we are a service, now determine the scope ...
76                     if $(grep "urn:AGL:permission::partner:scope-platform" config.xml) ; then
77                         SERVICE_PLATFORM=1
78                     else
79                         SERVICE_USER=1
80                     fi
81                 else
82                     # we are an application
83                     APPLICATION_USER=1
84                     # no other type known (yet)
85                 fi
86         else
87                 echo "DEBUG: fail to unzip"
88         fi
89
90         echo "DEBUG: list current pkgs"
91         # TODO mktemp
92         LIST='list'
93         afm-util list --all > $LIST
94         if [ $? -ne 0 ];then
95                 echo "ERROR: afm-util list exit with error"
96                 continue
97         fi
98         if [ ! -s "$LIST" ];then
99                 echo "ERROR: afm-util list is empty"
100                 continue
101         fi
102
103         echo "DEBUG: check presence of $WGTNAME"
104         NAMEID=$(grep id\\\":\\\"${WGTNAME}@ $LIST | cut -d\" -f4 | cut -d\\ -f1)
105         if [ ! -z "$NAMEID" ];then
106                 echo "DEBUG: $WGTNAME already installed as $NAMEID"
107                 # need to kill then deinstall
108                 afm-util ps | grep -q $WGTNAME
109                 if [ $? -eq 0 ];then
110                         echo "DEBUG: kill $WGTNAME"
111                         afm-util kill $WGTNAME
112                         if [ $? -ne 0 ];then
113                                 echo "ERROR: afm-util kill"
114                                 lava-test-case afm-util-pre-kill-$WGTNAME --result fail
115                                 continue
116                         else
117                                 lava-test-case afm-util-pre-kill-$WGTNAME --result pass
118                         fi
119                 else
120                         echo "DEBUG: no need to kill $WGTNAME"
121                 fi
122
123                 echo "DEBUG: deinstall $WGTNAME"
124                 afm-util remove $NAMEID
125                 if [ $? -ne 0 ];then
126                         echo "ERROR: afm-util remove"
127                         lava-test-case afm-util-remove-$WGTNAME --result fail
128                         continue
129                 else
130                         lava-test-case afm-util-remove-$WGTNAME --result pass
131                 fi
132         else
133                 echo "DEBUG: $WGTNAME not installed"
134         fi
135         grep id $LIST
136
137         echo "DEBUG: install $wgtfile"
138         OUT="out"
139         afm-util install $wgtfile > $OUT
140         if [ $? -ne 0 ];then
141                 echo "ERROR: afm-util install"
142                 lava-test-case afm-util-install-$WGTNAME --result fail
143                 continue
144         else
145                 lava-test-case afm-util-install-$WGTNAME --result pass
146         fi
147         # message is like \"added\":\"mediaplayer@0.1\"
148         NAMEID=$(grep d\\\":\\\"${WGTNAME}@ $OUT | cut -d\" -f4 | cut -d\\ -f1)
149         if [ -z "$NAMEID" ];then
150                 echo "ERROR: Cannot get nameid"
151                 continue
152         fi
153         echo "DEBUG: $WGTNAME is installed as $NAMEID"
154
155         afm-util list --all > $LIST
156         if [ $? -ne 0 ];then
157                 echo "ERROR: afm-util list exit with error"
158                 continue
159         fi
160         if [ ! -s "$LIST" ];then
161                 echo "ERROR: afm-util list is empty"
162                 continue
163         fi
164         echo "DEBUG: Verify that $WGTNAME is installed"
165         grep -q $NAMEID $LIST
166         if [ $? -ne 0 ];then
167                 echo "ERROR: $WGTNAME is not installed"
168                 # for debugging, give full output
169                 echo "DEBUG: start of list"
170                 cat $LIST
171                 echo "DEBUG: end of list"
172         fi
173
174         afm-util info $NAMEID
175         if [ $? -ne 0 ];then
176                 echo "ERROR: afm-util info"
177                 lava-test-case afm-util-info-$WGTNAME --result fail
178         else
179                 lava-test-case afm-util-info-$WGTNAME --result pass
180         fi
181
182         echo "DEBUG: check if we see the package with systemctl list-units (before start)"
183         systemctl list-units --full | grep "afm.*$WGTNAME"
184         echo "DEBUG: check if we see the package with systemctl -a (before start)"
185         systemctl -a |grep "afm.*$WGTNAME"
186
187         echo "DEBUG: start $NAMEID"
188         do_afm_util start $NAMEID > "rid"
189         if [ $? -ne 0 ];then
190                 echo "ERROR: afm-util start"
191                 lava-test-case afm-util-start-$WGTNAME --result fail
192                 continue
193         else
194                 lava-test-case afm-util-start-$WGTNAME --result pass
195         fi
196
197         echo "DEBUG: check if we see the package with systemctl list-units (after start)"
198         systemctl list-units --full | grep "afm.*$WGTNAME"
199         echo "DEBUG: check if we see the package with systemctl -a (after start)"
200         systemctl -a |grep "afm.*$WGTNAME"
201
202         echo "DEBUG: Get RID for $NAMEID"
203         PSLIST="pslist"
204         afm-util ps > $PSLIST
205         if [ $? -ne 0 ];then
206                 echo "ERROR: afm-util ps"
207                 lava-test-case afm-util-ps-$WGTNAME --result fail
208                 continue
209         else
210                 cat $PSLIST
211                 lava-test-case afm-util-ps-$WGTNAME --result pass
212         fi
213         # TODO, compare RID with the list in $PSLIST"
214         RID="$(cat rid)"
215
216         echo "DEBUG: status $NAMEID ($RID)"
217         afm-util status $RID
218         if [ $? -ne 0 ];then
219                 echo "ERROR: afm-util status"
220                 lava-test-case afm-util-status-$WGTNAME --result fail
221                 continue
222         else
223                 lava-test-case afm-util-status-$WGTNAME --result pass
224         fi
225
226         echo "DEBUG: kill $NAMEID ($RID)"
227         afm-util kill $NAMEID
228         if [ $? -ne 0 ];then
229                 echo "ERROR: afm-util kill"
230                 lava-test-case afm-util-kill-$WGTNAME --result fail
231                 continue
232         else
233                 lava-test-case afm-util-kill-$WGTNAME --result pass
234         fi
235
236         echo "DEBUG: start2 $NAMEID"
237         do_afm_util start $NAMEID
238         if [ $? -ne 0 ];then
239                 echo "ERROR: afm-util start2"
240                 lava-test-case afm-util-start2-$WGTNAME --result fail
241                 continue
242         else
243                 lava-test-case afm-util-start2-$WGTNAME --result pass
244         fi
245 done