6c669c1110e928586ff19afd344f78bcb351480f
[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:"$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             echo $res
57             if [ $res -eq 0 ]; then
58                 echo "  Expected json body :"$result
59                 echo "  Body as expected"
60             else
61                 echo "  Expected json body :"$result
62                 echo "Exiting....."
63                 exit 1
64             fi
65         else
66             body="$(echo $body | tr -d '\n' )"
67             if [ "$RESULT" == "$body" ]; then
68                 echo "  Expected body      :"$RESULT
69                 echo "  Body as expected"
70             else
71                 echo "  Expected body      :"$RESULT
72                 echo "Exiting....."
73                 exit 1
74             fi
75         fi
76     fi
77 }