blob: dbd42976d55fe3e6f736bd0c2b74805c597d5ac0 [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# Automated test script for mrstub container
21
BjornMagnussonXA72667f12020-04-24 09:20:18 +020022# Run the build_and_start with the same arg as this script
23if [ $# -ne 1 ]; then
24 echo "Usage: ./basic_test nonsecure|secure"
25 exit 1
26fi
27if [ "$1" != "nonsecure" ] && [ "$1" != "secure" ]; then
28 echo "Usage: ./basic_test nonsecure|secure"
29 exit 1
30fi
31
32if [ $1 == "nonsecure" ]; then
33 #Default http port for the simulator
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010034 PORT=3904
BjornMagnussonXA72667f12020-04-24 09:20:18 +020035 # Set http protocol
36 HTTPX="http"
37else
38 #Default https port for the mr-stub
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010039 PORT=3905
BjornMagnussonXA72667f12020-04-24 09:20:18 +020040 # Set https protocol
41 HTTPX="https"
42fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +010043
44# source function to do curl and check result
45. ../common/do_curl_function.sh
46
47echo "=== Stub hello world ==="
48RESULT="OK"
49do_curl GET / 200
50
51echo "=== Stub reset ==="
52RESULT="OK"
53do_curl GET /reset 200
54
55## Test with json response
56
57echo "=== Send a request ==="
58RESULT="*"
59#create payload
60echo "{\"data\": \"data-value\"}" > .tmp.json
61
62do_curl POST '/send-request?operation=PUT&url=/test' 200 .tmp.json
63#Save id for later
64CORRID=$body
65
66echo "=== Fetch a response, shall be empty ==="
67RESULT=""
68do_curl GET '/receive-response?correlationid='$CORRID 204
69
70echo "=== Fetch a request ==="
71RESULT="json:[{\"apiVersion\":\"1.0\",\"operation\":\"PUT\",\"correlationId\":\""$CORRID"\",\"originatorId\": \"849e6c6b420\",\"payload\":{\"data\": \"data-value\"},\"requestId\":\"23343221\", \"target\":\"policy-agent\", \"timestamp\":\"????\", \"type\":\"request\",\"url\":\"/test\"}]"
72do_curl GET '/events/A1-POLICY-AGENT-READ/users/policy-agent' 200
73
74echo "=== Send a json response ==="
BjornMagnussonXA0f9d5172020-06-17 15:43:39 +020075# Create minimal accepted response message, array
BjornMagnussonXA80a92002020-03-19 14:31:06 +010076echo "[{\"correlationId\": \""$CORRID"\", \"message\": {\"test\":\"testresponse\"}, \"status\": \"200\"}]" > .tmp.json
ecaiyanlinux67355802020-05-11 12:53:23 +020077RESULT="{}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +010078do_curl POST /events/A1-POLICY-AGENT-WRITE 200 .tmp.json
79
80echo "=== Fetch a response ==="
81RESULT="{\"test\": \"testresponse\"}200"
82do_curl GET '/receive-response?correlationid='$CORRID 200
83
BjornMagnussonXA0f9d5172020-06-17 15:43:39 +020084echo "=== Send a json response ==="
85# Create minimal accepted response message, single message - no array
86echo "{\"correlationId\": \""$CORRID"\", \"message\": {\"test\":\"testresponse2\"}, \"status\": \"200\"}" > .tmp.json
87RESULT="{}"
88do_curl POST /events/A1-POLICY-AGENT-WRITE 200 .tmp.json
89
90echo "=== Fetch a response ==="
91RESULT="{\"test\": \"testresponse2\"}200"
92do_curl GET '/receive-response?correlationid='$CORRID 200
93
BjornMagnussonXA80a92002020-03-19 14:31:06 +010094### Test with plain text response
95
96echo "=== Send a request ==="
97RESULT="*"
98do_curl POST '/send-request?operation=GET&url=/test2' 200
99#Save id for later
100CORRID=$body
101
102echo "=== Fetch a response, shall be empty ==="
103RESULT=""
104do_curl GET '/receive-response?correlationid='$CORRID 204
105
106echo "=== Fetch a request ==="
107RESULT="json:[{\"apiVersion\":\"1.0\",\"operation\":\"GET\",\"correlationId\":\""$CORRID"\",\"originatorId\": \"849e6c6b420\",\"payload\":{},\"requestId\":\"23343221\", \"target\":\"policy-agent\", \"timestamp\":\"????\", \"type\":\"request\",\"url\":\"/test2\"}]"
108do_curl GET '/events/A1-POLICY-AGENT-READ/users/policy-agent' 200
109
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200110echo "=== Fetch a request, empty. Shall delay 10 seconds ==="
111T1=$SECONDS
112RESULT="json:[]"
113do_curl GET '/events/A1-POLICY-AGENT-READ/users/policy-agent' 200
114T2=$SECONDS
115if [ $(($T2-$T1)) -lt 10 ] || [ $(($T2-$T1)) -gt 15 ]; then
116 echo "Delay to short or too long"$(($T2-$T1))". Should be default 10 sec"
117 exit 1
118else
119 echo " Delay ok:"$(($T2-$T1))
120fi
121
122echo "=== Fetch a request, empty. Shall delay 5 seconds ==="
123T1=$SECONDS
124RESULT="json:[]"
125do_curl GET '/events/A1-POLICY-AGENT-READ/users/policy-agent?timeout=5000' 200
126T2=$SECONDS
127if [ $(($T2-$T1)) -lt 5 ] || [ $(($T2-$T1)) -gt 7 ]; then
ecaiyanlinux67355802020-05-11 12:53:23 +0200128 echo "Delay too short or too long"$(($T2-$T1))". Should be 10 sec"
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200129 exit 1
130else
131 echo " Delay ok:"$(($T2-$T1))
132fi
133
134echo "=== Fetch a request with limit 25, shall be empty. ==="
135RESULT="json-array-size:0"
136do_curl GET '/events/A1-POLICY-AGENT-READ/users/policy-agent?timeout=1000&limit=25' 200
137
138echo "=== Send 5 request to test limit on MR GET==="
139RESULT="*"
140for i in {1..5}
141do
142 do_curl POST '/send-request?operation=GET&url=/test2' 200
143done
144
145echo "=== Fetch a request with limit 3. ==="
146RESULT="json-array-size:3"
147do_curl GET '/events/A1-POLICY-AGENT-READ/users/policy-agent?timeout=1000&limit=3' 200
148
149echo "=== Fetch a request with limit 3, shall return 2. ==="
150RESULT="json-array-size:2"
151do_curl GET '/events/A1-POLICY-AGENT-READ/users/policy-agent?timeout=1000&limit=3' 200
152
153echo "=== Fetch a request with limit 3, shall return 0. ==="
154RESULT="json-array-size:0"
155do_curl GET '/events/A1-POLICY-AGENT-READ/users/policy-agent?timeout=1000&limit=3' 200
156
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100157echo "=== Send a json response ==="
158# Create minimal accepted response message
159echo "[{\"correlationId\": \""$CORRID"\", \"message\": \"test2-response\", \"status\": \"200\"}]" > .tmp.json
ecaiyanlinux67355802020-05-11 12:53:23 +0200160RESULT="{}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100161do_curl POST /events/A1-POLICY-AGENT-WRITE 200 .tmp.json
162
163echo "=== Fetch a response ==="
164RESULT="test2-response200"
165do_curl GET '/receive-response?correlationid='$CORRID 200
166
167echo "********************"
168echo "*** All tests ok ***"
ecaiyanlinux67355802020-05-11 12:53:23 +0200169echo "********************"