blob: 17f80a580d5c21d4cb0921fe4056c4a96e208462 [file] [log] [blame]
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001#!/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
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010022# If operation sufffix is '_BATCH' the the send and get response is split in two sequences,
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020023# one for sending the requests and one for receiving the response
24# but only when using the DMAAP interface
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010025# 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>)
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020027# (Not for test scripts)
28__do_curl_to_api() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +010029 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
30 echo " (${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010031 proxyflag=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +010032 if [ ! -z "$KUBE_PROXY_PATH" ]; then
33 if [ $KUBE_PROXY_HTTPX == "http" ]; then
34 proxyflag=" --proxy $KUBE_PROXY_PATH"
35 else
36 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010037 fi
38 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +010039
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020040 paramError=0
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010041 input_url=$3
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020042 if [ $# -gt 0 ]; then
43 if [ $1 == "PA" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010044 __ADAPTER=$PA_ADAPTER
45 __ADAPTER_TYPE=$PA_ADAPTER_TYPE
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020046 __RETRY_CODES=$AGENT_RETRY_CODES
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010047 if [ $PMS_VERSION != "V1" ]; then
48 input_url=$PMS_API_PREFIX$3
49 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020050 elif [ $1 == "ECS" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010051 __ADAPTER=$ECS_ADAPTER
52 __ADAPTER_TYPE=$ECS_ADAPTER_TYPE
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020053 __RETRY_CODES=$ECS_RETRY_CODES
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010054 elif [ $1 == "CR" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010055 __ADAPTER=$CR_ADAPTER
56 __ADAPTER_TYPE=$CR_ADAPTER_TYPE
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010057 __RETRY_CODES=""
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +010058 elif [ $1 == "RC" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010059 __ADAPTER=$RC_ADAPTER
60 __ADAPTER_TYPE=$RC_ADAPTER_TYPE
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +010061 __RETRY_CODES=""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010062 elif [ $1 == "NGW" ]; then
63 __ADAPTER=$NGW_ADAPTER
64 __ADAPTER_TYPE=$NGW_ADAPTER_TYPE
65 __RETRY_CODES=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +010066 elif [ $1 == "DMAAPADP" ]; then
67 __ADAPTER=$DMAAP_ADP_ADAPTER
68 __ADAPTER_TYPE=$DMAAP_ADP_ADAPTER_TYPE
69 __RETRY_CODES=""
70 elif [ $1 == "DMAAPMED" ]; then
71 __ADAPTER=$DMAAP_MED_ADAPTER
72 __ADAPTER_TYPE=$DMAAP_MED_ADAPTER_TYPE
73 __RETRY_CODES=""
74 elif [ $1 == "MRSTUB" ]; then
75 __ADAPTER=$MR_STUB_ADAPTER
76 __ADAPTER_TYPE=$MR_STUB_ADAPTER_TYPE
77 __RETRY_CODES=""
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020078 else
79 paramError=1
80 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010081 if [ $__ADAPTER_TYPE == "MR-HTTP" ]; then
82 __ADAPTER=$MR_ADAPTER_HTTP
83 fi
84 if [ $__ADAPTER_TYPE == "MR-HTTPS" ]; then
85 __ADAPTER=$MR_ADAPTER_HTTPS
86 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020087 fi
88 if [ $# -lt 3 ] || [ $# -gt 4 ]; then
89 paramError=1
90 else
91 timeout=""
92 oper=""
93 file=''
94 httpcode=" -sw %{http_code}"
95 accept=''
96 content=''
97 batch=0
98 if [[ $2 == *"_BATCH" ]]; then
99 batch=1
100 fi
101 if [ $# -gt 3 ]; then
102 content=" -H Content-Type:application/json"
103 fi
104 if [ $2 == "GET" ] || [ $2 == "GET_BATCH" ]; then
105 oper="GET"
106 if [ $# -ne 3 ]; then
107 paramError=1
108 fi
109 elif [ $2 == "PUT" ] || [ $2 == "PUT_BATCH" ]; then
110 oper="PUT"
111 if [ $# -eq 4 ]; then
112 file=" --data-binary @$4"
113 fi
114 accept=" -H accept:application/json"
115 elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then
116 oper="POST"
117 accept=" -H accept:*/*"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100118 if [ $# -eq 4 ]; then
119 file=" --data-binary @$4"
120 accept=" -H accept:application/json"
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200121 fi
122 elif [ $2 == "DELETE" ] || [ $2 == "DELETE_BATCH" ]; then
123 oper="DELETE"
124 if [ $# -ne 3 ]; then
125 paramError=1
126 fi
127 elif [ $2 == "RESPONSE" ]; then
128 oper="RESPONSE"
129 if [ $# -ne 3 ]; then
130 paramError=1
131 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100132 #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
133 if [ $__ADAPTER_TYPE == "REST" ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200134 paramError=1
135 fi
136 else
137 paramError=1
138 fi
139 fi
140
141 if [ $paramError -eq 1 ]; then
142 ((RES_CONF_FAIL++))
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100143 echo "-Incorrect number of parameters to __do_curl_to_api " $@ >> $HTTPLOG
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200144 echo "-Expected: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>)" >> $HTTPLOG
145 echo "-Returning response 000" >> $HTTPLOG
146 echo "-000"
147 return 1
148 fi
149
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100150 #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
151 if [ $__ADAPTER_TYPE == "REST" ]; then
152 url=" "${__ADAPTER}${input_url}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200153 oper=" -X "$oper
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100154 curlString="curl -k $proxyflag "${oper}${timeout}${httpcode}${accept}${content}${url}${file}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200155 echo " CMD: "$curlString >> $HTTPLOG
156 if [ $# -eq 4 ]; then
157 echo " FILE: $(<$4)" >> $HTTPLOG
158 fi
159
160 # Do retry for configured response codes, otherwise only one attempt
161 maxretries=5
162 while [ $maxretries -ge 0 ]; do
163
164 let maxretries=maxretries-1
165 res=$($curlString)
166 retcode=$?
167 if [ $retcode -ne 0 ]; then
168 echo " RETCODE: "$retcode >> $HTTPLOG
169 echo "000"
170 return 1
171 fi
172 retry=0
173 echo " RESP: "$res >> $HTTPLOG
174 status=${res:${#res}-3}
175 if [ ! -z "${__RETRY_CODES}" ]; then
176 for retrycode in $__RETRY_CODES; do
177 if [ $retrycode -eq $status ]; then
178 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED >> $HTTPLOG
179 sleep 1
180 retry=1
181 fi
182 done
183 fi
184 if [ $retry -eq 0 ]; then
185 maxretries=-1
186 fi
187 done
188 echo $res
189 return 0
190 else
191 if [ $oper != "RESPONSE" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100192 requestUrl=$input_url
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200193 if [ $2 == "PUT" ] && [ $# -eq 4 ]; then
194 payload="$(cat $4 | tr -d '\n' | tr -d ' ' )"
195 echo "payload: "$payload >> $HTTPLOG
196 file=" --data-binary "$payload
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100197 elif [ $# -eq 4 ]; then
198 echo " FILE: $(cat $4)" >> $HTTPLOG
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200199 fi
200 #urlencode the request url since it will be carried by send-request url
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100201 requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$input_url")
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200202 url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100203 curlString="curl -k $proxyflag -X POST${timeout}${httpcode}${content}${url}${file}"
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200204 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 if [ $status -ne 200 ]; then
215 echo "000"
216 return 1
217 fi
218 cid=${res:0:${#res}-3}
219 if [[ $batch -eq 1 ]]; then
220 echo $cid"200"
221 return 0
222 fi
223 fi
224 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
225 if [ $oper == "RESPONSE" ]; then
226 cid=$3
227 fi
228 url=" "${__ADAPTER}"/receive-response?correlationid="${cid}
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100229 curlString="curl -k $proxyflag -X GET"${timeout}${httpcode}${url}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200230 echo " CMD: "$curlString >> $HTTPLOG
231 res=$($curlString)
232 retcode=$?
233 if [ $retcode -ne 0 ]; then
234 echo " RETCODE: "$retcode >> $HTTPLOG
235 echo "000"
236 return 1
237 fi
238 echo " RESP: "$res >> $HTTPLOG
239 status=${res:${#res}-3}
240 TS=$SECONDS
241 # wait of the reply from the agent/ECS...
242 while [ $status -eq 204 ]; do
243 if [ $(($SECONDS - $TS)) -gt 90 ]; then
244 echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
245 echo "000"
246 return 1
247 fi
248 sleep 0.01
249 echo " CMD: "$curlString >> $HTTPLOG
250 res=$($curlString)
251 if [ $retcode -ne 0 ]; then
252 echo " RETCODE: "$retcode >> $HTTPLOG
253 echo "000"
254 return 1
255 fi
256 echo " RESP: "$res >> $HTTPLOG
257 status=${res:${#res}-3}
258 done
259 if [ $status -eq 200 ]; then
260 body=${res:0:${#res}-3}
261 echo $body
262 return 0
263 fi
264 echo "Status not 200, returning response 000" >> $HTTPLOG
265 echo "0000"
266 return 1
267 fi
268 fi
269}