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