BjornMagnussonXA | 80a9200 | 2020-03-19 14:31: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 management functions and test functions for RICSIM A1 simulators |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31: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 | __RICSIM_imagesetup() { |
| 28 | __check_and_create_image_var RICSIM "RIC_SIM_IMAGE" "RIC_SIM_IMAGE_BASE" "RIC_SIM_IMAGE_TAG" $1 "$RIC_SIM_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 var may contain: 'remote', 'remote-remove' or 'local' |
| 36 | __RICSIM_imagepull() { |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 37 | __check_and_pull_image $1 "$RIC_SIM_DISPLAY_NAME" $RIC_SIM_PREFIX"_"$RIC_SIM_BASE RIC_SIM_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 | __RICSIM_image_data() { |
| 44 | echo -e "$RIC_SIM_DISPLAY_NAME\t$(docker images --format $1 $RIC_SIM_IMAGE)" >> $2 |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 45 | if [ ! -z "$RIC_SIM_IMAGE_SOURCE" ]; then |
| 46 | echo -e "-- source image --\t$(docker images --format $1 $RIC_SIM_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 | __RICSIM_kube_scale_zero() { |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 54 | __kube_scale_all_resources $KUBE_A1SIM_NAMESPACE autotest RICSIM |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 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 | __RICSIM_kube_scale_zero_and_wait() { |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 60 | #__kube_scale_and_wait_all_resources $KUBE_A1SIM_NAMESPACE app $KUBE_A1SIM_NAMESPACE"-"$RIC_SIM_PREFIX |
BjornMagnussonXA | dfdca18 | 2021-12-10 10:43:32 +0100 | [diff] [blame] | 61 | __kube_scale_and_wait_all_resources $KUBE_A1SIM_NAMESPACE app $KUBE_A1SIM_NAMESPACE"-a1simulator" |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | # Delete all kube resouces for the app |
| 65 | # This function is called for apps managed by the test script. |
| 66 | __RICSIM_kube_delete_all() { |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 67 | __kube_delete_all_resources $KUBE_A1SIM_NAMESPACE autotest RICSIM |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | # Store docker logs |
| 71 | # This function is called for apps managed by the test script. |
| 72 | # args: <log-dir> <file-prexix> |
| 73 | __RICSIM_store_docker_logs() { |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 74 | if [ $RUNMODE == "KUBE" ]; then |
BjornMagnussonXA | cb6113e | 2022-02-17 15:01:28 +0100 | [diff] [blame] | 75 | for podname in $(kubectl $KUBECONF get pods -n $KUBE_A1SIM_NAMESPACE -l "autotest=RICSIM" -o custom-columns=":metadata.name"); do |
| 76 | kubectl $KUBECONF logs -n $KUBE_A1SIM_NAMESPACE $podname --tail=-1 > $1$2_$podname.log 2>&1 |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 77 | done |
| 78 | else |
| 79 | |
| 80 | rics=$(docker ps --filter "name=$RIC_SIM_PREFIX" --filter "network=$DOCKER_SIM_NWNAME" --filter "status=running" --format {{.Names}}) |
| 81 | for ric in $rics; do |
| 82 | docker logs $ric > $1$2_$ric.log 2>&1 |
| 83 | done |
| 84 | fi |
| 85 | } |
| 86 | |
| 87 | # Initial setup of protocol, host and ports |
| 88 | # This function is called for apps managed by the test script. |
| 89 | # args: - |
| 90 | __RICSIM_initial_setup() { |
| 91 | use_simulator_http |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 92 | } |
| 93 | |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 94 | # Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers |
| 95 | # For docker, the namespace shall be excluded |
| 96 | # This function is called for apps managed by the test script as well as for prestarted apps. |
| 97 | # args: - |
| 98 | __RICSIM_statisics_setup() { |
BjornMagnussonXA | 79e3700 | 2021-11-22 13:36:04 +0100 | [diff] [blame] | 99 | for ((RICSIM_INSTANCE=10; RICSIM_INSTANCE>0; RICSIM_INSTANCE-- )); do |
| 100 | if [ $RUNMODE == "KUBE" ]; then |
| 101 | RICSIM_INSTANCE_KUBE=$(($RICSIM_INSTANCE-1)) |
| 102 | echo -n " RICSIM_G1_$RICSIM_INSTANCE_KUBE ${RIC_SIM_PREFIX}-g1-$RICSIM_INSTANCE_KUBE $KUBE_A1SIM_NAMESPACE " |
| 103 | echo -n " RICSIM_G2_$RICSIM_INSTANCE_KUBE ${RIC_SIM_PREFIX}-g2-$RICSIM_INSTANCE_KUBE $KUBE_A1SIM_NAMESPACE " |
| 104 | echo -n " RICSIM_G3_$RICSIM_INSTANCE_KUBE ${RIC_SIM_PREFIX}-g3-$RICSIM_INSTANCE_KUBE $KUBE_A1SIM_NAMESPACE " |
| 105 | else |
BjornMagnussonXA | 51f04f0 | 2021-11-23 09:22:35 +0100 | [diff] [blame] | 106 | if [ $DOCKER_COMPOSE_VERION == "V1" ]; then |
| 107 | echo -n " RICSIM_G1_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}_g1_$RICSIM_INSTANCE " |
| 108 | echo -n " RICSIM_G2_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}_g2_$RICSIM_INSTANCE " |
| 109 | echo -n " RICSIM_G3_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}_g3_$RICSIM_INSTANCE " |
| 110 | else |
| 111 | echo -n " RICSIM_G1_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}-g1-$RICSIM_INSTANCE " |
| 112 | echo -n " RICSIM_G2_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}-g2-$RICSIM_INSTANCE " |
| 113 | echo -n " RICSIM_G3_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}-g3-$RICSIM_INSTANCE " |
| 114 | fi |
BjornMagnussonXA | 79e3700 | 2021-11-22 13:36:04 +0100 | [diff] [blame] | 115 | fi |
| 116 | done |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 117 | } |
| 118 | |
BjornMagnussonXA | e60d04e | 2021-12-27 13:38:01 +0100 | [diff] [blame] | 119 | # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied |
| 120 | # args: - |
| 121 | __RICSIM_test_requirements() { |
| 122 | : |
| 123 | } |
| 124 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 125 | ####################################################### |
| 126 | |
| 127 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 128 | RIC_SIM_HTTPX="http" |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 129 | RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT |
| 130 | |
| 131 | |
BjornMagnussonXA | 51f04f0 | 2021-11-23 09:22:35 +0100 | [diff] [blame] | 132 | #Vars for container count |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 133 | G1_COUNT=0 |
| 134 | G2_COUNT=0 |
| 135 | G3_COUNT=0 |
| 136 | G4_COUNT=0 |
| 137 | G5_COUNT=0 |
| 138 | |
| 139 | |
| 140 | ########################### |
| 141 | ### RIC Simulator functions |
| 142 | ########################### |
| 143 | |
| 144 | use_simulator_http() { |
| 145 | echo -e $BOLD"RICSIM protocol setting"$EBOLD |
| 146 | echo -e " Using $BOLD http $EBOLD towards the simulators" |
| 147 | RIC_SIM_HTTPX="http" |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 148 | RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT |
| 149 | echo "" |
| 150 | } |
| 151 | |
| 152 | use_simulator_https() { |
| 153 | echo -e $BOLD"RICSIM protocol setting"$EBOLD |
| 154 | echo -e " Using $BOLD https $EBOLD towards the simulators" |
| 155 | RIC_SIM_HTTPX="https" |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 156 | RIC_SIM_PORT=$RIC_SIM_INTERNAL_SECURE_PORT |
| 157 | echo "" |
| 158 | } |
| 159 | |
| 160 | # Start one group (ricsim_g1, ricsim_g2 .. ricsim_g5) with a number of RIC Simulators using a given A interface |
| 161 | # 'ricsim' may be set on command line to other prefix |
| 162 | # args: ricsim_g1|ricsim_g2|ricsim_g3|ricsim_g4|ricsim_g5 <count> <interface-id> |
| 163 | # (Function for test scripts) |
| 164 | start_ric_simulators() { |
| 165 | |
| 166 | echo -e $BOLD"Starting $RIC_SIM_DISPLAY_NAME"$EBOLD |
| 167 | |
| 168 | if [ $RUNMODE == "KUBE" ]; then |
| 169 | |
| 170 | # Check if app shall be fully managed by the test script |
| 171 | __check_included_image "RICSIM" |
| 172 | retcode_i=$? |
| 173 | |
| 174 | # Check if app shall only be used by the testscipt |
| 175 | __check_prestarted_image "RICSIM" |
| 176 | retcode_p=$? |
| 177 | |
| 178 | if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then |
| 179 | echo -e $RED"The $1 app is not included as managed nor prestarted in this test script"$ERED |
| 180 | echo -e $RED"The $1 will not be started"$ERED |
| 181 | exit |
| 182 | fi |
| 183 | if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then |
| 184 | echo -e $RED"The $1 stub app is included both as managed and prestarted in this test script"$ERED |
| 185 | echo -e $RED"The $1 will not be started"$ERED |
| 186 | exit |
| 187 | fi |
| 188 | |
| 189 | if [ $retcode_p -eq 0 ]; then |
| 190 | echo -e " Using existing $1 statefulset and service" |
| 191 | echo " Using existing simulator deployment and service for statefulset $1" |
| 192 | echo " Setting $1 replicas=$2" |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 193 | __kube_scale statefulset $1 $KUBE_A1SIM_NAMESPACE $2 |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 194 | echo "" |
| 195 | return |
| 196 | fi |
| 197 | fi |
| 198 | |
| 199 | RIC1=$RIC_SIM_PREFIX"_g1" |
| 200 | RIC2=$RIC_SIM_PREFIX"_g2" |
| 201 | RIC3=$RIC_SIM_PREFIX"_g3" |
| 202 | RIC4=$RIC_SIM_PREFIX"_g4" |
| 203 | RIC5=$RIC_SIM_PREFIX"_g5" |
| 204 | |
| 205 | if [ $# != 3 ]; then |
| 206 | ((RES_CONF_FAIL++)) |
| 207 | __print_err "need three args, $RIC1|$RIC2|$RIC3|$RIC4|$RIC5 <count> <interface-id>" $@ |
| 208 | exit 1 |
| 209 | fi |
| 210 | |
| 211 | echo " $2 simulators using basename: $1 on interface: $3" |
| 212 | #Set env var for simulator count and A1 interface vesion for the given group |
| 213 | if [ $1 == "$RIC1" ]; then |
| 214 | G1_COUNT=$2 |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 215 | elif [ $1 == "$RIC2" ]; then |
| 216 | G2_COUNT=$2 |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 217 | elif [ $1 == "$RIC3" ]; then |
| 218 | G3_COUNT=$2 |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 219 | elif [ $1 == "$RIC4" ]; then |
| 220 | G4_COUNT=$2 |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 221 | elif [ $1 == "$RIC5" ]; then |
| 222 | G5_COUNT=$2 |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 223 | else |
| 224 | ((RES_CONF_FAIL++)) |
| 225 | __print_err "need three args, $RIC1|$RIC2|$RIC3|$RIC4|$RIC5 <count> <interface-id>" $@ |
| 226 | exit 1 |
| 227 | fi |
| 228 | |
| 229 | if [ $RUNMODE == "KUBE" ]; then |
| 230 | |
| 231 | if [ $retcode_i -eq 0 ]; then |
| 232 | |
| 233 | #export needed env var for statefulset |
| 234 | export RIC_SIM_SET_NAME=$(echo "$1" | tr '_' '-') #kube does not accept underscore in names |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 235 | export KUBE_A1SIM_NAMESPACE |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 236 | export RIC_SIM_IMAGE |
| 237 | #Adding 1 more instance, instance 0 is never used. This is done to keep test scripts compatible |
| 238 | # with docker that starts instance index on 1..... |
| 239 | export RIC_SIM_COUNT=$(($2+1)) |
| 240 | export A1_VERSION=$3 |
| 241 | export RIC_SIM_INTERNAL_PORT |
| 242 | export RIC_SIM_INTERNAL_SECURE_PORT |
| 243 | |
| 244 | echo -e " Creating $POLICY_AGENT_APP_NAME app and expose service" |
| 245 | |
| 246 | #Check if nonrtric namespace exists, if not create it |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 247 | __kube_create_namespace $KUBE_A1SIM_NAMESPACE |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 248 | |
| 249 | # Create service |
| 250 | input_yaml=$SIM_GROUP"/"$RIC_SIM_COMPOSE_DIR"/"svc.yaml |
| 251 | output_yaml=$PWD/tmp/ric_${1}_svc.yaml |
| 252 | __kube_create_instance service $RIC_SIM_SET_NAME $input_yaml $output_yaml |
| 253 | |
| 254 | # Create app |
| 255 | input_yaml=$SIM_GROUP"/"$RIC_SIM_COMPOSE_DIR"/"app.yaml |
BjornMagnussonXA | c8f92e9 | 2022-02-28 15:16:37 +0100 | [diff] [blame^] | 256 | output_yaml=$PWD/tmp/ric_${1}_app.yaml |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 257 | __kube_create_instance app $RIC_SIM_SET_NAME $input_yaml $output_yaml |
| 258 | |
| 259 | #Using only instance from index 1 to keep compatability with docker |
| 260 | for (( count=1; count<${RIC_SIM_COUNT}; count++ )); do |
| 261 | host=$(__find_sim_host $RIC_SIM_SET_NAME"-"$count) |
| 262 | __check_service_start $RIC_SIM_SET_NAME"-"$count $host"/" |
| 263 | done |
| 264 | fi |
| 265 | else |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 266 | __check_included_image 'RICSIM' |
| 267 | if [ $? -eq 1 ]; then |
| 268 | echo -e $RED"The Near-RT RIC Simulator app is not included as managed in this test script"$ERED |
| 269 | echo -e $RED"Near-RT RIC Simulator will not be started"$ERED |
| 270 | exit 1 |
| 271 | fi |
| 272 | |
| 273 | # Create .env file to compose project, all ric container will get this prefix |
| 274 | echo "COMPOSE_PROJECT_NAME="$RIC_SIM_PREFIX > $SIM_GROUP/$RIC_SIM_COMPOSE_DIR/.env |
| 275 | |
BjornMagnussonXA | 51f04f0 | 2021-11-23 09:22:35 +0100 | [diff] [blame] | 276 | #extract service name (group), g1, g2, g3, g4 or g5 from var $1 |
| 277 | #E.g. ricsim_g1 -> g1 is the service name |
| 278 | TMP_GRP=$1 |
| 279 | RICSIM_COMPOSE_SERVICE_NAME=$(echo "${TMP_GRP##*_}") |
| 280 | |
| 281 | export RICSIM_COMPOSE_A1_VERSION=$3 |
| 282 | export RICSIM_COMPOSE_SERVICE_NAME |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 283 | export RIC_SIM_INTERNAL_PORT |
| 284 | export RIC_SIM_INTERNAL_SECURE_PORT |
| 285 | export RIC_SIM_CERT_MOUNT_DIR |
| 286 | export DOCKER_SIM_NWNAME |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 287 | export RIC_SIM_DISPLAY_NAME |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 288 | |
BjornMagnussonXA | 8fbb226 | 2022-01-24 15:20:15 +0100 | [diff] [blame] | 289 | docker_args=" --scale $RICSIM_COMPOSE_SERVICE_NAME=$2" |
BjornMagnussonXA | 51f04f0 | 2021-11-23 09:22:35 +0100 | [diff] [blame] | 290 | |
| 291 | #Create a list of contsiner names |
| 292 | #Will be <ricsim-prefix>_<service-name>_<index> |
| 293 | # or |
| 294 | # <ricsim-prefix>-<service-name>-<index> |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 295 | app_data="" |
| 296 | cntr=1 |
BjornMagnussonXA | 51f04f0 | 2021-11-23 09:22:35 +0100 | [diff] [blame] | 297 | if [ $DOCKER_COMPOSE_VERION == "V1" ]; then |
| 298 | app_name_prefix=$RIC_SIM_PREFIX"_"$RICSIM_COMPOSE_SERVICE_NAME"_" |
| 299 | else |
| 300 | app_name_prefix=$RIC_SIM_PREFIX"-"$RICSIM_COMPOSE_SERVICE_NAME"-" |
| 301 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 302 | while [ $cntr -le $2 ]; do |
BjornMagnussonXA | 51f04f0 | 2021-11-23 09:22:35 +0100 | [diff] [blame] | 303 | app=$app_name_prefix$cntr |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 304 | app_data="$app_data $app" |
| 305 | let cntr=cntr+1 |
| 306 | done |
| 307 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 308 | __start_container $RIC_SIM_COMPOSE_DIR "" "$docker_args" $2 $app_data |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 309 | |
| 310 | cntr=1 |
| 311 | while [ $cntr -le $2 ]; do |
BjornMagnussonXA | 51f04f0 | 2021-11-23 09:22:35 +0100 | [diff] [blame] | 312 | if [ $DOCKER_COMPOSE_VERION == "V1" ]; then |
| 313 | app=$RIC_SIM_PREFIX"_"$RICSIM_COMPOSE_SERVICE_NAME"_"$cntr |
| 314 | else |
| 315 | app=$RIC_SIM_PREFIX"-"$RICSIM_COMPOSE_SERVICE_NAME"-"$cntr |
| 316 | fi |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 317 | __check_service_start $app $RIC_SIM_HTTPX"://"$app:$RIC_SIM_PORT$RIC_SIM_ALIVE_URL |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 318 | let cntr=cntr+1 |
| 319 | done |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 320 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 321 | fi |
| 322 | echo "" |
| 323 | return 0 |
| 324 | } |
| 325 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 326 | # Translate ric name to kube host name |
| 327 | # args: <ric-name> |
| 328 | # For test scripts |
| 329 | get_kube_sim_host() { |
| 330 | name=$(echo "$1" | tr '_' '-') #kube does not accept underscore in names |
| 331 | #example gnb_1_2 -> gnb-1-2 |
| 332 | set_name=$(echo $name | rev | cut -d- -f2- | rev) # Cut index part of ric name to get the name of statefulset |
| 333 | # example gnb-g1-2 -> gnb-g1 where gnb-g1-2 is the ric name and gnb-g1 is the set name |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 334 | echo $name"."$set_name"."$KUBE_A1SIM_NAMESPACE |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | # Helper function to get a the port and host name of a specific ric simulator |
| 338 | # args: <ric-id> |
| 339 | # (Not for test scripts) |
| 340 | __find_sim_host() { |
| 341 | if [ $RUNMODE == "KUBE" ]; then |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 342 | ricname=$(echo "$1" | tr '_' '-') # Kube does not accept underscore in names as docker do |
BjornMagnussonXA | dfdca18 | 2021-12-10 10:43:32 +0100 | [diff] [blame] | 343 | if [ -z "$RIC_SIM_COMMON_SVC_NAME" ]; then |
| 344 | ric_setname="${ricname%-*}" #Extract the stateful set name |
| 345 | else |
| 346 | ric_setname=$RIC_SIM_COMMON_SVC_NAME # Use the common svc name in the host name of the sims |
| 347 | fi |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 348 | echo $RIC_SIM_HTTPX"://"$ricname.$ric_setname.$KUBE_A1SIM_NAMESPACE":"$RIC_SIM_PORT |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 349 | else |
BjornMagnussonXA | 51f04f0 | 2021-11-23 09:22:35 +0100 | [diff] [blame] | 350 | if [ $DOCKER_COMPOSE_VERION == "V1" ]; then |
| 351 | echo $RIC_SIM_HTTPX"://"$1":"$RIC_SIM_PORT |
| 352 | else |
| 353 | ricname=$(echo "$1" | tr '_' '-') |
| 354 | echo $RIC_SIM_HTTPX"://"$ricname":"$RIC_SIM_PORT |
| 355 | fi |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 356 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 357 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | # Generate a UUID to use as prefix for policy ids |
| 361 | generate_policy_uuid() { |
| 362 | UUID=$(python3 -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().hex)') |
| 363 | #Reduce length to make space for serial id, uses 'a' as marker where the serial id is added |
| 364 | UUID=${UUID:0:${#UUID}-4}"a" |
| 365 | } |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 366 | |
| 367 | # Excute a curl cmd towards a ricsimulator and check the response code. |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 368 | # args: <expected-response-code> <curl-cmd-string> |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 369 | __execute_curl_to_sim() { |
| 370 | echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 371 | proxyflag="" |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 372 | if [ ! -z "$KUBE_PROXY_PATH" ]; then |
| 373 | if [ $KUBE_PROXY_HTTPX == "http" ]; then |
| 374 | proxyflag=" --proxy $KUBE_PROXY_PATH" |
| 375 | else |
| 376 | proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH" |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 377 | fi |
| 378 | fi |
| 379 | echo " CMD: $2 $proxyflag" >> $HTTPLOG |
| 380 | res="$($2 $proxyflag)" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 381 | echo " RESP: $res" >> $HTTPLOG |
| 382 | retcode=$? |
| 383 | if [ $retcode -ne 0 ]; then |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 384 | ((RES_CONF_FAIL++)) |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 385 | echo " RETCODE: "$retcode |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 386 | echo -e $RED" FAIL - fatal error when executing curl."$ERED |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 387 | return 1 |
| 388 | fi |
| 389 | status=${res:${#res}-3} |
| 390 | if [ $status -eq $1 ]; then |
| 391 | echo -e $GREEN" OK"$EGREEN |
| 392 | return 0 |
| 393 | fi |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 394 | echo -e $RED" FAIL - expected http response: "$1" but got http response: "$status $ERED |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 395 | ((RES_CONF_FAIL++)) |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 396 | return 1 |
| 397 | } |
| 398 | |
| 399 | # Tests if a variable value in the ricsimulator is equal to a target value and and optional timeout. |
| 400 | # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable is |
| 401 | # equal to the target or not. |
| 402 | # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds |
| 403 | # before setting pass or fail depending on if the variable value becomes equal to the target |
| 404 | # value or not. |
| 405 | # (Function for test scripts) |
| 406 | sim_equal() { |
| 407 | |
| 408 | if [ $# -eq 3 ] || [ $# -eq 4 ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 409 | host=$(__find_sim_host $1) |
| 410 | __var_test $1 "$host/counter/" $2 "=" $3 $4 |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 411 | return 0 |
| 412 | else |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 413 | __print_err "needs three or four args: <ric-id> <sim-param> <target-value> [ timeout ]" |
| 414 | return 1 |
| 415 | fi |
| 416 | } |
| 417 | |
| 418 | # Print a variable value from the RIC sim. |
| 419 | # args: <ric-id> <variable-name> |
| 420 | # (Function for test scripts) |
| 421 | sim_print() { |
| 422 | |
| 423 | if [ $# != 2 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 424 | __print_err "need two args, <ric-id> <sim-param>" $@ |
| 425 | exit 1 |
| 426 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 427 | host=$(__find_sim_host $1) |
| 428 | echo -e $BOLD"INFO(${BASH_LINENO[0]}): $1, $2 = $(__do_curl $host/counter/$2)"$EBOLD |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 429 | } |
| 430 | |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 431 | # Tests if a variable value in the RIC simulator contains the target string and and optional timeout |
| 432 | # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable contains |
| 433 | # the target or not. |
| 434 | # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds |
| 435 | # before setting pass or fail depending on if the variable value contains the target |
| 436 | # value or not. |
| 437 | # (Function for test scripts) |
| 438 | sim_contains_str() { |
| 439 | |
| 440 | if [ $# -eq 3 ] || [ $# -eq 4 ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 441 | host=$(__find_sim_host $1) |
| 442 | __var_test $1 "$host/counter/" $2 "contain_str" $3 $4 |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 443 | return 0 |
| 444 | else |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 445 | __print_err "needs three or four args: <ric-id> <sim-param> <target-value> [ timeout ]" |
| 446 | return 1 |
| 447 | fi |
| 448 | } |
| 449 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 450 | # Simulator API: Put a policy type in a ric |
| 451 | # args: <response-code> <ric-id> <policy-type-id> <policy-type-file> |
| 452 | # (Function for test scripts) |
| 453 | sim_put_policy_type() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 454 | __log_conf_start $@ |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 455 | if [ $# -ne 4 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 456 | __print_err "<response-code> <ric-id> <policy-type-id> <policy-type-file>" $@ |
| 457 | return 1 |
| 458 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 459 | host=$(__find_sim_host $2) |
| 460 | curlString="curl -X PUT -skw %{http_code} "$host"/policytype?id="$3" -H Content-Type:application/json --data-binary @"$4 |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 461 | __execute_curl_to_sim $1 "$curlString" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 462 | return $? |
| 463 | } |
| 464 | |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 465 | # Simulator API: Delete a policy type in a ric |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 466 | # <response-code> <ric-id> <policy-type-id> |
| 467 | # (Function for test scripts) |
| 468 | sim_delete_policy_type() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 469 | __log_conf_start $@ |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 470 | if [ $# -ne 3 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 471 | __print_err "<response-code> <ric-id> <policy_type_id>" $@ |
| 472 | return 1 |
| 473 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 474 | host=$(__find_sim_host $2) |
| 475 | curlString="curl -X DELETE -skw %{http_code} "$host"/policytype?id="$3 |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 476 | __execute_curl_to_sim $1 "$curlString" |
| 477 | return $? |
| 478 | } |
| 479 | |
| 480 | # Simulator API: Delete instances (and status), for one ric |
| 481 | # <response-code> <ric-id> |
| 482 | # (Function for test scripts) |
| 483 | sim_post_delete_instances() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 484 | __log_conf_start $@ |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 485 | if [ $# -ne 2 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 486 | __print_err "<response-code> <ric-id>" $@ |
| 487 | return 1 |
| 488 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 489 | host=$(__find_sim_host $2) |
| 490 | curlString="curl -X POST -skw %{http_code} "$host"/deleteinstances" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 491 | __execute_curl_to_sim $1 "$curlString" |
| 492 | return $? |
| 493 | } |
| 494 | |
| 495 | # Simulator API: Delete all (instances/types/statuses/settings), for one ric |
| 496 | # <response-code> <ric-id> |
| 497 | # (Function for test scripts) |
| 498 | sim_post_delete_all() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 499 | __log_conf_start $@ |
BjornMagnussonXA | a549157 | 2021-05-04 09:21:24 +0200 | [diff] [blame] | 500 | if [ $# -ne 2 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 501 | __print_err "<response-code> <numericic-id>" $@ |
| 502 | return 1 |
| 503 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 504 | host=$(__find_sim_host $2) |
| 505 | curlString="curl -X POST -skw %{http_code} "$host"/deleteall" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 506 | __execute_curl_to_sim $1 "$curlString" |
| 507 | return $? |
| 508 | } |
| 509 | |
| 510 | # Simulator API: Set (or reset) response code for next A1 message, for one ric |
| 511 | # <response-code> <ric-id> [<forced_response_code>] |
| 512 | # (Function for test scripts) |
| 513 | sim_post_forcedresponse() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 514 | __log_conf_start $@ |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 515 | if [ $# -ne 3 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 516 | __print_err "<response-code> <ric-id> <forced_response_code>" $@ |
| 517 | return 1 |
| 518 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 519 | host=$(__find_sim_host $2) |
| 520 | curlString="curl -X POST -skw %{http_code} "$host"/forceresponse" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 521 | if [ $# -eq 3 ]; then |
| 522 | curlString=$curlString"?code="$3 |
| 523 | fi |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 524 | __execute_curl_to_sim $1 "$curlString" |
| 525 | return $? |
| 526 | } |
| 527 | |
| 528 | # Simulator API: Set (or reset) A1 response delay, for one ric |
| 529 | # <response-code> <ric-id> [<delay-in-seconds>] |
| 530 | # (Function for test scripts) |
| 531 | sim_post_forcedelay() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 532 | __log_conf_start $@ |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 533 | if [ $# -ne 3 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 534 | __print_err "<response-code> <ric-id> [<delay-in-seconds>]" $@ |
| 535 | return 1 |
| 536 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 537 | host=$(__find_sim_host $2) |
| 538 | curlString="curl -X POST -skw %{http_code} $host/forcedelay" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 539 | if [ $# -eq 3 ]; then |
| 540 | curlString=$curlString"?delay="$3 |
| 541 | fi |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 542 | __execute_curl_to_sim $1 "$curlString" |
| 543 | return $? |
| 544 | } |