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