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
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=================================================
20 # This is a script that contains container/service managemnt functions test functions for Helm Manager
22 ################ Test engine functions ################
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"
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
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
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
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
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
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
76 docker logs $HELM_MANAGER_APP_NAME > $1$2_helmmanager.log 2>&1
80 # Initial setup of protocol, host and ports
81 # This function is called for apps managed by the test script.
83 __HELMMANAGER_initial_setup() {
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.
91 __HELMMANAGER_statisics_setup() {
92 if [ $RUNMODE == "KUBE" ]; then
93 echo "HELMMANAGER $HELM_MANAGER_APP_NAME $KUBE_NONRTRIC_NAMESPACE"
95 echo "HELMMANAGER $HELM_MANAGER_APP_NAME"
99 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
101 __HELMMANAGER_test_requirements() {
103 if [ $? -ne 0 ]; then
104 echo $RED" Helm3 is required for running helm manager tests. Pls install helm3"
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"
114 #######################################################
116 # Set http as the protocol to use for all communication to the Helm Manager
118 # (Function for test scripts)
119 use_helm_manager_http() {
120 __helm_manager_set_protocoll "http" $HELM_MANAGER_INTERNAL_PORT $HELM_MANAGER_EXTERNAL_PORT
123 # Set https as the protocol to use for all communication to the Helm Manager
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
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"
136 ## Access to Helm Manager
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
146 # Export env vars for config files, docker compose and kube resources
148 __helm_manager_export_vars() {
150 export HELM_MANAGER_APP_NAME
151 export HELM_MANAGER_DISPLAY_NAME
153 export DOCKER_SIM_NWNAME
154 export KUBE_NONRTRIC_NAMESPACE
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
168 # Start the Helm Manager container
170 # (Function for test scripts)
171 start_helm_manager() {
173 echo -e $BOLD"Starting $HELM_MANAGER_DISPLAY_NAME"$EBOLD
175 if [ $RUNMODE == "KUBE" ]; then
177 # Check if app shall be fully managed by the test script
178 __check_included_image "HELMMANAGER"
181 # Check if app shall only be used by the testscipt
182 __check_prestarted_image "HELMMANAGER"
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
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
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
202 if [ $retcode_i -eq 0 ]; then
204 echo -e " Creating $HELM_MANAGER_APP_NAME app and expose service"
206 #Check if nonrtric namespace exists, if not create it
207 __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
209 __helm_manager_export_vars
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
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
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
227 __check_service_start $HELM_MANAGER_APP_NAME $HELMMANAGER_SERVICE_PATH$HELM_MANAGER_ALIVE_URL
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
237 __helm_manager_export_vars
239 __start_container $HELM_MANAGER_COMPOSE_DIR "" NODOCKERARGS 1 $HELM_MANAGER_APP_NAME
241 __check_service_start $HELM_MANAGER_APP_NAME $HELMMANAGER_SERVICE_PATH$HELM_MANAGER_ALIVE_URL
246 # Excute a curl cmd towards the helm manager.
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
254 if [ ! -z "$KUBE_PROXY_PATH" ]; then
255 if [ $KUBE_PROXY_HTTPX == "http" ]; then
256 proxyflag=" --proxy $KUBE_PROXY_PATH"
258 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
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"
270 echo " Unknown operation $1" >> $HTTPLOG
274 echo " CMD: $curlstring" >> $HTTPLOG
277 echo " RESP: $res" >> $HTTPLOG
278 if [ $retcode -ne 0 ]; then
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() {
293 variablecount=$(($#-1))
294 if [ $# -eq 1 ]; then
296 elif [ $# -eq 2 ] && [ $2 == "EMPTY" ]; then
298 elif [ $(($variablecount%5)) -eq 0 ]; then
303 if [ $error_params -eq 1 ]; then
304 __print_err "<response-code>" $@
309 res="$(__execute_curl_to_helmmanger GET $query)"
310 status=${res:${#res}-3}
312 if [ $status -ne $1 ]; then
313 __log_test_fail_status_code $1 $status
317 if [ $# -gt 1 ]; then
318 body=${res:0:${#res}-3}
320 if [ $# -eq 1 ]; then
321 targetJson='{"charts":[]}'
323 targetJson='{"charts":['
325 for ((i=0; i<$#; i=i+5)); do
326 if [ "$i" -gt 0 ]; then
327 targetJson=$targetJson","
329 chart_version=${arr[$i+2]}
330 if [ $chart_version == "DEFAULT-VERSION" ]; then
331 chart_version="0.1.0"
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}'
335 targetJson=$targetJson']}'
337 echo " TARGET JSON: $targetJson" >> $HTTPLOG
338 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
340 if [ $res -ne 0 ]; then
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() {
356 if [ $# -ne 5 ]; then
357 __print_err "<response-code> <repo-name> <repo-protocol> <repo-address> <repo-port>" $@
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}
369 if [ $status -ne $1 ]; then
370 __log_test_fail_status_code $1 $status
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() {
384 if [ $# -ne 6 ]; then
385 __print_err "<response-code> <repo> <chart> <version> <release> <namespace> " $@
389 query="/helm/onboard/chart"
390 file="./tmp/chart.json"
392 if [ $chart_version == "DEFAULT-VERSION" ]; then
393 chart_version="0.1.0"
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"
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}
404 if [ $status -ne $1 ]; then
405 __log_test_fail_status_code $1 $status
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() {
419 if [ $# -ne 3 ]; then
420 __print_err "<response-code> <chart> <version>" $@
424 query="/helm/install"
425 file="./tmp/app-installation.json"
427 if [ $chart_version == "DEFAULT-VERSION" ]; then
428 chart_version="0.1.0"
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}
436 if [ $status -ne $1 ]; then
437 __log_test_fail_status_code $1 $status
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() {
451 if [ $# -ne 3 ]; then
452 __print_err "<response-code> <chart> <version> " $@
457 if [ $chart_version == "DEFAULT-VERSION" ]; then
458 chart_version="0.1.0"
460 query="/helm/uninstall/$2/$chart_version"
461 res="$(__execute_curl_to_helmmanger DELETE $query)"
462 status=${res:${#res}-3}
464 if [ $status -ne $1 ]; then
465 __log_test_fail_status_code $1 $status
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() {
479 if [ $# -ne 3 ]; then
480 __print_err "<response-code> <chart> <version> " $@
485 if [ $chart_version == "DEFAULT-VERSION" ]; then
486 chart_version="0.1.0"
488 query="/helm/chart/$2/$chart_version"
489 res="$(__execute_curl_to_helmmanger DELETE $query)"
490 status=${res:${#res}-3}
492 if [ $status -ne $1 ]; then
493 __log_test_fail_status_code $1 $status
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() {
507 if [ $# -ne 2 ]; then
508 __print_err "<repo-name> <repo-url>" $@
512 if [ $RUNMODE == "DOCKER" ]; then
513 retmsg=$(docker exec -it $HELM_MANAGER_APP_NAME helm repo add $1 $2)
515 if [ $retcode -ne 0 ]; then
516 __log_conf_fail_general " Cannot add repo to helm, return code: $retcode, msg: $retmsg"
520 retmsg=$(kubectl exec -it $HELM_MANAGER_APP_NAME -n $KUBE_NONRTRIC_NAMESPACE -- helm repo add $1 $2)
522 if [ $retcode -ne 0 ]; then
523 __log_conf_fail_general " Cannot add repo to helm, return code: $retcode, msg: $retmsg"