Upgraded test env with Kubernetes support
[nonrtric.git] / test / common / api_curl.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2020 Nordix Foundation. All rights reserved.
5 #  ========================================================================
6 #  Licensed under the Apache License, Version 2.0 (the "License");
7 #  you may not use this file except in compliance with the License.
8 #  You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #  Unless required by applicable law or agreed to in writing, software
13 #  distributed under the License is distributed on an "AS IS" BASIS,
14 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #  See the License for the specific language governing permissions and
16 #  limitations under the License.
17 #  ============LICENSE_END=================================================
18 #
19
20 # Generic function to query the agent/ECS via the REST or DMAAP interface.
21 # Used by all other agent/ECS api test functions
22 # If operation sufffix is '_BATCH' the the send and get response is split in two sequences,
23 # one for sending the requests and one for receiving the response
24 # but only when using the DMAAP interface
25 # REST or DMAAP is controlled of the base url of $XX_ADAPTER
26 # arg: (PA|ECS|CR|RC GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url>|<correlation-id> [<file>]) | (PA|ECS RESPONSE <correlation-id>)
27 # (Not for test scripts)
28 __do_curl_to_api() {
29         TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
30     echo " (${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
31         paramError=0
32         input_url=$3
33     if [ $# -gt 0 ]; then
34         if [ $1 == "PA" ]; then
35                         __ADAPTER=$PA_ADAPTER
36                         __ADAPTER_TYPE=$PA_ADAPTER_TYPE
37             __RETRY_CODES=$AGENT_RETRY_CODES
38                         if [ $PMS_VERSION != "V1" ]; then
39                                 input_url=$PMS_API_PREFIX$3
40                         fi
41         elif [ $1 == "ECS" ]; then
42                         __ADAPTER=$ECS_ADAPTER
43                         __ADAPTER_TYPE=$ECS_ADAPTER_TYPE
44             __RETRY_CODES=$ECS_RETRY_CODES
45                 elif [ $1 == "CR" ]; then
46                         __ADAPTER=$CR_ADAPTER
47                         __ADAPTER_TYPE=$CR_ADAPTER_TYPE
48             __RETRY_CODES=""
49                 elif [ $1 == "RC" ]; then
50                         __ADAPTER=$RC_ADAPTER
51                         __ADAPTER_TYPE=$RC_ADAPTER_TYPE
52             __RETRY_CODES=""
53         else
54             paramError=1
55         fi
56                 if [ $__ADAPTER_TYPE == "MR-HTTP" ]; then
57                         __ADAPTER=$MR_ADAPTER_HTTP
58                 fi
59                 if [ $__ADAPTER_TYPE == "MR-HTTPS" ]; then
60                         __ADAPTER=$MR_ADAPTER_HTTPS
61                 fi
62     fi
63     if [ $# -lt 3 ] || [ $# -gt 4 ]; then
64                 paramError=1
65     else
66                 timeout=""
67                 oper=""
68                 file=''
69                 httpcode=" -sw %{http_code}"
70                 accept=''
71                 content=''
72                 batch=0
73                 if [[ $2 == *"_BATCH" ]]; then
74                         batch=1
75                 fi
76                 if [ $# -gt 3 ]; then
77                         content=" -H Content-Type:application/json"
78                 fi
79                 if [ $2 == "GET" ] || [ $2 == "GET_BATCH" ]; then
80                         oper="GET"
81                         if [ $# -ne 3 ]; then
82                                 paramError=1
83                         fi
84                 elif [ $2 == "PUT" ] || [ $2 == "PUT_BATCH" ]; then
85                         oper="PUT"
86                         if [ $# -eq 4 ]; then
87                                 file=" --data-binary @$4"
88                         fi
89                         accept=" -H accept:application/json"
90                 elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then
91                         oper="POST"
92                         accept=" -H accept:*/*"
93                         if [ $# -ne 3 ]; then
94                                 paramError=1
95                         fi
96                 elif [ $2 == "DELETE" ] || [ $2 == "DELETE_BATCH" ]; then
97                         oper="DELETE"
98                         if [ $# -ne 3 ]; then
99                                 paramError=1
100                         fi
101                 elif [ $2 == "RESPONSE" ]; then
102                         oper="RESPONSE"
103                         if [ $# -ne 3 ]; then
104                                 paramError=1
105                         fi
106                         #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
107                         if [ $__ADAPTER_TYPE == "REST" ]; then
108                                 paramError=1
109                         fi
110                 else
111                         paramError=1
112                 fi
113         fi
114
115     if [ $paramError -eq 1 ]; then
116                 ((RES_CONF_FAIL++))
117         echo "-Incorrect number of parameters to __do_curl_to_api " $@ >> $HTTPLOG
118         echo "-Expected: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>)" >> $HTTPLOG
119         echo "-Returning response 000" >> $HTTPLOG
120         echo "-000"
121         return 1
122     fi
123
124     #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
125         if [ $__ADAPTER_TYPE == "REST" ]; then
126         url=" "${__ADAPTER}${input_url}
127         oper=" -X "$oper
128         curlString="curl -k "${oper}${timeout}${httpcode}${accept}${content}${url}${file}
129         echo " CMD: "$curlString >> $HTTPLOG
130                 if [ $# -eq 4 ]; then
131                         echo " FILE: $(<$4)" >> $HTTPLOG
132                 fi
133
134                 # Do retry for configured response codes, otherwise only one attempt
135                 maxretries=5
136                 while [ $maxretries -ge 0 ]; do
137
138                         let maxretries=maxretries-1
139                         res=$($curlString)
140                         retcode=$?
141                         if [ $retcode -ne 0 ]; then
142                                 echo " RETCODE: "$retcode >> $HTTPLOG
143                                 echo "000"
144                                 return 1
145                         fi
146                         retry=0
147                         echo " RESP: "$res >> $HTTPLOG
148                         status=${res:${#res}-3}
149                         if [ ! -z "${__RETRY_CODES}" ]; then
150                                 for retrycode in $__RETRY_CODES; do
151                                         if [ $retrycode -eq $status ]; then
152                                                 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED  >> $HTTPLOG
153                                                 sleep 1
154                                                 retry=1
155                                         fi
156                                 done
157                         fi
158                         if [ $retry -eq 0 ]; then
159                                 maxretries=-1
160                         fi
161                 done
162         echo $res
163         return 0
164     else
165                 if [ $oper != "RESPONSE" ]; then
166                         requestUrl=$input_url
167                         if [ $2 == "PUT" ] && [ $# -eq 4 ]; then
168                                 payload="$(cat $4 | tr -d '\n' | tr -d ' ' )"
169                                 echo "payload: "$payload >> $HTTPLOG
170                                 file=" --data-binary "$payload
171                         elif [ $# -eq 4 ]; then
172                                 echo " FILE: $(cat $4)" >> $HTTPLOG
173                         fi
174                         #urlencode the request url since it will be carried by send-request url
175                         requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))"  "$input_url")
176                         url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
177                         curlString="curl -k -X POST${timeout}${httpcode}${content}${url}${file}"
178                         echo " CMD: "$curlString >> $HTTPLOG
179                         res=$($curlString)
180                         retcode=$?
181                         if [ $retcode -ne 0 ]; then
182                                 echo " RETCODE: "$retcode >> $HTTPLOG
183                                 echo "000"
184                                 return 1
185                         fi
186                         echo " RESP: "$res >> $HTTPLOG
187                         status=${res:${#res}-3}
188                         if [ $status -ne 200 ]; then
189                                 echo "000"
190                                 return 1
191                         fi
192                         cid=${res:0:${#res}-3}
193                         if [[ $batch -eq 1 ]]; then
194                                 echo $cid"200"
195                                 return 0
196                         fi
197                 fi
198                 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
199                         if [ $oper == "RESPONSE" ]; then
200                                 cid=$3
201                         fi
202                         url=" "${__ADAPTER}"/receive-response?correlationid="${cid}
203                         curlString="curl -k -X GET"${timeout}${httpcode}${url}
204                         echo " CMD: "$curlString >> $HTTPLOG
205                         res=$($curlString)
206                         retcode=$?
207                         if [ $retcode -ne 0 ]; then
208                                 echo " RETCODE: "$retcode >> $HTTPLOG
209                                 echo "000"
210                                 return 1
211                         fi
212                         echo " RESP: "$res >> $HTTPLOG
213                         status=${res:${#res}-3}
214                         TS=$SECONDS
215                         # wait of the reply from the agent/ECS...
216                         while [ $status -eq 204 ]; do
217                                 if [ $(($SECONDS - $TS)) -gt 90 ]; then
218                                         echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
219                                         echo "000"
220                                         return 1
221                                 fi
222                                 sleep 0.01
223                                 echo " CMD: "$curlString >> $HTTPLOG
224                                 res=$($curlString)
225                                 if [ $retcode -ne 0 ]; then
226                                         echo " RETCODE: "$retcode >> $HTTPLOG
227                                         echo "000"
228                                         return 1
229                                 fi
230                                 echo " RESP: "$res >> $HTTPLOG
231                                 status=${res:${#res}-3}
232                         done
233                         if [ $status -eq 200 ]; then
234                                 body=${res:0:${#res}-3}
235                                 echo $body
236                                 return 0
237                         fi
238                         echo "Status not 200, returning response 000" >> $HTTPLOG
239                         echo "0000"
240                         return 1
241                 fi
242     fi
243 }