BjornMagnussonXA | de4d0f8 | 2020-11-29 16:04:06 +0100 | [diff] [blame] | 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 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 20 | # This is a script that contains container/service managemnt functions test functions for RAPP Catalogue API |
BjornMagnussonXA | de4d0f8 | 2020-11-29 16:04:06 +0100 | [diff] [blame] | 21 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 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 | __RC_imagesetup() { |
| 28 | __check_and_create_image_var RC "RAPP_CAT_IMAGE" "RAPP_CAT_IMAGE_BASE" "RAPP_CAT_IMAGE_TAG" $1 "$RAPP_CAT_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 | __RC_imagepull() { |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 37 | __check_and_pull_image $1 "$c" $RAPP_CAT_APP_NAME RAPP_CAT_IMAGE |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | # Generate a string for each included image using the app display name and a docker images format string |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 41 | # If a custom image repo is used then also the source image from the local repo is listed |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 42 | # arg: <docker-images-format-string> <file-to-append> |
| 43 | __RC_image_data() { |
| 44 | echo -e "$RAPP_CAT_DISPLAY_NAME\t$(docker images --format $1 $RAPP_CAT_IMAGE)" >> $2 |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 45 | if [ ! -z "$RAPP_CAT_IMAGE_SOURCE" ]; then |
| 46 | echo -e "-- source image --\t$(docker images --format $1 $RAPP_CAT_IMAGE_SOURCE)" >> $2 |
| 47 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 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 | __RC_kube_scale_zero() { |
| 54 | __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest RC |
| 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 | __RC_kube_scale_zero_and_wait() { |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 60 | __kube_scale_and_wait_all_resources $KUBE_NONRTRIC_NAMESPACE app "$KUBE_NONRTRIC_NAMESPACE"-rappcatalogueservice |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | # Delete all kube resouces for the app |
| 64 | # This function is called for apps managed by the test script. |
| 65 | __RC_kube_delete_all() { |
| 66 | __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest RC |
| 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 | __RC_store_docker_logs() { |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 73 | if [ $RUNMODE == "KUBE" ]; then |
| 74 | kubectl logs -l "autotest=RC" -n $KUBE_NONRTRIC_NAMESPACE --tail=-1 > $1$2_rc.log 2>&1 |
| 75 | else |
| 76 | docker logs $RAPP_CAT_APP_NAME > $1$2_rc.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 | __RC_initial_setup() { |
| 84 | use_rapp_catalogue_http |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 85 | } |
| 86 | |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 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 | __RC_statisics_setup() { |
| 92 | if [ $RUNMODE == "KUBE" ]; then |
| 93 | echo "RC $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE" |
| 94 | else |
| 95 | echo "RC $RAPP_CAT_APP_NAME" |
| 96 | fi |
| 97 | } |
| 98 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 99 | ####################################################### |
| 100 | |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 101 | # Set http as the protocol to use for all communication to the Rapp catalogue |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 102 | # args: - |
| 103 | # (Function for test scripts) |
| 104 | use_rapp_catalogue_http() { |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 105 | __rapp_catalogue_set_protocoll "http" $RAPP_CAT_INTERNAL_PORT $RAPP_CAT_EXTERNAL_PORT |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 106 | } |
| 107 | |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 108 | # Set https as the protocol to use for all communication to the Rapp catalogue |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 109 | # args: - |
| 110 | # (Function for test scripts) |
| 111 | use_rapp_catalogue_https() { |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 112 | __rapp_catalogue_set_protocoll "https" $RAPP_CAT_INTERNAL_SECURE_PORT $RAPP_CAT_EXTERNAL_SECURE_PORT |
| 113 | } |
| 114 | |
| 115 | # Setup paths to svc/container for internal and external access |
| 116 | # args: <protocol> <internal-port> <external-port> |
| 117 | __rapp_catalogue_set_protocoll() { |
| 118 | echo -e $BOLD"$RAPP_CAT_DISPLAY_NAME protocol setting"$EBOLD |
BjornMagnussonXA | 007b645 | 2021-11-29 08:03:38 +0100 | [diff] [blame] | 119 | echo -e " Using $BOLD $1 $EBOLD towards $RAPP_CAT_DISPLAY_NAME" |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 120 | |
| 121 | ## Access to Rapp catalogue |
| 122 | |
| 123 | RC_SERVICE_PATH=$1"://"$RAPP_CAT_APP_NAME":"$2 # docker access, container->container and script->container via proxy |
| 124 | if [ $RUNMODE == "KUBE" ]; then |
| 125 | RC_SERVICE_PATH=$1"://"$RAPP_CAT_APP_NAME.$KUBE_NONRTRIC_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy |
| 126 | fi |
| 127 | |
| 128 | # RC_ADAPTER used for switching between REST and DMAAP (only REST supported currently) |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 129 | RC_ADAPTER_TYPE="REST" |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 130 | RC_ADAPTER=$RC_SERVICE_PATH |
| 131 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 132 | echo "" |
| 133 | } |
| 134 | |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 135 | # Export env vars for config files, docker compose and kube resources |
| 136 | # args: |
| 137 | __rapp_catalogue_export_vars() { |
| 138 | |
| 139 | export RAPP_CAT_APP_NAME |
| 140 | export RAPP_CAT_DISPLAY_NAME |
| 141 | |
| 142 | export DOCKER_SIM_NWNAME |
| 143 | export KUBE_NONRTRIC_NAMESPACE |
| 144 | |
| 145 | export RAPP_CAT_IMAGE |
| 146 | export RAPP_CAT_INTERNAL_PORT |
| 147 | export RAPP_CAT_INTERNAL_SECURE_PORT |
| 148 | export RAPP_CAT_EXTERNAL_PORT |
| 149 | export RAPP_CAT_EXTERNAL_SECURE_PORT |
| 150 | } |
| 151 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 152 | # Start the RAPP Catalogue container |
| 153 | # args: - |
| 154 | # (Function for test scripts) |
| 155 | start_rapp_catalogue() { |
| 156 | |
| 157 | echo -e $BOLD"Starting $RAPP_CAT_DISPLAY_NAME"$EBOLD |
| 158 | |
| 159 | if [ $RUNMODE == "KUBE" ]; then |
| 160 | |
| 161 | # Check if app shall be fully managed by the test script |
| 162 | __check_included_image "RC" |
| 163 | retcode_i=$? |
| 164 | |
| 165 | # Check if app shall only be used by the testscipt |
| 166 | __check_prestarted_image "RC" |
| 167 | retcode_p=$? |
| 168 | |
| 169 | if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then |
| 170 | echo -e $RED"The $RAPP_CAT_APP_NAME app is not included as managed nor prestarted in this test script"$ERED |
| 171 | echo -e $RED"The $RAPP_CAT_APP_NAME will not be started"$ERED |
| 172 | exit |
| 173 | fi |
| 174 | if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then |
| 175 | echo -e $RED"The $RAPP_CAT_APP_NAME app is included both as managed and prestarted in this test script"$ERED |
| 176 | echo -e $RED"The $RAPP_CAT_APP_NAME will not be started"$ERED |
| 177 | exit |
| 178 | fi |
| 179 | |
| 180 | if [ $retcode_p -eq 0 ]; then |
| 181 | echo -e " Using existing $RAPP_CAT_APP_NAME deployment and service" |
| 182 | echo " Setting $RAPP_CAT_APP_NAME replicas=1" |
| 183 | __kube_scale deployment $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1 |
| 184 | fi |
| 185 | |
| 186 | if [ $retcode_i -eq 0 ]; then |
| 187 | |
| 188 | echo -e " Creating $RAPP_CAT_APP_NAME app and expose service" |
| 189 | |
| 190 | #Check if nonrtric namespace exists, if not create it |
| 191 | __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE |
| 192 | |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 193 | __rapp_catalogue_export_vars |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 194 | |
| 195 | #Create service |
| 196 | input_yaml=$SIM_GROUP"/"$RAPP_CAT_COMPOSE_DIR"/"svc.yaml |
| 197 | output_yaml=$PWD/tmp/rac_svc.yaml |
| 198 | __kube_create_instance service $RAPP_CAT_APP_NAME $input_yaml $output_yaml |
| 199 | |
| 200 | #Create app |
| 201 | input_yaml=$SIM_GROUP"/"$RAPP_CAT_COMPOSE_DIR"/"app.yaml |
| 202 | output_yaml=$PWD/tmp/rac_app.yaml |
| 203 | __kube_create_instance app $RAPP_CAT_APP_NAME $input_yaml $output_yaml |
| 204 | fi |
| 205 | |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 206 | __check_service_start $RAPP_CAT_APP_NAME $RC_SERVICE_PATH$RAPP_CAT_ALIVE_URL |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 207 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 208 | else |
| 209 | __check_included_image 'RC' |
| 210 | if [ $? -eq 1 ]; then |
| 211 | echo -e $RED"The RAPP Catalogue app is not included as managed in this test script"$ERED |
| 212 | echo -e $RED"The RAPP Catalogue will not be started"$ERED |
| 213 | exit |
| 214 | fi |
| 215 | |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 216 | __rapp_catalogue_export_vars |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 217 | |
| 218 | __start_container $RAPP_CAT_COMPOSE_DIR "" NODOCKERARGS 1 $RAPP_CAT_APP_NAME |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 219 | |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 220 | __check_service_start $RAPP_CAT_APP_NAME $RC_SERVICE_PATH$RAPP_CAT_ALIVE_URL |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 221 | fi |
| 222 | echo "" |
| 223 | } |
BjornMagnussonXA | de4d0f8 | 2020-11-29 16:04:06 +0100 | [diff] [blame] | 224 | |
| 225 | # Tests if a variable value in the RAPP Catalogue is equal to a target value and and optional timeout. |
| 226 | # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is |
| 227 | # equal to the target or not. |
| 228 | # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds |
| 229 | # before setting pass or fail depending on if the variable value becomes equal to the target |
| 230 | # value or not. |
| 231 | # (Function for test scripts) |
| 232 | rc_equal() { |
| 233 | if [ $# -eq 2 ] || [ $# -eq 3 ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 234 | #__var_test RC "$LOCALHOST_HTTP:$RC_EXTERNAL_PORT/" $1 "=" $2 $3 |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 235 | __var_test RC "$RC_SERVICE_PATH/" $1 "=" $2 $3 |
BjornMagnussonXA | de4d0f8 | 2020-11-29 16:04:06 +0100 | [diff] [blame] | 236 | else |
BjornMagnussonXA | 007b645 | 2021-11-29 08:03:38 +0100 | [diff] [blame] | 237 | __print_err "Wrong args to ics_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@ |
BjornMagnussonXA | de4d0f8 | 2020-11-29 16:04:06 +0100 | [diff] [blame] | 238 | fi |
| 239 | } |
| 240 | |
| 241 | |
| 242 | ########################################## |
| 243 | ######### RAPP Catalogue API ########## |
| 244 | ########################################## |
| 245 | #Function prefix: rapp_cat_api |
| 246 | |
| 247 | # API Test function: GET /services |
| 248 | # args: <response-code> [(<service-id> <version> <display-name> <description>)+ | EMPTY ] |
| 249 | # (Function for test scripts) |
| 250 | rapp_cat_api_get_services() { |
| 251 | __log_test_start $@ |
| 252 | |
| 253 | if [ $# -lt 1 ]; then |
| 254 | __print_err "<response-code> [(<service-id> <version> <display-name> <description>)+ | EMPTY ]" $@ |
| 255 | return 1 |
| 256 | fi |
| 257 | query="/services" |
| 258 | res="$(__do_curl_to_api RC GET $query)" |
| 259 | status=${res:${#res}-3} |
| 260 | |
| 261 | if [ $status -ne $1 ]; then |
| 262 | __log_test_fail_status_code $1 $status |
| 263 | return 1 |
| 264 | fi |
| 265 | |
| 266 | if [ $# -gt 1 ]; then |
| 267 | body=${res:0:${#res}-3} |
| 268 | targetJson="[" |
| 269 | arr=(${@:2}) |
| 270 | |
| 271 | if [ $# -eq 2 ]; then |
| 272 | targetJson="[]" |
| 273 | else |
| 274 | for ((i=0; i<$(($#-1)); i=i+4)); do |
| 275 | if [ "$targetJson" != "[" ]; then |
| 276 | targetJson=$targetJson"," |
| 277 | fi |
| 278 | targetJson=$targetJson"{\"name\": \"${arr[$i]}\",\"version\": \"${arr[$i+1]}\",\"display_name\": \"${arr[$i+2]}\",\"description\": \"${arr[$i+3]}\",\"registrationDate\": \"????\"}" |
| 279 | done |
| 280 | targetJson=$targetJson"]" |
| 281 | fi |
| 282 | echo " TARGET JSON: $targetJson" >> $HTTPLOG |
| 283 | res=$(python3 ../common/compare_json.py "$targetJson" "$body") |
| 284 | |
| 285 | if [ $res -ne 0 ]; then |
| 286 | __log_test_fail_body |
| 287 | return 1 |
| 288 | fi |
| 289 | fi |
| 290 | |
| 291 | __log_test_pass |
| 292 | return 0 |
| 293 | } |
| 294 | |
| 295 | # API Test function: PUT /services/{service-id} |
| 296 | # args: <response-code> <service-id> <version> <display-name> <description> |
| 297 | # (Function for test scripts) |
| 298 | rapp_cat_api_put_service() { |
| 299 | __log_test_start $@ |
| 300 | |
| 301 | if [ $# -ne 5 ]; then |
| 302 | __print_err "<response-code> <service-id> <version> <display-name> <description>" $@ |
| 303 | return 1 |
| 304 | fi |
| 305 | |
| 306 | inputJson="{\"version\": \"$3\",\"display_name\": \"$4\",\"description\": \"$5\"}" |
| 307 | file="./tmp/.p.json" |
| 308 | echo "$inputJson" > $file |
| 309 | query="/services/$2" |
| 310 | res="$(__do_curl_to_api RC PUT $query $file)" |
| 311 | status=${res:${#res}-3} |
| 312 | |
| 313 | if [ $status -ne $1 ]; then |
| 314 | __log_test_fail_status_code $1 $status |
| 315 | return 1 |
| 316 | fi |
| 317 | |
| 318 | __log_test_pass |
| 319 | return 0 |
| 320 | } |
| 321 | |
| 322 | # API Test function: GET /services/{service-id} |
| 323 | # args: <response-code> <service-id> |
| 324 | # (Function for test scripts) |
| 325 | rapp_cat_api_get_service() { |
| 326 | __log_test_start $@ |
| 327 | |
| 328 | if [ $# -lt 2 ] || [ $# -gt 5 ]; then |
| 329 | __print_err "<response-code> <service-id> <version> <display-name> <description>" $@ |
| 330 | return 1 |
| 331 | fi |
| 332 | |
| 333 | query="/services/$2" |
| 334 | res="$(__do_curl_to_api RC GET $query)" |
| 335 | status=${res:${#res}-3} |
| 336 | |
| 337 | if [ $status -ne $1 ]; then |
| 338 | __log_test_fail_status_code $1 $status |
| 339 | return 1 |
| 340 | fi |
| 341 | |
| 342 | if [ $# -gt 2 ]; then |
| 343 | body=${res:0:${#res}-3} |
| 344 | targetJson="{\"name\": \"$2\",\"version\": \"$3\",\"display_name\": \"$4\",\"description\": \"$5\",\"registrationDate\": \"????\"}" |
| 345 | echo " TARGET JSON: $targetJson" >> $HTTPLOG |
| 346 | res=$(python3 ../common/compare_json.py "$targetJson" "$body") |
| 347 | |
| 348 | if [ $res -ne 0 ]; then |
| 349 | __log_test_fail_body |
| 350 | return 1 |
| 351 | fi |
| 352 | fi |
| 353 | |
| 354 | __log_test_pass |
| 355 | return 0 |
| 356 | } |
| 357 | |
| 358 | # API Test function: DELETE /services/{service-id} |
| 359 | # args: <response-code> <service-id> |
| 360 | # (Function for test scripts) |
| 361 | rapp_cat_api_delete_service() { |
| 362 | __log_test_start $@ |
| 363 | |
| 364 | if [ $# -ne 2 ]; then |
| 365 | __print_err "<response-code> <service-id>" $@ |
| 366 | return 1 |
| 367 | fi |
| 368 | |
| 369 | query="/services/$2" |
| 370 | res="$(__do_curl_to_api RC DELETE $query)" |
| 371 | status=${res:${#res}-3} |
| 372 | |
| 373 | if [ $status -ne $1 ]; then |
| 374 | __log_test_fail_status_code $1 $status |
| 375 | return 1 |
| 376 | fi |
| 377 | |
| 378 | __log_test_pass |
| 379 | return 0 |
| 380 | } |