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 management functions and test functions for Chartmuseum
23 ################ Test engine functions ################
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"
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
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
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
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
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
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
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 logs -l "autotest=CHARTMUS" -n $KUBE_SIM_NAMESPACE --tail=-1 > $1$2_chartmuseum.log 2>&1
84 docker logs $CHART_MUS_APP_NAME > $1$2_chartmuseum.log 2>&1
88 # Initial setup of protocol, host and ports
89 # This function is called for apps managed by the test script.
91 __CHARTMUS_initial_setup() {
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.
99 __CHARTMUS_statisics_setup() {
100 if [ $RUNMODE == "KUBE" ]; then
101 echo "CHARTMUS $CHART_MUS_APP_NAME $KUBE_SIM_NAMESPACE"
103 echo "CHARTMUS $CHART_MUS_APP_NAME"
107 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
109 __CHARTMUS_test_requirements() {
113 #######################################################
115 # Set http as the protocol to use for all communication to the Chartmuseum
117 # (Function for test scripts)
118 use_chart_mus_http() {
119 __chart_mus_set_protocoll "http" $CHART_MUS_INTERNAL_PORT $CHART_MUS_EXTERNAL_PORT
122 # Set https as the protocol to use for all communication to the Chartmuseum
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
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"
135 ## Access to Chartmuseum
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
145 CHART_MUS_SERVICE_HTTPX=$1
150 ### Admin API functions Chartmuseum
152 ###########################
153 ### Chartmuseum functions
154 ###########################
156 # Export env vars for config files, docker compose and kube resources
158 __chartmuseum_export_vars() {
159 export CHART_MUS_APP_NAME
160 export CHART_MUS_DISPLAY_NAME
162 export DOCKER_SIM_NWNAME
163 export KUBE_SIM_NAMESPACE
165 export CHART_MUS_IMAGE
166 export CHART_MUS_INTERNAL_PORT
167 export CHART_MUS_EXTERNAL_PORT
169 export CHART_MUS_CHART_CONTR_CHARTS
174 # Start the Chartmuseum in the simulator group
176 # (Function for test scripts)
177 start_chart_museum() {
179 echo -e $BOLD"Starting $CHART_MUS_DISPLAY_NAME"$EBOLD
181 if [ $RUNMODE == "KUBE" ]; then
183 # Check if app shall be fully managed by the test script
184 __check_included_image "CHARTMUS"
187 # Check if app shall only be used by the testscipt
188 __check_prestarted_image "CHARTMUS"
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
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
202 if [ $retcode_p -eq 0 ]; then
203 echo -e " Using existing $CHART_MUS_APP_NAME deployment and service"
204 echo " Setting RC replicas=1"
205 __kube_scale deployment $CHART_MUS_APP_NAME $KUBE_SIM_NAMESPACE 1
208 if [ $retcode_i -eq 0 ]; then
209 echo -e " Creating $CHART_MUS_APP_NAME deployment and service"
211 __kube_create_namespace $KUBE_SIM_NAMESPACE
213 __chartmuseum_export_vars
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
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
226 __check_service_start $CHART_MUS_APP_NAME $CHART_MUS_SERVICE_PATH$CHART_MUS_ALIVE_URL
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
237 __chartmuseum_export_vars
239 __start_container $CHART_MUS_COMPOSE_DIR "" NODOCKERARGS 1 $CHART_MUS_APP_NAME
241 __check_service_start $CHART_MUS_APP_NAME $CHART_MUS_SERVICE_PATH$CHART_MUS_ALIVE_URL
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
253 if [ ! -z "$KUBE_PROXY_PATH" ]; then
254 if [ $KUBE_PROXY_HTTPX == "http" ]; then
255 proxyflag=" --proxy $KUBE_PROXY_PATH"
257 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
260 echo " CMD: $3 -skw %{http_code} $proxyflag" >> $HTTPLOG
261 res="$($3 -skw %{http_code} $proxyflag)"
262 echo " RESP: $res" >> $HTTPLOG
264 if [ $retcode -ne 0 ]; then
265 __log_conf_fail_general " Fatal error when executing curl, response: "$retcode
268 status=${res:${#res}-3}
269 if [ $status -eq $2 ]; then
270 if [ $1 == "TEST" ]; then
277 if [ $1 == "TEST" ]; then
278 __log_test_fail_status_code $2 $status
280 __log_conf_fail_status_code $2 $status
287 chartmus_upload_test_chart() {
289 if [ $# -ne 1 ]; then
290 __print_err "<chart-name>" $@
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
299 __execute_curl_to_chartmuseum CONF 201 "curl --data-binary @$chart_path $CHART_MUS_SERVICE_PATH/api/charts"
303 # arg: <chart-name> [<version>]
304 chartmus_delete_test_chart() {
306 if [ $# -gt 2 ]; then
307 __print_err "<chart-name> [<version>]" $@
310 if [ $# -eq 1 ]; then
311 chart_path="/$1/0.1.0"
315 __execute_curl_to_chartmuseum CONF 200 "curl -X DELETE $CHART_MUS_SERVICE_PATH/api/charts"$chart_path