BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +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 | |
| 20 | # This is a script that contains container/service management functions and test functions for Consul/CBS |
| 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 that exist with staging, snapshot,release tags |
| 27 | __CONSUL_imagesetup() { |
| 28 | __check_and_create_image_var CONSUL "CONSUL_IMAGE" "CONSUL_IMAGE_BASE" "CONSUL_IMAGE_TAG" REMOTE_PROXY "$CONSUL_DISPLAY_NAME" |
| 29 | |
| 30 | } |
| 31 | |
| 32 | # Create the image var used during the test |
| 33 | # arg: <image-tag-suffix> (selects staging, snapshot, release etc) |
| 34 | # <image-tag-suffix> is present only for images with staging, snapshot,release tags |
| 35 | __CBS_imagesetup() { |
| 36 | __check_and_create_image_var CBS "CBS_IMAGE" "CBS_IMAGE_BASE" "CBS_IMAGE_TAG" REMOTE_RELEASE_ONAP "$CBS_DISPLAY_NAME" |
| 37 | |
| 38 | } |
| 39 | |
| 40 | # Pull image from remote repo or use locally built image |
| 41 | # arg: <pull-policy-override> <pull-policy-original> |
| 42 | # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images |
| 43 | # <pull-policy-original> Shall be used for images that does not allow overriding |
| 44 | # Both var may contain: 'remote', 'remote-remove' or 'local' |
| 45 | __CONSUL_imagepull() { |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 46 | __check_and_pull_image $2 "$CONSUL_DISPLAY_NAME" $CONSUL_APP_NAME CONSUL_IMAGE |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | # Pull image from remote repo or use locally built image |
| 50 | # arg: <pull-policy-override> <pull-policy-original> |
| 51 | # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images |
| 52 | # <pull-policy-original> Shall be used for images that does not allow overriding |
| 53 | # Both var may contain: 'remote', 'remote-remove' or 'local' |
| 54 | __CBS_imagepull() { |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 55 | __check_and_pull_image $2 "$CBS_DISPLAY_NAME" $CBS_APP_NAME CBS_IMAGE |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | # Build image (only for simulator or interfaces stubs owned by the test environment) |
| 59 | # arg: <image-tag-suffix> (selects staging, snapshot, release etc) |
| 60 | # <image-tag-suffix> is present only for images with staging, snapshot,release tags |
| 61 | __CONSUL_imagebuild() { |
| 62 | echo -e $RED" Image for app CONSUL shall never be built"$ERED |
| 63 | } |
| 64 | |
| 65 | # Build image (only for simulator or interfaces stubs owned by the test environment) |
| 66 | # arg: <image-tag-suffix> (selects staging, snapshot, release etc) |
| 67 | # <image-tag-suffix> is present only for images with staging, snapshot,release tags |
| 68 | __CBS_imagebuild() { |
| 69 | echo -e $RED" Image for app CBS shall never be built"$ERED |
| 70 | } |
| 71 | |
| 72 | # 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] | 73 | # 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] | 74 | # arg: <docker-images-format-string> <file-to-append> |
| 75 | __CONSUL_image_data() { |
| 76 | echo -e "$CONSUL_DISPLAY_NAME\t$(docker images --format $1 $CONSUL_IMAGE)" >> $2 |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 77 | if [ ! -z "$CONSUL_IMAGE_SOURCE" ]; then |
| 78 | echo -e "-- source image --\t$(docker images --format $1 $CONSUL_IMAGE_SOURCE)" >> $2 |
| 79 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | # 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] | 83 | # 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] | 84 | # arg: <docker-images-format-string> <file-to-append> |
| 85 | __CBS_image_data() { |
| 86 | echo -e "$CBS_DISPLAY_NAME\t$(docker images --format $1 $CBS_IMAGE)" >> $2 |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 87 | if [ ! -z "$CBS_IMAGE_SOURCE" ]; then |
| 88 | echo -e "-- source image --\t$(docker images --format $1 $CBS_IMAGE_SOURCE)" >> $2 |
| 89 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | # Scale kubernetes resources to zero |
| 93 | # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action. |
| 94 | # This function is called for apps fully managed by the test script |
| 95 | __CONSUL_kube_scale_zero() { |
| 96 | echo -e $RED" Image for app CONSUL is not used in kube"$ERED |
| 97 | } |
| 98 | |
| 99 | # Scale kubernetes resources to zero |
| 100 | # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action. |
| 101 | # This function is called for apps fully managed by the test script |
| 102 | __CBS_kube_scale_zero() { |
| 103 | echo -e $RED" Image for app CBS is not used in kube"$ERED |
| 104 | } |
| 105 | |
| 106 | # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action. |
| 107 | # This function is called for prestarted apps not managed by the test script. |
| 108 | __CONSUL_kube_scale_zero_and_wait() { |
| 109 | echo -e $RED" CONSUL app is not used in kube"$ERED |
| 110 | } |
| 111 | |
| 112 | # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action. |
| 113 | # This function is called for prestarted apps not managed by the test script. |
| 114 | __CBS_kube_scale_zero_and_wait() { |
| 115 | echo -e $RED" CBS app is not used in kube"$ERED |
| 116 | } |
| 117 | |
| 118 | # Delete all kube resouces for the app |
| 119 | # This function is called for apps managed by the test script. |
| 120 | __CONSUL_kube_delete_all() { |
| 121 | echo -e $RED" CONSUL app is not used in kube"$ERED |
| 122 | } |
| 123 | |
| 124 | # Delete all kube resouces for the app |
| 125 | # This function is called for apps managed by the test script. |
| 126 | __CBS_kube_delete_all() { |
| 127 | echo -e $RED" CBS app is not used in kube"$ERED |
| 128 | } |
| 129 | |
| 130 | # Store docker logs |
| 131 | # This function is called for apps managed by the test script. |
| 132 | # args: <log-dir> <file-prexix> |
| 133 | __CONSUL_store_docker_logs() { |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 134 | if [ $RUNMODE == "KUBE" ]; then |
| 135 | : |
| 136 | else |
| 137 | docker logs $CONSUL_APP_NAME > $1/$2_consul.log 2>&1 |
| 138 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | # Store docker logs |
| 142 | # This function is called for apps managed by the test script. |
| 143 | # args: <log-dir> <file-prexix> |
| 144 | __CBS_store_docker_logs() { |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 145 | if [ $RUNMODE == "KUBE" ]; then |
| 146 | : |
| 147 | else |
| 148 | docker logs $CBS_APP_NAME > $1$2_cbs.log 2>&1 |
| 149 | body="$(__do_curl $LOCALHOST_HTTP:$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)" |
| 150 | echo "$body" > $1$2_consul_config.json 2>&1 |
| 151 | fi |
| 152 | } |
| 153 | |
| 154 | # Initial setup of protocol, host and ports |
| 155 | # This function is called for apps managed by the test script. |
| 156 | # args: - |
| 157 | __CONSUL_initial_setup() { |
| 158 | CONSUL_SERVICE_PATH="http://"$CONSUL_APP_NAME":"$CONSUL_INTERNAL_PORT |
| 159 | } |
| 160 | |
| 161 | # Initial setup of protocol, host and ports |
| 162 | # This function is called for apps managed by the test script. |
| 163 | # args: - |
| 164 | __CBS_initial_setup() { |
| 165 | CBS_SERVICE_PATH="http://"$CBS_APP_NAME":"$CBS_INTERNAL_PORT |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 166 | } |
| 167 | |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 168 | # Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers |
| 169 | # For docker, the namespace shall be excluded |
| 170 | # This function is called for apps managed by the test script as well as for prestarted apps. |
| 171 | # args: - |
| 172 | __CONSUL_statisics_setup() { |
| 173 | echo "" |
| 174 | } |
| 175 | |
| 176 | # Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers |
| 177 | # For docker, the namespace shall be excluded |
| 178 | # This function is called for apps managed by the test script as well as for prestarted apps. |
| 179 | # args: - |
| 180 | __CBS_statisics_setup() { |
| 181 | echo "" |
| 182 | } |
BjornMagnussonXA | e60d04e | 2021-12-27 13:38:01 +0100 | [diff] [blame^] | 183 | |
| 184 | # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied |
| 185 | # args: - |
| 186 | __CONSUL_test_requirements() { |
| 187 | : |
| 188 | } |
| 189 | |
| 190 | # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied |
| 191 | # args: - |
| 192 | __CBS_test_requirements() { |
| 193 | : |
| 194 | } |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 195 | ####################################################### |
| 196 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 197 | |
| 198 | #################### |
| 199 | ### Consul functions |
| 200 | #################### |
| 201 | |
| 202 | # Function to load config from a file into consul for the Policy Agent |
| 203 | # arg: <json-config-file> |
| 204 | # (Function for test scripts) |
| 205 | consul_config_app() { |
| 206 | |
| 207 | echo -e $BOLD"Configuring Consul"$EBOLD |
| 208 | |
| 209 | if [ $# -ne 1 ]; then |
| 210 | ((RES_CONF_FAIL++)) |
| 211 | __print_err "need one arg, <json-config-file>" $@ |
| 212 | exit 1 |
| 213 | fi |
| 214 | |
| 215 | echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1 |
| 216 | |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 217 | curlString="$CONSUL_SERVICE_PATH/v1/kv/${POLICY_AGENT_CONFIG_KEY}?dc=dc1 -X PUT -H Accept:application/json -H Content-Type:application/json -H X-Requested-With:XMLHttpRequest --data-binary @"$1 |
| 218 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 219 | result=$(__do_curl "$curlString") |
| 220 | if [ $? -ne 0 ]; then |
| 221 | echo -e $RED" FAIL - json config could not be loaded to consul" $ERED |
| 222 | ((RES_CONF_FAIL++)) |
| 223 | return 1 |
| 224 | fi |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 225 | body="$(__do_curl $CBS_SERVICE_PATH/service_component_all/$POLICY_AGENT_CONFIG_KEY)" |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 226 | echo $body > "./tmp/.output"$1 |
| 227 | |
| 228 | if [ $? -ne 0 ]; then |
| 229 | echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED |
| 230 | ((RES_CONF_FAIL++)) |
| 231 | return 1 |
| 232 | else |
| 233 | targetJson=$(< $1) |
| 234 | targetJson="{\"config\":"$targetJson"}" |
| 235 | echo "TARGET JSON: $targetJson" >> $HTTPLOG |
| 236 | res=$(python3 ../common/compare_json.py "$targetJson" "$body") |
| 237 | if [ $res -ne 0 ]; then |
| 238 | echo -e $RED" FAIL - policy json config read from consul/cbs is not equal to the intended json config...." $ERED |
| 239 | ((RES_CONF_FAIL++)) |
| 240 | return 1 |
| 241 | else |
| 242 | echo -e $GREEN" Config loaded ok to consul"$EGREEN |
| 243 | fi |
| 244 | fi |
| 245 | |
| 246 | echo "" |
| 247 | |
| 248 | } |
| 249 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 250 | # Start Consul and CBS |
| 251 | # args: - |
| 252 | # (Function for test scripts) |
| 253 | start_consul_cbs() { |
| 254 | |
| 255 | echo -e $BOLD"Starting $CONSUL_DISPLAY_NAME and $CBS_DISPLAY_NAME"$EBOLD |
| 256 | __check_included_image 'CONSUL' |
| 257 | if [ $? -eq 1 ]; then |
| 258 | echo -e $RED"The Consul image has not been checked for this test run due to arg to the test script"$ERED |
| 259 | echo -e $RED"Consul will not be started"$ERED |
| 260 | exit |
| 261 | fi |
| 262 | export CONSUL_APP_NAME |
| 263 | export CONSUL_INTERNAL_PORT |
| 264 | export CONSUL_EXTERNAL_PORT |
| 265 | export CBS_APP_NAME |
| 266 | export CBS_INTERNAL_PORT |
| 267 | export CBS_EXTERNAL_PORT |
| 268 | export CONSUL_HOST |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 269 | export CONSUL_DISPLAY_NAME |
| 270 | export CBS_DISPLAY_NAME |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 271 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 272 | __start_container $CONSUL_CBS_COMPOSE_DIR "" NODOCKERARGS 2 $CONSUL_APP_NAME $CBS_APP_NAME |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 273 | |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 274 | __check_service_start $CONSUL_APP_NAME $CONSUL_SERVICE_PATH$CONSUL_ALIVE_URL |
| 275 | __check_service_start $CBS_APP_NAME $CBS_SERVICE_PATH$CBS_ALIVE_URL |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 276 | |
| 277 | echo "" |
| 278 | } |
| 279 | |