Add functions test cases for ECS
[nonrtric.git] / test / common / ricsimulator_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
21 ### Admin API functions for the RIC simulator
22
23
24 # Excute a curl cmd towards a ricsimulator and check the response code.
25 # args: <expected-response-code> <curl-cmd-string>
26 __execute_curl_to_sim() {
27         echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
28         echo " CMD: $2" >> $HTTPLOG
29         res="$($2)"
30         echo " RESP: $res" >> $HTTPLOG
31         retcode=$?
32     if [ $retcode -ne 0 ]; then
33                 echo " RETCODE: "$retcode
34         echo -e $RED" FAIL - fatal error when executing curl."$ERED
35         return 1
36     fi
37     status=${res:${#res}-3}
38     if [ $status -eq $1 ]; then
39         echo -e $GREEN" OK"$EGREEN
40         return 0
41     fi
42     echo -e $RED" FAIL - expected http response: "$1" but got http response: "$status $ERED
43     return 1
44 }
45
46 # Tests if a variable value in the ricsimulator is equal to a target value and and optional timeout.
47 # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable is
48 # equal to the target or not.
49 # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
50 # before setting pass or fail depending on if the variable value becomes equal to the target
51 # value or not.
52 # (Function for test scripts)
53 sim_equal() {
54
55         if [ $# -eq 3 ] || [ $# -eq 4 ]; then
56                 app=$1
57                 port=$(__find_sim_port $app)
58                 __var_test $app "$RIC_SIM_LOCALHOST$port/counter/" $2 "=" $3 $4
59                 return 0
60         else
61                 ((RES_CONF_FAIL++))
62                 __print_err "needs three or four args: <ric-id> <sim-param> <target-value> [ timeout ]"
63                 return 1
64         fi
65 }
66
67 # Print a variable value from the RIC sim.
68 # args: <ric-id> <variable-name>
69 # (Function for test scripts)
70 sim_print() {
71
72         if [ $# != 2 ]; then
73                 ((RES_CONF_FAIL++))
74         __print_err "need two args, <ric-id> <sim-param>" $@
75                 exit 1
76         fi
77         app=$1
78         port=$(__find_sim_port $app)
79         echo -e $BOLD"INFO(${BASH_LINENO[0]}): $app, $2 = $(__do_curl $RIC_SIM_LOCALHOST$port/counter/$2)"$EBOLD
80 }
81
82 # Tests if a variable value in the RIC simulator contains the target string and and optional timeout
83 # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
84 # the target or not.
85 # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
86 # before setting pass or fail depending on if the variable value contains the target
87 # value or not.
88 # (Function for test scripts)
89 sim_contains_str() {
90
91         if [ $# -eq 3 ] || [ $# -eq 4 ]; then
92                 app=$1
93                 port=$(__find_sim_port $app)
94                 __var_test $app "$RIC_SIM_LOCALHOST$port/counter/" $2 "contain_str" $3 $4
95                 return 0
96         else
97                 ((RES_CONF_FAIL++))
98                 __print_err "needs three or four args: <ric-id> <sim-param> <target-value> [ timeout ]"
99                 return 1
100         fi
101 }
102
103 # Simulator API: Put a policy type in a ric
104 # args: <response-code> <ric-id> <policy-type-id> <policy-type-file>
105 # (Function for test scripts)
106 sim_put_policy_type() {
107         echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
108         if [ $# -ne 4 ]; then
109                 ((RES_CONF_FAIL++))
110                 __print_err "<response-code> <ric-id> <policy-type-id> <policy-type-file>" $@
111                 return 1
112         fi
113         app=$2
114         res=$(__find_sim_port $app)
115
116     curlString="curl -X PUT -skw %{http_code} $RIC_SIM_LOCALHOST"$res"/policytype?id="$3" -H Content-Type:application/json --data-binary @"$4
117
118         __execute_curl_to_sim $1 "$curlString"
119         return $?
120 }
121
122 # DSimulator API: Delete a policy type in a ric
123 # <response-code> <ric-id> <policy-type-id>
124 # (Function for test scripts)
125 sim_delete_policy_type() {
126         echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
127         if [ $# -ne 3 ]; then
128                 ((RES_CONF_FAIL++))
129                 __print_err "<response-code> <ric-id> <policy_type_id>" $@
130                 return 1
131         fi
132         app=$2
133         res=$(__find_sim_port $app)
134
135     curlString="curl -X DELETE -skw %{http_code} $RIC_SIM_LOCALHOST"$res"/policytype?id="$3
136
137     __execute_curl_to_sim $1 "$curlString"
138         return $?
139 }
140
141 # Simulator API: Delete instances (and status), for one ric
142 # <response-code> <ric-id>
143 # (Function for test scripts)
144 sim_post_delete_instances() {
145         echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
146         if [ $# -ne 2 ]; then
147                 ((RES_CONF_FAIL++))
148                 __print_err "<response-code> <ric-id>" $@
149                 return 1
150         fi
151         app=$2
152         res=$(__find_sim_port $app)
153
154     curlString="curl -X POST -skw %{http_code} $RIC_SIM_LOCALHOST"$res"/deleteinstances"
155
156     __execute_curl_to_sim $1 "$curlString"
157         return $?
158 }
159
160 # Simulator API: Delete all (instances/types/statuses/settings), for one ric
161 # <response-code> <ric-id>
162 # (Function for test scripts)
163 sim_post_delete_all() {
164         echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
165         if [ $# -ne 3 ]; then
166                 ((RES_CONF_FAIL++))
167                 __print_err "<response-code> <numericic-id>" $@
168                 return 1
169         fi
170         app=$2
171         res=$(__find_sim_port $app)
172
173     curlString="curl -X POST -skw %{http_code} $RIC_SIM_LOCALHOST"$res"/deleteall"
174
175     __execute_curl_to_sim $1 "$curlString"
176         return $?
177 }
178
179 # Simulator API: Set (or reset) response code for next A1 message, for one ric
180 # <response-code> <ric-id> [<forced_response_code>]
181 # (Function for test scripts)
182 sim_post_forcedresponse() {
183         echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
184         if [ $# -ne 3 ]; then
185                 ((RES_CONF_FAIL++))
186                 __print_err "<response-code> <ric-id> <forced_response_code>" $@
187                 return 1
188         fi
189         app=$2
190         res=$(__find_sim_port $app)
191
192     curlString="curl -X POST -skw %{http_code} $RIC_SIM_LOCALHOST"$res"/forceresponse"
193         if [ $# -eq 3 ]; then
194                 curlString=$curlString"?code="$3
195         fi
196
197     __execute_curl_to_sim $1 "$curlString"
198         return $?
199 }
200
201 # Simulator API: Set (or reset) A1 response delay, for one ric
202 # <response-code> <ric-id> [<delay-in-seconds>]
203 # (Function for test scripts)
204 sim_post_forcedelay() {
205         echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
206         if [ $# -ne 3 ]; then
207                 ((RES_CONF_FAIL++))
208                 __print_err "<response-code> <ric-id> [<delay-in-seconds>]" $@
209                 return 1
210         fi
211         app=$2
212         res=$(__find_sim_port $app)
213
214     curlString="curl -X POST -skw %{http_code} $RIC_SIM_LOCALHOST$res/forcedelay"
215         if [ $# -eq 3 ]; then
216                 curlString=$curlString"?delay="$3
217         fi
218
219     __execute_curl_to_sim $1 "$curlString"
220         return $?
221 }