blob: ac945fe023a78fae225bef4b1a8fa859eb0b24ca [file] [log] [blame]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001#!/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
22# Function to execute curl towards a container (or process) and compare + print result
23# Intended use is for basic test scripts where testing is done with curl and the returned response and payload need to be checked.
24# args: GET|PUT|POST|DELETE <url> <target-response-code> [<json-file>]
25# All calls made to 'localhost:'<port>.
26# Expects env PORT set to intended port number
27# Expects env RESULT to contain the target response body.
BjornMagnussonXA72667f12020-04-24 09:20:18 +020028# Optional env HTTPX shall contain protocol 'http' or 'https'. If not set, 'http' is used. For 'https' all cert errors are ignored
BjornMagnussonXA80a92002020-03-19 14:31:06 +010029# RESULT="*" means that returned payload is not checked, may container any text
BjornMagnussonXA72667f12020-04-24 09:20:18 +020030# RESULT="<text>" means that the returned payload has to match the <text> exactly
BjornMagnussonXA80a92002020-03-19 14:31:06 +010031# RESULT="json:<returned-payload>" means that the returned json payload is compared with the expected result (order of json keys and index is irrelevant)
BjornMagnussonXA72667f12020-04-24 09:20:18 +020032# RESULT="json-array-size:<integer-size>" means that the returned json payload shall contain the number of element given by the <integer-size>
BjornMagnussonXA80a92002020-03-19 14:31:06 +010033# Env BODY contains the response body after the call
34# Any error will stop script execution
35# How to use in a test script: source this file into your bash test script to the make the function available.
36
37do_curl() {
38 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
39 if [ $# -lt 3 ]; then
40 echo "Need 3 or more parameters, <http-operation> <url> <response-code> [file]: "$@
41 echo "Exting test script....."
42 exit 1
43 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +020044
45 if [ -z $HTTPX ]; then
46 if [ "$HTTPX" != "http" ] && [ "$HTTPX" != "https" ]; then
47 echo "Env var HTTPX shall be set to 'http' or 'https'"
48 echo "Exting test script....."
49 exit 1
50 fi
51 PROT="http"
52 else
53 PROT=$HTTPX
54 fi
55
56 curlstr="curl -X "$1" -skw %{http_code} ${PROT}://localhost:$PORT$2 -H accept:*/*"
BjornMagnussonXA80a92002020-03-19 14:31:06 +010057 if [ $# -gt 3 ]; then
58 curlstr=$curlstr" -H Content-Type:application/json --data-binary @"$4
59 fi
60 echo " CMD:"$curlstr
61 res=$($curlstr)
62 status=${res:${#res}-3}
63 body=${res:0:${#res}-3}
64 export body
65 if [ $status -ne $3 ]; then
66 echo " Error status:"$status" Expected status: "$3
67 echo " Body: "$body
68 echo "Exting test script....."
69 exit 1
70 else
71 echo " OK, code: "$status" (Expected)"
72 echo " Body: "$body
73 if [ "$RESULT" == "*" ]; then
74 echo " Body contents not checked"
75 elif [[ "$RESULT" == "json:"* ]]; then
76 result=${RESULT:5:${#RESULT}}
77 #Find dir of the common dir
78 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
79 res=$(python ${DIR}/compare_json.py "$result" "$body")
80 if [ $res -eq 0 ]; then
81 echo " Body as expected"
82 else
83 echo " Expected json body: "$result
84 echo "Exiting....."
85 exit 1
86 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +020087 elif [[ "$RESULT" == "json-array-size:"* ]]; then
88 count=${RESULT:16:${#RESULT}}
89 #Find dir of the common dir
90 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020091 echo $body > ./tmp/.tmp.json
92 res=$(python ${DIR}/count_json_elements.py ./tmp/.tmp.json)
BjornMagnussonXA72667f12020-04-24 09:20:18 +020093 if [ $res -eq $count ]; then
94 echo " Body (array size) as expected"
95 else
96 echo " Expected json array size: "$count
97 echo "Exiting....."
98 exit 1
99 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100100 else
101 body="$(echo $body | tr -d '\n' )"
102 if [ "$RESULT" == "$body" ]; then
103 echo " Body as expected"
104 else
105 echo " Expected body: "$RESULT
106 echo "Exiting....."
107 exit 1
108 fi
109 fi
110 fi
111}