Merge "Callout hooks towards external server for create and delete operations"
[sim/a1-interface.git] / near-rt-ric-simulator / test / common / test_common.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 # 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 do_curl() {
30     if [ $# -lt 3 ]; then
31         echo "Need 3 or more parameters, <http-operation> <url> <response-code> [file]: "$@
32         echo "Exiting test script....."
33         exit 1
34     fi
35     curlstr="curl -X "$1" -skw %{http_code} $HTTPX://localhost:"${PORT}${2}" -H accept:*/*"
36     if [ $# -gt 3 ]; then
37         curlstr=$curlstr" -H Content-Type:application/json --data-binary @"$4
38     fi
39     echo "  CMD (${BASH_LINENO[0]}):"$curlstr
40     res=$($curlstr)
41     status=${res:${#res}-3}
42     body=${res:0:${#res}-3}
43     if [ $status -ne $3 ]; then
44         echo "  Error status :"$status" Expected status: "$3
45         echo "  Body         :"$body
46         echo "Exiting test script....."
47         exit 1
48     else
49         echo "  OK, code           :"$status"     (Expected)"
50         echo "  Body               :"$body
51         if [ "$RESULT" == "*" ]; then
52             echo "  Body contents not checked"
53         elif [[ "$RESULT" == "json:"* ]]; then
54             result=${RESULT:5:${#RESULT}} #Remove 'json:' from the result string
55             res=$(python ../common/compare_json.py "$result" "$body")
56             if [ $res -eq 0 ]; then
57                 echo "  Expected json body :"$result
58                 echo "  Body as expected"
59             else
60                 echo "  Expected json body :"$result
61                 echo "Exiting....."
62                 exit 1
63             fi
64         else
65             body="$(echo $body | tr -d '\n' )"
66             if [ "$RESULT" == "$body" ]; then
67                 echo "  Expected body      :"$RESULT
68                 echo "  Body as expected"
69             else
70                 echo "  Expected body      :"$RESULT
71                 echo "Exiting....."
72                 exit 1
73             fi
74         fi
75     fi
76 }
77
78 do_curl_ext_srv() {
79     if [ $# -lt 3 ]; then
80         echo "Need 3 or more parameters, <http-operation> <url> <response-code> [file]: "$@
81         echo "Exiting test script....."
82         exit 1
83     fi
84     curlstr="curl -X "$1" -skw %{http_code} $HTTPX_EXT_SRV://localhost:"${PORT_EXT_SRV}${2}" -H accept:*/*"
85     if [ $# -gt 3 ]; then
86         curlstr=$curlstr" -H Content-Type:application/json --data-binary @"$4
87     fi
88     echo "  CMD (${BASH_LINENO[0]}):"$curlstr
89     res=$($curlstr)
90     status=${res:${#res}-3}
91     body=${res:0:${#res}-3}
92     if [ $status -ne $3 ]; then
93         echo "  Error status :"$status" Expected status: "$3
94         echo "  Body         :"$body
95         echo "Exiting test script....."
96         exit 1
97     else
98         echo "  OK, code           :"$status"     (Expected)"
99         echo "  Body               :"$body
100         if [ "$RESULT" == "*" ]; then
101             echo "  Body contents not checked"
102         elif [[ "$RESULT" == "json:"* ]]; then
103             result=${RESULT:5:${#RESULT}} #Remove 'json:' from the result string
104             res=$(python ../common/compare_json.py "$result" "$body")
105             if [ $res -eq 0 ]; then
106                 echo "  Expected json body :"$result
107                 echo "  Body as expected"
108             else
109                 echo "  Expected json body :"$result
110                 echo "Exiting....."
111                 exit 1
112             fi
113         else
114             body="$(echo $body | tr -d '\n' )"
115             if [ "$RESULT" == "$body" ]; then
116                 echo "  Expected body      :"$RESULT
117                 echo "  Body as expected"
118             else
119                 echo "  Expected body      :"$RESULT
120                 echo "Exiting....."
121                 exit 1
122             fi
123         fi
124     fi
125 }