blob: 611fc4dc446ef3e8e29e814ee4196276b6d018cb [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
22# If operation prefix 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 $ADAPTER
26# arg: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>)
27# (Not for test scripts)
28__do_curl_to_api() {
29 echo "(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
30 paramError=0
31
32 if [ $# -gt 0 ]; then
33 if [ $1 == "PA" ]; then
34 __ADAPTER=$ADAPTER
35 __RESTBASE=$RESTBASE
36 __RESTBASE_SECURE=$RESTBASE_SECURE
37 __RETRY_CODES=$AGENT_RETRY_CODES
38 elif [ $1 == "ECS" ]; then
39 __ADAPTER=$ECS_ADAPTER
40 __RESTBASE=$ECS_RESTBASE
41 __RESTBASE_SECURE=$ECS_RESTBASE_SECURE
42 __RETRY_CODES=$ECS_RETRY_CODES
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010043 elif [ $1 == "CR" ]; then
44 __ADAPTER=$CR_ADAPTER
45 __RESTBASE=$CR_RESTBASE
46 __RESTBASE_SECURE=$CR_RESTBASE_SECURE
47 __RETRY_CODES=""
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020048 else
49 paramError=1
50 fi
51 fi
52 if [ $# -lt 3 ] || [ $# -gt 4 ]; then
53 paramError=1
54 else
55 timeout=""
56 oper=""
57 file=''
58 httpcode=" -sw %{http_code}"
59 accept=''
60 content=''
61 batch=0
62 if [[ $2 == *"_BATCH" ]]; then
63 batch=1
64 fi
65 if [ $# -gt 3 ]; then
66 content=" -H Content-Type:application/json"
67 fi
68 if [ $2 == "GET" ] || [ $2 == "GET_BATCH" ]; then
69 oper="GET"
70 if [ $# -ne 3 ]; then
71 paramError=1
72 fi
73 elif [ $2 == "PUT" ] || [ $2 == "PUT_BATCH" ]; then
74 oper="PUT"
75 if [ $# -eq 4 ]; then
76 file=" --data-binary @$4"
77 fi
78 accept=" -H accept:application/json"
79 elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then
80 oper="POST"
81 accept=" -H accept:*/*"
82 if [ $# -ne 3 ]; then
83 paramError=1
84 fi
85 elif [ $2 == "DELETE" ] || [ $2 == "DELETE_BATCH" ]; then
86 oper="DELETE"
87 if [ $# -ne 3 ]; then
88 paramError=1
89 fi
90 elif [ $2 == "RESPONSE" ]; then
91 oper="RESPONSE"
92 if [ $# -ne 3 ]; then
93 paramError=1
94 fi
95 if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
96 paramError=1
97 fi
98 else
99 paramError=1
100 fi
101 fi
102
103 if [ $paramError -eq 1 ]; then
104 ((RES_CONF_FAIL++))
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100105 echo "-Incorrect number of parameters to __do_curl_to_api " $@ >> $HTTPLOG
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200106 echo "-Expected: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>)" >> $HTTPLOG
107 echo "-Returning response 000" >> $HTTPLOG
108 echo "-000"
109 return 1
110 fi
111
112 if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
113 url=" "${__ADAPTER}${3}
114 oper=" -X "$oper
115 curlString="curl -k "${oper}${timeout}${httpcode}${accept}${content}${url}${file}
116 echo " CMD: "$curlString >> $HTTPLOG
117 if [ $# -eq 4 ]; then
118 echo " FILE: $(<$4)" >> $HTTPLOG
119 fi
120
121 # Do retry for configured response codes, otherwise only one attempt
122 maxretries=5
123 while [ $maxretries -ge 0 ]; do
124
125 let maxretries=maxretries-1
126 res=$($curlString)
127 retcode=$?
128 if [ $retcode -ne 0 ]; then
129 echo " RETCODE: "$retcode >> $HTTPLOG
130 echo "000"
131 return 1
132 fi
133 retry=0
134 echo " RESP: "$res >> $HTTPLOG
135 status=${res:${#res}-3}
136 if [ ! -z "${__RETRY_CODES}" ]; then
137 for retrycode in $__RETRY_CODES; do
138 if [ $retrycode -eq $status ]; then
139 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED >> $HTTPLOG
140 sleep 1
141 retry=1
142 fi
143 done
144 fi
145 if [ $retry -eq 0 ]; then
146 maxretries=-1
147 fi
148 done
149 echo $res
150 return 0
151 else
152 if [ $oper != "RESPONSE" ]; then
153 requestUrl=$3
154 if [ $2 == "PUT" ] && [ $# -eq 4 ]; then
155 payload="$(cat $4 | tr -d '\n' | tr -d ' ' )"
156 echo "payload: "$payload >> $HTTPLOG
157 file=" --data-binary "$payload
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100158 elif [ $# -eq 4 ]; then
159 echo " FILE: $(cat $4)" >> $HTTPLOG
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200160 fi
161 #urlencode the request url since it will be carried by send-request url
162 requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$3")
163 url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
164 curlString="curl -k -X POST${timeout}${httpcode}${content}${url}${file}"
165 echo " CMD: "$curlString >> $HTTPLOG
166 res=$($curlString)
167 retcode=$?
168 if [ $retcode -ne 0 ]; then
169 echo " RETCODE: "$retcode >> $HTTPLOG
170 echo "000"
171 return 1
172 fi
173 echo " RESP: "$res >> $HTTPLOG
174 status=${res:${#res}-3}
175 if [ $status -ne 200 ]; then
176 echo "000"
177 return 1
178 fi
179 cid=${res:0:${#res}-3}
180 if [[ $batch -eq 1 ]]; then
181 echo $cid"200"
182 return 0
183 fi
184 fi
185 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
186 if [ $oper == "RESPONSE" ]; then
187 cid=$3
188 fi
189 url=" "${__ADAPTER}"/receive-response?correlationid="${cid}
190 curlString="curl -k -X GET"${timeout}${httpcode}${url}
191 echo " CMD: "$curlString >> $HTTPLOG
192 res=$($curlString)
193 retcode=$?
194 if [ $retcode -ne 0 ]; then
195 echo " RETCODE: "$retcode >> $HTTPLOG
196 echo "000"
197 return 1
198 fi
199 echo " RESP: "$res >> $HTTPLOG
200 status=${res:${#res}-3}
201 TS=$SECONDS
202 # wait of the reply from the agent/ECS...
203 while [ $status -eq 204 ]; do
204 if [ $(($SECONDS - $TS)) -gt 90 ]; then
205 echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
206 echo "000"
207 return 1
208 fi
209 sleep 0.01
210 echo " CMD: "$curlString >> $HTTPLOG
211 res=$($curlString)
212 if [ $retcode -ne 0 ]; then
213 echo " RETCODE: "$retcode >> $HTTPLOG
214 echo "000"
215 return 1
216 fi
217 echo " RESP: "$res >> $HTTPLOG
218 status=${res:${#res}-3}
219 done
220 if [ $status -eq 200 ]; then
221 body=${res:0:${#res}-3}
222 echo $body
223 return 0
224 fi
225 echo "Status not 200, returning response 000" >> $HTTPLOG
226 echo "0000"
227 return 1
228 fi
229 fi
230}