blob: f30b87610e47dafb3ef1920e85fad9eece5c99fd [file] [log] [blame]
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001#!/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### Admin API functions producer stub
22
23
24# Excute a curl cmd towards the prodstub simulator and check the response code.
25# args: TEST|CONF <expected-response-code> <curl-cmd-string> [<json-file-to-compare-output>]
26__execute_curl_to_prodstub() {
27 #echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
28 echo "(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
29 echo " CMD: $3" >> $HTTPLOG
30 res="$($3)"
31 echo " RESP: $res" >> $HTTPLOG
32 retcode=$?
33 if [ $retcode -ne 0 ]; then
34 echo " RETCODE: "$retcode
35 echo -e $RED" FAIL - fatal error when executing curl."$ERED
36 return 1
37 fi
38 status=${res:${#res}-3}
39 if [ $status -eq $2 ]; then
40 if [ $# -eq 4 ]; then
41 body=${res:0:${#res}-3}
42 jobfile=$(cat $4)
43 echo " TARGET JSON: $jobfile" >> $HTTPLOG
44 res=$(python3 ../common/compare_json.py "$jobfile" "$body")
45 if [ $res -ne 0 ]; then
46 echo -e $RED" FAIL, returned body not correct"$ERED
47 return 1
48 fi
49 fi
50 if [ $1 == "TEST" ]; then
51 echo -e $GREEN" PASS"$EGREEN
52 else
53 echo -e $GREEN" OK"$EGREEN
54 fi
55 return 0
56 fi
57 echo -e $RED" FAIL - expected http response: "$2" but got http response: "$status $ERED
58 return 1
59}
60
61# Prodstub API: Set (or reset) response code for producer supervision
62# <response-code> <producer-id> [<forced_response_code>]
63# (Function for test scripts)
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +020064prodstub_arm_producer() {
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020065 echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
66 echo "CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ >> $HTTPLOG
67 if [ $# -ne 2 ] && [ $# -ne 3 ]; then
68 ((RES_CONF_FAIL++))
69 __print_err "<response-code> <producer-id> [<forced_response_code>]" $@
70 return 1
71 fi
72
73 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/supervision/"$2
74 if [ $# -eq 3 ]; then
75 curlString=$curlString"?response="$3
76 fi
77
78 __execute_curl_to_prodstub CONF $1 "$curlString"
79 retcode=$?
80 if [ $? -ne 0 ]; then
81 ((RES_CONF_FAIL++))
82 fi
83 return $retcode
84}
85
86# Prodstub API: Set (or reset) response code job create
87# <response-code> <producer-id> <job-id> [<forced_response_code>]
88# (Function for test scripts)
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +020089prodstub_arm_job_create() {
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020090 echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
91 echo "CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ >> $HTTPLOG
92 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
93 ((RES_CONF_FAIL++))
94 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
95 return 1
96 fi
97
98 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/create/$2/$3"
99 if [ $# -eq 4 ]; then
100 curlString=$curlString"?response="$4
101 fi
102
103 __execute_curl_to_prodstub CONF $1 "$curlString"
104 retcode=$?
105 if [ $? -ne 0 ]; then
106 ((RES_CONF_FAIL++))
107 fi
108 return $retcode
109}
110
111# Prodstub API: Set (or reset) response code job delete
112# <response-code> <producer-id> <job-id> [<forced_response_code>]
113# (Function for test scripts)
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200114prodstub_arm_job_delete() {
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200115 echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
116 echo "CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ >> $HTTPLOG
117 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
118 ((RES_CONF_FAIL++))
119 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
120 return 1
121 fi
122
123 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/delete/$2/$3"
124 if [ $# -eq 4 ]; then
125 curlString=$curlString"?response="$4
126 fi
127
128 __execute_curl_to_prodstub CONF $1 "$curlString"
129 retcode=$?
130 if [ $? -ne 0 ]; then
131 ((RES_CONF_FAIL++))
132 fi
133 return $retcode
134}
135
136# Prodstub API: Arm a type of a producer
137# <response-code> <producer-id> <type-id>
138# (Function for test scripts)
139prodstub_arm_type() {
140 echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
141 echo "CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ >> $HTTPLOG
142 if [ $# -ne 3 ]; then
143 ((RES_CONF_FAIL++))
144 __print_err "<response-code> <producer-id> <type-id>" $@
145 return 1
146 fi
147
148 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/type/$2/$3"
149
150 __execute_curl_to_prodstub CONF $1 "$curlString"
151 retcode=$?
152 if [ $? -ne 0 ]; then
153 ((RES_CONF_FAIL++))
154 fi
155 return $retcode
156}
157
158# Prodstub API: Disarm a type in a producer
159# <response-code> <producer-id> <type-id>
160# (Function for test scripts)
161prodstub_disarm_type() {
162 echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
163 echo "CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ >> $HTTPLOG
164 if [ $# -ne 3 ]; then
165 ((RES_CONF_FAIL++))
166 __print_err "<response-code> <producer-id> <type-id>" $@
167 return 1
168 fi
169
170 curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_LOCALHOST/arm/type/$2/$3"
171
172 __execute_curl_to_prodstub CONF $1 "$curlString"
173 retcode=$?
174 if [ $? -ne 0 ]; then
175 ((RES_CONF_FAIL++))
176 fi
177 return $retcode
178}
179
180# Prodstub API: Get job data for a job and compare with a target job json
181# <response-code> <producer-id> <job-id> <type-id> <target-url> <template-job-file>
182# (Function for test scripts)
183prodstub_check_jobdata() {
184 echo -e $BOLD"TEST(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
185 echo "TEST(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ >> $HTTPLOG
186 if [ $# -ne 6 ]; then
187 ((RES_FAIL++))
188 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <template-job-file>" $@
189 return 1
190 fi
191 if [ -f $6 ]; then
192 jobfile=$(cat $6)
193 jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
194 else
195 echo -e $RED" FAIL. Template file "$6" for jobdata, does not exist"$ERED
196 return 1
197 fi
198 targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"ei_job_data\":$jobfile}"
199 file="./tmp/.p.json"
200 echo "$targetJson" > $file
201
202 curlString="curl -X GET -skw %{http_code} $PROD_STUB_LOCALHOST/jobdata/$2/$3"
203
204 __execute_curl_to_prodstub TEST $1 "$curlString" $file
205 retcode=$?
206 if [ $? -ne 0 ]; then
207 ((RES_FAIL++))
208 fi
209 return $retcode
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200210}
211
212# Tests if a variable value in the prod stub is equal to a target value and and optional timeout.
213# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
214# equal to the target or not.
215# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
216# before setting pass or fail depending on if the variable value becomes equal to the target
217# value or not.
218# (Function for test scripts)
219prodstub_equal() {
220 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
221 __var_test "PRODSTUB" "$LOCALHOST$PROD_STUB_EXTERNAL_PORT/counter/" $1 "=" $2 $3
222 else
223 ((RES_CONF_FAIL++))
224 __print_err "Wrong args to prodstub_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
225 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200226}