Update version number in container-tag for F Maintenance Release
[sim/a1-interface.git] / near-rt-ric-simulator / test / common / test_common.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2020-2022 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 # Function to execute curl and compare + print result
21
22 # Note: Env var PORT must be set to the intended port number
23 # Notre Env var HTTPX must be set to either 'http' or 'https'
24
25 #args: <http-operation> <url> <response-code> [file]
26 #Expects the env $RESULT to contain the expected RESULT.
27 #If json, the RESULT shall begin with 'json:'.
28 #Any json parameter with unknown value shall be given as "????" to skip checking the value.
29 #The requestid parameter is being introduced in the fifth order.
30 do_curl() {
31     if [ $# -lt 3 ]; then
32         echo "Need 3 or more parameters, <http-operation> <url> <response-code> [file]: "$@
33         echo "Exiting test script....."
34         exit 1
35     fi
36     curlstr="curl -X "$1" -skw %{http_code} $HTTPX://localhost:"${PORT}${2}" -H accept:*/*"
37     if [ $# -eq 4 ]; then
38         curlstr=$curlstr" -H Content-Type:application/json --data-binary @"$4
39     fi
40     if [ $# -ge 5 ]; then
41         curlstr=$curlstr" -H Content-Type:application/json --data-binary @"$4" -H requestid:"$5
42     fi
43     echo "  CMD (${BASH_LINENO[0]}):"$curlstr
44     res=$($curlstr)
45     status=${res:${#res}-3}
46     body=${res:0:${#res}-3}
47     if [ $status -ne $3 ]; then
48         echo "  Error status :"$status" Expected status: "$3
49         echo "  Body         :"$body
50         echo "Exiting test script....."
51         exit 1
52     else
53         echo "  OK, code           :"$status"     (Expected)"
54         echo "  Body               :"$body
55         if [ "$RESULT" == "*" ]; then
56             echo "  Body contents not checked"
57         elif [[ "$RESULT" == "json:"* ]]; then
58             result=${RESULT:5:${#RESULT}} #Remove 'json:' from the result string
59             res=$(python ../common/compare_json.py "$result" "$body")
60             if [ $res -eq 0 ]; then
61                 echo "  Expected json body :"$result
62                 echo "  Body as expected"
63             else
64                 echo "  Expected json body :"$result
65                 echo "Exiting....."
66                 exit 1
67             fi
68         else
69             body="$(echo $body | tr -d '\n' )"
70             if [ "$RESULT" == "$body" ]; then
71                 echo "  Expected body      :"$RESULT
72                 echo "  Body as expected"
73             else
74                 echo "  Expected body      :"$RESULT
75                 echo "Exiting....."
76                 exit 1
77             fi
78         fi
79     fi
80 }
81
82 # Triggers publish_event_to_kafka_bus.py script to send msg to Kafka broker
83 # The aim of this function is to realize error related test cases only
84 # The request_id for the Kafka msg, should be passed here as a function parameter
85 publish_response_event() {
86     if [ $# -ne 2 ]; then
87         echo "Need 2 parameter, <request_id> <target_topic>"
88         echo "Exiting test script....."
89         exit 1
90     fi
91     res=$(python ../common/publish_response_event_to_kafka_bus.py "$1" "$2")
92     if [ $res -eq 0 ]; then
93         echo "  Result as expected  "
94     else
95         echo "  Result not expected  "
96         echo "  Exiting.....  "
97         exit 1
98     fi
99 }
100
101 # Creates 16 digits random number using letters and numbers only
102 get_random_number() {
103     r_num=$(tr -dc A-Za-z0-9 < /dev/urandom | head -c 16)
104     echo $r_num
105 }
106
107 # It is being used to cross-test-cases in between A1 sim and external server
108 # The parameter it holds all with regards to External Server relates e.g. HTTPX_EXT_SRV and PORT_EXT_SRV
109 do_curl_ext_srv() {
110     if [ $# -lt 3 ]; then
111         echo "Need 3 or more parameters, <http-operation> <url> <response-code> [file]: "$@
112         echo "Exiting test script....."
113         exit 1
114     fi
115     curlstr="curl -X "$1" -skw %{http_code} $HTTPX_EXT_SRV://localhost:"${PORT_EXT_SRV}${2}" -H accept:*/*"
116     if [ $# -gt 3 ]; then
117         curlstr=$curlstr" -H Content-Type:application/json --data-binary @"$4
118     fi
119     echo "  CMD (${BASH_LINENO[0]}):"$curlstr
120     res=$($curlstr)
121     status=${res:${#res}-3}
122     body=${res:0:${#res}-3}
123     if [ $status -ne $3 ]; then
124         echo "  Error status :"$status" Expected status: "$3
125         echo "  Body         :"$body
126         echo "Exiting test script....."
127         exit 1
128     else
129         echo "  OK, code           :"$status"     (Expected)"
130         echo "  Body               :"$body
131         if [ "$RESULT" == "*" ]; then
132             echo "  Body contents not checked"
133         elif [[ "$RESULT" == "json:"* ]]; then
134             result=${RESULT:5:${#RESULT}} #Remove 'json:' from the result string
135             res=$(python ../common/compare_json.py "$result" "$body")
136             if [ $res -eq 0 ]; then
137                 echo "  Expected json body :"$result
138                 echo "  Body as expected"
139             else
140                 echo "  Expected json body :"$result
141                 echo "Exiting....."
142                 exit 1
143             fi
144         else
145             body="$(echo $body | tr -d '\n' )"
146             if [ "$RESULT" == "$body" ]; then
147                 echo "  Expected body      :"$RESULT
148                 echo "  Body as expected"
149             else
150                 echo "  Expected body      :"$RESULT
151                 echo "Exiting....."
152                 exit 1
153             fi
154         fi
155     fi
156 }