blob: f792d697bc40fdbc01b5c6b1b0c8897aba10c1c2 [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
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010022
23################ Test engine functions ################
24
25# Create the image var used during the test
26# arg: <image-tag-suffix> (selects staging, snapshot, release etc)
27# <image-tag-suffix> is present only for images with staging, snapshot,release tags
28__PRODSTUB_imagesetup() {
29 __check_and_create_image_var PRODSTUB "PROD_STUB_IMAGE" "PROD_STUB_IMAGE_BASE" "PROD_STUB_IMAGE_TAG" LOCAL "$PROD_STUB_DISPLAY_NAME"
30}
31
32# Pull image from remote repo or use locally built image
33# arg: <pull-policy-override> <pull-policy-original>
34# <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
35# <pull-policy-original> Shall be used for images that does not allow overriding
36# Both var may contain: 'remote', 'remote-remove' or 'local'
37__PRODSTUB_imagepull() {
BjornMagnussonXAa69cd902021-04-22 23:46:10 +020038 echo -e $RED"Image for app PRODSTUB shall never be pulled from remote repo"$ERED
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010039}
40
41# Build image (only for simulator or interfaces stubs owned by the test environment)
42# arg: <image-tag-suffix> (selects staging, snapshot, release etc)
43# <image-tag-suffix> is present only for images with staging, snapshot,release tags
44__PRODSTUB_imagebuild() {
45 cd ../prodstub
46 echo " Building PRODSTUB - $PROD_STUB_DISPLAY_NAME - image: $PROD_STUB_IMAGE"
47 docker build --build-arg NEXUS_PROXY_REPO=$NEXUS_PROXY_REPO -t $PROD_STUB_IMAGE . &> .dockererr
48 if [ $? -eq 0 ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +020049 echo -e $GREEN" Build Ok"$EGREEN
50 __retag_and_push_image PROD_STUB_IMAGE
51 if [ $? -ne 0 ]; then
52 exit 1
53 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010054 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +020055 echo -e $RED" Build Failed"$ERED
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010056 ((RES_CONF_FAIL++))
57 cat .dockererr
58 echo -e $RED"Exiting...."$ERED
59 exit 1
60 fi
61}
62
63# Generate a string for each included image using the app display name and a docker images format string
BjornMagnussonXA483ee332021-04-08 01:35:24 +020064# If a custom image repo is used then also the source image from the local repo is listed
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010065# arg: <docker-images-format-string> <file-to-append>
66__PRODSTUB_image_data() {
67 echo -e "$PROD_STUB_DISPLAY_NAME\t$(docker images --format $1 $PROD_STUB_IMAGE)" >> $2
BjornMagnussonXA483ee332021-04-08 01:35:24 +020068 if [ ! -z "$PROD_STUB_IMAGE_SOURCE" ]; then
69 echo -e "-- source image --\t$(docker images --format $1 $PROD_STUB_IMAGE_SOURCE)" >> $2
70 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010071}
72
73# Scale kubernetes resources to zero
74# All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
75# This function is called for apps fully managed by the test script
76__PRODSTUB_kube_scale_zero() {
77 __kube_scale_all_resources $KUBE_SIM_NAMESPACE autotest PRODSTUB
78}
79
80# Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
81# This function is called for prestarted apps not managed by the test script.
82__PRODSTUB_kube_scale_zero_and_wait() {
83 echo -e $RED" PRODSTUB app is not scaled in this state"$ERED
84}
85
86# Delete all kube resouces for the app
87# This function is called for apps managed by the test script.
88__PRODSTUB_kube_delete_all() {
89 __kube_delete_all_resources $KUBE_SIM_NAMESPACE autotest PRODSTUB
90}
91
92# Store docker logs
93# This function is called for apps managed by the test script.
94# args: <log-dir> <file-prexix>
95__PRODSTUB_store_docker_logs() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +010096 if [ $RUNMODE == "KUBE" ]; then
97 kubectl logs -l "autotest=PRODSTUB" -n $KUBE_SIM_NAMESPACE --tail=-1 > $1$2_prodstub.log 2>&1
98 else
99 docker logs $PROD_STUB_APP_NAME > $1$2_prodstub.log 2>&1
100 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100101}
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100102
103# Initial setup of protocol, host and ports
104# This function is called for apps managed by the test script.
105# args: -
106__PRODSTUB_initial_setup() {
107 use_prod_stub_http
108}
109
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100110# Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers
111# For docker, the namespace shall be excluded
112# This function is called for apps managed by the test script as well as for prestarted apps.
113# args: -
114__PRODSTUB_statisics_setup() {
115 if [ $RUNMODE == "KUBE" ]; then
116 echo "PRODSTUB $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE"
117 else
118 echo "PRODSTUB $PROD_STUB_APP_NAME"
119 fi
120}
121
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100122#######################################################
123
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100124# Set http as the protocol to use for all communication to the Prod stub sim
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100125# args: -
126# (Function for test scripts)
127use_prod_stub_http() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100128 __prod_stub_set_protocoll "http" $PROD_STUB_INTERNAL_PORT $PROD_STUB_EXTERNAL_PORT
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100129}
130
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100131# Set https as the protocol to use for all communication to the Prod stub sim
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100132# args: -
133# (Function for test scripts)
134use_prod_stub_https() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100135 __prod_stub_set_protocoll "https" $PROD_STUB_INTERNAL_SECURE_PORT $PROD_STUB_EXTERNAL_SECURE_PORT
136}
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100137
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100138# Setup paths to svc/container for internal and external access
139# args: <protocol> <internal-port> <external-port>
140__prod_stub_set_protocoll() {
141 echo -e $BOLD"$PROD_STUB_DISPLAY_NAME protocol setting"$EBOLD
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100142 echo -e " Using $BOLD $1 $EBOLD towards $PROD_STUB_DISPLAY_NAME"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100143
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100144 ## Access to Prod stub sim
145
146 PROD_STUB_SERVICE_PATH=$1"://"$PROD_STUB_APP_NAME":"$2 # docker access, container->container and script->container via proxy
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100147 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100148 PROD_STUB_SERVICE_PATH=$1"://"$PROD_STUB_APP_NAME.$KUBE_SIM_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100149 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100150
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100151 echo ""
152}
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200153
154### Admin API functions producer stub
155
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100156###########################
157### Producer stub functions
158###########################
159
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100160# Export env vars for config files, docker compose and kube resources
161# args:
162__prodstub_export_vars() {
163 export PROD_STUB_APP_NAME
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100164 export PROD_STUB_DISPLAY_NAME
165
166 export DOCKER_SIM_NWNAME
167 export KUBE_SIM_NAMESPACE
168
169 export PROD_STUB_IMAGE
170 export PROD_STUB_INTERNAL_PORT
171 export PROD_STUB_INTERNAL_SECURE_PORT
172 export PROD_STUB_EXTERNAL_PORT
173 export PROD_STUB_EXTERNAL_SECURE_PORT
174}
175
176
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100177# Start the Producer stub in the simulator group
178# args: -
179# (Function for test scripts)
180start_prod_stub() {
181
182 echo -e $BOLD"Starting $PROD_STUB_DISPLAY_NAME"$EBOLD
183
184 if [ $RUNMODE == "KUBE" ]; then
185
186 # Check if app shall be fully managed by the test script
187 __check_included_image "PRODSTUB"
188 retcode_i=$?
189
190 # Check if app shall only be used by the testscipt
191 __check_prestarted_image "PRODSTUB"
192 retcode_p=$?
193
194 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100195 echo -e $RED"The $ICS_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
196 echo -e $RED"The $ICS_APP_NAME will not be started"$ERED
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100197 exit
198 fi
199 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100200 echo -e $RED"The $ICS_APP_NAME app is included both as managed and prestarted in this test script"$ERED
201 echo -e $RED"The $ICS_APP_NAME will not be started"$ERED
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100202 exit
203 fi
204
205 if [ $retcode_p -eq 0 ]; then
206 echo -e " Using existing $PROD_STUB_APP_NAME deployment and service"
207 echo " Setting RC replicas=1"
208 __kube_scale deployment $PROD_STUB_APP_NAME $KUBE_SIM_NAMESPACE 1
209 fi
210
211 if [ $retcode_i -eq 0 ]; then
212 echo -e " Creating $PROD_STUB_APP_NAME deployment and service"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100213
214 __kube_create_namespace $KUBE_SIM_NAMESPACE
215
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100216 __prodstub_export_vars
217
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100218 # Create service
219 input_yaml=$SIM_GROUP"/"$PROD_STUB_COMPOSE_DIR"/"svc.yaml
220 output_yaml=$PWD/tmp/prodstub_svc.yaml
221 __kube_create_instance service $PROD_STUB_APP_NAME $input_yaml $output_yaml
222
223 # Create app
224 input_yaml=$SIM_GROUP"/"$PROD_STUB_COMPOSE_DIR"/"app.yaml
225 output_yaml=$PWD/tmp/prodstub_app.yaml
226 __kube_create_instance app $PROD_STUB_APP_NAME $input_yaml $output_yaml
227 fi
228
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100229 __check_service_start $PROD_STUB_APP_NAME $PROD_STUB_SERVICE_PATH$PROD_STUB_ALIVE_URL
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100230
231 echo -ne " Service $PROD_STUB_APP_NAME - reset "$SAMELINE
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100232 result=$(__do_curl $PROD_STUB_SERVICE_PATH/reset)
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100233 if [ $? -ne 0 ]; then
234 echo -e " Service $PROD_STUB_APP_NAME - reset $RED Failed $ERED - will continue"
235 else
236 echo -e " Service $PROD_STUB_APP_NAME - reset $GREEN OK $EGREEN"
237 fi
238 else
239
240 # Check if docker app shall be fully managed by the test script
241 __check_included_image 'PRODSTUB'
242 if [ $? -eq 1 ]; then
243 echo -e $RED"The Producer stub app is not included as managed in this test script"$ERED
244 echo -e $RED"The Producer stub will not be started"$ERED
245 exit
246 fi
247
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100248 __prodstub_export_vars
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100249
250 __start_container $PROD_STUB_COMPOSE_DIR "" NODOCKERARGS 1 $PROD_STUB_APP_NAME
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100251
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100252 __check_service_start $PROD_STUB_APP_NAME $PROD_STUB_SERVICE_PATH$PROD_STUB_ALIVE_URL
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100253 fi
254 echo ""
255 return 0
256}
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200257
258# Excute a curl cmd towards the prodstub simulator and check the response code.
259# args: TEST|CONF <expected-response-code> <curl-cmd-string> [<json-file-to-compare-output>]
260__execute_curl_to_prodstub() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100261 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
262 echo "(${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100263 proxyflag=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100264 if [ ! -z "$KUBE_PROXY_PATH" ]; then
265 if [ $KUBE_PROXY_HTTPX == "http" ]; then
266 proxyflag=" --proxy $KUBE_PROXY_PATH"
267 else
268 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100269 fi
270 fi
271 echo " CMD: $3 $proxyflag" >> $HTTPLOG
272 res="$($3 $proxyflag)"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200273 echo " RESP: $res" >> $HTTPLOG
274 retcode=$?
275 if [ $retcode -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100276 __log_conf_fail_general " Fatal error when executing curl, response: "$retcode
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200277 return 1
278 fi
279 status=${res:${#res}-3}
280 if [ $status -eq $2 ]; then
281 if [ $# -eq 4 ]; then
282 body=${res:0:${#res}-3}
283 jobfile=$(cat $4)
284 echo " TARGET JSON: $jobfile" >> $HTTPLOG
285 res=$(python3 ../common/compare_json.py "$jobfile" "$body")
286 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100287 if [ $1 == "TEST" ]; then
288 __log_test_fail_body
289 else
290 __log_conf_fail_body
291 fi
292 return 1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200293 fi
294 fi
295 if [ $1 == "TEST" ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100296 __log_test_pass
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200297 else
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100298 __log_conf_ok
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200299 fi
300 return 0
301 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100302 if [ $1 == "TEST" ]; then
303 __log_test_fail_status_code $2 $status
304 else
305 __log_conf_fail_status_code $2 $status
306 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200307 return 1
308}
309
310# Prodstub API: Set (or reset) response code for producer supervision
311# <response-code> <producer-id> [<forced_response_code>]
312# (Function for test scripts)
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200313prodstub_arm_producer() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100314 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200315 if [ $# -ne 2 ] && [ $# -ne 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200316 __print_err "<response-code> <producer-id> [<forced_response_code>]" $@
317 return 1
318 fi
319
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100320 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/supervision/"$2
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200321 if [ $# -eq 3 ]; then
322 curlString=$curlString"?response="$3
323 fi
324
325 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100326 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200327}
328
329# Prodstub API: Set (or reset) response code job create
330# <response-code> <producer-id> <job-id> [<forced_response_code>]
331# (Function for test scripts)
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200332prodstub_arm_job_create() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100333 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200334 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200335 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
336 return 1
337 fi
338
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100339 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/create/$2/$3"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200340 if [ $# -eq 4 ]; then
341 curlString=$curlString"?response="$4
342 fi
343
344 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100345 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200346}
347
348# Prodstub API: Set (or reset) response code job delete
349# <response-code> <producer-id> <job-id> [<forced_response_code>]
350# (Function for test scripts)
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200351prodstub_arm_job_delete() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100352 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200353 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200354 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
355 return 1
356 fi
357
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100358 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/delete/$2/$3"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200359 if [ $# -eq 4 ]; then
360 curlString=$curlString"?response="$4
361 fi
362
363 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100364 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200365}
366
367# Prodstub API: Arm a type of a producer
368# <response-code> <producer-id> <type-id>
369# (Function for test scripts)
370prodstub_arm_type() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100371 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200372 if [ $# -ne 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200373 __print_err "<response-code> <producer-id> <type-id>" $@
374 return 1
375 fi
376
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100377 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/type/$2/$3"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200378
379 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100380 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200381}
382
383# Prodstub API: Disarm a type in a producer
384# <response-code> <producer-id> <type-id>
385# (Function for test scripts)
386prodstub_disarm_type() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100387 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200388 if [ $# -ne 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200389 __print_err "<response-code> <producer-id> <type-id>" $@
390 return 1
391 fi
392
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100393 curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_SERVICE_PATH/arm/type/$2/$3"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200394
395 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100396 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200397}
398
399# Prodstub API: Get job data for a job and compare with a target job json
BjornMagnussonXA2138b632020-11-30 21:17:32 +0100400# <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200401# (Function for test scripts)
402prodstub_check_jobdata() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100403 __log_test_start $@
BjornMagnussonXA2138b632020-11-30 21:17:32 +0100404 if [ $# -ne 7 ]; then
405 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200406 return 1
407 fi
BjornMagnussonXA2138b632020-11-30 21:17:32 +0100408 if [ -f $7 ]; then
409 jobfile=$(cat $7)
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200410 jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
411 else
BjornMagnussonXA9d8fafb2021-05-10 11:11:49 +0200412 __log_test_fail_general "Template file "$7" for jobdata, does not exist"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200413 return 1
414 fi
BjornMagnussonXA2138b632020-11-30 21:17:32 +0100415 targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"ei_job_data\":$jobfile}"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200416 file="./tmp/.p.json"
417 echo "$targetJson" > $file
418
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100419 curlString="curl -X GET -skw %{http_code} $PROD_STUB_SERVICE_PATH/jobdata/$2/$3"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200420
421 __execute_curl_to_prodstub TEST $1 "$curlString" $file
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100422 return $?
423}
424
BjornMagnussonXAc963b732021-01-20 14:24:13 +0100425# Prodstub API: Get job data for a job and compare with a target job json
426# <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
427# (Function for test scripts)
428prodstub_check_jobdata_2() {
429 __log_test_start $@
430 if [ $# -ne 7 ]; then
431 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
432 return 1
433 fi
434 if [ -f $7 ]; then
435 jobfile=$(cat $7)
436 jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
437 else
BjornMagnussonXA9d8fafb2021-05-10 11:11:49 +0200438 __log_test_fail_general "Template file "$7" for jobdata, does not exist"
BjornMagnussonXAc963b732021-01-20 14:24:13 +0100439 return 1
440 fi
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100441 if [[ "$ICS_FEATURE_LEVEL" == *"INFO-TYPES"* ]]; then
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +0200442 targetJson="{\"info_job_identity\":\"$3\",\"info_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"info_job_data\":$jobfile,\"last_updated\":\"????\"}"
443 else
444 targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"ei_job_data\":$jobfile,\"last_updated\":\"????\"}"
445 fi
446 file="./tmp/.p.json"
BjornMagnussonXAc963b732021-01-20 14:24:13 +0100447 echo "$targetJson" > $file
448
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100449 curlString="curl -X GET -skw %{http_code} $PROD_STUB_SERVICE_PATH/jobdata/$2/$3"
BjornMagnussonXAc963b732021-01-20 14:24:13 +0100450
451 __execute_curl_to_prodstub TEST $1 "$curlString" $file
452 return $?
453}
454
BjornMagnussonXAce4b14c2021-05-11 15:40:03 +0200455# Prodstub API: Get job data for a job and compare with a target job json (info-jobs)
456# <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
457# (Function for test scripts)
458prodstub_check_jobdata_3() {
459 __log_test_start $@
460 if [ $# -ne 7 ]; then
461 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
462 return 1
463 fi
464 if [ -f $7 ]; then
465 jobfile=$(cat $7)
466 jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
467 else
468 __log_test_fail_general "Template file "$7" for jobdata, does not exist"
469 return 1
470 fi
471 targetJson="{\"info_job_identity\":\"$3\",\"info_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"info_job_data\":$jobfile,\"last_updated\":\"????\"}"
472 file="./tmp/.p.json"
473 echo "$targetJson" > $file
474
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100475 curlString="curl -X GET -skw %{http_code} $PROD_STUB_SERVICE_PATH/jobdata/$2/$3"
BjornMagnussonXAce4b14c2021-05-11 15:40:03 +0200476
477 __execute_curl_to_prodstub TEST $1 "$curlString" $file
478 return $?
479}
480
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100481# Prodstub API: Delete the job data
482# <response-code> <producer-id> <job-id>
483# (Function for test scripts)
484prodstub_delete_jobdata() {
485 __log_conf_start
486 if [ $# -ne 3 ]; then
487 __print_err "<response-code> <producer-id> <job-id> " $@
488 return 1
489 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100490 curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_SERVICE_PATH/jobdata/$2/$3"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100491
492 __execute_curl_to_prodstub CONF $1 "$curlString"
493 return $?
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200494}
495
496# Tests if a variable value in the prod stub is equal to a target value and and optional timeout.
497# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
498# equal to the target or not.
499# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
500# before setting pass or fail depending on if the variable value becomes equal to the target
501# value or not.
502# (Function for test scripts)
503prodstub_equal() {
504 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100505 __var_test "PRODSTUB" "$PROD_STUB_SERVICE_PATH/counter/" $1 "=" $2 $3
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200506 else
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200507 __print_err "Wrong args to prodstub_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
508 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200509}