3cd8b335502c57f781e9a6f9e91b78e985995c4e
[nonrtric.git] / test / common / chartmus_api_functions.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2021-2023 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 Chartmuseum
21
22
23 ################ Test engine functions ################
24
25 # Create the image var used during the test
26 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
27 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
28 __CHARTMUS_imagesetup() {
29         __check_and_create_image_var CHARTMUS "CHART_MUS_IMAGE" "CHART_MUS_IMAGE_BASE" "CHART_MUS_IMAGE_TAG" REMOTE_OTHER "$CHART_MUS_DISPLAY_NAME"
30 }
31
32 # Pull image from remote repo or use locally built image
33 # arg: <pull-policy-override> <pull-policy-original>
34 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
35 # <pull-policy-original> Shall be used for images that does not allow overriding
36 # Both var may contain: 'remote', 'remote-remove' or 'local'
37 __CHARTMUS_imagepull() {
38         __check_and_pull_image $2 "$CHART_MUS_DISPLAY_NAME" $CHART_MUS_APP_NAME CHART_MUS_IMAGE
39 }
40
41 # Build image (only for simulator or interfaces stubs owned by the test environment)
42 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
43 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
44 __CHARTMUS_imagebuild() {
45         echo -e $RED" Image for app CHARTMUS shall never be built"$ERED
46 }
47
48 # Generate a string for each included image using the app display name and a docker images format string
49 # If a custom image repo is used then also the source image from the local repo is listed
50 # arg: <docker-images-format-string> <file-to-append>
51 __CHARTMUS_image_data() {
52         echo -e "$CHART_MUS_DISPLAY_NAME\t$(docker images --format $1 $CHART_MUS_IMAGE)" >>   $2
53         if [ ! -z "$CHART_MUS_IMAGE_SOURCE" ]; then
54                 echo -e "-- source image --\t$(docker images --format $1 $CHART_MUS_IMAGE_SOURCE)" >>   $2
55         fi
56 }
57
58 # Scale kubernetes resources to zero
59 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
60 # This function is called for apps fully managed by the test script
61 __CHARTMUS_kube_scale_zero() {
62         __kube_scale_all_resources $KUBE_SIM_NAMESPACE autotest CHARTMUS
63 }
64
65 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
66 # This function is called for prestarted apps not managed by the test script.
67 __CHARTMUS_kube_scale_zero_and_wait() {
68         echo -e $RED" CHARTMUS app is not scaled in this state"$ERED
69 }
70
71 # Delete all kube resouces for the app
72 # This function is called for apps managed by the test script.
73 __CHARTMUS_kube_delete_all() {
74         __kube_delete_all_resources $KUBE_SIM_NAMESPACE autotest CHARTMUS
75 }
76
77 # Store docker logs
78 # This function is called for apps managed by the test script.
79 # args: <log-dir> <file-prexix>
80 __CHARTMUS_store_docker_logs() {
81         if [ $RUNMODE == "KUBE" ]; then
82                 kubectl $KUBECONF  logs -l "autotest=CHARTMUS" -n $KUBE_SIM_NAMESPACE --tail=-1 > $1$2_chartmuseum.log 2>&1
83         else
84                 docker logs $CHART_MUS_APP_NAME > $1$2_chartmuseum.log 2>&1
85         fi
86 }
87
88 # Initial setup of protocol, host and ports
89 # This function is called for apps managed by the test script.
90 # args: -
91 __CHARTMUS_initial_setup() {
92         use_chart_mus_http
93 }
94
95 # Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers
96 # For docker, the namespace shall be excluded
97 # This function is called for apps managed by the test script as well as for prestarted apps.
98 # args: -
99 __CHARTMUS_statisics_setup() {
100         if [ $RUNMODE == "KUBE" ]; then
101                 echo "CHARTMUS $CHART_MUS_APP_NAME $KUBE_SIM_NAMESPACE"
102         else
103                 echo "CHARTMUS $CHART_MUS_APP_NAME"
104         fi
105 }
106
107 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
108 # args: -
109 __CHARTMUS_test_requirements() {
110         :
111 }
112
113 #######################################################
114
115 # Set http as the protocol to use for all communication to the Chartmuseum
116 # args: -
117 # (Function for test scripts)
118 use_chart_mus_http() {
119         __chart_mus_set_protocoll "http" $CHART_MUS_INTERNAL_PORT $CHART_MUS_EXTERNAL_PORT
120 }
121
122 # Set https as the protocol to use for all communication to the Chartmuseum
123 # args: -
124 # (Function for test scripts)
125 use_chart_mus_https() {
126         __chart_mus_set_protocoll "https" $CHART_MUS_INTERNAL_SECURE_PORT $CHART_MUS_EXTERNAL_SECURE_PORT
127 }
128
129 # Setup paths to svc/container for internal and external access
130 # args: <protocol> <internal-port> <external-port>
131 __chart_mus_set_protocoll() {
132         echo -e $BOLD"$CHART_MUS_DISPLAY_NAME protocol setting"$EBOLD
133         echo -e " Using $BOLD $1 $EBOLD towards $CHART_MUS_DISPLAY_NAME"
134
135         ## Access to Chartmuseum
136
137         CHART_MUS_SERVICE_PATH=$1"://"$CHART_MUS_APP_NAME":"$2  # docker access, container->container and script->container via proxy
138         CHART_MUS_SERVICE_PORT=$2
139         CHART_MUS_SERVICE_HOST=$CHART_MUS_APP_NAME
140         if [ $RUNMODE == "KUBE" ]; then
141                 CHART_MUS_SERVICE_PATH=$1"://"$CHART_MUS_APP_NAME.$KUBE_SIM_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
142                 CHART_MUS_SERVICE_PORT=$3
143                 CHART_MUS_SERVICE_HOST=$CHART_MUS_APP_NAME.$KUBE_SIM_NAMESPACE
144         fi
145         CHART_MUS_SERVICE_HTTPX=$1
146
147         echo ""
148 }
149
150 ### Admin API functions Chartmuseum
151
152 ###########################
153 ### Chartmuseum functions
154 ###########################
155
156 # Export env vars for config files, docker compose and kube resources
157 # args:
158 __chartmuseum_export_vars() {
159         export CHART_MUS_APP_NAME
160         export CHART_MUS_DISPLAY_NAME
161
162         export DOCKER_SIM_NWNAME
163         export KUBE_SIM_NAMESPACE
164
165         export CHART_MUS_IMAGE
166         export CHART_MUS_INTERNAL_PORT
167         export CHART_MUS_EXTERNAL_PORT
168
169         export CHART_MUS_CHART_CONTR_CHARTS
170
171 }
172
173
174 # Start the Chartmuseum in the simulator group
175 # args: -
176 # (Function for test scripts)
177 start_chart_museum() {
178
179         echo -e $BOLD"Starting $CHART_MUS_DISPLAY_NAME"$EBOLD
180
181         if [ $RUNMODE == "KUBE" ]; then
182
183                 # Check if app shall be fully managed by the test script
184                 __check_included_image "CHARTMUS"
185                 retcode_i=$?
186
187                 # Check if app shall only be used by the testscipt
188                 __check_prestarted_image "CHARTMUS"
189                 retcode_p=$?
190
191                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
192                         echo -e $RED"The $CHART_MUS_NAME app is not included as managed nor prestarted in this test script"$ERED
193                         echo -e $RED"The $CHART_MUS_APP_NAME will not be started"$ERED
194                         exit
195                 fi
196                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
197                         echo -e $RED"The $CHART_MUS_APP_NAME app is included both as managed and prestarted in this test script"$ERED
198                         echo -e $RED"The $CHART_MUS_APP_NAME will not be started"$ERED
199                         exit
200                 fi
201
202                 if [ $retcode_p -eq 0 ]; then
203                         echo -e " Using existing $CHART_MUS_APP_NAME deployment and service"
204                         echo " Setting CHARTMUS replicas=1"
205                         __kube_scale deployment $CHART_MUS_APP_NAME $KUBE_SIM_NAMESPACE 1
206                 fi
207
208                 if [ $retcode_i -eq 0 ]; then
209                         echo -e " Creating $CHART_MUS_APP_NAME deployment and service"
210
211             __kube_create_namespace $KUBE_SIM_NAMESPACE
212
213                         __chartmuseum_export_vars
214
215                         # Create service
216                         input_yaml=$SIM_GROUP"/"$CHART_MUS_COMPOSE_DIR"/"svc.yaml
217                         output_yaml=$PWD/tmp/chartmus_svc.yaml
218                         __kube_create_instance service $CHART_MUS_APP_NAME $input_yaml $output_yaml
219
220                         # Create app
221                         input_yaml=$SIM_GROUP"/"$CHART_MUS_COMPOSE_DIR"/"app.yaml
222                         output_yaml=$PWD/tmp/chartmus_app.yaml
223                         __kube_create_instance app $CHART_MUS_APP_NAME $input_yaml $output_yaml
224                 fi
225
226                 __check_service_start $CHART_MUS_APP_NAME $CHART_MUS_SERVICE_PATH$CHART_MUS_ALIVE_URL
227         else
228
229                 # Check if docker app shall be fully managed by the test script
230                 __check_included_image 'CHARTMUS'
231                 if [ $? -eq 1 ]; then
232                         echo -e $RED"The Chartmuseum app is not included as managed in this test script"$ERED
233                         echo -e $RED"The Chartmuseum will not be started"$ERED
234                         exit
235                 fi
236
237                 __chartmuseum_export_vars
238
239                 __start_container $CHART_MUS_COMPOSE_DIR "" NODOCKERARGS 1 $CHART_MUS_APP_NAME
240
241         __check_service_start $CHART_MUS_APP_NAME $CHART_MUS_SERVICE_PATH$CHART_MUS_ALIVE_URL
242         fi
243     echo ""
244     return 0
245 }
246
247 # Excute a curl cmd towards the chartmuseum simulator and check the response code.
248 # args: TEST|CONF <expected-response-code> <curl-cmd-string>
249 __execute_curl_to_chartmuseum() {
250     TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
251     echo "(${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
252         proxyflag=""
253         if [ ! -z "$KUBE_PROXY_PATH" ]; then
254                 if [ $KUBE_PROXY_HTTPX == "http" ]; then
255                         proxyflag=" --proxy $KUBE_PROXY_PATH"
256                 else
257                         proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
258                 fi
259         fi
260         echo " CMD: $3 -skw %{http_code} $proxyflag" >> $HTTPLOG
261         res=$($3 -skw %{http_code} $proxyflag)
262         echo " RESP: $res" >> $HTTPLOG
263         retcode=$?
264     if [ $retcode -ne 0 ]; then
265         __log_conf_fail_general " Fatal error when executing curl, response: "$retcode
266         return 1
267     fi
268     status=${res:${#res}-3}
269     if [ $status -eq $2 ]; then
270         if [ $1 == "TEST" ]; then
271             __log_test_pass
272         else
273             __log_conf_ok
274         fi
275         return 0
276     fi
277     if [ $1 == "TEST" ]; then
278         __log_test_fail_status_code $2 $status
279         else
280         __log_conf_fail_status_code $2 $status
281     fi
282     return 1
283 }
284
285 # upload helmchart
286 # arg: <chart-name>
287 chartmus_upload_test_chart() {
288         __log_conf_start $@
289     if [ $# -ne 1 ]; then
290         __print_err "<chart-name>" $@
291         return 1
292     fi
293         chart_path=$TESTENV_TEMP_FILES/$1"-0.1.0.tgz"
294         if [ ! -f "$chart_path" ]; then
295                 echo -e $RED" Cannot find package chart: $chart_path"$ERED
296                 __log_conf_fail_general
297                 return 1
298         fi
299         __execute_curl_to_chartmuseum CONF 201 "curl --data-binary @$chart_path $CHART_MUS_SERVICE_PATH/api/charts"
300 }
301
302 # delete helmchart
303 # arg: <chart-name> [<version>]
304 chartmus_delete_test_chart() {
305         __log_conf_start $@
306     if [ $# -gt 2 ]; then
307         __print_err "<chart-name> [<version>]" $@
308         return 1
309     fi
310         if [ $# -eq 1 ]; then
311                 chart_path="/$1/0.1.0"
312         else
313                 chart_path="/$1/$2"
314         fi
315         __execute_curl_to_chartmuseum CONF 200 "curl -X DELETE $CHART_MUS_SERVICE_PATH/api/charts"$chart_path
316 }