Merge "Adding owner to jobInfo"
[nonrtric.git] / test / common / prodstub_api_functions.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
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     TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
28     echo "(${BASH_LINENO[0]}) - ${TIMESTAMP}: ${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         __log_conf_fail_general " Fatal error when executing curl, response: "$retcode
35         return 1
36     fi
37     status=${res:${#res}-3}
38     if [ $status -eq $2 ]; then
39         if [ $# -eq 4 ]; then
40             body=${res:0:${#res}-3}
41             jobfile=$(cat $4)
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
46                     __log_test_fail_body
47                  else
48                     __log_conf_fail_body
49                 fi
50                 return 1
51             fi
52         fi
53         if [ $1 == "TEST" ]; then
54             __log_test_pass
55         else
56             __log_conf_ok
57         fi
58         return 0
59     fi
60     if [ $1 == "TEST" ]; then
61         __log_test_fail_status_code $2 $status
62         else
63         __log_conf_fail_status_code $2 $status
64     fi
65     return 1
66 }
67
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() {
72         __log_conf_start $@
73         if [ $# -ne 2 ] && [ $# -ne 3 ]; then
74                 __print_err "<response-code> <producer-id> [<forced_response_code>]" $@
75                 return 1
76         fi
77
78     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/supervision/"$2
79         if [ $# -eq 3 ]; then
80                 curlString=$curlString"?response="$3
81         fi
82
83     __execute_curl_to_prodstub CONF $1 "$curlString"
84     return $?
85 }
86
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() {
91         __log_conf_start $@
92         if [ $# -ne 3 ] && [ $# -ne 4 ]; then
93                 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
94                 return 1
95         fi
96
97     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/create/$2/$3"
98         if [ $# -eq 4 ]; then
99                 curlString=$curlString"?response="$4
100         fi
101
102     __execute_curl_to_prodstub CONF $1 "$curlString"
103     return $?
104 }
105
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() {
110         __log_conf_start $@
111         if [ $# -ne 3 ] && [ $# -ne 4 ]; then
112                 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
113                 return 1
114         fi
115
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
119         fi
120
121     __execute_curl_to_prodstub CONF $1 "$curlString"
122     return $?
123 }
124
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() {
129         __log_conf_start $@
130         if [ $# -ne 3 ]; then
131                 __print_err "<response-code> <producer-id> <type-id>" $@
132                 return 1
133         fi
134
135     curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/type/$2/$3"
136
137     __execute_curl_to_prodstub CONF $1 "$curlString"
138     return $?
139 }
140
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() {
145         __log_conf_start $@
146         if [ $# -ne 3 ]; then
147                 __print_err "<response-code> <producer-id> <type-id>" $@
148                 return 1
149         fi
150
151     curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_LOCALHOST/arm/type/$2/$3"
152
153     __execute_curl_to_prodstub CONF $1 "$curlString"
154     return $?
155 }
156
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> <template-job-file>
159 # (Function for test scripts)
160 prodstub_check_jobdata() {
161         __log_test_start $@
162         if [ $# -ne 6 ]; then
163                 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <template-job-file>" $@
164                 return 1
165         fi
166     if [ -f $6 ]; then
167         jobfile=$(cat $6)
168         jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
169     else
170         _log_test_fail_general "Template file "$6" for jobdata, does not exist"
171         return 1
172     fi
173     targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"ei_job_data\":$jobfile}"
174     file="./tmp/.p.json"
175         echo "$targetJson" > $file
176
177     curlString="curl -X GET -skw %{http_code} $PROD_STUB_LOCALHOST/jobdata/$2/$3"
178
179     __execute_curl_to_prodstub TEST $1 "$curlString" $file
180     return $?
181 }
182
183 # Prodstub API: Delete the job data
184 # <response-code> <producer-id> <job-id>
185 # (Function for test scripts)
186 prodstub_delete_jobdata() {
187         __log_conf_start
188         if [ $# -ne 3 ]; then
189                 __print_err "<response-code> <producer-id> <job-id> " $@
190                 return 1
191         fi
192     curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_LOCALHOST/jobdata/$2/$3"
193
194     __execute_curl_to_prodstub CONF $1 "$curlString"
195     return $?
196 }
197
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
203 # value or not.
204 # (Function for test scripts)
205 prodstub_equal() {
206         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
207                 __var_test "PRODSTUB" "$LOCALHOST$PROD_STUB_EXTERNAL_PORT/counter/" $1 "=" $2 $3
208         else
209                 __print_err "Wrong args to prodstub_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
210         fi
211 }