blob: 4dfe06b77d140b00e15ca466e8ffd85c13300960 [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
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010020# This is a script that contains container/service management functions and test functions for Producer stub
21
22## Access to Prod stub sim
23# Direct access
24PROD_STUB_HTTPX="http"
25PROD_STUB_HOST_NAME=$LOCALHOST_NAME
26PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_PORT
27
28#Docker/Kube internal path
29if [ $RUNMODE == "KUBE" ]; then
30 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_PORT
31else
32 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME":"$PROD_STUB_INTERNAL_PORT
33fi
34
35# Set http as the protocol to use for all communication to the Producer stub
36# args: -
37# (Function for test scripts)
38use_prod_stub_http() {
39 echo -e $BOLD"Producer stub protocol setting"$EBOLD
40 echo -e " Using $BOLD http $EBOLD towards Producer stub"
41
42 PROD_STUB_HTTPX="http"
43 PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_PORT
44
45 if [ $RUNMODE == "KUBE" ]; then
46 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME"."$KUBE_SIM_NAMESPACE":"$PROD_STUB_EXTERNAL_PORT
47 else
48 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME":"$PROD_STUB_INTERNAL_PORT
49 fi
50
51 echo ""
52}
53
54# Set https as the protocol to use for all communication to the Producer stub
55# args: -
56# (Function for test scripts)
57use_prod_stub_https() {
58 echo -e $BOLD"Producer stub protocol setting"$EBOLD
59 echo -e " Using $BOLD https $EBOLD towards Producer stub"
60
61 PROD_STUB_HTTPX="https"
62 PROD_STUB_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_HOST_NAME":"$PROD_STUB_EXTERNAL_SECURE_PORT
63
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
66 else
67 PROD_STUB_SERVICE_PATH=$PROD_STUB_HTTPX"://"$PROD_STUB_APP_NAME":"$PROD_STUB_INTERNAL_SECURE_PORT
68 fi
69 echo ""
70}
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020071
72### Admin API functions producer stub
73
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010074###########################
75### Producer stub functions
76###########################
77
78# Start the Producer stub in the simulator group
79# args: -
80# (Function for test scripts)
81start_prod_stub() {
82
83 echo -e $BOLD"Starting $PROD_STUB_DISPLAY_NAME"$EBOLD
84
85 if [ $RUNMODE == "KUBE" ]; then
86
87 # Check if app shall be fully managed by the test script
88 __check_included_image "PRODSTUB"
89 retcode_i=$?
90
91 # Check if app shall only be used by the testscipt
92 __check_prestarted_image "PRODSTUB"
93 retcode_p=$?
94
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
98 exit
99 fi
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
103 exit
104 fi
105
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
110 fi
111
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
121
122 __kube_create_namespace $KUBE_SIM_NAMESPACE
123
124 # Create service
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
128
129 # Create app
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
133 fi
134
135 PROD_STUB_HOST_NAME=$(__kube_get_service_host $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE)
136
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")
139
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
144 else
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
147 fi
148
149 __check_service_start $PROD_STUB_APP_NAME $PROD_STUB_PATH$PROD_STUB_ALIVE_URL
150
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"
155 else
156 echo -e " Service $PROD_STUB_APP_NAME - reset $GREEN OK $EGREEN"
157 fi
158 else
159
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
165 exit
166 fi
167
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
175
176 __start_container $PROD_STUB_COMPOSE_DIR NODOCKERARGS 1 $PROD_STUB_APP_NAME
177
178 __check_service_start $PROD_STUB_APP_NAME $PROD_STUB_PATH$PROD_STUB_ALIVE_URL
179 fi
180 echo ""
181 return 0
182}
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200183
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() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100187 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
188 echo "(${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200189 echo " CMD: $3" >> $HTTPLOG
190 res="$($3)"
191 echo " RESP: $res" >> $HTTPLOG
192 retcode=$?
193 if [ $retcode -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100194 __log_conf_fail_general " Fatal error when executing curl, response: "$retcode
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200195 return 1
196 fi
197 status=${res:${#res}-3}
198 if [ $status -eq $2 ]; then
199 if [ $# -eq 4 ]; then
200 body=${res:0:${#res}-3}
201 jobfile=$(cat $4)
202 echo " TARGET JSON: $jobfile" >> $HTTPLOG
203 res=$(python3 ../common/compare_json.py "$jobfile" "$body")
204 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100205 if [ $1 == "TEST" ]; then
206 __log_test_fail_body
207 else
208 __log_conf_fail_body
209 fi
210 return 1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200211 fi
212 fi
213 if [ $1 == "TEST" ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100214 __log_test_pass
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200215 else
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100216 __log_conf_ok
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200217 fi
218 return 0
219 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100220 if [ $1 == "TEST" ]; then
221 __log_test_fail_status_code $2 $status
222 else
223 __log_conf_fail_status_code $2 $status
224 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200225 return 1
226}
227
228# Prodstub API: Set (or reset) response code for producer supervision
229# <response-code> <producer-id> [<forced_response_code>]
230# (Function for test scripts)
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200231prodstub_arm_producer() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100232 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200233 if [ $# -ne 2 ] && [ $# -ne 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200234 __print_err "<response-code> <producer-id> [<forced_response_code>]" $@
235 return 1
236 fi
237
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100238 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/supervision/"$2
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200239 if [ $# -eq 3 ]; then
240 curlString=$curlString"?response="$3
241 fi
242
243 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100244 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200245}
246
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)
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200250prodstub_arm_job_create() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100251 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200252 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200253 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
254 return 1
255 fi
256
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100257 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/create/$2/$3"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200258 if [ $# -eq 4 ]; then
259 curlString=$curlString"?response="$4
260 fi
261
262 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100263 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200264}
265
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)
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200269prodstub_arm_job_delete() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100270 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200271 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200272 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
273 return 1
274 fi
275
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100276 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/delete/$2/$3"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200277 if [ $# -eq 4 ]; then
278 curlString=$curlString"?response="$4
279 fi
280
281 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100282 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200283}
284
285# Prodstub API: Arm a type of a producer
286# <response-code> <producer-id> <type-id>
287# (Function for test scripts)
288prodstub_arm_type() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100289 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200290 if [ $# -ne 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200291 __print_err "<response-code> <producer-id> <type-id>" $@
292 return 1
293 fi
294
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100295 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_PATH/arm/type/$2/$3"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200296
297 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100298 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200299}
300
301# Prodstub API: Disarm a type in a producer
302# <response-code> <producer-id> <type-id>
303# (Function for test scripts)
304prodstub_disarm_type() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100305 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200306 if [ $# -ne 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200307 __print_err "<response-code> <producer-id> <type-id>" $@
308 return 1
309 fi
310
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100311 curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_PATH/arm/type/$2/$3"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200312
313 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100314 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200315}
316
317# Prodstub API: Get job data for a job and compare with a target job json
BjornMagnussonXA2138b632020-11-30 21:17:32 +0100318# <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200319# (Function for test scripts)
320prodstub_check_jobdata() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100321 __log_test_start $@
BjornMagnussonXA2138b632020-11-30 21:17:32 +0100322 if [ $# -ne 7 ]; then
323 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200324 return 1
325 fi
BjornMagnussonXA2138b632020-11-30 21:17:32 +0100326 if [ -f $7 ]; then
327 jobfile=$(cat $7)
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200328 jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
329 else
BjornMagnussonXA2138b632020-11-30 21:17:32 +0100330 _log_test_fail_general "Template file "$7" for jobdata, does not exist"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200331 return 1
332 fi
BjornMagnussonXA2138b632020-11-30 21:17:32 +0100333 targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"ei_job_data\":$jobfile}"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200334 file="./tmp/.p.json"
335 echo "$targetJson" > $file
336
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100337 curlString="curl -X GET -skw %{http_code} $PROD_STUB_PATH/jobdata/$2/$3"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200338
339 __execute_curl_to_prodstub TEST $1 "$curlString" $file
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100340 return $?
341}
342
343# Prodstub API: Delete the job data
344# <response-code> <producer-id> <job-id>
345# (Function for test scripts)
346prodstub_delete_jobdata() {
347 __log_conf_start
348 if [ $# -ne 3 ]; then
349 __print_err "<response-code> <producer-id> <job-id> " $@
350 return 1
351 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100352 curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_PATH/jobdata/$2/$3"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100353
354 __execute_curl_to_prodstub CONF $1 "$curlString"
355 return $?
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200356}
357
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
363# value or not.
364# (Function for test scripts)
365prodstub_equal() {
366 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100367 __var_test "PRODSTUB" "$PROD_STUB_PATH/counter/" $1 "=" $2 $3
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200368 else
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200369 __print_err "Wrong args to prodstub_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
370 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200371}