BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame^] | 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 the Dmaap Adatper |
| 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 | __DMAAPADP_imagesetup() { |
| 29 | __check_and_create_image_var DMAAPADP "DMAAP_ADP_IMAGE" "DMAAP_ADP_IMAGE_BASE" "DMAAP_ADP_IMAGE_TAG" $1 "$DMAAP_ADP_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 | __DMAAPADP_imagepull() { |
| 38 | __check_and_pull_image $1 "$DMAAP_ADP_DISPLAY_NAME" $DMAAP_ADP_APP_NAME DMAAP_ADP_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 | __DMAAPADP_imagebuild() { |
| 45 | echo -e $RED" Image for app DMAAPADP 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 | __DMAAPADP_image_data() { |
| 52 | echo -e "$DMAAP_ADP_DISPLAY_NAME\t$(docker images --format $1 $DMAAP_ADP_IMAGE)" >> $2 |
| 53 | if [ ! -z "$DMAAP_ADP_IMAGE_SOURCE" ]; then |
| 54 | echo -e "-- source image --\t$(docker images --format $1 $DMAAP_ADP_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 | __DMAAPADP_kube_scale_zero() { |
| 62 | __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest DMAAPADP |
| 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 | __DMAAPADP_kube_scale_zero_and_wait() { |
| 68 | __kube_scale_and_wait_all_resources $KUBE_NONRTRIC_NAMESPACE app "$KUBE_NONRTRIC_NAMESPACE"-dmaapadapterservice |
| 69 | } |
| 70 | |
| 71 | # Delete all kube resouces for the app |
| 72 | # This function is called for apps managed by the test script. |
| 73 | __DMAAPADP_kube_delete_all() { |
| 74 | __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest DMAAPADP |
| 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 | __DMAAPADP_store_docker_logs() { |
| 81 | if [ $RUNMODE == "KUBE" ]; then |
| 82 | kubectl logs -l "autotest=DMAAPADP" -n $KUBE_NONRTRIC_NAMESPACE --tail=-1 > $1$2_dmaapadapter.log 2>&1 |
| 83 | else |
| 84 | docker logs $DMAAP_ADP_APP_NAME > $1$2_dmaapadapter.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 | __DMAAPADP_initial_setup() { |
| 92 | use_dmaapadp_http |
| 93 | } |
| 94 | |
| 95 | ####################################################### |
| 96 | |
| 97 | # Set http as the protocol to use for all communication to the Dmaap adapter |
| 98 | # args: - |
| 99 | # (Function for test scripts) |
| 100 | use_dmaapadp_http() { |
| 101 | __dmaapadp_set_protocoll "http" $DMAAP_ADP_INTERNAL_PORT $DMAAP_ADP_EXTERNAL_PORT |
| 102 | } |
| 103 | |
| 104 | # Set https as the protocol to use for all communication to the Dmaap adapter |
| 105 | # args: - |
| 106 | # (Function for test scripts) |
| 107 | use_dmaapadp_https() { |
| 108 | __dmaapadp_set_protocoll "https" $DMAAP_ADP_INTERNAL_SECURE_PORT $DMAAP_ADP_EXTERNAL_SECURE_PORT |
| 109 | } |
| 110 | |
| 111 | # Setup paths to svc/container for internal and external access |
| 112 | # args: <protocol> <internal-port> <external-port> |
| 113 | __dmaapadp_set_protocoll() { |
| 114 | echo -e $BOLD"$DMAAP_ADP_DISPLAY_NAME protocol setting"$EBOLD |
| 115 | echo -e " Using $BOLD http $EBOLD towards $DMAAP_ADP_DISPLAY_NAME" |
| 116 | |
| 117 | ## Access to Dmaap adapter |
| 118 | |
| 119 | DMAAP_ADP_SERVICE_PATH=$1"://"$DMAAP_ADP_APP_NAME":"$2 # docker access, container->container and script->container via proxy |
| 120 | if [ $RUNMODE == "KUBE" ]; then |
| 121 | DMAAP_ADP_SERVICE_PATH=$1"://"$DMAAP_ADP_APP_NAME.$KUBE_NONRTRIC_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy |
| 122 | fi |
| 123 | |
| 124 | # DMAAP_ADP_ADAPTER used for switching between REST and DMAAP (only REST supported currently) |
| 125 | DMAAP_ADP_ADAPTER_TYPE="REST" |
| 126 | DMAAP_ADP_ADAPTER=$DMAAP_ADP_SERVICE_PATH |
| 127 | |
| 128 | echo "" |
| 129 | } |
| 130 | |
| 131 | # Export env vars for config files, docker compose and kube resources |
| 132 | # args: PROXY|NOPROXY |
| 133 | __dmaapadp_export_vars() { |
| 134 | |
| 135 | export DMAAP_ADP_APP_NAME |
| 136 | export DMAAP_ADP_DISPLAY_NAME |
| 137 | |
| 138 | export KUBE_NONRTRIC_NAMESPACE |
| 139 | export DOCKER_SIM_NWNAME |
| 140 | |
| 141 | export DMAAP_ADP_IMAGE |
| 142 | |
| 143 | export DMAAP_ADP_INTERNAL_PORT |
| 144 | export DMAAP_ADP_INTERNAL_SECURE_PORT |
| 145 | export DMAAP_ADP_EXTERNAL_PORT |
| 146 | export DMAAP_ADP_EXTERNAL_SECURE_PORT |
| 147 | |
| 148 | export DMAAP_ADP_CONFIG_MOUNT_PATH |
| 149 | export DMAAP_ADP_DATA_MOUNT_PATH |
| 150 | export DMAAP_ADP_HOST_MNT_DIR |
| 151 | export DMAAP_ADP_CONFIG_FILE |
| 152 | export DMAAP_ADP_DATA_FILE |
| 153 | |
| 154 | export DMAAP_ADP_CONFIG_CONFIGMAP_NAME=$DMAAP_ADP_APP_NAME"-config" |
| 155 | export DMAAP_ADP_DATA_CONFIGMAP_NAME=$DMAAP_ADP_APP_NAME"-data" |
| 156 | |
| 157 | export DMMAAP_ADP_PROXY_FLAG="false" |
| 158 | |
| 159 | if [ $1 == "PROXY" ]; then |
| 160 | export DMAAP_ADP_HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_CONFIG_PORT #Set if proxy is started |
| 161 | export DMAAP_ADP_HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_CONFIG_HOST_NAME #Set if proxy is started |
| 162 | if [ $DMAAP_ADP_HTTP_PROXY_CONFIG_PORT -eq 0 ] || [ -z "$DMAAP_ADP_HTTP_PROXY_CONFIG_HOST_NAME" ]; then |
| 163 | echo -e $YELLOW" Warning: HTTP PROXY will not be configured, proxy app not started"$EYELLOW |
| 164 | else |
| 165 | echo " Configured with http proxy" |
| 166 | fi |
| 167 | export DMMAAP_ADP_PROXY_FLAG="true" |
| 168 | else |
| 169 | export DMAAP_ADP_HTTP_PROXY_CONFIG_PORT=0 |
| 170 | export DMAAP_ADP_HTTP_PROXY_CONFIG_HOST_NAME="" |
| 171 | echo " Configured without http proxy" |
| 172 | fi |
| 173 | |
| 174 | |
| 175 | # paths to other components |
| 176 | export ECS_SERVICE_PATH |
| 177 | export DMAAP_ADP_SERVICE_PATH |
| 178 | export MR_SERVICE_PATH |
| 179 | |
| 180 | } |
| 181 | |
| 182 | # Start the Dmaap Adapter |
| 183 | # args: - |
| 184 | # (Function for test scripts) |
| 185 | start_dmaapadp() { |
| 186 | |
| 187 | echo -e $BOLD"Starting $DMAAP_ADP_DISPLAY_NAME"$EBOLD |
| 188 | |
| 189 | if [ $RUNMODE == "KUBE" ]; then |
| 190 | |
| 191 | # Check if app shall be fully managed by the test script |
| 192 | __check_included_image "DMAAPADP" |
| 193 | retcode_i=$? |
| 194 | |
| 195 | # Check if app shall only be used by the testscipt |
| 196 | __check_prestarted_image "DMAAPADP" |
| 197 | retcode_p=$? |
| 198 | |
| 199 | if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then |
| 200 | echo -e $RED"The $DMAAP_ADP_APP_NAME app is not included as managed nor prestarted in this test script"$ERED |
| 201 | echo -e $RED"The $DMAAP_ADP_APP_NAME will not be started"$ERED |
| 202 | exit |
| 203 | fi |
| 204 | if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then |
| 205 | echo -e $RED"The $DMAAP_ADP_APP_NAME app is included both as managed and prestarted in this test script"$ERED |
| 206 | echo -e $RED"The $DMAAP_ADP_APP_NAME will not be started"$ERED |
| 207 | exit |
| 208 | fi |
| 209 | |
| 210 | # Check if app shall be used - not managed - by the test script |
| 211 | if [ $retcode_p -eq 0 ]; then |
| 212 | echo -e " Using existing $DMAAP_ADP_APP_NAME deployment and service" |
| 213 | echo " Setting DMAAPADP replicas=1" |
| 214 | __kube_scale statefulset $DMAAP_ADP_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1 |
| 215 | fi |
| 216 | |
| 217 | if [ $retcode_i -eq 0 ]; then |
| 218 | echo -e " Creating $DMAAP_ADP_APP_NAME deployment and service" |
| 219 | |
| 220 | __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE |
| 221 | |
| 222 | __dmaapadp_export_vars $1 |
| 223 | |
| 224 | # Create config map for config |
| 225 | configfile=$PWD/tmp/$DMAAP_ADP_CONFIG_FILE |
| 226 | #cp $2 $configfile |
| 227 | envsubst < $2 > $configfile |
| 228 | output_yaml=$PWD/tmp/dmaapadp_cfc.yaml |
| 229 | __kube_create_configmap $DMAAP_ADP_CONFIG_CONFIGMAP_NAME $KUBE_NONRTRIC_NAMESPACE autotest DMAAPADP $configfile $output_yaml |
| 230 | |
| 231 | # Create config map for data |
| 232 | data_json=$PWD/tmp/$DMAAP_ADP_DATA_FILE |
| 233 | if [ $# -lt 3 ]; then |
| 234 | #create empty dummy file |
| 235 | echo "{}" > $data_json |
| 236 | else |
| 237 | cp $3 $data_json |
| 238 | fi |
| 239 | output_yaml=$PWD/tmp/dmaapadp_cfd.yaml |
| 240 | __kube_create_configmap $DMAAP_ADP_DATA_CONFIGMAP_NAME $KUBE_NONRTRIC_NAMESPACE autotest DMAAPADP $data_json $output_yaml |
| 241 | |
| 242 | |
| 243 | # Create service |
| 244 | input_yaml=$SIM_GROUP"/"$DMAAP_ADP_COMPOSE_DIR"/"svc.yaml |
| 245 | output_yaml=$PWD/tmp/dmaapadp_svc.yaml |
| 246 | __kube_create_instance service $DMAAP_ADP_APP_NAME $input_yaml $output_yaml |
| 247 | |
| 248 | # Create app |
| 249 | input_yaml=$SIM_GROUP"/"$DMAAP_ADP_COMPOSE_DIR"/"app.yaml |
| 250 | output_yaml=$PWD/tmp/dmaapadp_app.yaml |
| 251 | __kube_create_instance app $DMAAP_ADP_APP_NAME $input_yaml $output_yaml |
| 252 | |
| 253 | fi |
| 254 | |
| 255 | __check_service_start $DMAAP_ADP_APP_NAME $DMAAP_ADP_SERVICE_PATH$DMAAP_ADP_ALIVE_URL |
| 256 | |
| 257 | else |
| 258 | # Check if docker app shall be fully managed by the test script |
| 259 | __check_included_image 'DMAAPADP' |
| 260 | if [ $? -eq 1 ]; then |
| 261 | echo -e $RED"The $DMAAP_ADP_DISPLAY_NAME app is not included in this test script"$ERED |
| 262 | echo -e $RED"The $DMAAP_ADP_DISPLAY_NAME will not be started"$ERED |
| 263 | exit |
| 264 | fi |
| 265 | |
| 266 | __dmaapadp_export_vars $1 |
| 267 | |
| 268 | dest_file=$SIM_GROUP/$DMAAP_ADP_COMPOSE_DIR/$DMAAP_ADP_HOST_MNT_DIR/$DMAAP_ADP_CONFIG_FILE |
| 269 | |
| 270 | envsubst < $2 > $dest_file |
| 271 | |
| 272 | dest_file=$SIM_GROUP/$DMAAP_ADP_COMPOSE_DIR/$DMAAP_ADP_HOST_MNT_DIR/$DMAAP_ADP_DATA_FILE |
| 273 | |
| 274 | if [ $# -lt 3 ]; then |
| 275 | #create empty dummy file |
| 276 | echo "{}" > $dest_file |
| 277 | else |
| 278 | envsubst < $3 > $dest_file |
| 279 | fi |
| 280 | |
| 281 | __start_container $DMAAP_ADP_COMPOSE_DIR "" NODOCKERARGS 1 $DMAAP_ADP_APP_NAME |
| 282 | |
| 283 | __check_service_start $DMAAP_ADP_APP_NAME $DMAAP_ADP_SERVICE_PATH$DMAAP_ADP_ALIVE_URL |
| 284 | fi |
| 285 | echo "" |
| 286 | } |
| 287 | |
| 288 | # Turn on trace level tracing |
| 289 | # args: - |
| 290 | # (Function for test scripts) |
| 291 | set_dmaapadp_trace() { |
| 292 | echo -e $BOLD"$DMAAP_ADP_DISPLAY_NAME trace logging"$EBOLD |
| 293 | curlString="$DMAAP_ADP_SERVICE_PATH$DMAAP_ADP_ACTUATOR -X POST -H Content-Type:application/json -d {\"configuredLevel\":\"trace\"}" |
| 294 | result=$(__do_curl "$curlString") |
| 295 | if [ $? -ne 0 ]; then |
| 296 | __print_err "could not set trace mode" $@ |
| 297 | ((RES_CONF_FAIL++)) |
| 298 | return 1 |
| 299 | fi |
| 300 | echo "" |
| 301 | return 0 |
| 302 | } |