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=================================================
20 # This is a script that contains container/service management functions and test functions for Producer stub
22 ## Access to Prod stub sim
24 PROD_STUB_HTTPX="http"
25 PROD_STUB_HOST_NAME=$LOCALHOST_NAME
26 PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_PORT
28 #Docker/Kube internal path
29 if [ $RUNMODE == "KUBE" ]; then
30 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_PORT
32 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME":"$PROD_STUB_INTERNAL_PORT
35 # Set http as the protocol to use for all communication to the Producer stub
37 # (Function for test scripts)
38 use_prod_stub_http() {
39 echo -e $BOLD"Producer stub protocol setting"$EBOLD
40 echo -e " Using $BOLD http $EBOLD towards Producer stub"
42 PROD_STUB_HTTPX="http"
43 PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_PORT
45 if [ $RUNMODE == "KUBE" ]; then
46 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_PORT
48 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME":"$PROD_STUB_INTERNAL_PORT
54 # Set https as the protocol to use for all communication to the Producer stub
56 # (Function for test scripts)
57 use_prod_stub_https() {
58 echo -e $BOLD"Producer stub protocol setting"$EBOLD
59 echo -e " Using $BOLD https $EBOLD towards Producer stub"
61 PROD_STUB_HTTPX="https"
62 PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_SECURE_PORT
64 if [ $RUNMODE == "KUBE" ]; then
65 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_SECURE_PORT
67 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME":"$PROD_STUB_INTERNAL_SECURE_PORT
72 ### Admin API functions producer stub
74 ###########################
75 ### Producer stub functions
76 ###########################
78 # Start the Producer stub in the simulator group
80 # (Function for test scripts)
83 echo -e $BOLD"Starting $PROD_STUB_DISPLAY_NAME"$EBOLD
85 if [ $RUNMODE == "KUBE" ]; then
87 # Check if app shall be fully managed by the test script
88 __check_included_image "PRODSTUB"
91 # Check if app shall only be used by the testscipt
92 __check_prestarted_image "PRODSTUB"
95 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
96 echo -e $RED"The $ECS_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
97 echo -e $RED"The $ECS_APP_NAME will not be started"$ERED
100 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
101 echo -e $RED"The $ECS_APP_NAME app is included both as managed and prestarted in this test script"$ERED
102 echo -e $RED"The $ECS_APP_NAME will not be started"$ERED
106 if [ $retcode_p -eq 0 ]; then
107 echo -e " Using existing $PROD_STUB_APP_NAME deployment and service"
108 echo " Setting RC replicas=1"
109 __kube_scale deployment $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE 1
112 if [ $retcode_i -eq 0 ]; then
113 echo -e " Creating $PROD_STUB_APP_NAME deployment and service"
114 export PROD_STUB_APP_NAME
115 export KUBE_SIM_NAMESPACE
116 export PROD_STUB_IMAGE
117 export PROD_STUB_INTERNAL_PORT
118 export PROD_STUB_INTERNAL_SECURE_PORT
119 export PROD_STUB_EXTERNAL_PORT
120 export PROD_STUB_EXTERNAL_SECURE_PORT
122 __kube_create_namespace $KUBE_SIM_NAMESPACE
125 input_yaml=$SIM_GROUP"/"$PROD_STUB_COMPOSE_DIR"/"svc.yaml
126 output_yaml=$PWD/tmp/prodstub_svc.yaml
127 __kube_create_instance service $PROD_STUB_APP_NAME $input_yaml $output_yaml
130 input_yaml=$SIM_GROUP"/"$PROD_STUB_COMPOSE_DIR"/"app.yaml
131 output_yaml=$PWD/tmp/prodstub_app.yaml
132 __kube_create_instance app $PROD_STUB_APP_NAME $input_yaml $output_yaml
135 PROD_STUB_HOST_NAME=$(__kube_get_service_host $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE)
137 PROD_STUB_EXTERNAL_PORT=$(__kube_get_service_port $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE "http")
138 PROD_STUB_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE "https")
140 echo " Host IP, http port, https port: $PROD_STUB_HOST_NAME $PROD_STUB_EXTERNAL_PORT $PROD_STUB_EXTERNAL_SECURE_PORT"
141 if [ $PROD_STUB_HTTPX == "http" ]; then
142 PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_PORT
143 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_PORT
145 PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_SECURE_PORT
146 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_SECURE_PORT
149 __check_service_start $PROD_STUB_APP_NAME $PROD_STUB_PATH$PROD_STUB_ALIVE_URL
151 echo -ne " Service $PROD_STUB_APP_NAME - reset "$SAMELINE
152 result=$(__do_curl $PROD_STUB_PATH/reset)
153 if [ $? -ne 0 ]; then
154 echo -e " Service $PROD_STUB_APP_NAME - reset $RED Failed $ERED - will continue"
156 echo -e " Service $PROD_STUB_APP_NAME - reset $GREEN OK $EGREEN"
160 # Check if docker app shall be fully managed by the test script
161 __check_included_image 'PRODSTUB'
162 if [ $? -eq 1 ]; then
163 echo -e $RED"The Producer stub app is not included as managed in this test script"$ERED
164 echo -e $RED"The Producer stub will not be started"$ERED
168 export PROD_STUB_APP_NAME
169 export PROD_STUB_APP_NAME_ALIAS
170 export PROD_STUB_INTERNAL_PORT
171 export PROD_STUB_EXTERNAL_PORT
172 export PROD_STUB_INTERNAL_SECURE_PORT
173 export PROD_STUB_EXTERNAL_SECURE_PORT
174 export DOCKER_SIM_NWNAME
176 __start_container $PROD_STUB_COMPOSE_DIR NODOCKERARGS 1 $PROD_STUB_APP_NAME
178 __check_service_start $PROD_STUB_APP_NAME $PROD_STUB_PATH$PROD_STUB_ALIVE_URL
184 # Excute a curl cmd towards the prodstub simulator and check the response code.
185 # args: TEST|CONF <expected-response-code> <curl-cmd-string> [<json-file-to-compare-output>]
186 __execute_curl_to_prodstub() {
187 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
188 echo "(${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
189 echo " CMD: $3" >> $HTTPLOG
191 echo " RESP: $res" >> $HTTPLOG
193 if [ $retcode -ne 0 ]; then
194 __log_conf_fail_general " Fatal error when executing curl, response: "$retcode
197 status=${res:${#res}-3}
198 if [ $status -eq $2 ]; then
199 if [ $# -eq 4 ]; then
200 body=${res:0:${#res}-3}
202 echo " TARGET JSON: $jobfile" >> $HTTPLOG
203 res=$(python3 ../common/compare_json.py "$jobfile" "$body")
204 if [ $res -ne 0 ]; then
205 if [ $1 == "TEST" ]; then
213 if [ $1 == "TEST" ]; then
220 if [ $1 == "TEST" ]; then
221 __log_test_fail_status_code $2 $status
223 __log_conf_fail_status_code $2 $status
228 # Prodstub API: Set (or reset) response code for producer supervision
229 # <response-code> <producer-id> [<forced_response_code>]
230 # (Function for test scripts)
231 prodstub_arm_producer() {
233 if [ $# -ne 2 ] && [ $# -ne 3 ]; then
234 __print_err "<response-code> <producer-id> [<forced_response_code>]" $@
238 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/supervision/"$2
239 if [ $# -eq 3 ]; then
240 curlString=$curlString"?response="$3
243 __execute_curl_to_prodstub CONF $1 "$curlString"
247 # Prodstub API: Set (or reset) response code job create
248 # <response-code> <producer-id> <job-id> [<forced_response_code>]
249 # (Function for test scripts)
250 prodstub_arm_job_create() {
252 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
253 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
257 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/create/$2/$3"
258 if [ $# -eq 4 ]; then
259 curlString=$curlString"?response="$4
262 __execute_curl_to_prodstub CONF $1 "$curlString"
266 # Prodstub API: Set (or reset) response code job delete
267 # <response-code> <producer-id> <job-id> [<forced_response_code>]
268 # (Function for test scripts)
269 prodstub_arm_job_delete() {
271 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
272 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
276 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/delete/$2/$3"
277 if [ $# -eq 4 ]; then
278 curlString=$curlString"?response="$4
281 __execute_curl_to_prodstub CONF $1 "$curlString"
285 # Prodstub API: Arm a type of a producer
286 # <response-code> <producer-id> <type-id>
287 # (Function for test scripts)
288 prodstub_arm_type() {
290 if [ $# -ne 3 ]; then
291 __print_err "<response-code> <producer-id> <type-id>" $@
295 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/type/$2/$3"
297 __execute_curl_to_prodstub CONF $1 "$curlString"
301 # Prodstub API: Disarm a type in a producer
302 # <response-code> <producer-id> <type-id>
303 # (Function for test scripts)
304 prodstub_disarm_type() {
306 if [ $# -ne 3 ]; then
307 __print_err "<response-code> <producer-id> <type-id>" $@
311 curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_PATH/arm/type/$2/$3"
313 __execute_curl_to_prodstub CONF $1 "$curlString"
317 # Prodstub API: Get job data for a job and compare with a target job json
318 # <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
319 # (Function for test scripts)
320 prodstub_check_jobdata() {
322 if [ $# -ne 7 ]; then
323 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
328 jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
330 _log_test_fail_general "Template file "$7" for jobdata, does not exist"
333 targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"ei_job_data\":$jobfile}"
335 echo "$targetJson" > $file
337 curlString="curl -X GET -skw %{http_code} $PROD_STUB_PATH/jobdata/$2/$3"
339 __execute_curl_to_prodstub TEST $1 "$curlString" $file
343 # Prodstub API: Delete the job data
344 # <response-code> <producer-id> <job-id>
345 # (Function for test scripts)
346 prodstub_delete_jobdata() {
348 if [ $# -ne 3 ]; then
349 __print_err "<response-code> <producer-id> <job-id> " $@
352 curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_PATH/jobdata/$2/$3"
354 __execute_curl_to_prodstub CONF $1 "$curlString"
358 # Tests if a variable value in the prod stub is equal to a target value and and optional timeout.
359 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
360 # equal to the target or not.
361 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
362 # before setting pass or fail depending on if the variable value becomes equal to the target
364 # (Function for test scripts)
366 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
367 __var_test "PRODSTUB" "$PROD_STUB_PATH/counter/" $1 "=" $2 $3
369 __print_err "Wrong args to prodstub_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@