blob: b2d6a24028664661d38b73be63e4c7724bedb65e [file] [log] [blame]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001#!/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# This is a script that contains specific test functions for Policy Agent API
21
22### API functiond towards the Policy Agent
23
24# Generic function to query the agent via the REST or DMAAP interface.
25# Used by all other agent api test functions
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020026# If operation prefix is '_BATCH' the the send and get response is split in two sequences,
27# one for sending the requests and one for receiving the response
28# but only when using the DMAAP interface
BjornMagnussonXA80a92002020-03-19 14:31:06 +010029# REST or DMAAP is controlled of the base url of $ADAPTER
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020030# arg: (GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (RESPONSE <correlation-id>)
BjornMagnussonXA80a92002020-03-19 14:31:06 +010031# (Not for test scripts)
32__do_curl_to_agent() {
33 echo "(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
34 paramError=0
35
36 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
37 paramError=1
38 else
39 timeout=""
40 oper=""
41 file=''
42 httpcode=" -sw %{http_code}"
43 accept=''
44 content=''
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020045 batch=0
46 if [[ $1 == *"_BATCH" ]]; then
47 batch=1
48 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +010049 if [ $# -gt 2 ]; then
50 content=" -H Content-Type:application/json"
51 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020052 if [ $1 == "GET" ] || [ $1 == "GET_BATCH" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +010053 oper="GET"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020054 if [ $# -ne 2 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +010055 paramError=1
56 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020057 elif [ $1 == "PUT" ] || [ $1 == "PUT_BATCH" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +010058 oper="PUT"
59 if [ $# -eq 3 ]; then
60 file=" --data-binary @$3"
61 fi
62 accept=" -H accept:application/json"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020063 elif [ $1 == "POST" ] || [ $1 == "POST_BATCH" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +010064 oper="POST"
65 accept=" -H accept:*/*"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020066 if [ $# -ne 2 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +010067 paramError=1
68 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020069 elif [ $1 == "DELETE" ] || [ $1 == "DELETE_BATCH" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +010070 oper="DELETE"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020071 if [ $# -ne 2 ]; then
72 paramError=1
73 fi
74 elif [ $1 == "RESPONSE" ]; then
75 oper="RESPONSE"
76 if [ $# -ne 2 ]; then
77 paramError=1
78 fi
79 if ! [ $ADAPTER == $DMAAPBASE ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +010080 paramError=1
81 fi
82 else
83 paramError=1
84 fi
85 fi
86
87 if [ $paramError -eq 1 ]; then
88 ((RES_CONF_FAIL++))
89 echo "-Incorrect number of parameters to __do_curl_agent " $@ >> $HTTPLOG
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020090 echo "-Expected: (GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (RESPONSE <correlation-id>) [<file>]" >> $HTTPLOG
BjornMagnussonXA80a92002020-03-19 14:31:06 +010091 echo "-Returning response 000" >> $HTTPLOG
92 echo "-000"
93 return 1
94 fi
95
BjornMagnussonXA72667f12020-04-24 09:20:18 +020096 if [ $ADAPTER == $RESTBASE ] || [ $ADAPTER == $RESTBASE_SECURE ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +010097 url=" "${ADAPTER}${2}
98 oper=" -X "$oper
BjornMagnussonXA72667f12020-04-24 09:20:18 +020099 curlString="curl -k "${oper}${timeout}${httpcode}${accept}${content}${url}${file}
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100100 echo " CMD: "$curlString >> $HTTPLOG
101 if [ $# -eq 3 ]; then
102 echo " FILE: $(<$3)" >> $HTTPLOG
103 fi
104
105 # Do retry for configured response codes, otherwise only one attempt
106 maxretries=5
107 while [ $maxretries -ge 0 ]; do
108
109 let maxretries=maxretries-1
110 res=$($curlString)
111 retcode=$?
112 if [ $retcode -ne 0 ]; then
113 echo " RETCODE: "$retcode >> $HTTPLOG
114 echo "000"
115 return 1
116 fi
117 retry=0
118 echo " RESP: "$res >> $HTTPLOG
119 status=${res:${#res}-3}
120 if [ ! -z "${AGENT_RETRY_CODES}" ]; then
121 for retrycode in $AGENT_RETRY_CODES; do
122 if [ $retrycode -eq $status ]; then
123 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED >> $HTTPLOG
124 sleep 1
125 retry=1
126 fi
127 done
128 fi
129 if [ $retry -eq 0 ]; then
130 maxretries=-1
131 fi
132 done
133 echo $res
134 return 0
135 else
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200136 if [ $oper != "RESPONSE" ]; then
137 requestUrl=$2
138 if [ $1 == "PUT" ] && [ $# -eq 3 ]; then
139 payload="$(cat $3 | tr -d '\n' | tr -d ' ' )"
140 echo "payload: "$payload >> $HTTPLOG
141 file=" --data-binary "$payload
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100142 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200143 #urlencode the request url since it will be carried by send-request url
144 requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$2")
145 url=" "${ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
146 curlString="curl -X POST${timeout}${httpcode}${content}${url}${file}"
147 echo " CMD: "$curlString >> $HTTPLOG
148 res=$($curlString)
149 retcode=$?
150 if [ $retcode -ne 0 ]; then
151 echo " RETCODE: "$retcode >> $HTTPLOG
152 echo "000"
153 return 1
154 fi
155 echo " RESP: "$res >> $HTTPLOG
156 status=${res:${#res}-3}
157 if [ $status -ne 200 ]; then
158 echo "000"
159 return 1
160 fi
161 cid=${res:0:${#res}-3}
162 if [[ $batch -eq 1 ]]; then
163 echo $cid"200"
164 return 0
165 fi
166 fi
167 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
168 if [ $oper == "RESPONSE" ]; then
169 cid=$2
170 fi
171 url=" "${ADAPTER}"/receive-response?correlationid="${cid}
172 curlString="curl -X GET"${timeout}${httpcode}${url}
173 echo " CMD: "$curlString >> $HTTPLOG
174 res=$($curlString)
175 retcode=$?
176 if [ $retcode -ne 0 ]; then
177 echo " RETCODE: "$retcode >> $HTTPLOG
178 echo "000"
179 return 1
180 fi
181 echo " RESP: "$res >> $HTTPLOG
182 status=${res:${#res}-3}
183 TS=$SECONDS
184 # wait of the reply from the agent...
185 while [ $status -eq 204 ]; do
186 if [ $(($SECONDS - $TS)) -gt 90 ]; then
187 echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
188 echo "000"
189 return 1
190 fi
191 sleep 0.01
192 echo " CMD: "$curlString >> $HTTPLOG
193 res=$($curlString)
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 done
202 if [ $status -eq 200 ]; then
203 body=${res:0:${#res}-3}
204 echo $body
205 return 0
206 fi
207 echo "Status not 200, returning response 000" >> $HTTPLOG
208 echo "0000"
209 return 1
210 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100211 fi
212}
213
214
215#########################################################
216#### Test case functions A1 Policy management service
217#########################################################
218
219# This function compare the size, towards a target value, of a json array returned from <url> of the Policy Agent.
220# This is done immediately by setting PASS or FAIL or wait up to and optional timeout before setting PASS or FAIL
221# args: json:<url> <target-value> [<timeout-in-seconds]
222# (Function for test scripts)
223api_equal() {
224 echo "(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
225 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
226 if [[ $1 == "json:"* ]]; then
227 __var_test "Policy Agent" $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT"/" $1 "=" $2 $3
228 return 0
229 fi
230 fi
231
232 ((RES_CONF_FAIL++))
233 __print_err "needs two or three args: json:<json-array-param> <target-value> [ timeout ]" $@
234 return 1
235}
236
237# API Test function: GET /policies
238# args: <response-code> <ric-id>|NORIC <service-id>|NOSERVICE <policy-ype-id>|NOTYPE [ NOID | [<policy-id> <ric-id> <service-id> EMPTY|<policy-type-id> <template-file>]*]
239# (Function for test scripts)
240api_get_policies() {
241 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
242 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
243 ((RES_TEST++))
244 paramError=0
245 if [ $# -lt 4 ]; then
246 paramError=1
247 elif [ $# -eq 5 ] && [ $5 != "NOID" ]; then
248 paramError=1
249 elif [ $# -gt 4 ] && [ $(($#%5)) -ne 4 ]; then
250 paramError=1
251 fi
252
253 if [ $paramError -ne 0 ]; then
254 __print_err "<response-code> <ric-id>|NORIC <service-id>|NOSERVICE <policy-ype-id>|NOTYPE [ NOID | [<policy-id> <ric-id> <service-id> EMPTY|<policy-type-id> <template-file>]*]" $@
255 return 1
256 fi
257 queryparams=""
258 if [ $2 != "NORIC" ]; then
259 queryparams="?ric="$2
260 fi
261 if [ $3 != "NOSERVICE" ]; then
262 if [ -z $queryparams ]; then
263 queryparams="?service="$3
264 else
265 queryparams=$queryparams"&service="$3
266 fi
267 fi
268 if [ $4 != "NOTYPE" ]; then
269 if [ -z $queryparams ]; then
270 queryparams="?type="$4
271 else
272 queryparams=$queryparams"&type="$4
273 fi
274 fi
275
276 query="/policies"$queryparams
277 res="$(__do_curl_to_agent GET $query)"
278 status=${res:${#res}-3}
279
280 if [ $status -ne $1 ]; then
281 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
282 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200283 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100284 return 1
285 fi
286
287 if [ $# -gt 4 ]; then
288 if [ $# -eq 5 ] && [ $5 == "NOID" ]; then
289 targetJson="["
290 else
291 body=${res:0:${#res}-3}
292 targetJson="["
293 arr=(${@:5})
294
295 for ((i=0; i<$(($#-4)); i=i+5)); do
296
297 if [ "$targetJson" != "[" ]; then
298 targetJson=$targetJson","
299 fi
300 targetJson=$targetJson"{\"id\":\"${arr[$i]}\",\"lastModified\":\"????\",\"ric\":\"${arr[$i+1]}\",\"service\":\"${arr[$i+2]}\",\"type\":"
301 if [ "${arr[$i+3]}" == "EMPTY" ]; then
302 targetJson=$targetJson"\"\","
303 else
304 targetJson=$targetJson"\"${arr[$i+3]}\","
305 fi
306 file=".p.json"
307 sed 's/XXX/'${arr[$i]}'/g' ${arr[$i+4]} > $file
308 json=$(cat $file)
309 targetJson=$targetJson"\"json\":"$json"}"
310 done
311 fi
312
313 targetJson=$targetJson"]"
314 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200315 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100316
317 if [ $res -ne 0 ]; then
318 echo -e $RED" FAIL, returned body not correct"$ERED
319 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200320 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100321 return 1
322 fi
323 fi
324
325 ((RES_PASS++))
326 echo -e $GREEN" PASS"$EGREEN
327 return 0
328
329}
330
331# API Test function: GET /policy
332#args: <response-code> <policy-id> [<template-file>]
333# (Function for test scripts)
334api_get_policy() {
335 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
336 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
337 ((RES_TEST++))
338
339 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
340 __print_err "<response-code> <policy-id> [<template-file>] " $@
341 return 1
342 fi
343
344 query="/policy?id=$2"
345 res="$(__do_curl_to_agent GET $query)"
346 status=${res:${#res}-3}
347
348 if [ $status -ne $1 ]; then
349 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
350 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200351 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100352 return 1
353 fi
354
355 if [ $# -eq 3 ]; then
356 #Create a policy json to compare with
357 body=${res:0:${#res}-3}
358 file=".p.json"
359 sed 's/XXX/'${2}'/g' $3 > $file
360 targetJson=$(< $file)
361 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200362 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100363 if [ $res -ne 0 ]; then
364 echo -e $RED" FAIL, returned body not correct"$ERED
365 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200366 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100367 return 1
368 fi
369 fi
370
371 ((RES_PASS++))
372 echo -e $GREEN" PASS"$EGREEN
373 return 0
374}
375
376# API Test function: PUT /policy
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200377# args: <response-code> <service-name> <ric-id> <policytype-id> <policy-id> <transient> <template-file> [<count>]
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100378# (Function for test scripts)
379api_put_policy() {
380 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
381 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
382 ((RES_TEST++))
383
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200384 if [ $# -lt 7 ] || [ $# -gt 8 ]; then
385 __print_err "<response-code> <service-name> <ric-id> <policytype-id> <policy-id> <transient> <template-file> [<count>]" $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100386 return 1
387 fi
388
389 ric=$3
390 count=0
391 max=1
392
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200393 if [ $# -eq 8 ]; then
394 max=$8
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100395 fi
396
397 pid=$5
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200398 file=$7
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100399
400 while [ $count -lt $max ]; do
401 query="/policy?id=$pid&ric=$ric&service=$2"
402
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200403 if [ $4 != "NOTYPE" ]; then
404 query=$query"&type=$4"
405 fi
406
407 if [ $6 != NOTRANSIENT ]; then
408 query=$query"&transient=$6"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100409 fi
410
411 file=".p.json"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200412 sed 's/XXX/'${pid}'/g' $7 > $file
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100413 res="$(__do_curl_to_agent PUT $query $file)"
414 status=${res:${#res}-3}
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200415 echo -ne " Creating "$count"("$max")${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100416 if [ $status -ne $1 ]; then
417 let pid=$pid+1
418 echo " Created "$count"?("$max")"
419 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
420 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200421 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100422 return 1
423 fi
424
425 let pid=$pid+1
426 let count=$count+1
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200427 echo -ne " Created "$count"("$max")${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100428 done
429 echo ""
430
431 ((RES_PASS++))
432 echo -e $GREEN" PASS"$EGREEN
433 return 0
434}
435
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200436# API Test function: PUT /policy to run in batch
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200437# args: <response-code> <service-name> <ric-id> <policytype-id> <policy-id> <transient> <template-file> [<count>]
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200438# (Function for test scripts)
439api_put_policy_batch() {
440 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
441 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
442 ((RES_TEST++))
443
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200444 if [ $# -lt 7 ] || [ $# -gt 8 ]; then
445 __print_err "<response-code> <service-name> <ric-id> <policytype-id> <policy-id> <transient> <template-file> [<count>]" $@
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200446 return 1
447 fi
448
449 ric=$3
450 count=0
451 max=1
452
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200453 if [ $# -eq 8 ]; then
454 max=$8
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200455 fi
456
457 pid=$5
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200458 file=$7
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200459 ARR=""
460 while [ $count -lt $max ]; do
461 query="/policy?id=$pid&ric=$ric&service=$2"
462
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200463 if [ $4 != "NOTYPE" ]; then
464 query=$query"&type=$4"
465 fi
466
467 if [ $6 != NOTRANSIENT ]; then
468 query=$query"&transient=$6"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200469 fi
470
471 file=".p.json"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200472 sed 's/XXX/'${pid}'/g' $7 > $file
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200473 res="$(__do_curl_to_agent PUT_BATCH $query $file)"
474 status=${res:${#res}-3}
475 echo -ne " Requested(batch) "$count"("$max")${SAMELINE}"
476
477 if [ $status -ne 200 ]; then
478 let pid=$pid+1
479 echo " Requested(batch) "$count"?("$max")"
480 echo -e $RED" FAIL. Exepected status 200 (in request), got "$status $ERED
481 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200482 __check_stop_at_error
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200483 return 1
484 fi
485 cid=${res:0:${#res}-3}
486 ARR=$ARR" "$cid
487 let pid=$pid+1
488 let count=$count+1
489 echo -ne " Requested(batch) "$count"("$max")${SAMELINE}"
490 done
491
492 echo ""
493 count=0
494 for cid in $ARR; do
495
496 res="$(__do_curl_to_agent RESPONSE $cid)"
497 status=${res:${#res}-3}
498 echo -ne " Created(batch) "$count"("$max")${SAMELINE}"
499
500 if [ $status -ne $1 ]; then
501 let pid=$pid+1
502 echo " Created(batch) "$count"?("$max")"
503 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
504 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200505 __check_stop_at_error
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200506 return 1
507 fi
508
509 let count=$count+1
510 echo -ne " Created(batch) "$count"("$max")${SAMELINE}"
511 done
512
513 echo ""
514
515 ((RES_PASS++))
516 echo -e $GREEN" PASS"$EGREEN
517 return 0
518}
519
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200520# API Test function: PUT /policy to run in i parallel for a number of rics
521# args: <response-code> <service-name> <ric-id-base> <number-of-rics> <policytype-id> <policy-start-id> <transient> <template-file> <count-per-ric> <number-of-threads>
522# (Function for test scripts)
523api_put_policy_parallel() {
524 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
525 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
526 ((RES_TEST++))
527
528 if [ $# -ne 10 ]; then
529 __print_err " <response-code> <service-name> <ric-id-base> <number-of-rics> <policytype-id> <policy-start-id> <transient> <template-file> <count-per-ric> <number-of-threads>" $@
530 return 1
531 fi
532 resp_code=$1; shift;
533 serv=$1; shift
534 ric_base=$1; shift;
535 num_rics=$1; shift;
536 type=$1; shift;
537 start_id=$1; shift;
538 transient=$1; shift;
539 template=$1; shift;
540 count=$1; shift;
541 pids=$1; shift;
542
543 if [ $ADAPTER != $RESTBASE ] && [ $ADAPTER != $RESTBASE_SECURE ]; then
544 echo " Info - api_put_policy_parallel uses only the agent REST interface - create over dmaap in parallel is not supported"
545 echo " Info - will execute over agent REST"
546 fi
547
548 if [ $serv == "NOSERVICE" ]; then
549 serv=""
550 fi
551 query="/policy?service=$serv"
552
553 if [ $type != "NOTYPE" ]; then
554 query=$query"&type=$type"
555 fi
556
557 if [ $transient != NOTRANSIENT ]; then
558 query=$query"&transient=$transient"
559 fi
560
561 urlbase=${ADAPTER}${query}
562
563 for ((i=1; i<=$pids; i++))
564 do
565 echo "" > ".pid${i}.res.txt"
566 echo $resp_code $urlbase $ric_base $num_rics $start_id $template $count $pids $i > ".pid${i}.txt"
567 echo $i
568 done | xargs -n 1 -I{} -P $pids bash -c '{
569 arg=$(echo {})
570 echo " Parallel process $arg started"
571 tmp=$(< ".pid${arg}.txt")
572 python3 ../common/create_policies_process.py $tmp > .pid${arg}.res.txt
573 }'
574 msg=""
575 for ((i=1; i<=$pids; i++))
576 do
577 file=".pid${i}.res.txt"
578 tmp=$(< $file)
579 if [ -z "$tmp" ]; then
580 echo " Process $i : unknown result (result file empty"
581 msg="failed"
582 else
583 res=${tmp:0:1}
584 if [ $res == "0" ]; then
585 echo " Process $i : OK"
586 else
587 echo " Process $i : failed - "${tmp:1}
588 msg="failed"
589 fi
590 fi
591 done
592 if [ -z $msg ]; then
593 echo " $(($count*$num_rics)) policies created/updated"
594 ((RES_PASS++))
595 echo -e $GREEN" PASS"$EGREEN
596 return 0
597 fi
598
599 echo -e $RED" FAIL. One of more processes failed to execute" $ERED
600 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200601 __check_stop_at_error
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200602 return 1
603}
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100604
605# API Test function: DELETE /policy
606# args: <response-code> <policy-id> [count]
607# (Function for test scripts)
608api_delete_policy() {
609 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
610 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
611 ((RES_TEST++))
612
613 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
614 __print_err "<response-code> <policy-id> [count]" $@
615 return 1
616 fi
617
618 count=0
619 max=1
620
621 if [ $# -eq 3 ]; then
622 max=$3
623 fi
624
625 pid=$2
626
627 while [ $count -lt $max ]; do
628 query="/policy?id="$pid
629 res="$(__do_curl_to_agent DELETE $query)"
630 status=${res:${#res}-3}
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200631 echo -ne " Deleting "$count"("$max")${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100632
633 if [ $status -ne $1 ]; then
634 echo " Deleted "$count"?("$max")"
635 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
636 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200637 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100638 return 1
639 fi
640 let pid=$pid+1
641 let count=$count+1
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200642 echo -ne " Deleted "$count"("$max")${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100643 done
644 echo ""
645
646 ((RES_PASS++))
647 echo -e $GREEN" PASS"$EGREEN
648 return 0
649}
650
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200651# API Test function: DELETE /policy to run in batch
652# args: <response-code> <policy-id> [count]
653# (Function for test scripts)
654api_delete_policy_batch() {
655 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
656 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
657 ((RES_TEST++))
658
659 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
660 __print_err "<response-code> <policy-id> [count]" $@
661 return 1
662 fi
663
664 count=0
665 max=1
666
667 if [ $# -eq 3 ]; then
668 max=$3
669 fi
670
671 pid=$2
672 ARR=""
673 while [ $count -lt $max ]; do
674 query="/policy?id="$pid
675 res="$(__do_curl_to_agent DELETE_BATCH $query)"
676 status=${res:${#res}-3}
677 echo -ne " Requested(batch) "$count"("$max")${SAMELINE}"
678
679 if [ $status -ne 200 ]; then
680 let pid=$pid+1
681 echo " Requested(batch) "$count"?("$max")"
682 echo -e $RED" FAIL. Exepected status 200 (in request), got "$status $ERED
683 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200684 __check_stop_at_error
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200685 return 1
686 fi
687 cid=${res:0:${#res}-3}
688 ARR=$ARR" "$cid
689 let pid=$pid+1
690 let count=$count+1
691 echo -ne " Requested(batch) "$count"("$max")${SAMELINE}"
692 done
693
694 echo ""
695
696 count=0
697 for cid in $ARR; do
698
699 res="$(__do_curl_to_agent RESPONSE $cid)"
700 status=${res:${#res}-3}
701 echo -ne " Deleted(batch) "$count"("$max")${SAMELINE}"
702
703 if [ $status -ne $1 ]; then
704 let pid=$pid+1
705 echo " Deleted(batch) "$count"?("$max")"
706 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
707 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200708 __check_stop_at_error
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200709 return 1
710 fi
711
712 let count=$count+1
713 echo -ne " Deleted(batch) "$count"("$max")${SAMELINE}"
714 done
715
716 ((RES_PASS++))
717 echo -e $GREEN" PASS"$EGREEN
718 return 0
719}
720
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200721# API Test function: DELETE /policy to run in i parallel for a number of rics
722# args: <response-code> <number-of-rics> <policy-start-id> <count-per-ric> <number-of-threads>
723# (Function for test scripts)
724api_delete_policy_parallel() {
725 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
726 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
727 ((RES_TEST++))
728
729 if [ $# -ne 5 ]; then
730 __print_err " <response-code> <ric-id-base> <number-of-rics> <policy-start-id> <count-per-ric> <number-of-threads>" $@
731 return 1
732 fi
733 resp_code=$1; shift;
734 num_rics=$1; shift;
735 start_id=$1; shift;
736 count=$1; shift;
737 pids=$1; shift;
738
739 if [ $ADAPTER != $RESTBASE ] && [ $ADAPTER != $RESTBASE_SECURE ]; then
740 echo " Info - api_delete_policy_parallel uses only the agent REST interface - create over dmaap in parallel is not supported"
741 echo " Info - will execute over agent REST"
742 fi
743
744 query="/policy"
745
746 urlbase=${ADAPTER}${query}
747
748 for ((i=1; i<=$pids; i++))
749 do
750 echo "" > ".pid${i}.del.res.txt"
751 echo $resp_code $urlbase $num_rics $start_id $count $pids $i > ".pid${i}.del.txt"
752 echo $i
753 done | xargs -n 1 -I{} -P $pids bash -c '{
754 arg=$(echo {})
755 echo " Parallel process $arg started"
756 tmp=$(< ".pid${arg}.del.txt")
757 python3 ../common/delete_policies_process.py $tmp > .pid${arg}.del.res.txt
758 }'
759 msg=""
760 for ((i=1; i<=$pids; i++))
761 do
762 file=".pid${i}.del.res.txt"
763 tmp=$(< $file)
764 if [ -z "$tmp" ]; then
765 echo " Process $i : unknown result (result file empty"
766 msg="failed"
767 else
768 res=${tmp:0:1}
769 if [ $res == "0" ]; then
770 echo " Process $i : OK"
771 else
772 echo " Process $i : failed - "${tmp:1}
773 msg="failed"
774 fi
775 fi
776 done
777 if [ -z $msg ]; then
778 echo " $(($count*$num_rics)) deleted"
779 ((RES_PASS++))
780 echo -e $GREEN" PASS"$EGREEN
781 return 0
782 fi
783
784 echo -e $RED" FAIL. One of more processes failed to execute" $ERED
785 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200786 __check_stop_at_error
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200787 return 1
788}
789
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100790# API Test function: GET /policy_ids
791# args: <response-code> <ric-id>|NORIC <service-id>|NOSERVICE <type-id>|NOTYPE ([<policy-instance-id]*|NOID)
792# (Function for test scripts)
793api_get_policy_ids() {
794 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
795 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
796 ((RES_TEST++))
797
798 if [ $# -lt 4 ]; then
799 __print_err "<response-code> <ric-id>|NORIC <service-id>|NOSERVICE <type-id>|NOTYPE ([<policy-instance-id]*|NOID)" $@
800 return 1
801 fi
802
803 queryparams=""
804
805 if [ $2 != "NORIC" ]; then
806 queryparams="?ric="$2
807 fi
808
809 if [ $3 != "NOSERVICE" ]; then
810 if [ -z $queryparams ]; then
811 queryparams="?service="$3
812 else
813 queryparams=$queryparams"&service="$3
814 fi
815 fi
816 if [ $4 != "NOTYPE" ]; then
817 if [ -z $queryparams ]; then
818 queryparams="?type="$4
819 else
820 queryparams=$queryparams"&type="$4
821 fi
822 fi
823
824 query="/policy_ids"$queryparams
825 res="$(__do_curl_to_agent GET $query)"
826 status=${res:${#res}-3}
827
828 if [ $status -ne $1 ]; then
829 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
830 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200831 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100832 return 1
833 fi
834
835 if [ $# -gt 4 ]; then
836 body=${res:0:${#res}-3}
837 targetJson="["
838
839 for pid in ${@:5} ; do
840 if [ "$targetJson" != "[" ]; then
841 targetJson=$targetJson","
842 fi
843 if [ $pid != "NOID" ]; then
844 targetJson=$targetJson"\"$pid\""
845 fi
846 done
847
848 targetJson=$targetJson"]"
849 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200850 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100851
852 if [ $res -ne 0 ]; then
853 echo -e $RED" FAIL, returned body not correct"$ERED
854 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200855 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100856 return 1
857 fi
858 fi
859
860 ((RES_PASS++))
861 echo -e $GREEN" PASS"$EGREEN
862 return 0
863}
864
865# API Test function: GET /policy_schema
866# args: <response-code> <policy-type-id> [<schema-file>]
867# (Function for test scripts)
868api_get_policy_schema() {
869 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
870 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
871 ((RES_TEST++))
872
873 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
874 __print_err "<response-code> <policy-type-id> [<schema-file>]" $@
875 return 1
876 fi
877
878 query="/policy_schema?id=$2"
879 res="$(__do_curl_to_agent GET $query)"
880 status=${res:${#res}-3}
881
882 if [ $status -ne $1 ]; then
883 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
884 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200885 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100886 return 1
887 fi
888
889 if [ $# -eq 3 ]; then
890
891 body=${res:0:${#res}-3}
892
893 targetJson=$(< $3)
894 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200895 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100896
897 if [ $res -ne 0 ]; then
898 echo -e $RED" FAIL, returned body not correct"$ERED
899 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200900 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100901 return 1
902 fi
903 fi
904
905 ((RES_PASS++))
906 echo -e $GREEN" PASS"$EGREEN
907 return 0
908}
909
910# API Test function: GET /policy_schemas
911# args: <response-code> <ric-id>|NORIC [<schema-file>|NOFILE]*
912# (Function for test scripts)
913api_get_policy_schemas() {
914 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
915 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
916 ((RES_TEST++))
917
918 if [ $# -lt 2 ]; then
919 __print_err "<response-code> <ric-id>|NORIC [<schema-file>|NOFILE]*" $@
920 return 1
921 fi
922
923 query="/policy_schemas"
924 if [ $2 != "NORIC" ]; then
925 query=$query"?ric="$2
926 fi
927
928 res="$(__do_curl_to_agent GET $query)"
929 status=${res:${#res}-3}
930
931 if [ $status -ne $1 ]; then
932 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
933 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200934 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100935 return 1
936 fi
937
938 if [ $# -gt 2 ]; then
939 body=${res:0:${#res}-3}
940 targetJson="["
941
942 for file in ${@:3} ; do
943 if [ "$targetJson" != "[" ]; then
944 targetJson=$targetJson","
945 fi
946 if [ $file == "NOFILE" ]; then
947 targetJson=$targetJson"{}"
948 else
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200949 targetJson=$targetJson$(< $file)
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100950 fi
951 done
952
953 targetJson=$targetJson"]"
954 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200955 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100956
957 if [ $res -ne 0 ]; then
958 echo -e $RED" FAIL, returned body not correct"$ERED
959 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200960 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100961 return 1
962 fi
963 fi
964
965 ((RES_PASS++))
966 echo -e $GREEN" PASS"$EGREEN
967 return 0
968}
969
970# API Test function: GET /policy_status
971# arg: <response-code> <policy-id> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)
972# (Function for test scripts)
973api_get_policy_status() {
974 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
975 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
976 ((RES_TEST++))
977 if [ $# -lt 4 ] || [ $# -gt 5 ]; then
978 __print_err "<response-code> <policy-id> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)" $@
979 return 1
980 fi
981
982 targetJson=""
983
984 if [ $3 == "STD" ]; then
985 targetJson="{\"enforceStatus\":\"$4\""
986 if [ $# -eq 5 ]; then
987 targetJson=$targetJson",\"reason\":\"$5\""
988 fi
989 targetJson=$targetJson"}"
990 elif [ $3 == "OSC" ]; then
991 targetJson="{\"instance_status\":\"$4\""
992 if [ $# -eq 5 ]; then
993 targetJson=$targetJson",\"has_been_deleted\":\"$5\""
994 fi
995 targetJson=$targetJson",\"created_at\":\"????\"}"
996 else
997 __print_err "<response-code> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)" $@
998 return 1
999 fi
1000
1001 query="/policy_status?id="$2
1002
1003 res="$(__do_curl_to_agent GET $query)"
1004 status=${res:${#res}-3}
1005
1006 if [ $status -ne $1 ]; then
1007 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1008 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001009 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001010 return 1
1011 fi
1012
1013 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1014 body=${res:0:${#res}-3}
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001015 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001016
1017 if [ $res -ne 0 ]; then
1018 echo -e $RED" FAIL, returned body not correct"$ERED
1019 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001020 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001021 return 1
1022 fi
1023
1024 ((RES_PASS++))
1025 echo -e $GREEN" PASS"$EGREEN
1026 return 0
1027}
1028
1029# API Test function: GET /policy_types
1030# args: <response-code> [<ric-id>|NORIC [<policy-type-id>|EMPTY [<policy-type-id>]*]]
1031# (Function for test scripts)
1032api_get_policy_types() {
1033 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1034 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1035 ((RES_TEST++))
1036
1037 if [ $# -lt 1 ]; then
1038 __print_err "<response-code> [<ric-id>|NORIC [<policy-type-id>|EMPTY [<policy-type-id>]*]]" $@
1039 return 1
1040 fi
1041
1042 if [ $# -eq 1 ]; then
1043 query="/policy_types"
1044 elif [ $2 == "NORIC" ]; then
1045 query="/policy_types"
1046 else
1047 query="/policy_types?ric=$2"
1048 fi
1049
1050 res="$(__do_curl_to_agent GET $query)"
1051 status=${res:${#res}-3}
1052
1053 if [ $status -ne $1 ]; then
1054 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1055 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001056 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001057 return 1
1058 fi
1059
1060 if [ $# -gt 2 ]; then
1061 body=${res:0:${#res}-3}
1062 targetJson="["
1063
1064 for pid in ${@:3} ; do
1065 if [ "$targetJson" != "[" ]; then
1066 targetJson=$targetJson","
1067 fi
1068 if [ $pid == "EMPTY" ]; then
1069 pid=""
1070 fi
1071 targetJson=$targetJson"\"$pid\""
1072 done
1073
1074 targetJson=$targetJson"]"
1075 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001076 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001077
1078 if [ $res -ne 0 ]; then
1079 echo -e $RED" FAIL, returned body not correct"$ERED
1080 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001081 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001082 return 1
1083 fi
1084 fi
1085
1086 ((RES_PASS++))
1087 echo -e $GREEN" PASS"$EGREEN
1088 return 0
1089}
1090
1091#########################################################
1092#### Test case functions Health check
1093#########################################################
1094
1095# API Test function: GET /status
1096# args: <response-code>
1097# (Function for test scripts)
1098api_get_status() {
1099 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1100 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1101 ((RES_TEST++))
1102 if [ $# -ne 1 ]; then
1103 __print_err "<response-code>" $@
1104 return 1
1105 fi
1106 query="/status"
1107 res="$(__do_curl_to_agent GET $query)"
1108 status=${res:${#res}-3}
1109
1110 if [ $status -ne $1 ]; then
1111 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1112 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001113 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001114 return 1
1115 fi
1116
1117 ((RES_PASS++))
1118 echo -e $GREEN" PASS"$EGREEN
1119 return 0
1120}
1121
1122#########################################################
1123#### Test case functions RIC Repository
1124#########################################################
1125
1126# API Test function: GET /ric
1127# args: <reponse-code> <management-element-id> [<ric-id>]
1128# (Function for test scripts)
1129api_get_ric() {
1130 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1131 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1132 ((RES_TEST++))
1133 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
1134 __print_err "<reponse-code> <management-element-id> [<ric-id>]" $@
1135 return 1
1136 fi
1137
1138 query="/ric?managedElementId="$2
1139
1140 res="$(__do_curl_to_agent GET $query)"
1141 status=${res:${#res}-3}
1142
1143 if [ $status -ne $1 ]; then
1144 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1145 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001146 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001147 return 1
1148 fi
1149
1150 if [ $# -eq 3 ]; then
1151 body=${res:0:${#res}-3}
1152 if [ "$body" != "$3" ]; then
1153 echo -e $RED" FAIL, returned body not correct"$ERED
1154 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001155 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001156 return 1
1157 fi
1158 fi
1159
1160 ((RES_PASS++))
1161 echo -e $GREEN" PASS"$EGREEN
1162 return 0
1163}
1164
1165# API test function: GET /rics
1166# args: <reponse-code> <policy-type-id>|NOTYPE [<space-separate-string-of-ricinfo>]
1167# example of <space-separate-string-of-ricinfo> = "ricsim_g1_1:me1_ricsim_g1_1,me2_ricsim_g1_1:1,2,4 ricsim_g1_1:me2_........."
1168# format of ric-info: <ric-id>:<list-of-mes>:<list-of-policy-type-ids>
1169# (Function for test scripts)
1170api_get_rics() {
1171 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1172 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1173 ((RES_TEST++))
1174
1175 if [ $# -lt 2 ]; then
1176 __print_err "<reponse-code> <policy-type-id>|NOTYPE [<space-separate-string-of-ricinfo>]" $@
1177 return 1
1178 fi
1179
1180 query="/rics"
1181 if [ $2 != "NOTYPE" ]; then
1182 query="/rics?policyType="$2
1183 fi
1184
1185 res="$(__do_curl_to_agent GET $query)"
1186 status=${res:${#res}-3}
1187
1188 if [ $status -ne $1 ]; then
1189 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1190 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001191 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001192 return 1
1193 fi
1194
1195 if [ $# -gt 2 ]; then
1196 body=${res:0:${#res}-3}
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001197 res=$(python3 ../common/create_rics_json.py ".tmp_rics.json" "$3" )
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001198 if [ $res -ne 0 ]; then
1199 echo -e $RED" FAIL, could not create target ric info json"$ERED
1200 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001201 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001202 return 1
1203 fi
1204
1205 targetJson=$(<.tmp_rics.json)
1206 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001207 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001208 if [ $res -ne 0 ]; then
1209 echo -e $RED" FAIL, returned body not correct"$ERED
1210 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001211 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001212 return 1
1213 fi
1214 fi
1215
1216 ((RES_PASS++))
1217 echo -e $GREEN" PASS"$EGREEN
1218 return 0
1219}
1220
1221##################################################################
1222#### API Test case functions Service registry and supervision ####
1223##################################################################
1224
1225# API test function: PUT /service
1226# args: <response-code> <service-name> <keepalive-timeout> <callbackurl>
1227# (Function for test scripts)
1228api_put_service() {
1229 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1230 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1231 ((RES_TEST++))
1232 if [ $# -ne 4 ]; then
1233 __print_err "<response-code> <service-name> <keepalive-timeout> <callbackurl>" $@
1234 return 1
1235 fi
1236
1237 query="/service"
1238 json="{\"callbackUrl\": \""$4"\",\"keepAliveIntervalSeconds\": \""$3"\",\"serviceName\": \""$2"\"}"
1239 file=".tmp.json"
1240 echo "$json" > $file
1241
1242 res="$(__do_curl_to_agent PUT $query $file)"
1243 status=${res:${#res}-3}
1244
1245 if [ $status -ne $1 ]; then
1246 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1247 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001248 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001249 return 1
1250 fi
1251
1252 ((RES_PASS++))
1253 echo -e $GREEN" PASS"$EGREEN
1254 return 0
1255}
1256
1257# API test function: GET /services
1258#args: <response-code> [ (<query-service-name> <target-service-name> <keepalive-timeout> <callbackurl>) | (NOSERVICE <target-service-name> <keepalive-timeout> <callbackurl> [<target-service-name> <keepalive-timeout> <callbackurl>]* )]
1259# (Function for test scripts)
1260api_get_services() {
1261 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1262 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1263 ((RES_TEST++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001264 #Number of accepted parameters: 1, 2, 4, 7, 10, 13,...
1265 paramError=1
1266 if [ $# -eq 1 ]; then
1267 paramError=0
1268 elif [ $# -eq 2 ] && [ $2 != "NOSERVICE" ]; then
1269 paramError=0
1270 elif [ $# -eq 5 ]; then
1271 paramError=0
1272 elif [ $# -gt 5 ] && [ $2 == "NOSERVICE" ]; then
1273 argLen=$(($#-2))
1274 if [ $(($argLen%3)) -eq 0 ]; then
1275 paramError=0
1276 fi
1277 fi
1278
1279 if [ $paramError -ne 0 ]; then
1280 __print_err "<response-code> [ (<query-service-name> <target-service-name> <keepalive-timeout> <callbackurl>) | (NOSERVICE <target-service-name> <keepalive-timeout> <callbackurl> [<target-service-name> <keepalive-timeout> <callbackurl>]* )]" $@
1281 return 1
1282 fi
1283
1284 query="/services"
1285
1286 if [ $# -gt 1 ] && [ $2 != "NOSERVICE" ]; then
1287 query="/services?name="$2
1288 fi
1289
1290 res="$(__do_curl_to_agent GET $query)"
1291 status=${res:${#res}-3}
1292
1293 if [ $status -ne $1 ]; then
1294 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1295 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001296 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001297 return 1
1298 fi
1299
1300 if [ $# -gt 2 ]; then
1301 variableArgCount=$(($#-2))
1302 body=${res:0:${#res}-3}
1303 targetJson="["
1304 shift; shift;
1305 cntr=0
1306 while [ $cntr -lt $variableArgCount ]; do
1307 servicename=$1; shift;
1308 timeout=$1; shift;
1309 callback=$1; shift;
1310 if [ $cntr -gt 0 ]; then
1311 targetJson=$targetJson","
1312 fi
1313 # timeSinceLastActivitySeconds value cannot be checked since value varies
1314 targetJson=$targetJson"{\"serviceName\": \""$servicename"\",\"keepAliveIntervalSeconds\": "$timeout",\"timeSinceLastActivitySeconds\":\"????\",\"callbackUrl\": \""$callback"\"}"
1315 let cntr=cntr+3
1316 done
1317 targetJson=$targetJson"]"
1318 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001319 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001320 if [ $res -ne 0 ]; then
1321 echo -e $RED" FAIL, returned body not correct"$ERED
1322 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001323 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001324 return 1
1325 fi
1326 fi
1327
1328 ((RES_PASS++))
1329 echo -e $GREEN" PASS"$EGREEN
1330 return 0
1331}
1332
1333# API test function: GET /services (only checking service names)
1334# args: <response-code> [<service-name>]*"
1335# (Function for test scripts)
1336api_get_service_ids() {
1337 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1338 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1339 ((RES_TEST++))
1340
1341 if [ $# -lt 1 ]; then
1342 __print_err "<response-code> [<service-name>]*" $@
1343 return 1
1344 fi
1345
1346 query="/services"
1347 res="$(__do_curl_to_agent GET $query)"
1348 status=${res:${#res}-3}
1349
1350 if [ $status -ne $1 ]; then
1351 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1352 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001353 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001354 return 1
1355 fi
1356
1357 body=${res:0:${#res}-3}
1358 targetJson="["
1359 for rapp in ${@:2} ; do
1360 if [ "$targetJson" != "[" ]; then
1361 targetJson=$targetJson","
1362 fi
1363 targetJson=$targetJson"{\"callbackUrl\":\"????\",\"keepAliveIntervalSeconds\":\"????\",\"serviceName\":\""$rapp"\",\"timeSinceLastActivitySeconds\":\"????\"}"
1364 done
1365
1366 targetJson=$targetJson"]"
1367 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001368 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001369
1370 if [ $res -ne 0 ]; then
1371 echo -e $RED" FAIL, returned body not correct"$ERED
1372 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001373 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001374 return 1
1375 fi
1376
1377 ((RES_PASS++))
1378 echo -e $GREEN" PASS"$EGREEN
1379 return 0
1380}
1381
1382# API test function: DELETE /services
1383# args: <response-code> <service-name>
1384# (Function for test scripts)
1385api_delete_services() {
1386 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1387 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1388 ((RES_TEST++))
1389
1390 if [ $# -ne 2 ]; then
1391 __print_err "<response-code> <service-name>" $@
1392 return 1
1393 fi
1394
1395 query="/services?name="$2
1396 res="$(__do_curl_to_agent DELETE $query)"
1397 status=${res:${#res}-3}
1398
1399 if [ $status -ne $1 ]; then
1400 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1401 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001402 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001403 return 1
1404 fi
1405
1406 ((RES_PASS++))
1407 echo -e $GREEN" PASS"$EGREEN
1408 return 0
1409}
1410
1411# API test function: PUT /services/keepalive
1412# args: <response-code> <service-name>
1413# (Function for test scripts)
1414api_put_services_keepalive() {
1415 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1416 echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1417 ((RES_TEST++))
1418
1419 if [ $# -ne 2 ]; then
1420 __print_err "<response-code> <service-name>" $@
1421 return 1
1422 fi
1423
1424 query="/services/keepalive?name="$2
1425 res="$(__do_curl_to_agent PUT $query)"
1426 status=${res:${#res}-3}
1427
1428 if [ $status -ne $1 ]; then
1429 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1430 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001431 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001432 return 1
1433 fi
1434
1435 ((RES_PASS++))
1436 echo -e $GREEN" PASS"$EGREEN
1437 return 0
1438}
1439