Support for test of PMS persistency
[nonrtric.git] / test / common / ecs_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 # This is a script that contains container/service management functions and test functions for ECS
21
22 ################ Test engine functions ################
23
24 # Create the image var used during the test
25 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
26 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
27 __ECS_imagesetup() {
28         __check_and_create_image_var ECS "ECS_IMAGE" "ECS_IMAGE_BASE" "ECS_IMAGE_TAG" $1 "$ECS_DISPLAY_NAME"
29 }
30
31 # Pull image from remote repo or use locally built image
32 # arg: <pull-policy-override> <pull-policy-original>
33 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
34 # <pull-policy-original> Shall be used for images that does not allow overriding
35 # Both var may contain: 'remote', 'remote-remove' or 'local'
36 __ECS_imagepull() {
37         __check_and_pull_image $1 "$ECS_DISPLAY_NAME" $ECS_APP_NAME ECS_IMAGE
38 }
39
40 # Build image (only for simulator or interfaces stubs owned by the test environment)
41 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
42 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
43 __ECS_imagebuild() {
44         echo -e $RED" Image for app ECS shall never be built"$ERED
45 }
46
47 # Generate a string for each included image using the app display name and a docker images format string
48 # If a custom image repo is used then also the source image from the local repo is listed
49 # arg: <docker-images-format-string> <file-to-append>
50 __ECS_image_data() {
51         echo -e "$ECS_DISPLAY_NAME\t$(docker images --format $1 $ECS_IMAGE)" >>   $2
52         if [ ! -z "$ECS_IMAGE_SOURCE" ]; then
53                 echo -e "-- source image --\t$(docker images --format $1 $ECS_IMAGE_SOURCE)" >>   $2
54         fi
55 }
56
57 # Scale kubernetes resources to zero
58 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
59 # This function is called for apps fully managed by the test script
60 __ECS_kube_scale_zero() {
61         __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest ECS
62 }
63
64 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
65 # This function is called for prestarted apps not managed by the test script.
66 __ECS_kube_scale_zero_and_wait() {
67         __kube_scale_and_wait_all_resources $KUBE_NONRTRIC_NAMESPACE app nonrtric-enrichmentservice
68 }
69
70 # Delete all kube resouces for the app
71 # This function is called for apps managed by the test script.
72 __ECS_kube_delete_all() {
73         __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest ECS
74 }
75
76 # Store docker logs
77 # This function is called for apps managed by the test script.
78 # args: <log-dir> <file-prexix>
79 __ECS_store_docker_logs() {
80         docker logs $ECS_APP_NAME > $1$2_ecs.log 2>&1
81 }
82 #######################################################
83
84
85 ## Access to ECS
86 # Host name may be changed if app started by kube
87 # Direct access
88 ECS_HTTPX="http"
89 ECS_HOST_NAME=$LOCALHOST_NAME
90 ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_PORT
91
92 # ECS_ADAPTER used for switch between REST and DMAAP (only REST supported currently)
93 ECS_ADAPTER_TYPE="REST"
94 ECS_ADAPTER=$ECS_PATH
95
96 # Make curl retries towards ECS for http response codes set in this env var, space separated list of codes
97 ECS_RETRY_CODES=""
98
99 #Save first worker node the pod is started on
100 __ECS_WORKER_NODE=""
101
102 ###########################
103 ### ECS functions
104 ###########################
105
106 # All calls to ECS will be directed to the ECS REST interface from now on
107 # args: -
108 # (Function for test scripts)
109 use_ecs_rest_http() {
110         echo -e $BOLD"ECS protocol setting"$EBOLD
111         echo -e " Using $BOLD http $EBOLD and $BOLD REST $EBOLD towards ECS"
112         ECS_HTTPX="http"
113         ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_PORT
114
115         ECS_ADAPTER_TYPE="REST"
116         ECS_ADAPTER=$ECS_PATH
117         echo ""
118 }
119
120 # All calls to ECS will be directed to the ECS REST interface from now on
121 # args: -
122 # (Function for test scripts)
123 use_ecs_rest_https() {
124         echo -e $BOLD"ECS protocol setting"$EBOLD
125         echo -e " Using $BOLD https $EBOLD and $BOLD REST $EBOLD towards ECS"
126         ECS_HTTPX="https"
127         ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_SECURE_PORT
128
129         ECS_ADAPTER_TYPE="REST"
130         ECS_ADAPTER=$ECS_PATH
131         echo ""
132 }
133
134 # All calls to ECS will be directed to the ECS dmaap interface over http from now on
135 # args: -
136 # (Function for test scripts)
137 use_ecs_dmaap_http() {
138         echo -e $BOLD"ECS dmaap protocol setting"$EBOLD
139         echo -e $RED" - NOT SUPPORTED - "$ERED
140         echo -e " Using $BOLD http $EBOLD and $BOLD DMAAP $EBOLD towards ECS"
141         ECS_ADAPTER_TYPE="MR-HTTP"
142         echo ""
143 }
144
145 # All calls to ECS will be directed to the ECS dmaap interface over https from now on
146 # args: -
147 # (Function for test scripts)
148 use_ecs_dmaap_https() {
149         echo -e $BOLD"RICSIM protocol setting"$EBOLD
150         echo -e $RED" - NOT SUPPORTED - "$ERED
151         echo -e " Using $BOLD https $EBOLD and $BOLD REST $EBOLD towards ECS"
152         ECS_ADAPTER_TYPE="MR-HTTPS"
153         echo ""
154 }
155
156 # Start the ECS
157 # args: PROXY|NOPROXY <config-file>
158 # (Function for test scripts)
159 start_ecs() {
160
161         echo -e $BOLD"Starting $ECS_DISPLAY_NAME"$EBOLD
162
163         if [ $RUNMODE == "KUBE" ]; then
164
165                 # Check if app shall be fully managed by the test script
166                 __check_included_image "ECS"
167                 retcode_i=$?
168
169                 # Check if app shall only be used by the testscipt
170                 __check_prestarted_image "ECS"
171                 retcode_p=$?
172
173                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
174                         echo -e $RED"The $ECS_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
175                         echo -e $RED"The $ECS_APP_NAME will not be started"$ERED
176                         exit
177                 fi
178                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
179                         echo -e $RED"The $ECS_APP_NAME app is included both as managed and prestarted in this test script"$ERED
180                         echo -e $RED"The $ECS_APP_NAME will not be started"$ERED
181                         exit
182                 fi
183
184
185                 if [ $retcode_p -eq 0 ]; then
186                         echo -e " Using existing $ECS_APP_NAME deployment and service"
187                         echo " Setting ECS replicas=1"
188                         __kube_scale deployment $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
189                 fi
190
191                 # Check if app shall be fully managed by the test script
192                 if [ $retcode_i -eq 0 ]; then
193                         echo -e " Creating $ECS_APP_NAME app and expose service"
194
195                         #Check if nonrtric namespace exists, if not create it
196                         __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
197
198                         export ECS_APP_NAME
199                         export KUBE_NONRTRIC_NAMESPACE
200                         export ECS_IMAGE
201                         export ECS_INTERNAL_PORT
202                         export ECS_INTERNAL_SECURE_PORT
203                         export ECS_EXTERNAL_PORT
204                         export ECS_EXTERNAL_SECURE_PORT
205                         export ECS_CONFIG_MOUNT_PATH
206                         export ECS_CONFIG_CONFIGMAP_NAME=$ECS_APP_NAME"-config"
207                         export ECS_DATA_CONFIGMAP_NAME=$ECS_APP_NAME"-data"
208                         export ECS_CONTAINER_MNT_DIR
209
210                         export ECS_DATA_PV_NAME=$ECS_APP_NAME"-pv"
211                         export ECS_DATA_PVC_NAME=$ECS_APP_NAME"-pvc"
212                         #Create a unique path for the pv each time to prevent a previous volume to be reused
213                         export ECS_PV_PATH="ecsdata-"$(date +%s)
214
215                         if [ $1 == "PROXY" ]; then
216                                 ECS_HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_CONFIG_PORT  #Set if proxy is started
217                                 ECS_HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_CONFIG_HOST_NAME #Set if proxy is started
218                                 if [ $ECS_HTTP_PROXY_CONFIG_PORT -eq 0 ] || [ -z "$ECS_HTTP_PROXY_CONFIG_HOST_NAME" ]; then
219                                         echo -e $YELLOW" Warning: HTTP PROXY will not be configured, proxy app not started"$EYELLOW
220                                 else
221                                         echo " Configured with http proxy"
222                                 fi
223                         else
224                                 ECS_HTTP_PROXY_CONFIG_PORT=0
225                                 ECS_HTTP_PROXY_CONFIG_HOST_NAME=""
226                                 echo " Configured without http proxy"
227                         fi
228                         export ECS_HTTP_PROXY_CONFIG_PORT
229                         export ECS_HTTP_PROXY_CONFIG_HOST_NAME
230
231                         # Create config map for config
232                         datafile=$PWD/tmp/$ECS_CONFIG_FILE
233                         cp $2 $datafile
234                         output_yaml=$PWD/tmp/ecs_cfc.yaml
235                         __kube_create_configmap $ECS_CONFIG_CONFIGMAP_NAME $KUBE_NONRTRIC_NAMESPACE autotest ECS $datafile $output_yaml
236
237                         # Create pv
238                         input_yaml=$SIM_GROUP"/"$ECS_COMPOSE_DIR"/"pv.yaml
239                         output_yaml=$PWD/tmp/ecs_pv.yaml
240                         __kube_create_instance pv $ECS_APP_NAME $input_yaml $output_yaml
241
242                         # Create pvc
243                         input_yaml=$SIM_GROUP"/"$ECS_COMPOSE_DIR"/"pvc.yaml
244                         output_yaml=$PWD/tmp/ecs_pvc.yaml
245                         __kube_create_instance pvc $ECS_APP_NAME $input_yaml $output_yaml
246
247                         # Create service
248                         input_yaml=$SIM_GROUP"/"$ECS_COMPOSE_DIR"/"svc.yaml
249                         output_yaml=$PWD/tmp/ecs_svc.yaml
250                         __kube_create_instance service $ECS_APP_NAME $input_yaml $output_yaml
251
252                         # Create app
253                         input_yaml=$SIM_GROUP"/"$ECS_COMPOSE_DIR"/"app.yaml
254                         output_yaml=$PWD/tmp/ecs_app.yaml
255                         __kube_create_instance app $ECS_APP_NAME $input_yaml $output_yaml
256                 fi
257
258                 # Tie the ECS to a worker node so that ECS will always be scheduled to the same worker node if the ECS pod is restarted
259                 # A PVC of type hostPath is mounted to ECS, for persistent storage, so the ECS must always be on the node which mounted the volume
260
261                 # Keep the initial worker node in case the pod need to be "restarted" - must be made to the same node due to a volume mounted on the host
262                 __ECS_WORKER_NODE=$(kubectl get pod -l "autotest=ECS" -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.items[*].spec.nodeName}')
263                 if [ -z "$__ECS_WORKER_NODE" ]; then
264                         echo -e $YELLOW" Cannot find worker node for pod for $ECS_APP_NAME, persistency may not work"$EYELLOW
265                 fi
266
267                 echo " Retrieving host and ports for service..."
268                 ECS_HOST_NAME=$(__kube_get_service_host $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
269                 ECS_EXTERNAL_PORT=$(__kube_get_service_port $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE "http")
270                 ECS_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE "https")
271
272                 echo " Host IP, http port, https port: $ECS_HOST_NAME $ECS_EXTERNAL_PORT $ECS_EXTERNAL_SECURE_PORT"
273
274                 if [ $ECS_HTTPX == "http" ]; then
275                         ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_PORT
276                 else
277                         ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_SECURE_PORT
278                 fi
279
280                 __check_service_start $ECS_APP_NAME $ECS_PATH$ECS_ALIVE_URL
281
282                 if [ $ECS_ADAPTER_TYPE == "REST" ]; then
283                         ECS_ADAPTER=$ECS_PATH
284                 fi
285         else
286                 __check_included_image 'ECS'
287                 if [ $? -eq 1 ]; then
288                         echo -e $RED"The ECS app is not included in this test script"$ERED
289                         echo -e $RED"ECS will not be started"$ERED
290                         exit 1
291                 fi
292
293                 curdir=$PWD
294                 cd $SIM_GROUP
295                 cd ecs
296                 cd $ECS_HOST_MNT_DIR
297                 #cd ..
298                 if [ -d db ]; then
299                         if [ "$(ls -A $DIR)" ]; then
300                                 echo -e $BOLD" Cleaning files in mounted dir: $PWD/db"$EBOLD
301                                 rm -rf db/*  &> /dev/null
302                                 if [ $? -ne 0 ]; then
303                                         echo -e $RED" Cannot remove database files in: $PWD"$ERED
304                                         exit 1
305                                 fi
306                         fi
307                 else
308                         echo " No files in mounted dir or dir does not exists"
309                 fi
310                 cd $curdir
311
312                 export ECS_APP_NAME
313                 export ECS_APP_NAME_ALIAS
314                 export ECS_HOST_MNT_DIR
315                 export ECS_CONTAINER_MNT_DIR
316                 export ECS_CONFIG_MOUNT_PATH
317                 export ECS_CONFIG_FILE
318                 export ECS_INTERNAL_PORT
319                 export ECS_EXTERNAL_PORT
320                 export ECS_INTERNAL_SECURE_PORT
321                 export ECS_EXTERNAL_SECURE_PORT
322                 export DOCKER_SIM_NWNAME
323                 export ECS_DISPLAY_NAME
324
325                 if [ $1 == "PROXY" ]; then
326                         ECS_HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_CONFIG_PORT  #Set if proxy is started
327                         ECS_HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_CONFIG_HOST_NAME #Set if proxy is started
328                         if [ $ECS_HTTP_PROXY_CONFIG_PORT -eq 0 ] || [ -z "$ECS_HTTP_PROXY_CONFIG_HOST_NAME" ]; then
329                                 echo -e $YELLOW" Warning: HTTP PROXY will not be configured, proxy app not started"$EYELLOW
330                         else
331                                 echo " Configured with http proxy"
332                         fi
333                 else
334                         ECS_HTTP_PROXY_CONFIG_PORT=0
335                         ECS_HTTP_PROXY_CONFIG_HOST_NAME=""
336                         echo " Configured without http proxy"
337                 fi
338                 export ECS_HTTP_PROXY_CONFIG_PORT
339                 export ECS_HTTP_PROXY_CONFIG_HOST_NAME
340
341                 dest_file=$SIM_GROUP/$ECS_COMPOSE_DIR/$ECS_HOST_MNT_DIR/$ECS_CONFIG_FILE
342
343                 envsubst < $2 > $dest_file
344
345                 __start_container $ECS_COMPOSE_DIR "" NODOCKERARGS 1 $ECS_APP_NAME
346
347                 __check_service_start $ECS_APP_NAME $ECS_PATH$ECS_ALIVE_URL
348         fi
349         echo ""
350         return 0
351 }
352
353 # Stop the ecs
354 # args: -
355 # args: -
356 # (Function for test scripts)
357 stop_ecs() {
358         echo -e $BOLD"Stopping $ECS_DISPLAY_NAME"$EBOLD
359
360         if [ $RUNMODE == "KUBE" ]; then
361                 __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest ECS
362                 echo "  Deleting the replica set - a new will be started when the app is started"
363                 tmp=$(kubectl delete rs -n $KUBE_NONRTRIC_NAMESPACE -l "autotest=ECS")
364                 if [ $? -ne 0 ]; then
365                         echo -e $RED" Could not delete replica set "$RED
366                         ((RES_CONF_FAIL++))
367                         return 1
368                 fi
369         else
370                 docker stop $ECS_APP_NAME &> ./tmp/.dockererr
371                 if [ $? -ne 0 ]; then
372                         __print_err "Could not stop $ECS_APP_NAME" $@
373                         cat ./tmp/.dockererr
374                         ((RES_CONF_FAIL++))
375                         return 1
376                 fi
377         fi
378         echo -e $BOLD$GREEN"Stopped"$EGREEN$EBOLD
379         echo ""
380         return 0
381 }
382
383 # Start a previously stopped ecs
384 # args: -
385 # (Function for test scripts)
386 start_stopped_ecs() {
387         echo -e $BOLD"Starting (the previously stopped) $ECS_DISPLAY_NAME"$EBOLD
388
389         if [ $RUNMODE == "KUBE" ]; then
390
391                 # Tie the PMS to the same worker node it was initially started on
392                 # A PVC of type hostPath is mounted to PMS, for persistent storage, so the PMS must always be on the node which mounted the volume
393                 if [ -z "$__ECS_WORKER_NODE" ]; then
394                         echo -e $RED" No initial worker node found for pod "$RED
395                         ((RES_CONF_FAIL++))
396                         return 1
397                 else
398                         echo -e $BOLD" Setting nodeSelector kubernetes.io/hostname=$__ECS_WORKER_NODE to deployment for $ECS_APP_NAME. Pod will always run on this worker node: $__PA_WORKER_NODE"$BOLD
399                         echo -e $BOLD" The mounted volume is mounted as hostPath and only available on that worker node."$BOLD
400                         tmp=$(kubectl patch deployment $ECS_APP_NAME -n $KUBE_NONRTRIC_NAMESPACE --patch '{"spec": {"template": {"spec": {"nodeSelector": {"kubernetes.io/hostname": "'$__ECS_WORKER_NODE'"}}}}}')
401                         if [ $? -ne 0 ]; then
402                                 echo -e $YELLOW" Cannot set nodeSelector to deployment for $ECS_APP_NAME, persistency may not work"$EYELLOW
403                         fi
404                         __kube_scale deployment $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
405                 fi
406
407         else
408                 docker start $ECS_APP_NAME &> ./tmp/.dockererr
409                 if [ $? -ne 0 ]; then
410                         __print_err "Could not start (the stopped) $ECS_APP_NAME" $@
411                         cat ./tmp/.dockererr
412                         ((RES_CONF_FAIL++))
413                         return 1
414                 fi
415         fi
416         __check_service_start $ECS_APP_NAME $ECS_PATH$ECS_ALIVE_URL
417         if [ $? -ne 0 ]; then
418                 return 1
419         fi
420         echo ""
421         return 0
422 }
423
424 # Turn on debug level tracing in ECS
425 # args: -
426 # (Function for test scripts)
427 set_ecs_debug() {
428         echo -e $BOLD"Setting ecs debug logging"$EBOLD
429         curlString="$ECS_PATH$ECS_ACTUATOR -X POST  -H Content-Type:application/json -d {\"configuredLevel\":\"debug\"}"
430         result=$(__do_curl "$curlString")
431         if [ $? -ne 0 ]; then
432                 __print_err "Could not set debug mode" $@
433                 ((RES_CONF_FAIL++))
434                 return 1
435         fi
436         echo ""
437         return 0
438 }
439
440 # Turn on trace level tracing in ECS
441 # args: -
442 # (Function for test scripts)
443 set_ecs_trace() {
444         echo -e $BOLD"Setting ecs trace logging"$EBOLD
445         curlString="$ECS_PATH/actuator/loggers/org.oransc.enrichment -X POST  -H Content-Type:application/json -d {\"configuredLevel\":\"trace\"}"
446         result=$(__do_curl "$curlString")
447         if [ $? -ne 0 ]; then
448                 __print_err "Could not set trace mode" $@
449                 ((RES_CONF_FAIL++))
450                 return 1
451         fi
452         echo ""
453         return 0
454 }
455
456 # Perform curl retries when making direct call to ECS for the specified http response codes
457 # Speace separated list of http response codes
458 # args: [<response-code>]*
459 use_ecs_retries() {
460         echo -e $BOLD"Do curl retries to the ECS REST inteface for these response codes:$@"$EBOLD
461         ECS_RETRY_CODES=$@
462         echo ""
463         return 0
464 }
465
466 # Check the ecs logs for WARNINGs and ERRORs
467 # args: -
468 # (Function for test scripts)
469 check_ecs_logs() {
470         __check_container_logs "ECS" $ECS_APP_NAME $ECS_LOGPATH WARN ERR
471 }
472
473
474 # Tests if a variable value in the ECS is equal to a target value and and optional timeout.
475 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
476 # equal to the target or not.
477 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
478 # before setting pass or fail depending on if the variable value becomes equal to the target
479 # value or not.
480 # (Function for test scripts)
481 ecs_equal() {
482         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
483                 __var_test ECS "$ECS_PATH/" $1 "=" $2 $3
484         else
485                 __print_err "Wrong args to ecs_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
486         fi
487 }
488
489
490 ##########################################
491 ######### A1-E Enrichment  API ##########
492 ##########################################
493 #Function prefix: ecs_api_a1
494
495 # API Test function: GET /A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs
496 # args: <response-code> <type-id>  <owner-id>|NOOWNER [ EMPTY | <job-id>+ ]
497 # args (flat uri structure): <response-code> <type-id>|NOTYPE  <owner-id>|NOOWNER [ EMPTY | <job-id>+ ]
498 # (Function for test scripts)
499 ecs_api_a1_get_job_ids() {
500         __log_test_start $@
501
502         if [ -z "$FLAT_A1_EI" ]; then
503                 # Valid number of parameters 4,5,6 etc
504         if [ $# -lt 3 ]; then
505                         __print_err "<response-code> <type-id>  <owner-id>|NOOWNER [ EMPTY | <job-id>+ ]" $@
506                         return 1
507                 fi
508         else
509                 echo -e $YELLOW"INTERFACE - FLAT URI STRUCTURE"$EYELLOW
510                 # Valid number of parameters 4,5,6 etc
511         if [ $# -lt 3 ]; then
512                         __print_err "<response-code> <type-id>|NOTYPE  <owner-id>|NOOWNER [ EMPTY | <job-id>+ ]" $@
513                         return 1
514                 fi
515         fi
516         search=""
517         if [ $3 != "NOWNER" ]; then
518                 search="?owner="$3
519         fi
520
521         if [  -z "$FLAT_A1_EI" ]; then
522                 query="/A1-EI/v1/eitypes/$2/eijobs$search"
523         else
524                 if [ $2 != "NOTYPE" ]; then
525                         if [ -z "$search" ]; then
526                                 search="?eiTypeId="$2
527                         else
528                                 search=$search"&eiTypeId="$2
529                         fi
530                 fi
531                 query="/A1-EI/v1/eijobs$search"
532         fi
533     res="$(__do_curl_to_api ECS GET $query)"
534     status=${res:${#res}-3}
535
536         if [ $status -ne $1 ]; then
537                 __log_test_fail_status_code $1 $status
538                 return 1
539         fi
540
541         if [ $# -gt 3 ]; then
542                 body=${res:0:${#res}-3}
543                 targetJson="["
544
545                 for pid in ${@:4} ; do
546                         if [ "$targetJson" != "[" ]; then
547                                 targetJson=$targetJson","
548                         fi
549                         if [ $pid != "EMPTY" ]; then
550                                 targetJson=$targetJson"\"$pid\""
551                         fi
552                 done
553
554                 targetJson=$targetJson"]"
555                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
556                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
557
558                 if [ $res -ne 0 ]; then
559                         __log_test_fail_body
560                         return 1
561                 fi
562         fi
563
564         __log_test_pass
565         return 0
566 }
567
568 # API Test function: GET ​/A1-EI​/v1​/eitypes​/{eiTypeId}
569 # args: <response-code> <type-id> [<schema-file>]
570 # (Function for test scripts)
571 ecs_api_a1_get_type() {
572         __log_test_start $@
573
574     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
575                 __print_err "<response-code> <type-id> [<schema-file>]" $@
576                 return 1
577         fi
578
579         query="/A1-EI/v1/eitypes/$2"
580     res="$(__do_curl_to_api ECS GET $query)"
581     status=${res:${#res}-3}
582
583         if [ $status -ne $1 ]; then
584                 __log_test_fail_status_code $1 $status
585                 return 1
586         fi
587
588         if [ $# -eq 3 ]; then
589                 body=${res:0:${#res}-3}
590                 if [ -f $3 ]; then
591                         schema=$(cat $3)
592                 else
593                         __log_test_fail_general "Schema file "$3", does not exist"
594                         return 1
595                 fi
596                 if [ -z "$FLAT_A1_EI" ]; then
597                         targetJson="{\"eiJobParametersSchema\":$schema}"
598                 else
599                         targetJson=$schema
600                 fi
601                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
602                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
603
604                 if [ $res -ne 0 ]; then
605                         __log_test_fail_body
606                         return 1
607                 fi
608         fi
609
610         __log_test_pass
611         return 0
612 }
613
614 # API Test function: GET /A1-EI/v1/eitypes
615 # args: <response-code> [ (EMPTY | [<type-id>]+) ]
616 # (Function for test scripts)
617 ecs_api_a1_get_type_ids() {
618         __log_test_start $@
619
620     if [ $# -lt 1 ]; then
621                 __print_err "<response-code> [ (EMPTY | [<type-id>]+) ]" $@
622                 return 1
623         fi
624
625         query="/A1-EI/v1/eitypes"
626     res="$(__do_curl_to_api ECS GET $query)"
627     status=${res:${#res}-3}
628
629         if [ $status -ne $1 ]; then
630                 __log_test_fail_status_code $1 $status
631                 return 1
632         fi
633         if [ $# -gt 1 ]; then
634                 body=${res:0:${#res}-3}
635                 targetJson="["
636                 if [ $2 != "EMPTY" ]; then
637                         for pid in ${@:2} ; do
638                                 if [ "$targetJson" != "[" ]; then
639                                         targetJson=$targetJson","
640                                 fi
641                                 targetJson=$targetJson"\"$pid\""
642                         done
643                 fi
644                 targetJson=$targetJson"]"
645                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
646                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
647
648                 if [ $res -ne 0 ]; then
649                         __log_test_fail_body
650                         return 1
651                 fi
652         fi
653
654         __log_test_pass
655         return 0
656 }
657
658 # API Test function: GET ​/A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs​/{eiJobId}​/status
659 # args: <response-code> <type-id> <job-id> [<status>]
660 # args (flat uri structure): <response-code> <job-id> [<status> [<timeout>]]
661 # (Function for test scripts)
662 ecs_api_a1_get_job_status() {
663         __log_test_start $@
664
665         if [ -z "$FLAT_A1_EI" ]; then
666                 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
667                         __print_err "<response-code> <type-id> <job-id> [<status>]" $@
668                         return 1
669                 fi
670
671                 query="/A1-EI/v1/eitypes/$2/eijobs/$3/status"
672
673                 res="$(__do_curl_to_api ECS GET $query)"
674                 status=${res:${#res}-3}
675
676                 if [ $status -ne $1 ]; then
677                         __log_test_fail_status_code $1 $status
678                         return 1
679                 fi
680                 if [ $# -eq 4 ]; then
681                         body=${res:0:${#res}-3}
682                         targetJson="{\"operationalState\": \"$4\"}"
683                         echo " TARGET JSON: $targetJson" >> $HTTPLOG
684                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
685
686                         if [ $res -ne 0 ]; then
687                                 __log_test_fail_body
688                                 return 1
689                         fi
690                 fi
691         else
692                 echo -e $YELLOW"INTERFACE - FLAT URI STRUCTURE"$EYELLOW
693                 if [ $# -lt 2 ] && [ $# -gt 4 ]; then
694                         __print_err "<response-code> <job-id> [<status> [<timeout>]]" $@
695                         return 1
696                 fi
697
698                 query="/A1-EI/v1/eijobs/$2/status"
699
700                 start=$SECONDS
701                 for (( ; ; )); do
702                         res="$(__do_curl_to_api ECS GET $query)"
703                         status=${res:${#res}-3}
704
705                         if [ $# -eq 4 ]; then
706                                 duration=$((SECONDS-start))
707                                 echo -ne " Response=${status} after ${duration} seconds, waiting for ${3} ${SAMELINE}"
708                                 if [ $duration -gt $4 ]; then
709                                         echo ""
710                                         duration=-1  #Last iteration
711                                 fi
712                         else
713                                 duration=-1 #single test, no wait
714                         fi
715
716                         if [ $status -ne $1 ]; then
717                                 if [ $duration -eq -1 ]; then
718                                         __log_test_fail_status_code $1 $status
719                                         return 1
720                                 fi
721                         fi
722                         if [ $# -ge 3 ] && [ $status -eq $1 ]; then
723                                 body=${res:0:${#res}-3}
724                                 targetJson="{\"eiJobStatus\": \"$3\"}"
725                                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
726                                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
727
728                                 if [ $res -ne 0 ]; then
729                                         if [ $duration -eq -1 ]; then
730                                                 __log_test_fail_body
731                                                 return 1
732                                         fi
733                                 else
734                                         duration=-1  #Goto pass
735                                 fi
736                         fi
737                         if [ $duration -eq -1 ]; then
738                                 if [ $# -eq 4 ]; then
739                                         echo ""
740                                 fi
741                                 __log_test_pass
742                                 return 0
743                         else
744                                 sleep 1
745                         fi
746                 done
747         fi
748
749         __log_test_pass
750         return 0
751 }
752
753 # API Test function: GET ​/A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs​/{eiJobId}
754 # args: <response-code> <type-id> <job-id> [<target-url> <owner-id> <template-job-file>]
755 # args (flat uri structure): <response-code> <job-id> [<type-id> <target-url> <owner-id> <template-job-file>]
756 # (Function for test scripts)
757 ecs_api_a1_get_job() {
758         __log_test_start $@
759
760         if [  -z "$FLAT_A1_EI" ]; then
761                 if [ $# -ne 3 ] && [ $# -ne 6 ]; then
762                         __print_err "<response-code> <type-id> <job-id> [<target-url> <owner-id> <template-job-file>]" $@
763                         return 1
764                 fi
765                 query="/A1-EI/v1/eitypes/$2/eijobs/$3"
766         else
767                 echo -e $YELLOW"INTERFACE - FLAT URI STRUCTURE"$EYELLOW
768                 if [ $# -ne 2 ] && [ $# -ne 7 ]; then
769                         __print_err "<response-code> <job-id> [<type-id> <target-url> <owner-id> <notification-url> <template-job-file>]" $@
770                         return 1
771                 fi
772                 query="/A1-EI/v1/eijobs/$2"
773         fi
774     res="$(__do_curl_to_api ECS GET $query)"
775     status=${res:${#res}-3}
776
777         if [ $status -ne $1 ]; then
778                 __log_test_fail_status_code $1 $status
779                 return 1
780         fi
781
782         if [  -z "$FLAT_A1_EI" ]; then
783                 if [ $# -eq 6 ]; then
784                         body=${res:0:${#res}-3}
785
786                         if [ -f $6 ]; then
787                                 jobfile=$(cat $6)
788                                 jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
789                         else
790                                 _log_test_fail_general "Job template file "$6", does not exist"
791                                 return 1
792                         fi
793                         targetJson="{\"targetUri\": \"$4\",\"jobOwner\": \"$5\",\"jobParameters\": $jobfile}"
794                         echo " TARGET JSON: $targetJson" >> $HTTPLOG
795                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
796
797                         if [ $res -ne 0 ]; then
798                                 __log_test_fail_body
799                                 return 1
800                         fi
801                 fi
802         else
803                 if [ $# -eq 7 ]; then
804                         body=${res:0:${#res}-3}
805
806                         if [ -f $7 ]; then
807                                 jobfile=$(cat $7)
808                                 jobfile=$(echo "$jobfile" | sed "s/XXXX/$2/g")
809                         else
810                                 _log_test_fail_general "Job template file "$6", does not exist"
811                                 return 1
812                         fi
813                         targetJson="{\"eiTypeId\": \"$3\", \"jobResultUri\": \"$4\",\"jobOwner\": \"$5\",\"jobStatusNotificationUri\": \"$6\",\"jobDefinition\": $jobfile}"
814                         echo " TARGET JSON: $targetJson" >> $HTTPLOG
815                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
816
817                         if [ $res -ne 0 ]; then
818                                 __log_test_fail_body
819                                 return 1
820                         fi
821                 fi
822         fi
823
824         __log_test_pass
825         return 0
826 }
827
828 # API Test function: DELETE ​/A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs​/{eiJobId}
829 # args: <response-code> <type-id> <job-id>
830 # args (flat uri structure): <response-code> <job-id>
831 # (Function for test scripts)
832 ecs_api_a1_delete_job() {
833         __log_test_start $@
834
835         if [  -z "$FLAT_A1_EI" ]; then
836                 if [ $# -ne 3 ]; then
837                         __print_err "<response-code> <type-id> <job-id>" $@
838                         return 1
839                 fi
840
841                 query="/A1-EI/v1/eitypes/$2/eijobs/$3"
842         else
843                 echo -e $YELLOW"INTERFACE - FLAT URI STRUCTURE"$EYELLOW
844                 if [ $# -ne 2 ]; then
845                         __print_err "<response-code> <job-id>" $@
846                         return 1
847                 fi
848                 query="/A1-EI/v1/eijobs/$2"
849         fi
850     res="$(__do_curl_to_api ECS DELETE $query)"
851     status=${res:${#res}-3}
852
853         if [ $status -ne $1 ]; then
854                 __log_test_fail_status_code $1 $status
855                 return 1
856         fi
857
858         __log_test_pass
859         return 0
860 }
861
862 # API Test function: PUT ​/A1-EI​/v1​/eitypes​/{eiTypeId}​/eijobs​/{eiJobId}
863 # args: <response-code> <type-id> <job-id> <target-url> <owner-id> <template-job-file>
864 # args (flat uri structure): <response-code> <job-id> <type-id> <target-url> <owner-id> <notification-url> <template-job-file>
865 # (Function for test scripts)
866 ecs_api_a1_put_job() {
867         __log_test_start $@
868
869         if [  -z "$FLAT_A1_EI" ]; then
870                 if [ $# -lt 6 ]; then
871                         __print_err "<response-code> <type-id> <job-id> <target-url> <owner-id> <template-job-file>" $@
872                         return 1
873                 fi
874                 if [ -f $6 ]; then
875                         jobfile=$(cat $6)
876                         jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
877                 else
878                         _log_test_fail_general "Job template file "$6", does not exist"
879                         return 1
880                 fi
881
882                 inputJson="{\"targetUri\": \"$4\",\"jobOwner\": \"$5\",\"jobParameters\": $jobfile}"
883                 file="./tmp/.p.json"
884                 echo "$inputJson" > $file
885
886                 query="/A1-EI/v1/eitypes/$2/eijobs/$3"
887         else
888                 echo -e $YELLOW"INTERFACE - FLAT URI STRUCTURE"$EYELLOW
889                 if [ $# -lt 7 ]; then
890                         __print_err "<response-code> <job-id> <type-id> <target-url> <owner-id> <notification-url> <template-job-file>" $@
891                         return 1
892                 fi
893                 if [ -f $7 ]; then
894                         jobfile=$(cat $7)
895                         jobfile=$(echo "$jobfile" | sed "s/XXXX/$2/g")
896                 else
897                         _log_test_fail_general "Job template file "$7", does not exist"
898                         return 1
899                 fi
900
901                 inputJson="{\"eiTypeId\": \"$3\", \"jobResultUri\": \"$4\",\"jobOwner\": \"$5\",\"jobStatusNotificationUri\": \"$6\",\"jobDefinition\": $jobfile}"
902                 file="./tmp/.p.json"
903                 echo "$inputJson" > $file
904
905                 query="/A1-EI/v1/eijobs/$2"
906         fi
907
908     res="$(__do_curl_to_api ECS PUT $query $file)"
909     status=${res:${#res}-3}
910
911         if [ $status -ne $1 ]; then
912                 __log_test_fail_status_code $1 $status
913                 return 1
914         fi
915
916         __log_test_pass
917         return 0
918 }
919
920
921 ##########################################
922 ####   Enrichment Data Producer API   ####
923 ##########################################
924 # Function prefix: ecs_api_edp
925
926 # API Test function: GET /ei-producer/v1/eitypes
927 # args: <response-code> [ EMPTY | <type-id>+]
928 # (Function for test scripts)
929 ecs_api_edp_get_type_ids() {
930         __log_test_start $@
931
932     if [ $# -lt 1 ]; then
933                 __print_err "<response-code> [ EMPTY | <type-id>+]" $@
934                 return 1
935         fi
936
937         query="/ei-producer/v1/eitypes"
938     res="$(__do_curl_to_api ECS GET $query)"
939     status=${res:${#res}-3}
940
941         if [ $status -ne $1 ]; then
942                 __log_test_fail_status_code $1 $status
943                 return 1
944         fi
945
946         if [ $# -gt 1 ]; then
947                 body=${res:0:${#res}-3}
948                 targetJson="["
949                 if [ $2 != "EMPTY" ]; then
950                         for pid in ${@:2} ; do
951                                 if [ "$targetJson" != "[" ]; then
952                                         targetJson=$targetJson","
953                                 fi
954                                 targetJson=$targetJson"\"$pid\""
955                         done
956                 fi
957                 targetJson=$targetJson"]"
958                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
959                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
960
961                 if [ $res -ne 0 ]; then
962                         __log_test_fail_body
963                         return 1
964                 fi
965         fi
966
967         __log_test_pass
968         return 0
969 }
970
971 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}/status
972 # args: <response-code> <producer-id> [<status> [<timeout>]]
973 # (Function for test scripts)
974 ecs_api_edp_get_producer_status() {
975         __log_test_start $@
976
977     if [ $# -lt 2 ] || [ $# -gt 4 ]; then
978                 __print_err "<response-code> <producer-id> [<status> [<timeout>]]" $@
979                 return 1
980         fi
981
982         query="/ei-producer/v1/eiproducers/$2/status"
983         start=$SECONDS
984         for (( ; ; )); do
985                 res="$(__do_curl_to_api ECS GET $query)"
986                 status=${res:${#res}-3}
987
988                 if [ $# -eq 4 ]; then
989                         duration=$((SECONDS-start))
990                         echo -ne " Response=${status} after ${duration} seconds, waiting for ${3} ${SAMELINE}"
991                         if [ $duration -gt $4 ]; then
992                                 echo ""
993                                 duration=-1  #Last iteration
994                         fi
995                 else
996                         duration=-1 #single test, no wait
997                 fi
998
999                 if [ $status -ne $1 ]; then
1000                         if [ $duration -eq -1 ]; then
1001                                 __log_test_fail_status_code $1 $status
1002                                 return 1
1003                         fi
1004                 fi
1005                 if [ $# -ge 3 ] && [ $status -eq $1 ]; then
1006                         body=${res:0:${#res}-3}
1007                         targetJson="{\"operational_state\": \"$3\"}"
1008                         echo " TARGET JSON: $targetJson" >> $HTTPLOG
1009                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1010
1011                         if [ $res -ne 0 ]; then
1012                                 if [ $duration -eq -1 ]; then
1013                                         __log_test_fail_body
1014                                         return 1
1015                                 fi
1016                         else
1017                                 duration=-1  #Goto pass
1018                         fi
1019                 fi
1020                 if [ $duration -eq -1 ]; then
1021                         if [ $# -eq 4 ]; then
1022                                 echo ""
1023                         fi
1024                         __log_test_pass
1025                         return 0
1026                 else
1027                         sleep 1
1028                 fi
1029         done
1030 }
1031
1032
1033 # API Test function: GET /ei-producer/v1/eiproducers
1034 # args (v1_1): <response-code> [ EMPTY | <producer-id>+]
1035 # (Function for test scripts)
1036 ecs_api_edp_get_producer_ids() {
1037         __log_test_start $@
1038
1039     if [ $# -lt 1 ]; then
1040                 __print_err "<response-code> [ EMPTY | <producer-id>+]" $@
1041                 return 1
1042         fi
1043
1044         query="/ei-producer/v1/eiproducers"
1045     res="$(__do_curl_to_api ECS GET $query)"
1046     status=${res:${#res}-3}
1047
1048         if [ $status -ne $1 ]; then
1049                 __log_test_fail_status_code $1 $status
1050                 return 1
1051         fi
1052
1053         if [ $# -gt 1 ]; then
1054                 body=${res:0:${#res}-3}
1055                 targetJson="["
1056
1057                 for pid in ${@:2} ; do
1058                         if [ "$targetJson" != "[" ]; then
1059                                 targetJson=$targetJson","
1060                         fi
1061                         if [ $pid != "EMPTY" ]; then
1062                                 targetJson=$targetJson"\"$pid\""
1063                         fi
1064                 done
1065
1066                 targetJson=$targetJson"]"
1067                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1068                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1069
1070                 if [ $res -ne 0 ]; then
1071                         __log_test_fail_body
1072                         return 1
1073                 fi
1074         fi
1075
1076         __log_test_pass
1077         return 0
1078 }
1079
1080 # API Test function: GET /ei-producer/v1/eiproducers
1081 # args (v1_2): <response-code> [ ( NOTYPE | <type-id> ) [ EMPTY | <producer-id>+] ]
1082 # (Function for test scripts)
1083 ecs_api_edp_get_producer_ids_2() {
1084         __log_test_start $@
1085
1086     if [ $# -lt 1 ]; then
1087                 __print_err "<response-code> [ ( NOTYPE | <type-id> ) [ EMPTY | <producer-id>+] ]" $@
1088                 return 1
1089         fi
1090
1091         query="/ei-producer/v1/eiproducers"
1092         if [ $# -gt 1 ] && [ $2 != "NOTYPE" ]; then
1093                 query=$query"?ei_type_id=$2"
1094         fi
1095     res="$(__do_curl_to_api ECS GET $query)"
1096     status=${res:${#res}-3}
1097
1098         if [ $status -ne $1 ]; then
1099                 __log_test_fail_status_code $1 $status
1100                 return 1
1101         fi
1102
1103         if [ $# -gt 2 ]; then
1104                 body=${res:0:${#res}-3}
1105                 targetJson="["
1106
1107                 for pid in ${@:3} ; do
1108                         if [ "$targetJson" != "[" ]; then
1109                                 targetJson=$targetJson","
1110                         fi
1111                         if [ $pid != "EMPTY" ]; then
1112                                 targetJson=$targetJson"\"$pid\""
1113                         fi
1114                 done
1115
1116                 targetJson=$targetJson"]"
1117                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1118                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1119
1120                 if [ $res -ne 0 ]; then
1121                         __log_test_fail_body
1122                         return 1
1123                 fi
1124         fi
1125
1126         __log_test_pass
1127         return 0
1128 }
1129
1130 # API Test function: GET /ei-producer/v1/eitypes/{eiTypeId}
1131 # args: (v1_1) <response-code> <type-id> [<job-schema-file> (EMPTY | [<producer-id>]+)]
1132 # (Function for test scripts)
1133 ecs_api_edp_get_type() {
1134         __log_test_start $@
1135
1136         paramError=1
1137         if [ $# -eq 2 ]; then
1138                 paramError=0
1139         fi
1140         if [ $# -gt 3 ]; then
1141                 paramError=0
1142         fi
1143     if [ $paramError -ne 0 ]; then
1144                 __print_err "<response-code> <type-id> [<job-schema-file> 'EMPTY' | ([<producer-id>]+)]" $@
1145                 return 1
1146         fi
1147
1148         query="/ei-producer/v1/eitypes/$2"
1149     res="$(__do_curl_to_api ECS GET $query)"
1150     status=${res:${#res}-3}
1151
1152         if [ $status -ne $1 ]; then
1153                 __log_test_fail_status_code $1 $status
1154                 return 1
1155         fi
1156         if [ $# -gt 3 ]; then
1157                 body=${res:0:${#res}-3}
1158
1159                 if [ -f $3 ]; then
1160                         schema=$(cat $3)
1161                 else
1162                         __log_test_fail_general "Job template file "$3", does not exist"
1163                         return 1
1164                 fi
1165
1166                 targetJson=""
1167                 if [ $4 != "EMPTY" ]; then
1168                         for pid in ${@:4} ; do
1169                                 if [ "$targetJson" != "" ]; then
1170                                         targetJson=$targetJson","
1171                                 fi
1172                                 targetJson=$targetJson"\"$pid\""
1173                         done
1174                 fi
1175                 targetJson="{\"ei_job_data_schema\":$schema, \"ei_producer_ids\": [$targetJson]}"
1176
1177                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1178                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1179
1180                 if [ $res -ne 0 ]; then
1181                         __log_test_fail_body
1182                         return 1
1183                 fi
1184         fi
1185         __log_test_pass
1186         return 0
1187 }
1188
1189 # API Test function: GET /ei-producer/v1/eitypes/{eiTypeId}
1190 # args: (v1_2) <response-code> <type-id> [<job-schema-file> ]
1191 # (Function for test scripts)
1192 ecs_api_edp_get_type_2() {
1193         __log_test_start $@
1194
1195         paramError=1
1196         if [ $# -eq 2 ]; then
1197                 paramError=0
1198         fi
1199         if [ $# -eq 3 ]; then
1200                 paramError=0
1201         fi
1202     if [ $paramError -ne 0 ]; then
1203                 __print_err "<response-code> <type-id> [<job-schema-file> ]" $@
1204                 return 1
1205         fi
1206
1207         query="/ei-producer/v1/eitypes/$2"
1208     res="$(__do_curl_to_api ECS GET $query)"
1209     status=${res:${#res}-3}
1210
1211         if [ $status -ne $1 ]; then
1212                 __log_test_fail_status_code $1 $status
1213                 return 1
1214         fi
1215         if [ $# -eq 3 ]; then
1216                 body=${res:0:${#res}-3}
1217
1218                 if [ -f $3 ]; then
1219                         schema=$(cat $3)
1220                 else
1221                         __log_test_fail_general "Job template file "$3", does not exist"
1222                         return 1
1223                 fi
1224
1225                 targetJson="{\"ei_job_data_schema\":$schema}"
1226
1227                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1228                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1229
1230                 if [ $res -ne 0 ]; then
1231                         __log_test_fail_body
1232                         return 1
1233                 fi
1234         fi
1235         __log_test_pass
1236         return 0
1237 }
1238
1239 # API Test function: PUT /ei-producer/v1/eitypes/{eiTypeId}
1240 # args: (v1_2) <response-code> <type-id> <job-schema-file>
1241 # (Function for test scripts)
1242 ecs_api_edp_put_type_2() {
1243         __log_test_start $@
1244
1245     if [ $# -ne 3 ]; then
1246                 __print_err "<response-code> <type-id> <job-schema-file>" $@
1247                 return 1
1248         fi
1249
1250         if [ ! -f $3 ]; then
1251                 __log_test_fail_general "Job schema file "$3", does not exist"
1252                 return 1
1253         fi
1254         schema=$(cat $3)
1255         input_json="{\"ei_job_data_schema\":$schema}"
1256         file="./tmp/put_type.json"
1257         echo $input_json > $file
1258
1259         query="/ei-producer/v1/eitypes/$2"
1260     res="$(__do_curl_to_api ECS PUT $query $file)"
1261     status=${res:${#res}-3}
1262
1263         if [ $status -ne $1 ]; then
1264                 __log_test_fail_status_code $1 $status
1265                 return 1
1266         fi
1267
1268         __log_test_pass
1269         return 0
1270 }
1271
1272 # API Test function: DELETE /ei-producer/v1/eitypes/{eiTypeId}
1273 # args: (v1_2) <response-code> <type-id>
1274 # (Function for test scripts)
1275 ecs_api_edp_delete_type_2() {
1276         __log_test_start $@
1277
1278     if [ $# -ne 2 ]; then
1279                 __print_err "<response-code> <type-id>" $@
1280                 return 1
1281         fi
1282
1283         query="/ei-producer/v1/eitypes/$2"
1284     res="$(__do_curl_to_api ECS DELETE $query)"
1285     status=${res:${#res}-3}
1286
1287         if [ $status -ne $1 ]; then
1288                 __log_test_fail_status_code $1 $status
1289                 return 1
1290         fi
1291
1292         __log_test_pass
1293         return 0
1294 }
1295
1296 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}
1297 # args: (v1_1) <response-code> <producer-id> [<job-callback> <supervision-callback> (EMPTY | [<type-id> <schema-file>]+) ]
1298 # (Function for test scripts)
1299 ecs_api_edp_get_producer() {
1300         __log_test_start $@
1301
1302         #Possible arg count: 2, 5 6, 8, 10 etc
1303         paramError=1
1304         if [ $# -eq 2 ]; then
1305                 paramError=0
1306         fi
1307         if [ $# -eq 5 ] && [ "$5" == "EMPTY" ]; then
1308                 paramError=0
1309         fi
1310         variablecount=$(($#-4))
1311         if [ $# -gt 5 ] && [ $(($variablecount%2)) -eq 0 ]; then
1312                 paramError=0
1313         fi
1314
1315     if [ $paramError -ne 0 ]; then
1316                 __print_err "<response-code> <producer-id> [<job-callback> <supervision-callback> (NOID | [<type-id> <schema-file>]+) ]" $@
1317                 return 1
1318         fi
1319
1320         query="/ei-producer/v1/eiproducers/$2"
1321     res="$(__do_curl_to_api ECS GET $query)"
1322     status=${res:${#res}-3}
1323
1324         if [ $status -ne $1 ]; then
1325                 __log_test_fail_status_code $1 $status
1326                 return 1
1327         fi
1328
1329         if [ $# -gt 2 ]; then
1330                 body=${res:0:${#res}-3}
1331                 targetJson="["
1332                 if [ $# -gt 5 ]; then
1333                         arr=(${@:5})
1334                         for ((i=0; i<$(($#-5)); i=i+2)); do
1335                                 if [ "$targetJson" != "[" ]; then
1336                                         targetJson=$targetJson","
1337                                 fi
1338                                 if [ -f ${arr[$i+1]} ]; then
1339                                         schema=$(cat ${arr[$i+1]})
1340                                 else
1341                                         _log_test_fail_general "Schema file "${arr[$i+1]}", does not exist"
1342                                         return 1
1343                                 fi
1344
1345                                 targetJson=$targetJson"{\"ei_type_identity\":\"${arr[$i]}\",\"ei_job_data_schema\":$schema}"
1346                         done
1347                 fi
1348                 targetJson=$targetJson"]"
1349                 if [ $# -gt 4 ]; then
1350                         targetJson="{\"supported_ei_types\":$targetJson,\"ei_job_callback_url\": \"$3\",\"ei_producer_supervision_callback_url\": \"$4\"}"
1351                 fi
1352                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1353                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1354
1355                 if [ $res -ne 0 ]; then
1356                         __log_test_fail_body
1357                         return 1
1358                 fi
1359         fi
1360
1361         __log_test_pass
1362         return 0
1363 }
1364
1365 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}
1366 # args (v1_2): <response-code> <producer-id> [<job-callback> <supervision-callback> (EMPTY | <type-id>+) ]
1367 # (Function for test scripts)
1368 ecs_api_edp_get_producer_2() {
1369         __log_test_start $@
1370
1371         #Possible arg count: 2, 5, 6, 7, 8 etc
1372         paramError=1
1373         if [ $# -eq 2 ]; then
1374                 paramError=0
1375         fi
1376         if [ $# -eq 5 ] && [ "$5" == "EMPTY" ]; then
1377                 paramError=0
1378         fi
1379         if [ $# -ge 5 ]; then
1380                 paramError=0
1381         fi
1382
1383     if [ $paramError -ne 0 ]; then
1384                 __print_err "<response-code> <producer-id> [<job-callback> <supervision-callback> (EMPTY | <type-id>+) ]" $@
1385                 return 1
1386         fi
1387
1388         query="/ei-producer/v1/eiproducers/$2"
1389     res="$(__do_curl_to_api ECS GET $query)"
1390     status=${res:${#res}-3}
1391
1392         if [ $status -ne $1 ]; then
1393                 __log_test_fail_status_code $1 $status
1394                 return 1
1395         fi
1396
1397         if [ $# -gt 2 ]; then
1398                 body=${res:0:${#res}-3}
1399                 targetJson="["
1400                 if [ $# -gt 4 ] && [ "$5" != "EMPTY" ]; then
1401                         arr=(${@:5})
1402                         for ((i=0; i<$(($#-4)); i=i+1)); do
1403                                 if [ "$targetJson" != "[" ]; then
1404                                         targetJson=$targetJson","
1405                                 fi
1406                                 targetJson=$targetJson"\"${arr[$i]}\""
1407                         done
1408                 fi
1409                 targetJson=$targetJson"]"
1410                 if [ $# -gt 4 ]; then
1411                         targetJson="{\"supported_ei_types\":$targetJson,\"ei_job_callback_url\": \"$3\",\"ei_producer_supervision_callback_url\": \"$4\"}"
1412                 fi
1413                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1414                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1415
1416                 if [ $res -ne 0 ]; then
1417                         __log_test_fail_body
1418                         return 1
1419                 fi
1420         fi
1421
1422         __log_test_pass
1423         return 0
1424 }
1425
1426 # API Test function: DELETE /ei-producer/v1/eiproducers/{eiProducerId}
1427 # args: <response-code> <producer-id>
1428 # (Function for test scripts)
1429 ecs_api_edp_delete_producer() {
1430         __log_test_start $@
1431
1432     if [ $# -lt 2 ]; then
1433                 __print_err "<response-code> <producer-id>" $@
1434                 return 1
1435         fi
1436
1437         query="/ei-producer/v1/eiproducers/$2"
1438     res="$(__do_curl_to_api ECS DELETE $query)"
1439     status=${res:${#res}-3}
1440
1441         if [ $status -ne $1 ]; then
1442                 __log_test_fail_status_code $1 $status
1443                 return 1
1444         fi
1445
1446         __log_test_pass
1447         return 0
1448 }
1449
1450 # API Test function: PUT /ei-producer/v1/eiproducers/{eiProducerId}
1451 # args: (v1_1) <response-code> <producer-id> <job-callback> <supervision-callback> NOTYPE|[<type-id> <schema-file>]+
1452 # (Function for test scripts)
1453 ecs_api_edp_put_producer() {
1454         __log_test_start $@
1455
1456         #Valid number of parametrer 5,6,8,10,
1457         paramError=1
1458         if  [ $# -eq 5 ] && [ "$5" == "NOTYPE" ]; then
1459                 paramError=0
1460         elif [ $# -gt 5 ] && [ $(($#%2)) -eq 0 ]; then
1461                 paramError=0
1462         fi
1463         if [ $paramError -ne 0 ]; then
1464                 __print_err "<response-code> <producer-id> <job-callback> <supervision-callback> NOTYPE|[<type-id> <schema-file>]+" $@
1465                 return 1
1466         fi
1467
1468         inputJson="["
1469         if [ $# -gt 5 ]; then
1470                 arr=(${@:5})
1471                 for ((i=0; i<$(($#-5)); i=i+2)); do
1472                         if [ "$inputJson" != "[" ]; then
1473                                 inputJson=$inputJson","
1474                         fi
1475                         if [ -f ${arr[$i+1]} ]; then
1476                                 schema=$(cat ${arr[$i+1]})
1477                         else
1478                                 _log_test_fail_general "Schema file "${arr[$i+1]}", does not exist"
1479                                 return 1
1480                         fi
1481                         inputJson=$inputJson"{\"ei_type_identity\":\"${arr[$i]}\",\"ei_job_data_schema\":$schema}"
1482                 done
1483         fi
1484         inputJson="\"supported_ei_types\":"$inputJson"]"
1485
1486         inputJson=$inputJson",\"ei_job_callback_url\": \"$3\",\"ei_producer_supervision_callback_url\": \"$4\""
1487
1488         inputJson="{"$inputJson"}"
1489
1490         file="./tmp/.p.json"
1491         echo "$inputJson" > $file
1492         query="/ei-producer/v1/eiproducers/$2"
1493     res="$(__do_curl_to_api ECS PUT $query $file)"
1494     status=${res:${#res}-3}
1495
1496         if [ $status -ne $1 ]; then
1497                 __log_test_fail_status_code $1 $status
1498                 return 1
1499         fi
1500
1501         __log_test_pass
1502         return 0
1503 }
1504
1505 # API Test function: PUT /ei-producer/v1/eiproducers/{eiProducerId}
1506 # args: (v1_2) <response-code> <producer-id> <job-callback> <supervision-callback> NOTYPE|[<type-id>+]
1507 # (Function for test scripts)
1508 ecs_api_edp_put_producer_2() {
1509         __log_test_start $@
1510
1511         #Valid number of parametrer 5,6,8,10,
1512         paramError=1
1513         if  [ $# -eq 5 ] && [ "$5" == "NOTYPE" ]; then
1514                 paramError=0
1515         elif [ $# -ge 5 ]; then
1516                 paramError=0
1517         fi
1518         if [ $paramError -ne 0 ]; then
1519                 __print_err "<response-code> <producer-id> <job-callback> <supervision-callback> NOTYPE|[<type-id>+]" $@
1520                 return 1
1521         fi
1522
1523         inputJson="["
1524         if [ $# -gt 4 ] && [ "$5" != "NOTYPE" ]; then
1525                 arr=(${@:5})
1526                 for ((i=0; i<$(($#-4)); i=i+1)); do
1527                         if [ "$inputJson" != "[" ]; then
1528                                 inputJson=$inputJson","
1529                         fi
1530                         inputJson=$inputJson"\""${arr[$i]}"\""
1531                 done
1532         fi
1533         inputJson="\"supported_ei_types\":"$inputJson"]"
1534
1535         inputJson=$inputJson",\"ei_job_callback_url\": \"$3\",\"ei_producer_supervision_callback_url\": \"$4\""
1536
1537         inputJson="{"$inputJson"}"
1538
1539         file="./tmp/.p.json"
1540         echo "$inputJson" > $file
1541         query="/ei-producer/v1/eiproducers/$2"
1542     res="$(__do_curl_to_api ECS PUT $query $file)"
1543     status=${res:${#res}-3}
1544
1545         if [ $status -ne $1 ]; then
1546                 __log_test_fail_status_code $1 $status
1547                 return 1
1548         fi
1549
1550         __log_test_pass
1551         return 0
1552 }
1553
1554 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}/eijobs
1555 # args: (V1-1) <response-code> <producer-id> (EMPTY | [<job-id> <type-id> <target-url> <job-owner> <template-job-file>]+)
1556 # (Function for test scripts)
1557 ecs_api_edp_get_producer_jobs() {
1558         __log_test_start $@
1559
1560         #Valid number of parameter 2,3,7,11
1561         paramError=1
1562         if [ $# -eq 2 ]; then
1563                 paramError=0
1564         fi
1565         if [ $# -eq 3 ] && [ "$3" == "EMPTY" ]; then
1566                 paramError=0
1567         fi
1568         variablecount=$(($#-2))
1569         if [ $# -gt 3 ] && [ $(($variablecount%5)) -eq 0 ]; then
1570                 paramError=0
1571         fi
1572         if [ $paramError -eq 1 ]; then
1573                 __print_err "<response-code> <producer-id> (EMPTY | [<job-id> <type-id> <target-url> <job-owner> <template-job-file>]+)" $@
1574                 return 1
1575         fi
1576
1577         query="/ei-producer/v1/eiproducers/$2/eijobs"
1578     res="$(__do_curl_to_api ECS GET $query)"
1579     status=${res:${#res}-3}
1580         if [ $status -ne $1 ]; then
1581                 __log_test_fail_status_code $1 $status
1582                 return 1
1583         fi
1584         if [ $# -gt 2 ]; then
1585                 body=${res:0:${#res}-3}
1586                 targetJson="["
1587                 if [ $# -gt 3 ]; then
1588                         arr=(${@:3})
1589                         for ((i=0; i<$(($#-3)); i=i+5)); do
1590                                 if [ "$targetJson" != "[" ]; then
1591                                         targetJson=$targetJson","
1592                                 fi
1593                                 if [ -f ${arr[$i+4]} ]; then
1594                                         jobfile=$(cat ${arr[$i+4]})
1595                                         jobfile=$(echo "$jobfile" | sed "s/XXXX/${arr[$i]}/g")
1596                                 else
1597                                         _log_test_fail_general "Job template file "${arr[$i+4]}", does not exist"
1598                                         return 1
1599                                 fi
1600                                 targetJson=$targetJson"{\"ei_job_identity\":\"${arr[$i]}\",\"ei_type_identity\":\"${arr[$i+1]}\",\"target_uri\":\"${arr[$i+2]}\",\"owner\":\"${arr[$i+3]}\",\"ei_job_data\":$jobfile}"
1601                         done
1602                 fi
1603                 targetJson=$targetJson"]"
1604
1605                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1606                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1607
1608                 if [ $res -ne 0 ]; then
1609                         __log_test_fail_body
1610                         return 1
1611                 fi
1612         fi
1613
1614         __log_test_pass
1615         return 0
1616 }
1617
1618 # API Test function: GET /ei-producer/v1/eiproducers/{eiProducerId}/eijobs
1619 # args: (V1-2) <response-code> <producer-id> (EMPTY | [<job-id> <type-id> <target-url> <job-owner> <template-job-file>]+)
1620 # (Function for test scripts)
1621 ecs_api_edp_get_producer_jobs_2() {
1622         __log_test_start $@
1623
1624         #Valid number of parameter 2,3,7,11
1625         paramError=1
1626         if [ $# -eq 2 ]; then
1627                 paramError=0
1628         fi
1629         if [ $# -eq 3 ] && [ "$3" == "EMPTY" ]; then
1630                 paramError=0
1631         fi
1632         variablecount=$(($#-2))
1633         if [ $# -gt 3 ] && [ $(($variablecount%5)) -eq 0 ]; then
1634                 paramError=0
1635         fi
1636         if [ $paramError -eq 1 ]; then
1637                 __print_err "<response-code> <producer-id> (EMPTY | [<job-id> <type-id> <target-url> <job-owner> <template-job-file>]+)" $@
1638                 return 1
1639         fi
1640
1641         query="/ei-producer/v1/eiproducers/$2/eijobs"
1642     res="$(__do_curl_to_api ECS GET $query)"
1643     status=${res:${#res}-3}
1644         if [ $status -ne $1 ]; then
1645                 __log_test_fail_status_code $1 $status
1646                 return 1
1647         fi
1648         if [ $# -gt 2 ]; then
1649                 body=${res:0:${#res}-3}
1650                 targetJson="["
1651                 if [ $# -gt 3 ]; then
1652                         arr=(${@:3})
1653                         for ((i=0; i<$(($#-3)); i=i+5)); do
1654                                 if [ "$targetJson" != "[" ]; then
1655                                         targetJson=$targetJson","
1656                                 fi
1657                                 if [ -f ${arr[$i+4]} ]; then
1658                                         jobfile=$(cat ${arr[$i+4]})
1659                                         jobfile=$(echo "$jobfile" | sed "s/XXXX/${arr[$i]}/g")
1660                                 else
1661                                         _log_test_fail_general "Job template file "${arr[$i+4]}", does not exist"
1662                                         return 1
1663                                 fi
1664                                 targetJson=$targetJson"{\"ei_job_identity\":\"${arr[$i]}\",\"ei_type_identity\":\"${arr[$i+1]}\",\"target_uri\":\"${arr[$i+2]}\",\"owner\":\"${arr[$i+3]}\",\"ei_job_data\":$jobfile, \"last_updated\":\"????\"}"
1665                         done
1666                 fi
1667                 targetJson=$targetJson"]"
1668
1669                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1670                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1671
1672                 if [ $res -ne 0 ]; then
1673                         __log_test_fail_body
1674                         return 1
1675                 fi
1676         fi
1677
1678         __log_test_pass
1679         return 0
1680 }
1681
1682 ##########################################
1683 ####          Service status          ####
1684 ##########################################
1685 # Function prefix: ecs_api_service
1686
1687 # API Test function: GET ​/status
1688 # args: <response-code>
1689 # (Function for test scripts)
1690 ecs_api_service_status() {
1691         __log_test_start $@
1692
1693     if [ $# -lt 1 ]; then
1694                 __print_err "<response-code> [<producer-id>]*|NOID" $@
1695                 return 1
1696         fi
1697         res="$(__do_curl_to_api ECS GET /status)"
1698     status=${res:${#res}-3}
1699         if [ $status -ne $1 ]; then
1700                 __log_test_fail_status_code $1 $status
1701                 return 1
1702         fi
1703         __log_test_pass
1704         return 0
1705 }
1706
1707
1708 ##########################################
1709 ####          Reset jobs              ####
1710 ##########################################
1711 # Function prefix: ecs_api_admin
1712
1713 # Admin to remove all jobs
1714 # args: <response-code> [ <type> ]
1715 # (Function for test scripts)
1716
1717 ecs_api_admin_reset() {
1718         __log_test_start $@
1719
1720         if [  -z "$FLAT_A1_EI" ]; then
1721                 query="/A1-EI/v1/eitypes/$2/eijobs"
1722         else
1723                 query="/A1-EI/v1/eijobs"
1724         fi
1725     res="$(__do_curl_to_api ECS GET $query)"
1726     status=${res:${#res}-3}
1727
1728         if [ $status -ne 200 ]; then
1729                 __log_test_fail_status_code $1 $status
1730                 return 1
1731         fi
1732
1733         #Remove brackets and response code
1734         body=${res:1:${#res}-4}
1735         list=$(echo ${body//,/ })
1736         list=$(echo ${list//[/})
1737         list=$(echo ${list//]/})
1738         list=$(echo ${list//\"/})
1739         list=$list" "
1740         for job in $list; do
1741                 if [  -z "$FLAT_A1_EI" ]; then
1742                         echo "Not supported for non-flat EI api"
1743                 else
1744                         query="/A1-EI/v1/eijobs/$job"
1745                         res="$(__do_curl_to_api ECS DELETE $query)"
1746                         status=${res:${#res}-3}
1747                         if [ $status -ne 204 ]; then
1748                                 __log_test_fail_status_code $1 $status
1749                                 return 1
1750                         fi
1751                         echo " Deleted job: "$job
1752                 fi
1753         done
1754
1755         __log_test_pass
1756         return 0
1757 }