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