Fix distribution of Kafka messages
[nonrtric.git] / test / common / helmmanager_api_functions.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2021 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 managemnt functions test functions for Helm Manager
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 __HELMMANAGER_imagesetup() {
28         __check_and_create_image_var HELMMANAGER "HELM_MANAGER_IMAGE" "HELM_MANAGER_IMAGE_BASE" "HELM_MANAGER_IMAGE_TAG" $1 "$HELM_MANAGER_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 arg var may contain: 'remote', 'remote-remove' or 'local'
36 __HELMMANAGER_imagepull() {
37         __check_and_pull_image $1 "$HELM_MANAGER_DISPLAY_NAME" $HELM_MANAGER_APP_NAME HELM_MANAGER_IMAGE
38 }
39
40 # Generate a string for each included image using the app display name and a docker images format string
41 # If a custom image repo is used then also the source image from the local repo is listed
42 # arg: <docker-images-format-string> <file-to-append>
43 __HELMMANAGER_image_data() {
44         echo -e "$HELM_MANAGER_DISPLAY_NAME\t$(docker images --format $1 $HELM_MANAGER_IMAGE)" >>   $2
45         if [ ! -z "$HELM_MANAGER_IMAGE_SOURCE" ]; then
46                 echo -e "-- source image --\t$(docker images --format $1 $HELM_MANAGER_IMAGE_SOURCE)" >>   $2
47         fi
48 }
49
50 # Scale kubernetes resources to zero
51 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
52 # This function is called for apps fully managed by the test script
53 __HELMMANAGER_kube_scale_zero() {
54         __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest HELMMANAGER
55 }
56
57 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
58 # This function is called for prestarted apps not managed by the test script.
59 __HELMMANAGER_kube_scale_zero_and_wait() {
60         __kube_scale_and_wait_all_resources $KUBE_NONRTRIC_NAMESPACE app "$KUBE_NONRTRIC_NAMESPACE"-helmmanagerservice
61 }
62
63 # Delete all kube resouces for the app
64 # This function is called for apps managed by the test script.
65 __HELMMANAGER_kube_delete_all() {
66         __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest HELMMANAGER
67 }
68
69 # Store docker logs
70 # This function is called for apps managed by the test script.
71 # args: <log-dir> <file-prexix>
72 __HELMMANAGER_store_docker_logs() {
73         if [ $RUNMODE == "KUBE" ]; then
74                 kubectl  logs -l "autotest=HELMMANAGER" -n $KUBE_NONRTRIC_NAMESPACE --tail=-1 > $1$2_helmmanager.log 2>&1
75         else
76                 docker logs $HELM_MANAGER_APP_NAME > $1$2_helmmanager.log 2>&1
77         fi
78 }
79
80 # Initial setup of protocol, host and ports
81 # This function is called for apps managed by the test script.
82 # args: -
83 __HELMMANAGER_initial_setup() {
84         use_helm_manager_http
85 }
86
87 # Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers
88 # For docker, the namespace shall be excluded
89 # This function is called for apps managed by the test script as well as for prestarted apps.
90 # args: -
91 __HELMMANAGER_statisics_setup() {
92         if [ $RUNMODE == "KUBE" ]; then
93                 echo "HELMMANAGER $HELM_MANAGER_APP_NAME $KUBE_NONRTRIC_NAMESPACE"
94         else
95                 echo "HELMMANAGER $HELM_MANAGER_APP_NAME"
96         fi
97 }
98
99 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
100 # args: -
101 __HELMMANAGER_test_requirements() {
102         tmp=$(which helm)
103         if [ $? -ne 0 ]; then
104                 echo $RED" Helm3 is required for running helm manager tests. Pls install helm3"
105                 exit 1
106         fi
107         tmp_version=$(helm version | grep 'v3')
108         if [ -z "$tmp_version" ]; then
109                 echo $RED" Helm3 is required for running helm manager tests. Pls install helm3"
110                 exit 1
111         fi
112 }
113
114 #######################################################
115
116 # Set http as the protocol to use for all communication to the Helm Manager
117 # args: -
118 # (Function for test scripts)
119 use_helm_manager_http() {
120         __helm_manager_set_protocoll "http" $HELM_MANAGER_INTERNAL_PORT $HELM_MANAGER_EXTERNAL_PORT
121 }
122
123 # Set https as the protocol to use for all communication to the Helm Manager
124 # args: -
125 # (Function for test scripts)
126 use_helm_manager_https() {
127         __helm_manager_set_protocoll "https" $HELM_MANAGER_INTERNAL_SECURE_PORT $HELM_MANAGER_EXTERNAL_SECURE_PORT
128 }
129
130 # Setup paths to svc/container for internal and external access
131 # args: <protocol> <internal-port> <external-port>
132 __helm_manager_set_protocoll() {
133         echo -e $BOLD"$HELM_MANAGER_DISPLAY_NAME protocol setting"$EBOLD
134         echo -e " Using $BOLD $1 $EBOLD towards $HELM_MANAGER_DISPLAY_NAME"
135
136         ## Access to Helm Manager
137
138         HELMMANAGER_SERVICE_PATH=$1"://$HELM_MANAGER_USER:$HELM_MANAGER_PWD@"$HELM_MANAGER_APP_NAME":"$2  # docker access, container->container and script->container via proxy
139         if [ $RUNMODE == "KUBE" ]; then
140                 HELMMANAGER_SERVICE_PATH=$1"://$HELM_MANAGER_USER:$HELM_MANAGER_PWD@"$HELM_MANAGER_APP_NAME.$KUBE_NONRTRIC_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
141         fi
142
143         echo ""
144 }
145
146 # Export env vars for config files, docker compose and kube resources
147 # args:
148 __helm_manager_export_vars() {
149
150         export HELM_MANAGER_APP_NAME
151         export HELM_MANAGER_DISPLAY_NAME
152
153         export DOCKER_SIM_NWNAME
154         export KUBE_NONRTRIC_NAMESPACE
155
156         export HELM_MANAGER_EXTERNAL_PORT
157         export HELM_MANAGER_INTERNAL_PORT
158         export HELM_MANAGER_EXTERNAL_SECURE_PORT
159         export HELM_MANAGER_INTERNAL_SECURE_PORT
160         export HELM_MANAGER_CLUSTER_ROLE
161         export HELM_MANAGER_SA_NAME
162         export HELM_MANAGER_ALIVE_URL
163         export HELM_MANAGER_COMPOSE_DIR
164         export HELM_MANAGER_USER
165         export HELM_MANAGER_PWD
166 }
167
168 # Start the Helm Manager container
169 # args: -
170 # (Function for test scripts)
171 start_helm_manager() {
172
173         echo -e $BOLD"Starting $HELM_MANAGER_DISPLAY_NAME"$EBOLD
174
175         if [ $RUNMODE == "KUBE" ]; then
176
177                 # Check if app shall be fully managed by the test script
178                 __check_included_image "HELMMANAGER"
179                 retcode_i=$?
180
181                 # Check if app shall only be used by the testscipt
182                 __check_prestarted_image "HELMMANAGER"
183                 retcode_p=$?
184
185                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
186                         echo -e $RED"The $HELM_MANAGER_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
187                         echo -e $RED"The $HELM_MANAGER_APP_NAME will not be started"$ERED
188                         exit
189                 fi
190                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
191                         echo -e $RED"The $HELM_MANAGER_APP_NAME app is included both as managed and prestarted in this test script"$ERED
192                         echo -e $RED"The $HELM_MANAGER_APP_NAME will not be started"$ERED
193                         exit
194                 fi
195
196                 if [ $retcode_p -eq 0 ]; then
197                         echo -e " Using existing $HELM_MANAGER_APP_NAME deployment and service"
198                         echo " Setting $HELM_MANAGER_APP_NAME replicas=1"
199                         __kube_scale deployment $HELM_MANAGER_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
200                 fi
201
202                 if [ $retcode_i -eq 0 ]; then
203
204                         echo -e " Creating $HELM_MANAGER_APP_NAME app and expose service"
205
206                         #Check if nonrtric namespace exists, if not create it
207                         __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
208
209                         __helm_manager_export_vars
210
211                         #Create sa
212                         input_yaml=$SIM_GROUP"/"$HELM_MANAGER_COMPOSE_DIR"/"sa.yaml
213                         output_yaml=$PWD/tmp/helmmanager_sa_svc.yaml
214                         __kube_create_instance sa $HELM_MANAGER_APP_NAME $input_yaml $output_yaml
215
216                         #Create service
217                         input_yaml=$SIM_GROUP"/"$HELM_MANAGER_COMPOSE_DIR"/"svc.yaml
218                         output_yaml=$PWD/tmp/helmmanager_svc.yaml
219                         __kube_create_instance service $HELM_MANAGER_APP_NAME $input_yaml $output_yaml
220
221                         #Create app
222                         input_yaml=$SIM_GROUP"/"$HELM_MANAGER_COMPOSE_DIR"/"app.yaml
223                         output_yaml=$PWD/tmp/helmmanager_app.yaml
224                         __kube_create_instance app $HELM_MANAGER_APP_NAME $input_yaml $output_yaml
225                 fi
226
227                 __check_service_start $HELM_MANAGER_APP_NAME $HELMMANAGER_SERVICE_PATH$HELM_MANAGER_ALIVE_URL
228
229         else
230                 __check_included_image 'HELMMANAGER'
231                 if [ $? -eq 1 ]; then
232                         echo -e $RED"The Helm Manager app is not included as managed in this test script"$ERED
233                         echo -e $RED"The Helm Manager will not be started"$ERED
234                         exit
235                 fi
236
237                 __helm_manager_export_vars
238
239                 __start_container $HELM_MANAGER_COMPOSE_DIR "" NODOCKERARGS 1 $HELM_MANAGER_APP_NAME
240
241                 __check_service_start $HELM_MANAGER_APP_NAME $HELMMANAGER_SERVICE_PATH$HELM_MANAGER_ALIVE_URL
242         fi
243         echo ""
244 }
245
246 # Excute a curl cmd towards the helm manager.
247 # args: GET <path>
248 # args: POST <path> <file-to-post>
249 # args: POST3 <path> <name> <file-to-post> <name> <file-to-post> <name> <file-to-post>
250 __execute_curl_to_helmmanger() {
251     TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
252     echo "(${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
253         proxyflag=""
254         if [ ! -z "$KUBE_PROXY_PATH" ]; then
255                 if [ $KUBE_PROXY_HTTPX == "http" ]; then
256                         proxyflag=" --proxy $KUBE_PROXY_PATH"
257                 else
258                         proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
259                 fi
260         fi
261         if [ $1 == "GET" ]; then
262                 curlstring="curl -skw %{http_code} $proxyflag $HELMMANAGER_SERVICE_PATH$2"
263         elif [ $1 == "POST" ]; then
264                 curlstring="curl -skw %{http_code} $proxyflag $HELMMANAGER_SERVICE_PATH$2 -X POST --data-binary @$3 -H Content-Type:application/json"
265         elif [ $1 == "POST1_2" ]; then
266                 curlstring="curl -skw %{http_code} $proxyflag $HELMMANAGER_SERVICE_PATH$2 -X POST -F $3=<$4 -F $5=@$6 -F $7=@$8 "
267         elif [ $1 == "DELETE" ]; then
268                 curlstring="curl -skw %{http_code} $proxyflag $HELMMANAGER_SERVICE_PATH$2 -X DELETE"
269         else
270                 echo " Unknown operation $1" >> $HTTPLOG
271                 echo "000"
272                 return 1
273         fi
274         echo " CMD: $curlstring" >> $HTTPLOG
275         res="$($curlstring)"
276         retcode=$?
277         echo " RESP: $res" >> $HTTPLOG
278     if [ $retcode -ne 0 ]; then
279         echo "000"
280                 return 1
281     fi
282     echo $res
283         return 0
284 }
285
286 # API Test function: GET ​/helm/charts
287 # args: <response-code> [ EMPTY | ( <chart> <version> <namespace> <release> <repo> )+ ]
288 # (Function for test scripts)
289 helm_manager_api_get_charts() {
290         __log_test_start $@
291
292         error_params=1
293         variablecount=$(($#-1))
294     if [ $# -eq 1 ]; then
295                 error_params=0
296         elif [ $# -eq 2 ] && [ $2 == "EMPTY" ]; then
297                 error_params=0
298         elif [ $(($variablecount%5)) -eq 0 ]; then
299                 error_params=0
300         fi
301
302
303         if [ $error_params -eq 1 ]; then
304                 __print_err "<response-code>" $@
305                 return 1
306         fi
307
308         query="/helm/charts"
309     res="$(__execute_curl_to_helmmanger GET $query)"
310     status=${res:${#res}-3}
311
312         if [ $status -ne $1 ]; then
313                 __log_test_fail_status_code $1 $status
314                 return 1
315         fi
316
317         if [ $# -gt 1 ]; then
318                 body=${res:0:${#res}-3}
319                 shift
320                 if [ $# -eq 1 ]; then
321                         targetJson='{"charts":[]}'
322                 else
323                         targetJson='{"charts":['
324                         arr=(${@})
325                         for ((i=0; i<$#; i=i+5)); do
326                                 if [ "$i" -gt 0 ]; then
327                                         targetJson=$targetJson","
328                                 fi
329                                 chart_version=${arr[$i+2]}
330                                 if [ $chart_version == "DEFAULT-VERSION" ]; then
331                                         chart_version="0.1.0"
332                                 fi
333                                 targetJson=$targetJson'{"releaseName":"'${arr[$i+3]}'","chartId":{"name":"'${arr[$i+1]}'","version":"'0.1.0'"},"namespace":"'${arr[$i+4]}'","repository":{"repoName":"'${arr[$i+0]}'","protocol":null,"address":null,"port":null,"userName":null,"password":null},"overrideParams":null}'
334                         done
335                         targetJson=$targetJson']}'
336                 fi
337                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
338                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
339
340                 if [ $res -ne 0 ]; then
341                         __log_test_fail_body
342                         return 1
343                 fi
344         fi
345
346         __log_test_pass
347         return 0
348 }
349
350 # API Test function: POST ​/helm/repo - add repo
351 # args: <response-code> <repo-name> <repo-protocol> <repo-address> <repo-port>
352 # (Function for test scripts)
353 helm_manager_api_post_repo() {
354         __log_test_start $@
355
356     if [ $# -ne 5 ]; then
357                 __print_err "<response-code> <repo-name> <repo-protocol> <repo-address> <repo-port>" $@
358                 return 1
359         fi
360
361         query="/helm/repo"
362         file="./tmp/cm-repo.json"
363         file_data='{"address" : "'$4'","repoName": "'$2'","protocol": "'$3'","port": "'$5'"}'
364         echo $file_data > $file
365         echo " FILE: $file_data" >> $HTTPLOG
366     res="$(__execute_curl_to_helmmanger POST $query $file)"
367     status=${res:${#res}-3}
368
369         if [ $status -ne $1 ]; then
370                 __log_test_fail_status_code $1 $status
371                 return 1
372         fi
373
374         __log_test_pass
375         return 0
376 }
377
378 # API Test function: POST /helm/onboard/chart - onboard chart
379 # args: <response-code> <repo> <chart> <version> <release> <namespace>
380 # (Function for test scripts)
381 helm_manager_api_post_onboard_chart() {
382         __log_test_start $@
383
384     if [ $# -ne 6 ]; then
385                 __print_err "<response-code> <repo> <chart> <version> <release> <namespace> " $@
386                 return 1
387         fi
388
389         query="/helm/onboard/chart"
390         file="./tmp/chart.json"
391         chart_version=$4
392         if [ $chart_version == "DEFAULT-VERSION" ]; then
393                 chart_version="0.1.0"
394         fi
395         file_data='{"chartId":{"name":"'$3'","version":"'$chart_version'"},"namespace":"'$6'","repository":{"repoName":"'$2'"},"releaseName":"'$5'"}'
396         echo $file_data > $file
397         echo " FILE - ($file): $file_data" >> $HTTPLOG
398         file2="./tmp/override.yaml"
399         echo "" >> $file2
400         file3="$TESTENV_TEMP_FILES/"$3"-"$chart_version".tgz"
401     res="$(__execute_curl_to_helmmanger POST1_2 $query info $file values $file2 chart $file3)"
402     status=${res:${#res}-3}
403
404         if [ $status -ne $1 ]; then
405                 __log_test_fail_status_code $1 $status
406                 return 1
407         fi
408
409         __log_test_pass
410         return 0
411 }
412
413 # API Test function: POST /helm/install - install chart
414 # args: <response-code> <chart> <version>
415 # (Function for test scripts)
416 helm_manager_api_post_install_chart() {
417         __log_test_start $@
418
419     if [ $# -ne 3 ]; then
420                 __print_err "<response-code> <chart> <version>" $@
421                 return 1
422         fi
423
424         query="/helm/install"
425         file="./tmp/app-installation.json"
426         chart_version=$3
427         if [ $chart_version == "DEFAULT-VERSION" ]; then
428                 chart_version="0.1.0"
429         fi
430         file_data='{"name": "'$2'","version": "'$chart_version'"}'
431         echo $file_data > $file
432         echo " FILE - ($file): $file_data" >> $HTTPLOG
433     res="$(__execute_curl_to_helmmanger POST $query $file)"
434     status=${res:${#res}-3}
435
436         if [ $status -ne $1 ]; then
437                 __log_test_fail_status_code $1 $status
438                 return 1
439         fi
440
441         __log_test_pass
442         return 0
443 }
444
445 # API Test function: DELETE /helm/uninstall - uninstall chart
446 # args: <response-code> <chart> <version>
447 # (Function for test scripts)
448 helm_manager_api_uninstall_chart() {
449         __log_test_start $@
450
451     if [ $# -ne 3 ]; then
452                 __print_err "<response-code> <chart> <version> " $@
453                 return 1
454         fi
455
456         chart_version=$3
457         if [ $chart_version == "DEFAULT-VERSION" ]; then
458                 chart_version="0.1.0"
459         fi
460         query="/helm/uninstall/$2/$chart_version"
461     res="$(__execute_curl_to_helmmanger DELETE $query)"
462     status=${res:${#res}-3}
463
464         if [ $status -ne $1 ]; then
465                 __log_test_fail_status_code $1 $status
466                 return 1
467         fi
468
469         __log_test_pass
470         return 0
471 }
472
473 # API Test function: DELETE /helm/chart - delete chart
474 # args: <response-code> <chart> <version>
475 # (Function for test scripts)
476 helm_manager_api_delete_chart() {
477         __log_test_start $@
478
479     if [ $# -ne 3 ]; then
480                 __print_err "<response-code> <chart> <version> " $@
481                 return 1
482         fi
483
484         chart_version=$3
485         if [ $chart_version == "DEFAULT-VERSION" ]; then
486                 chart_version="0.1.0"
487         fi
488         query="/helm/chart/$2/$chart_version"
489     res="$(__execute_curl_to_helmmanger DELETE $query)"
490     status=${res:${#res}-3}
491
492         if [ $status -ne $1 ]; then
493                 __log_test_fail_status_code $1 $status
494                 return 1
495         fi
496
497         __log_test_pass
498         return 0
499 }
500
501 # Config function: Add repo in helm manager by helm using exec
502 # args: <repo-name> <repo-url>
503 # (Function for test scripts)
504 helm_manager_api_exec_add_repo() {
505         __log_conf_start $@
506
507     if [ $# -ne 2 ]; then
508                 __print_err "<repo-name> <repo-url>" $@
509                 return 1
510         fi
511
512         if [ $RUNMODE == "DOCKER" ]; then
513                 retmsg=$(docker exec -it $HELM_MANAGER_APP_NAME helm repo add $1 $2)
514                 retcode=$?
515                 if [ $retcode -ne 0 ]; then
516                         __log_conf_fail_general " Cannot add repo to helm, return code: $retcode, msg: $retmsg"
517                         return 1
518                 fi
519         else
520                 retmsg=$(kubectl exec -it $HELM_MANAGER_APP_NAME -n $KUBE_NONRTRIC_NAMESPACE -- helm repo add $1 $2)
521                 retcode=$?
522                 if [ $retcode -ne 0 ]; then
523                         __log_conf_fail_general " Cannot add repo to helm, return code: $retcode, msg: $retmsg"
524                         return 1
525                 fi
526         fi
527         __log_conf_ok
528         return 0
529 }
530