Merge "Add docker-compose file for ECS"
[nonrtric.git] / test / common / cr_api_functions.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 . ../common/api_curl.sh
21
22 ### Admin API functions for the Callback Reciver
23
24
25 # Excute a curl cmd towards a Callback Reciver admin interface and check the response code.
26 # args: <expected-response-code> <curl-cmd-string>
27 __execute_curl_to_cr() {
28         echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
29         echo " CMD: $2" >> $HTTPLOG
30         res="$($2)"
31         echo " RESP: $res" >> $HTTPLOG
32         retcode=$?
33     if [ $retcode -ne 0 ]; then
34                 ((RES_CONF_FAIL++))
35                 echo " RETCODE: "$retcode
36         echo -e $RED" FAIL - fatal error when executing curl."$ERED
37         return 1
38     fi
39     status=${res:${#res}-3}
40     if [ $status -eq $1 ]; then
41         echo -e $GREEN" OK"$EGREEN
42         return 0
43     fi
44     echo -e $RED" FAIL - expected http response: "$1" but got http response: "$status $ERED
45         ((RES_CONF_FAIL++))
46     return 1
47 }
48
49 # Tests if a variable value in the CR is equal to a target value and and optional timeout.
50 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
51 # equal to the target or not.
52 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
53 # before setting pass or fail depending on if the variable value becomes equal to the target
54 # value or not.
55 # (Function for test scripts)
56 cr_equal() {
57         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
58                 __var_test "CR" "$LOCALHOST$CR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
59         else
60                 ((RES_CONF_FAIL++))
61                 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
62         fi
63 }
64
65 # CR API: Check the contents of all current ric sync events from PMS
66 # <response-code> <id> [ EMPTY | ( <ric-id> )+ ]
67 # (Function for test scripts)
68 cr_api_check_all_sync_events() {
69         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
70     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
71         ((RES_TEST++))
72
73         if [ "$PMS_VERSION" != "V2" ]; then
74                 echo -e $RED" FAIL, function not supported"$ERED
75                 ((RES_FAIL++))
76                 __check_stop_at_error
77                 return 1
78         fi
79
80     if [ $# -lt 2 ]; then
81         __print_err "<response-code> <id> [ EMPTY | ( <ric-id> )+ ]" $@
82         return 1
83     fi
84
85         query="/get-all-events/"$2
86         res="$(__do_curl_to_api CR GET $query)"
87         status=${res:${#res}-3}
88
89         if [ $status -ne $1 ]; then
90                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
91                 ((RES_FAIL++))
92                 __check_stop_at_error
93                 return 1
94         fi
95
96         if [ $# -gt 2 ]; then
97                 body=${res:0:${#res}-3}
98                 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
99                         targetJson="["
100                 else
101                         targetJson="["
102                         arr=(${@:3})
103
104                         for ((i=0; i<$(($#-2)); i=i+1)); do
105
106                                 if [ "$targetJson" != "[" ]; then
107                                         targetJson=$targetJson","
108                                 fi
109                                 targetJson=$targetJson"{\"ric_id\":\"${arr[$i]}\",\"event_type\":\"AVAILABLE\"}"
110                         done
111                 fi
112
113                 targetJson=$targetJson"]"
114                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
115                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
116
117                 if [ $res -ne 0 ]; then
118                         echo -e $RED" FAIL, returned body not correct"$ERED
119                         ((RES_FAIL++))
120                         __check_stop_at_error
121                         return 1
122                 fi
123         fi
124         ((RES_PASS++))
125         echo -e $GREEN" PASS"$EGREEN
126         return 0
127 }