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
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 managemnt functions test functions for RAPP Catalogue API
22 ################ Test engine functions ################
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
28 __check_and_create_image_var RC "RAPP_CAT_IMAGE" "RAPP_CAT_IMAGE_BASE" "RAPP_CAT_IMAGE_TAG" $1 "$RAPP_CAT_DISPLAY_NAME"
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'
37 __check_and_pull_image $1 "$c" $RAPP_CAT_APP_NAME RAPP_CAT_IMAGE
40 # Generate a string for each included image using the app display name and a docker images format string
41 # If a custom image repo is used then also the source image from the local repo is listed
42 # arg: <docker-images-format-string> <file-to-append>
44 echo -e "$RAPP_CAT_DISPLAY_NAME\t$(docker images --format $1 $RAPP_CAT_IMAGE)" >> $2
45 if [ ! -z "$RAPP_CAT_IMAGE_SOURCE" ]; then
46 echo -e "-- source image --\t$(docker images --format $1 $RAPP_CAT_IMAGE_SOURCE)" >> $2
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
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() {
60 __kube_scale_and_wait_all_resources $KUBE_NONRTRIC_NAMESPACE app nonrtric-rappcatalogueservice
61 __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest RC
64 # Delete all kube resouces for the app
65 # This function is called for apps managed by the test script.
66 __RC_kube_delete_all() {
67 __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest RC
71 # This function is called for apps managed by the test script.
72 # args: <log-dir> <file-prexix>
73 __RC_store_docker_logs() {
74 docker logs $RAPP_CAT_APP_NAME > $1$2_rc.log 2>&1
77 #######################################################
79 ## Access to RAPP Catalogue
80 # Host name may be changed if app started by kube
81 # Direct access from script
83 RC_HOST_NAME=$LOCALHOST_NAME
84 RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_PORT
85 # RC_ADAPTER used for switch between REST and DMAAP (only REST supported currently)
86 RC_ADAPTER_TYPE="REST"
90 ###########################
92 ###########################
94 # Set http as the protocol to use for all communication to the RAPP Catalogue
96 # (Function for test scripts)
97 use_rapp_catalogue_http() {
98 echo -e $BOLD"RAPP Catalogue protocol setting"$EBOLD
99 echo -e " Using $BOLD http $EBOLD towards the RAPP Catalogue"
101 RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_PORT
102 RC_ADAPTER_TYPE="REST"
107 # Set https as the protocol to use for all communication to the RAPP Catalogue
109 # (Function for test scripts)
110 use_rapp_catalogue_https() {
111 echo -e $BOLD"RAPP Catalogue protocol setting"$EBOLD
112 echo -e " Using $BOLD https $EBOLD towards the RAPP Catalogue"
114 RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_SECURE_PORT
115 RC_ADAPTER_TYPE="REST"
120 # Start the RAPP Catalogue container
122 # (Function for test scripts)
123 start_rapp_catalogue() {
125 echo -e $BOLD"Starting $RAPP_CAT_DISPLAY_NAME"$EBOLD
127 if [ $RUNMODE == "KUBE" ]; then
129 # Check if app shall be fully managed by the test script
130 __check_included_image "RC"
133 # Check if app shall only be used by the testscipt
134 __check_prestarted_image "RC"
137 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
138 echo -e $RED"The $RAPP_CAT_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
139 echo -e $RED"The $RAPP_CAT_APP_NAME will not be started"$ERED
142 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
143 echo -e $RED"The $RAPP_CAT_APP_NAME app is included both as managed and prestarted in this test script"$ERED
144 echo -e $RED"The $RAPP_CAT_APP_NAME will not be started"$ERED
148 if [ $retcode_p -eq 0 ]; then
149 echo -e " Using existing $RAPP_CAT_APP_NAME deployment and service"
150 echo " Setting $RAPP_CAT_APP_NAME replicas=1"
151 __kube_scale deployment $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
154 if [ $retcode_i -eq 0 ]; then
156 echo -e " Creating $RAPP_CAT_APP_NAME app and expose service"
158 #Check if nonrtric namespace exists, if not create it
159 __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
161 export RAPP_CAT_APP_NAME
162 export KUBE_NONRTRIC_NAMESPACE
163 export RAPP_CAT_IMAGE
164 export RAPP_CAT_INTERNAL_PORT
165 export RAPP_CAT_INTERNAL_SECURE_PORT
166 export RAPP_CAT_EXTERNAL_PORT
167 export RAPP_CAT_EXTERNAL_SECURE_PORT
170 input_yaml=$SIM_GROUP"/"$RAPP_CAT_COMPOSE_DIR"/"svc.yaml
171 output_yaml=$PWD/tmp/rac_svc.yaml
172 __kube_create_instance service $RAPP_CAT_APP_NAME $input_yaml $output_yaml
175 input_yaml=$SIM_GROUP"/"$RAPP_CAT_COMPOSE_DIR"/"app.yaml
176 output_yaml=$PWD/tmp/rac_app.yaml
177 __kube_create_instance app $RAPP_CAT_APP_NAME $input_yaml $output_yaml
180 echo " Retrieving host and ports for service..."
181 RC_HOST_NAME=$(__kube_get_service_host $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
183 RAPP_CAT_EXTERNAL_PORT=$(__kube_get_service_port $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE "http")
184 RAPP_CAT_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE "https")
186 echo " Host IP, http port, https port: $RC_HOST_NAME $RAPP_CAT_EXTERNAL_PORT $RAPP_CAT_EXTERNAL_SECURE_PORT"
187 if [ $RC_HTTPX == "http" ]; then
188 RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_PORT
190 RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_SECURE_PORT
193 __check_service_start $RAPP_CAT_APP_NAME $RC_PATH$RAPP_CAT_ALIVE_URL
195 # Update the curl adapter if set to rest, no change if type dmaap
196 if [ $RC_ADAPTER_TYPE == "REST" ]; then
200 __check_included_image 'RC'
201 if [ $? -eq 1 ]; then
202 echo -e $RED"The RAPP Catalogue app is not included as managed in this test script"$ERED
203 echo -e $RED"The RAPP Catalogue will not be started"$ERED
207 export RAPP_CAT_APP_NAME
208 export RAPP_CAT_INTERNAL_PORT
209 export RAPP_CAT_EXTERNAL_PORT
210 export RAPP_CAT_INTERNAL_SECURE_PORT
211 export RAPP_CAT_EXTERNAL_SECURE_PORT
212 export DOCKER_SIM_NWNAME
214 export RAPP_CAT_DISPLAY_NAME
216 __start_container $RAPP_CAT_COMPOSE_DIR "" NODOCKERARGS 1 $RAPP_CAT_APP_NAME
218 __check_service_start $RAPP_CAT_APP_NAME $RC_PATH$RAPP_CAT_ALIVE_URL
223 # Tests if a variable value in the RAPP Catalogue is equal to a target value and and optional timeout.
224 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
225 # equal to the target or not.
226 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
227 # before setting pass or fail depending on if the variable value becomes equal to the target
229 # (Function for test scripts)
231 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
232 #__var_test RC "$LOCALHOST_HTTP:$RC_EXTERNAL_PORT/" $1 "=" $2 $3
233 __var_test RC "$RC_PATH/" $1 "=" $2 $3
235 __print_err "Wrong args to ecs_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
240 ##########################################
241 ######### RAPP Catalogue API ##########
242 ##########################################
243 #Function prefix: rapp_cat_api
245 # API Test function: GET /services
246 # args: <response-code> [(<service-id> <version> <display-name> <description>)+ | EMPTY ]
247 # (Function for test scripts)
248 rapp_cat_api_get_services() {
251 if [ $# -lt 1 ]; then
252 __print_err "<response-code> [(<service-id> <version> <display-name> <description>)+ | EMPTY ]" $@
256 res="$(__do_curl_to_api RC GET $query)"
257 status=${res:${#res}-3}
259 if [ $status -ne $1 ]; then
260 __log_test_fail_status_code $1 $status
264 if [ $# -gt 1 ]; then
265 body=${res:0:${#res}-3}
269 if [ $# -eq 2 ]; then
272 for ((i=0; i<$(($#-1)); i=i+4)); do
273 if [ "$targetJson" != "[" ]; then
274 targetJson=$targetJson","
276 targetJson=$targetJson"{\"name\": \"${arr[$i]}\",\"version\": \"${arr[$i+1]}\",\"display_name\": \"${arr[$i+2]}\",\"description\": \"${arr[$i+3]}\",\"registrationDate\": \"????\"}"
278 targetJson=$targetJson"]"
280 echo " TARGET JSON: $targetJson" >> $HTTPLOG
281 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
283 if [ $res -ne 0 ]; then
293 # API Test function: PUT /services/{service-id}
294 # args: <response-code> <service-id> <version> <display-name> <description>
295 # (Function for test scripts)
296 rapp_cat_api_put_service() {
299 if [ $# -ne 5 ]; then
300 __print_err "<response-code> <service-id> <version> <display-name> <description>" $@
304 inputJson="{\"version\": \"$3\",\"display_name\": \"$4\",\"description\": \"$5\"}"
306 echo "$inputJson" > $file
308 res="$(__do_curl_to_api RC PUT $query $file)"
309 status=${res:${#res}-3}
311 if [ $status -ne $1 ]; then
312 __log_test_fail_status_code $1 $status
320 # API Test function: GET /services/{service-id}
321 # args: <response-code> <service-id>
322 # (Function for test scripts)
323 rapp_cat_api_get_service() {
326 if [ $# -lt 2 ] || [ $# -gt 5 ]; then
327 __print_err "<response-code> <service-id> <version> <display-name> <description>" $@
332 res="$(__do_curl_to_api RC GET $query)"
333 status=${res:${#res}-3}
335 if [ $status -ne $1 ]; then
336 __log_test_fail_status_code $1 $status
340 if [ $# -gt 2 ]; then
341 body=${res:0:${#res}-3}
342 targetJson="{\"name\": \"$2\",\"version\": \"$3\",\"display_name\": \"$4\",\"description\": \"$5\",\"registrationDate\": \"????\"}"
343 echo " TARGET JSON: $targetJson" >> $HTTPLOG
344 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
346 if [ $res -ne 0 ]; then
356 # API Test function: DELETE /services/{service-id}
357 # args: <response-code> <service-id>
358 # (Function for test scripts)
359 rapp_cat_api_delete_service() {
362 if [ $# -ne 2 ]; then
363 __print_err "<response-code> <service-id>" $@
368 res="$(__do_curl_to_api RC DELETE $query)"
369 status=${res:${#res}-3}
371 if [ $status -ne $1 ]; then
372 __log_test_fail_status_code $1 $status