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 # arg: <docker-images-format-string> <file-to-append>
43 echo -e "$RAPP_CAT_DISPLAY_NAME\t$(docker images --format $1 $RAPP_CAT_IMAGE)" >> $2
46 # Scale kubernetes resources to zero
47 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
48 # This function is called for apps fully managed by the test script
49 __RC_kube_scale_zero() {
50 __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest RC
53 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
54 # This function is called for prestarted apps not managed by the test script.
55 __RC_kube_scale_zero_and_wait() {
56 __kube_scale_and_wait_all_resources $KUBE_NONRTRIC_NAMESPACE app nonrtric-rappcatalogueservice
57 __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest RC
60 # Delete all kube resouces for the app
61 # This function is called for apps managed by the test script.
62 __RC_kube_delete_all() {
63 __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest RC
67 # This function is called for apps managed by the test script.
68 # args: <log-dir> <file-prexix>
69 __RC_store_docker_logs() {
70 docker logs $RAPP_CAT_APP_NAME > $1$2_rc.log 2>&1
73 #######################################################
75 ## Access to RAPP Catalogue
76 # Host name may be changed if app started by kube
77 # Direct access from script
79 RC_HOST_NAME=$LOCALHOST_NAME
80 RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_PORT
81 # RC_ADAPTER used for switch between REST and DMAAP (only REST supported currently)
82 RC_ADAPTER_TYPE="REST"
86 ###########################
88 ###########################
90 # Set http as the protocol to use for all communication to the RAPP Catalogue
92 # (Function for test scripts)
93 use_rapp_catalogue_http() {
94 echo -e $BOLD"RAPP Catalogue protocol setting"$EBOLD
95 echo -e " Using $BOLD http $EBOLD towards the RAPP Catalogue"
97 RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_PORT
98 RC_ADAPTER_TYPE="REST"
103 # Set https as the protocol to use for all communication to the RAPP Catalogue
105 # (Function for test scripts)
106 use_rapp_catalogue_https() {
107 echo -e $BOLD"RAPP Catalogue protocol setting"$EBOLD
108 echo -e " Using $BOLD https $EBOLD towards the RAPP Catalogue"
110 RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_SECURE_PORT
111 RC_ADAPTER_TYPE="REST"
116 # Start the RAPP Catalogue container
118 # (Function for test scripts)
119 start_rapp_catalogue() {
121 echo -e $BOLD"Starting $RAPP_CAT_DISPLAY_NAME"$EBOLD
123 if [ $RUNMODE == "KUBE" ]; then
125 # Check if app shall be fully managed by the test script
126 __check_included_image "RC"
129 # Check if app shall only be used by the testscipt
130 __check_prestarted_image "RC"
133 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
134 echo -e $RED"The $RAPP_CAT_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
135 echo -e $RED"The $RAPP_CAT_APP_NAME will not be started"$ERED
138 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
139 echo -e $RED"The $RAPP_CAT_APP_NAME app is included both as managed and prestarted in this test script"$ERED
140 echo -e $RED"The $RAPP_CAT_APP_NAME will not be started"$ERED
144 if [ $retcode_p -eq 0 ]; then
145 echo -e " Using existing $RAPP_CAT_APP_NAME deployment and service"
146 echo " Setting $RAPP_CAT_APP_NAME replicas=1"
147 __kube_scale deployment $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
150 if [ $retcode_i -eq 0 ]; then
152 echo -e " Creating $RAPP_CAT_APP_NAME app and expose service"
154 #Check if nonrtric namespace exists, if not create it
155 __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
157 export RAPP_CAT_APP_NAME
158 export KUBE_NONRTRIC_NAMESPACE
159 export RAPP_CAT_IMAGE
160 export RAPP_CAT_INTERNAL_PORT
161 export RAPP_CAT_INTERNAL_SECURE_PORT
162 export RAPP_CAT_EXTERNAL_PORT
163 export RAPP_CAT_EXTERNAL_SECURE_PORT
166 input_yaml=$SIM_GROUP"/"$RAPP_CAT_COMPOSE_DIR"/"svc.yaml
167 output_yaml=$PWD/tmp/rac_svc.yaml
168 __kube_create_instance service $RAPP_CAT_APP_NAME $input_yaml $output_yaml
171 input_yaml=$SIM_GROUP"/"$RAPP_CAT_COMPOSE_DIR"/"app.yaml
172 output_yaml=$PWD/tmp/rac_app.yaml
173 __kube_create_instance app $RAPP_CAT_APP_NAME $input_yaml $output_yaml
176 echo " Retrieving host and ports for service..."
177 RC_HOST_NAME=$(__kube_get_service_host $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
179 RAPP_CAT_EXTERNAL_PORT=$(__kube_get_service_port $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE "http")
180 RAPP_CAT_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE "https")
182 echo " Host IP, http port, https port: $RC_HOST_NAME $RAPP_CAT_EXTERNAL_PORT $RAPP_CAT_EXTERNAL_SECURE_PORT"
183 if [ $RC_HTTPX == "http" ]; then
184 RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_PORT
186 RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_SECURE_PORT
189 __check_service_start $RAPP_CAT_APP_NAME $RC_PATH$RAPP_CAT_ALIVE_URL
191 # Update the curl adapter if set to rest, no change if type dmaap
192 if [ $RC_ADAPTER_TYPE == "REST" ]; then
196 __check_included_image 'RC'
197 if [ $? -eq 1 ]; then
198 echo -e $RED"The RAPP Catalogue app is not included as managed in this test script"$ERED
199 echo -e $RED"The RAPP Catalogue will not be started"$ERED
203 export RAPP_CAT_APP_NAME
204 export RAPP_CAT_INTERNAL_PORT
205 export RAPP_CAT_EXTERNAL_PORT
206 export RAPP_CAT_INTERNAL_SECURE_PORT
207 export RAPP_CAT_EXTERNAL_SECURE_PORT
208 export DOCKER_SIM_NWNAME
210 export RAPP_CAT_DISPLAY_NAME
212 __start_container $RAPP_CAT_COMPOSE_DIR "" NODOCKERARGS 1 $RAPP_CAT_APP_NAME
214 __check_service_start $RAPP_CAT_APP_NAME $RC_PATH$RAPP_CAT_ALIVE_URL
219 # Tests if a variable value in the RAPP Catalogue is equal to a target value and and optional timeout.
220 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
221 # equal to the target or not.
222 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
223 # before setting pass or fail depending on if the variable value becomes equal to the target
225 # (Function for test scripts)
227 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
228 #__var_test RC "$LOCALHOST_HTTP:$RC_EXTERNAL_PORT/" $1 "=" $2 $3
229 __var_test RC "$RC_PATH/" $1 "=" $2 $3
231 __print_err "Wrong args to ecs_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
236 ##########################################
237 ######### RAPP Catalogue API ##########
238 ##########################################
239 #Function prefix: rapp_cat_api
241 # API Test function: GET /services
242 # args: <response-code> [(<service-id> <version> <display-name> <description>)+ | EMPTY ]
243 # (Function for test scripts)
244 rapp_cat_api_get_services() {
247 if [ $# -lt 1 ]; then
248 __print_err "<response-code> [(<service-id> <version> <display-name> <description>)+ | EMPTY ]" $@
252 res="$(__do_curl_to_api RC GET $query)"
253 status=${res:${#res}-3}
255 if [ $status -ne $1 ]; then
256 __log_test_fail_status_code $1 $status
260 if [ $# -gt 1 ]; then
261 body=${res:0:${#res}-3}
265 if [ $# -eq 2 ]; then
268 for ((i=0; i<$(($#-1)); i=i+4)); do
269 if [ "$targetJson" != "[" ]; then
270 targetJson=$targetJson","
272 targetJson=$targetJson"{\"name\": \"${arr[$i]}\",\"version\": \"${arr[$i+1]}\",\"display_name\": \"${arr[$i+2]}\",\"description\": \"${arr[$i+3]}\",\"registrationDate\": \"????\"}"
274 targetJson=$targetJson"]"
276 echo " TARGET JSON: $targetJson" >> $HTTPLOG
277 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
279 if [ $res -ne 0 ]; then
289 # API Test function: PUT /services/{service-id}
290 # args: <response-code> <service-id> <version> <display-name> <description>
291 # (Function for test scripts)
292 rapp_cat_api_put_service() {
295 if [ $# -ne 5 ]; then
296 __print_err "<response-code> <service-id> <version> <display-name> <description>" $@
300 inputJson="{\"version\": \"$3\",\"display_name\": \"$4\",\"description\": \"$5\"}"
302 echo "$inputJson" > $file
304 res="$(__do_curl_to_api RC PUT $query $file)"
305 status=${res:${#res}-3}
307 if [ $status -ne $1 ]; then
308 __log_test_fail_status_code $1 $status
316 # API Test function: GET /services/{service-id}
317 # args: <response-code> <service-id>
318 # (Function for test scripts)
319 rapp_cat_api_get_service() {
322 if [ $# -lt 2 ] || [ $# -gt 5 ]; then
323 __print_err "<response-code> <service-id> <version> <display-name> <description>" $@
328 res="$(__do_curl_to_api RC GET $query)"
329 status=${res:${#res}-3}
331 if [ $status -ne $1 ]; then
332 __log_test_fail_status_code $1 $status
336 if [ $# -gt 2 ]; then
337 body=${res:0:${#res}-3}
338 targetJson="{\"name\": \"$2\",\"version\": \"$3\",\"display_name\": \"$4\",\"description\": \"$5\",\"registrationDate\": \"????\"}"
339 echo " TARGET JSON: $targetJson" >> $HTTPLOG
340 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
342 if [ $res -ne 0 ]; then
352 # API Test function: DELETE /services/{service-id}
353 # args: <response-code> <service-id>
354 # (Function for test scripts)
355 rapp_cat_api_delete_service() {
358 if [ $# -ne 2 ]; then
359 __print_err "<response-code> <service-id>" $@
364 res="$(__do_curl_to_api RC DELETE $query)"
365 status=${res:${#res}-3}
367 if [ $status -ne $1 ]; then
368 __log_test_fail_status_code $1 $status