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
10 # http://www.apache.org/licenses/LICENSE-2.0
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=================================================
21 ### Admin API functions producer stub
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 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
28 echo "(${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
29 echo " CMD: $3" >> $HTTPLOG
31 echo " RESP: $res" >> $HTTPLOG
33 if [ $retcode -ne 0 ]; then
34 __log_conf_fail_general " Fatal error when executing curl, response: "$retcode
37 status=${res:${#res}-3}
38 if [ $status -eq $2 ]; then
40 body=${res:0:${#res}-3}
42 echo " TARGET JSON: $jobfile" >> $HTTPLOG
43 res=$(python3 ../common/compare_json.py "$jobfile" "$body")
44 if [ $res -ne 0 ]; then
45 if [ $1 == "TEST" ]; then
53 if [ $1 == "TEST" ]; then
60 if [ $1 == "TEST" ]; then
61 __log_test_fail_status_code $2 $status
63 __log_conf_fail_status_code $2 $status
68 # Prodstub API: Set (or reset) response code for producer supervision
69 # <response-code> <producer-id> [<forced_response_code>]
70 # (Function for test scripts)
71 prodstub_arm_producer() {
73 if [ $# -ne 2 ] && [ $# -ne 3 ]; then
74 __print_err "<response-code> <producer-id> [<forced_response_code>]" $@
78 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/supervision/"$2
80 curlString=$curlString"?response="$3
83 __execute_curl_to_prodstub CONF $1 "$curlString"
87 # Prodstub API: Set (or reset) response code job create
88 # <response-code> <producer-id> <job-id> [<forced_response_code>]
89 # (Function for test scripts)
90 prodstub_arm_job_create() {
92 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
93 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
97 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/create/$2/$3"
99 curlString=$curlString"?response="$4
102 __execute_curl_to_prodstub CONF $1 "$curlString"
106 # Prodstub API: Set (or reset) response code job delete
107 # <response-code> <producer-id> <job-id> [<forced_response_code>]
108 # (Function for test scripts)
109 prodstub_arm_job_delete() {
111 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
112 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
116 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/delete/$2/$3"
117 if [ $# -eq 4 ]; then
118 curlString=$curlString"?response="$4
121 __execute_curl_to_prodstub CONF $1 "$curlString"
125 # Prodstub API: Arm a type of a producer
126 # <response-code> <producer-id> <type-id>
127 # (Function for test scripts)
128 prodstub_arm_type() {
130 if [ $# -ne 3 ]; then
131 __print_err "<response-code> <producer-id> <type-id>" $@
135 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/type/$2/$3"
137 __execute_curl_to_prodstub CONF $1 "$curlString"
141 # Prodstub API: Disarm a type in a producer
142 # <response-code> <producer-id> <type-id>
143 # (Function for test scripts)
144 prodstub_disarm_type() {
146 if [ $# -ne 3 ]; then
147 __print_err "<response-code> <producer-id> <type-id>" $@
151 curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_LOCALHOST/arm/type/$2/$3"
153 __execute_curl_to_prodstub CONF $1 "$curlString"
157 # Prodstub API: Get job data for a job and compare with a target job json
158 # <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
159 # (Function for test scripts)
160 prodstub_check_jobdata() {
162 if [ $# -ne 7 ]; then
163 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
168 jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
170 _log_test_fail_general "Template file "$7" for jobdata, does not exist"
173 targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"ei_job_data\":$jobfile}"
175 echo "$targetJson" > $file
177 curlString="curl -X GET -skw %{http_code} $PROD_STUB_LOCALHOST/jobdata/$2/$3"
179 __execute_curl_to_prodstub TEST $1 "$curlString" $file
183 # Prodstub API: Delete the job data
184 # <response-code> <producer-id> <job-id>
185 # (Function for test scripts)
186 prodstub_delete_jobdata() {
188 if [ $# -ne 3 ]; then
189 __print_err "<response-code> <producer-id> <job-id> " $@
192 curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_LOCALHOST/jobdata/$2/$3"
194 __execute_curl_to_prodstub CONF $1 "$curlString"
198 # Tests if a variable value in the prod stub is equal to a target value and and optional timeout.
199 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
200 # equal to the target or not.
201 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
202 # before setting pass or fail depending on if the variable value becomes equal to the target
204 # (Function for test scripts)
206 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
207 __var_test "PRODSTUB" "$LOCALHOST$PROD_STUB_EXTERNAL_PORT/counter/" $1 "=" $2 $3
209 __print_err "Wrong args to prodstub_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@