blob: 7c91705187faf38bdb298067bfcc0cbb0108cc06 [file] [log] [blame]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001#!/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
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010020# This is a script that contains management and test functions for Policy Agent
BjornMagnussonXA80a92002020-03-19 14:31:06 +010021
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010022################ 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__PA_imagesetup() {
28 __check_and_create_image_var PA "POLICY_AGENT_IMAGE" "POLICY_AGENT_IMAGE_BASE" "POLICY_AGENT_IMAGE_TAG" $1 "$POLICY_AGENT_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__PA_imagepull() {
BjornMagnussonXA483ee332021-04-08 01:35:24 +020037 __check_and_pull_image $1 "$POLICY_AGENT_DISPLAY_NAME" $POLICY_AGENT_APP_NAME POLICY_AGENT_IMAGE
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010038}
39
40# Build image (only for simulator or interfaces stubs owned by the test environment)
41# arg: <image-tag-suffix> (selects staging, snapshot, release etc)
42# <image-tag-suffix> is present only for images with staging, snapshot,release tags
43__PA_imagebuild() {
44 echo -e $RED" Image for app PA shall never be built"$ERED
45}
46
47# Generate a string for each included image using the app display name and a docker images format string
BjornMagnussonXA483ee332021-04-08 01:35:24 +020048# If a custom image repo is used then also the source image from the local repo is listed
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010049# arg: <docker-images-format-string> <file-to-append>
50__PA_image_data() {
51 echo -e "$POLICY_AGENT_DISPLAY_NAME\t$(docker images --format $1 $POLICY_AGENT_IMAGE)" >> $2
BjornMagnussonXA483ee332021-04-08 01:35:24 +020052 if [ ! -z "$POLICY_AGENT_IMAGE_SOURCE" ]; then
53 echo -e "-- source image --\t$(docker images --format $1 $POLICY_AGENT_IMAGE_SOURCE)" >> $2
54 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010055}
56
57# Scale kubernetes resources to zero
58# All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
59# This function is called for apps fully managed by the test script
60__PA_kube_scale_zero() {
61 __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest PA
62}
63
64# Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
65# This function is called for prestarted apps not managed by the test script.
66__PA_kube_scale_zero_and_wait() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +010067 __kube_scale_and_wait_all_resources $KUBE_NONRTRIC_NAMESPACE app "$KUBE_NONRTRIC_NAMESPACE"-policymanagementservice
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010068}
69
70# Delete all kube resouces for the app
71# This function is called for apps managed by the test script.
72__PA_kube_delete_all() {
73 __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest PA
74}
75
76# Store docker logs
77# This function is called for apps managed by the test script.
78# args: <log-dir> <file-prexix>
79__PA_store_docker_logs() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +010080 if [ $RUNMODE == "KUBE" ]; then
81 kubectl logs -l "autotest=PA" -n $KUBE_NONRTRIC_NAMESPACE --tail=-1 > $1$2_policy-agent.log 2>&1
82 else
83 docker logs $POLICY_AGENT_APP_NAME > $1$2_policy-agent.log 2>&1
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__PA_initial_setup() {
91 use_agent_rest_http
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010092}
93
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +010094# 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__PA_statisics_setup() {
99 if [ $RUNMODE == "KUBE" ]; then
100 echo "PA $POLICY_AGENT_APP_NAME $KUBE_NONRTRIC_NAMESPACE"
101 else
102 echo "PA $POLICY_AGENT_APP_NAME"
103 fi
104}
105
106
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100107#######################################################
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100108
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100109###########################
110### Policy Agents functions
111###########################
112
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100113# Set http as the protocol to use for all communication to the Policy Agent
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100114# args: -
115# (Function for test scripts)
116use_agent_rest_http() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100117 __agent_set_protocoll "http" $POLICY_AGENT_INTERNAL_PORT $POLICY_AGENT_EXTERNAL_PORT
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100118}
119
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100120# Set https as the protocol to use for all communication to the Policy Agent
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100121# args: -
122# (Function for test scripts)
123use_agent_rest_https() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100124 __agent_set_protocoll "https" $POLICY_AGENT_INTERNAL_SECURE_PORT $POLICY_AGENT_EXTERNAL_SECURE_PORT
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100125}
126
127# All calls to the agent will be directed to the agent dmaap interface over http from now on
128# args: -
129# (Function for test scripts)
130use_agent_dmaap_http() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100131 echo -e $BOLD"$POLICY_AGENT_DISPLAY_NAME dmaap protocol setting"$EBOLD
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100132 echo -e " Using $BOLD http $EBOLD and $BOLD DMAAP $EBOLD towards the agent"
133 PA_ADAPTER_TYPE="MR-HTTP"
134 echo ""
135}
136
137# All calls to the agent will be directed to the agent dmaap interface over https from now on
138# args: -
139# (Function for test scripts)
140use_agent_dmaap_https() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100141 echo -e $BOLD"$POLICY_AGENT_DISPLAY_NAME dmaap protocol setting"$EBOLD
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100142 echo -e " Using $BOLD https $EBOLD and $BOLD DMAAP $EBOLD towards the agent"
143 echo -e $YELLOW" Setting http instead of https - MR only uses http"$EYELLOW
144 PA_ADAPTER_TYPE="MR-HTTPS"
145 echo ""
146}
147
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100148# Setup paths to svc/container for internal and external access
149# args: <protocol> <internal-port> <external-port>
150__agent_set_protocoll() {
151 echo -e $BOLD"$POLICY_AGENT_DISPLAY_NAME protocol setting"$EBOLD
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100152 echo -e " Using $BOLD $1 $EBOLD towards $POLICY_AGENT_DISPLAY_NAME"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100153
154 ## Access to Dmaap adapter
155
156 PA_SERVICE_PATH=$1"://"$POLICY_AGENT_APP_NAME":"$2 # docker access, container->container and script->container via proxy
157 if [ $RUNMODE == "KUBE" ]; then
158 PA_SERVICE_PATH=$1"://"$POLICY_AGENT_APP_NAME.$KUBE_NONRTRIC_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
159 fi
160
161 # PA_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
162 PA_ADAPTER_TYPE="REST"
163 PA_ADAPTER=$PA_SERVICE_PATH
164
165 echo ""
166}
167
168# Make curl retries towards the agent for http response codes set in this env var, space separated list of codes
169AGENT_RETRY_CODES=""
170
171#Save first worker node the pod is started on
172__PA_WORKER_NODE=""
173
174# Export env vars for config files, docker compose and kube resources
175# args: PROXY|NOPROXY
176__export_agent_vars() {
177
178 export POLICY_AGENT_APP_NAME
179 export POLICY_AGENT_APP_NAME_ALIAS
180 export POLICY_AGENT_DISPLAY_NAME
181
182 export KUBE_NONRTRIC_NAMESPACE
183 export POLICY_AGENT_IMAGE
184 export POLICY_AGENT_INTERNAL_PORT
185 export POLICY_AGENT_INTERNAL_SECURE_PORT
186 export POLICY_AGENT_EXTERNAL_PORT
187 export POLICY_AGENT_EXTERNAL_SECURE_PORT
188 export POLICY_AGENT_CONFIG_MOUNT_PATH
189 export POLICY_AGENT_DATA_MOUNT_PATH
190 export POLICY_AGENT_CONFIG_CONFIGMAP_NAME=$POLICY_AGENT_APP_NAME"-config"
191 export POLICY_AGENT_DATA_CONFIGMAP_NAME=$POLICY_AGENT_APP_NAME"-data"
192 export POLICY_AGENT_PKG_NAME
193 export CONSUL_HOST
194 export CONSUL_INTERNAL_PORT
195 export CONFIG_BINDING_SERVICE
196 export POLICY_AGENT_CONFIG_KEY
197 export DOCKER_SIM_NWNAME
198 export POLICY_AGENT_HOST_MNT_DIR
199 export POLICY_AGENT_CONFIG_FILE
200
201 export POLICY_AGENT_DATA_PV_NAME=$POLICY_AGENT_APP_NAME"-pv"
202 export POLICY_AGENT_DATA_PVC_NAME=$POLICY_AGENT_APP_NAME"-pvc"
203 ##Create a unique path for the pv each time to prevent a previous volume to be reused
204 export POLICY_AGENT_PV_PATH="padata-"$(date +%s)
205 export POLICY_AGENT_CONTAINER_MNT_DIR
206
207 if [ $1 == "PROXY" ]; then
208 export AGENT_HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_CONFIG_PORT #Set if proxy is started
209 export AGENT_HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_CONFIG_HOST_NAME #Set if proxy is started
210 if [ $AGENT_HTTP_PROXY_CONFIG_PORT -eq 0 ] || [ -z "$AGENT_HTTP_PROXY_CONFIG_HOST_NAME" ]; then
211 echo -e $YELLOW" Warning: HTTP PROXY will not be configured, proxy app not started"$EYELLOW
212 else
213 echo " Configured with http proxy"
214 fi
215 else
216 export AGENT_HTTP_PROXY_CONFIG_PORT=0
217 export AGENT_HTTP_PROXY_CONFIG_HOST_NAME=""
218 echo " Configured without http proxy"
219 fi
220}
221
222
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100223# Start the policy agent
BjornMagnussonXAc963b732021-01-20 14:24:13 +0100224# args: (docker) PROXY|NOPROXY <config-file>
225# args: (kube) PROXY|NOPROXY <config-file> [ <data-file>]
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100226# (Function for test scripts)
227start_policy_agent() {
228 echo -e $BOLD"Starting $POLICY_AGENT_DISPLAY_NAME"$EBOLD
229
230 if [ $RUNMODE == "KUBE" ]; then
231
232 # Check if app shall be fully managed by the test script
233 __check_included_image "PA"
234 retcode_i=$?
235
236 # Check if app shall only be used by the testscipt
237 __check_prestarted_image "PA"
238 retcode_p=$?
239
240 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
241 echo -e $RED"The $POLICY_AGENT_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
242 echo -e $RED"The $POLICY_AGENT_APP_NAME will not be started"$ERED
243 exit
244 fi
245 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
246 echo -e $RED"The $POLICY_AGENT_APP_NAME app is included both as managed and prestarted in this test script"$ERED
247 echo -e $RED"The $POLICY_AGENT_APP_NAME will not be started"$ERED
248 exit
249 fi
250
251 if [ $retcode_p -eq 0 ]; then
252 echo -e " Using existing $POLICY_AGENT_APP_NAME deployment and service"
253 echo " Setting $POLICY_AGENT_APP_NAME replicas=1"
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +0200254 res_type=$(__kube_get_resource_type $POLICY_AGENT_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
255 __kube_scale $res_type $POLICY_AGENT_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100256 fi
257
258 if [ $retcode_i -eq 0 ]; then
259
260 echo -e " Creating $POLICY_AGENT_APP_NAME app and expose service"
261
262 #Check if nonrtric namespace exists, if not create it
263 __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
264
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100265 __export_agent_vars $1
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100266
267 # Create config map for config
268 configfile=$PWD/tmp/$POLICY_AGENT_CONFIG_FILE
269 cp $2 $configfile
270 output_yaml=$PWD/tmp/pa_cfc.yaml
271 __kube_create_configmap $POLICY_AGENT_CONFIG_CONFIGMAP_NAME $KUBE_NONRTRIC_NAMESPACE autotest PA $configfile $output_yaml
272
273 # Create config map for data
274 data_json=$PWD/tmp/$POLICY_AGENT_DATA_FILE
275 if [ $# -lt 3 ]; then
276 #create empty dummy file
277 echo "{}" > $data_json
278 else
279 cp $3 $data_json
280 fi
281 output_yaml=$PWD/tmp/pa_cfd.yaml
282 __kube_create_configmap $POLICY_AGENT_DATA_CONFIGMAP_NAME $KUBE_NONRTRIC_NAMESPACE autotest PA $data_json $output_yaml
283
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200284 ## Create pv
285 input_yaml=$SIM_GROUP"/"$POLICY_AGENT_COMPOSE_DIR"/"pv.yaml
286 output_yaml=$PWD/tmp/pa_pv.yaml
287 __kube_create_instance pv $POLICY_AGENT_APP_NAME $input_yaml $output_yaml
288
289 ## Create pvc
290 input_yaml=$SIM_GROUP"/"$POLICY_AGENT_COMPOSE_DIR"/"pvc.yaml
291 output_yaml=$PWD/tmp/pa_pvc.yaml
292 __kube_create_instance pvc $POLICY_AGENT_APP_NAME $input_yaml $output_yaml
293
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100294 # Create service
295 input_yaml=$SIM_GROUP"/"$POLICY_AGENT_COMPOSE_DIR"/"svc.yaml
296 output_yaml=$PWD/tmp/pa_svc.yaml
297 __kube_create_instance service $POLICY_AGENT_APP_NAME $input_yaml $output_yaml
298
299 # Create app
300 input_yaml=$SIM_GROUP"/"$POLICY_AGENT_COMPOSE_DIR"/"app.yaml
301 output_yaml=$PWD/tmp/pa_app.yaml
302 __kube_create_instance app $POLICY_AGENT_APP_NAME $input_yaml $output_yaml
303
304 fi
305
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200306 # Keep the initial worker node in case the pod need to be "restarted" - must be made to the same node due to a volume mounted on the host
BjornMagnussonXAa5491572021-05-04 09:21:24 +0200307 if [ $retcode_i -eq 0 ]; then
308 __PA_WORKER_NODE=$(kubectl get pod -l "autotest=PA" -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.items[*].spec.nodeName}')
309 if [ -z "$__PA_WORKER_NODE" ]; then
310 echo -e $YELLOW" Cannot find worker node for pod for $POLICY_AGENT_APP_NAME, persistency may not work"$EYELLOW
311 fi
312 else
313 echo -e $YELLOW" Persistency may not work for app $POLICY_AGENT_APP_NAME in multi-worker node config when running it as a prestarted app"$EYELLOW
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200314 fi
315
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100316 __check_service_start $POLICY_AGENT_APP_NAME $PA_SERVICE_PATH$POLICY_AGENT_ALIVE_URL
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100317
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100318 else
319 __check_included_image 'PA'
320 if [ $? -eq 1 ]; then
321 echo -e $RED"The Policy Agent app is not included in this test script"$ERED
322 echo -e $RED"The Policy Agent will not be started"$ERED
323 exit
324 fi
325
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200326 curdir=$PWD
327 cd $SIM_GROUP
328 cd policy_agent
329 cd $POLICY_AGENT_HOST_MNT_DIR
330 #cd ..
331 if [ -d db ]; then
332 if [ "$(ls -A $DIR)" ]; then
333 echo -e $BOLD" Cleaning files in mounted dir: $PWD/db"$EBOLD
334 rm -rf db/* &> /dev/null
335 if [ $? -ne 0 ]; then
336 echo -e $RED" Cannot remove database files in: $PWD"$ERED
337 exit 1
338 fi
339 fi
340 else
341 echo " No files in mounted dir or dir does not exists"
342 fi
343 cd $curdir
344
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100345 __export_agent_vars $1
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100346
347 dest_file=$SIM_GROUP/$POLICY_AGENT_COMPOSE_DIR/$POLICY_AGENT_HOST_MNT_DIR/application.yaml
348
349 envsubst < $2 > $dest_file
350
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100351 __start_container $POLICY_AGENT_COMPOSE_DIR "" NODOCKERARGS 1 $POLICY_AGENT_APP_NAME
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100352
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100353 __check_service_start $POLICY_AGENT_APP_NAME $PA_SERVICE_PATH$POLICY_AGENT_ALIVE_URL
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100354 fi
355 echo ""
356 return 0
357}
358
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200359# Stop the policy agent
360# args: -
361# args: -
362# (Function for test scripts)
363stop_policy_agent() {
364 echo -e $BOLD"Stopping $POLICY_AGENT_DISPLAY_NAME"$EBOLD
365
366 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXAa5491572021-05-04 09:21:24 +0200367
368 __check_prestarted_image "PA"
369 if [ $? -eq 0 ]; then
370 echo -e $YELLOW" Persistency may not work for app $POLICY_AGENT_APP_NAME in multi-worker node config when running it as a prestarted app"$EYELLOW
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +0200371 res_type=$(__kube_get_resource_type $POLICY_AGENT_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
372 __kube_scale $res_type $POLICY_AGENT_APP_NAME $KUBE_NONRTRIC_NAMESPACE 0
BjornMagnussonXAa5491572021-05-04 09:21:24 +0200373 return 0
374 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200375 __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest PA
376 echo " Deleting the replica set - a new will be started when the app is started"
377 tmp=$(kubectl delete rs -n $KUBE_NONRTRIC_NAMESPACE -l "autotest=PA")
378 if [ $? -ne 0 ]; then
379 echo -e $RED" Could not delete replica set "$RED
380 ((RES_CONF_FAIL++))
381 return 1
382 fi
383 else
384 docker stop $POLICY_AGENT_APP_NAME &> ./tmp/.dockererr
385 if [ $? -ne 0 ]; then
386 __print_err "Could not stop $POLICY_AGENT_APP_NAME" $@
387 cat ./tmp/.dockererr
388 ((RES_CONF_FAIL++))
389 return 1
390 fi
391 fi
392 echo -e $BOLD$GREEN"Stopped"$EGREEN$EBOLD
393 echo ""
394 return 0
395}
396
397# Start a previously stopped policy agent
398# args: -
399# (Function for test scripts)
400start_stopped_policy_agent() {
401 echo -e $BOLD"Starting (the previously stopped) $POLICY_AGENT_DISPLAY_NAME"$EBOLD
402
403 if [ $RUNMODE == "KUBE" ]; then
404
BjornMagnussonXAa5491572021-05-04 09:21:24 +0200405 __check_prestarted_image "PA"
406 if [ $? -eq 0 ]; then
407 echo -e $YELLOW" Persistency may not work for app $POLICY_AGENT_APP_NAME in multi-worker node config when running it as a prestarted app"$EYELLOW
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +0200408 res_type=$(__kube_get_resource_type $POLICY_AGENT_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
409 __kube_scale $res_type $POLICY_AGENT_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100410 __check_service_start $POLICY_AGENT_APP_NAME $PA_SERVICE_PATH$POLICY_AGENT_ALIVE_URL
BjornMagnussonXAa5491572021-05-04 09:21:24 +0200411 return 0
412 fi
413
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200414 # Tie the PMS to the same worker node it was initially started on
415 # A PVC of type hostPath is mounted to PMS, for persistent storage, so the PMS must always be on the node which mounted the volume
416 if [ -z "$__PA_WORKER_NODE" ]; then
417 echo -e $RED" No initial worker node found for pod "$RED
418 ((RES_CONF_FAIL++))
419 return 1
420 else
421 echo -e $BOLD" Setting nodeSelector kubernetes.io/hostname=$__PA_WORKER_NODE to deployment for $POLICY_AGENT_APP_NAME. Pod will always run on this worker node: $__PA_WORKER_NODE"$BOLD
422 echo -e $BOLD" The mounted volume is mounted as hostPath and only available on that worker node."$BOLD
423 tmp=$(kubectl patch deployment $POLICY_AGENT_APP_NAME -n $KUBE_NONRTRIC_NAMESPACE --patch '{"spec": {"template": {"spec": {"nodeSelector": {"kubernetes.io/hostname": "'$__PA_WORKER_NODE'"}}}}}')
424 if [ $? -ne 0 ]; then
425 echo -e $YELLOW" Cannot set nodeSelector to deployment for $POLICY_AGENT_APP_NAME, persistency may not work"$EYELLOW
426 fi
427 __kube_scale deployment $POLICY_AGENT_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
428 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200429 else
430 docker start $POLICY_AGENT_APP_NAME &> ./tmp/.dockererr
431 if [ $? -ne 0 ]; then
432 __print_err "Could not start (the stopped) $POLICY_AGENT_APP_NAME" $@
433 cat ./tmp/.dockererr
434 ((RES_CONF_FAIL++))
435 return 1
436 fi
437 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100438 __check_service_start $POLICY_AGENT_APP_NAME $PA_SERVICE_PATH$POLICY_AGENT_ALIVE_URL
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200439 if [ $? -ne 0 ]; then
440 return 1
441 fi
442 echo ""
443 return 0
444}
445
446
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100447# Function to perpare the consul configuration according to the current simulator configuration
448# args: SDNC|NOSDNC <output-file>
449# (Function for test scripts)
450prepare_consul_config() {
451 echo -e $BOLD"Prepare Consul config"$EBOLD
452
453 echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
454
455 if [ $# != 2 ]; then
456 ((RES_CONF_FAIL++))
457 __print_err "need two args, SDNC|NOSDNC <output-file>" $@
458 exit 1
459 fi
460
461 if [ $1 == "SDNC" ]; then
462 echo -e " Config$BOLD including SDNC$EBOLD configuration"
463 elif [ $1 == "NOSDNC" ]; then
464 echo -e " Config$BOLD excluding SDNC$EBOLD configuration"
465 else
466 ((RES_CONF_FAIL++))
467 __print_err "need two args, SDNC|NOSDNC <output-file>" $@
468 exit 1
469 fi
470
471 config_json="\n {"
472 if [ $1 == "SDNC" ]; then
473 config_json=$config_json"\n \"controller\": ["
474 config_json=$config_json"\n {"
475 config_json=$config_json"\n \"name\": \"$SDNC_APP_NAME\","
476 config_json=$config_json"\n \"baseUrl\": \"$SDNC_SERVICE_PATH\","
477 config_json=$config_json"\n \"userName\": \"$SDNC_USER\","
478 config_json=$config_json"\n \"password\": \"$SDNC_PWD\""
479 config_json=$config_json"\n }"
480 config_json=$config_json"\n ],"
481 fi
482
483 config_json=$config_json"\n \"streams_publishes\": {"
484 config_json=$config_json"\n \"dmaap_publisher\": {"
485 config_json=$config_json"\n \"type\": \"message-router\","
486 config_json=$config_json"\n \"dmaap_info\": {"
487 config_json=$config_json"\n \"topic_url\": \"$MR_SERVICE_PATH$MR_WRITE_URL\""
488 config_json=$config_json"\n }"
489 config_json=$config_json"\n }"
490 config_json=$config_json"\n },"
491 config_json=$config_json"\n \"streams_subscribes\": {"
492 config_json=$config_json"\n \"dmaap_subscriber\": {"
493 config_json=$config_json"\n \"type\": \"message-router\","
494 config_json=$config_json"\n \"dmaap_info\": {"
495 config_json=$config_json"\n \"topic_url\": \"$MR_SERVICE_PATH$MR_READ_URL\""
496 config_json=$config_json"\n }"
497 config_json=$config_json"\n }"
498 config_json=$config_json"\n },"
499
500 config_json=$config_json"\n \"ric\": ["
501
502 if [ $RUNMODE == "KUBE" ]; then
503 result=$(kubectl get pods -n $KUBE_A1SIM_NAMESPACE -o jsonpath='{.items[?(@.metadata.labels.autotest=="RICSIM")].metadata.name}')
504 rics=""
505 ric_cntr=0
506 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
507 for im in $result; do
508 if [[ $im != *"-0" ]]; then
509 ric_subdomain=$(kubectl get pod $im -n $KUBE_A1SIM_NAMESPACE -o jsonpath='{.spec.subdomain}')
510 rics=$rics" "$im"."$ric_subdomain"."$KUBE_A1SIM_NAMESPACE
511 let ric_cntr=ric_cntr+1
512 fi
513 done
514 fi
515 if [ $ric_cntr -eq 0 ]; then
516 echo $YELLOW"Warning: No rics found for the configuration"$EYELLOW
517 fi
518 else
519 rics=$(docker ps --filter "name=$RIC_SIM_PREFIX" --filter "network=$DOCKER_SIM_NWNAME" --filter "status=running" --format {{.Names}})
520 if [ $? -ne 0 ] || [ -z "$rics" ]; then
521 echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
522 ((RES_CONF_FAIL++))
523 return 1
524 fi
525 fi
526 cntr=0
527 for ric in $rics; do
528 if [ $cntr -gt 0 ]; then
529 config_json=$config_json"\n ,"
530 fi
531 config_json=$config_json"\n {"
532 if [ $RUNMODE == "KUBE" ]; then
533 ric_id=${ric%.*.*} #extract pod id from full hosthame
534 ric_id=$(echo "$ric_id" | tr '-' '_')
535 else
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100536 if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
537 ric_id=$ric
538 else
539 ric_id=$(echo "$ric" | tr '-' '_') #ric id still needs underscore as it is different from the container name
540 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100541 fi
542 echo " Found a1 sim: "$ric_id
543 config_json=$config_json"\n \"name\": \"$ric_id\","
544 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://$ric:$RIC_SIM_PORT\","
545 if [ $1 == "SDNC" ]; then
546 config_json=$config_json"\n \"controller\": \"$SDNC_APP_NAME\","
547 fi
548 config_json=$config_json"\n \"managedElementIds\": ["
549 config_json=$config_json"\n \"me1_$ric_id\","
550 config_json=$config_json"\n \"me2_$ric_id\""
551 config_json=$config_json"\n ]"
552 config_json=$config_json"\n }"
553 let cntr=cntr+1
554 done
555
556 config_json=$config_json"\n ]"
557 config_json=$config_json"\n}"
558
559 if [ $RUNMODE == "KUBE" ]; then
560 config_json="{\"config\":"$config_json"}"
561 fi
562
563 printf "$config_json">$2
564
565 echo ""
566}
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200567
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100568# Load the the appl config for the agent into a config map
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100569agent_load_config() {
570 echo -e $BOLD"Agent - load config from "$EBOLD$1
571 data_json=$PWD/tmp/$POLICY_AGENT_DATA_FILE
572 cp $1 $data_json
573 output_yaml=$PWD/tmp/pa_cfd.yaml
574 __kube_create_configmap $POLICY_AGENT_APP_NAME"-data" $KUBE_NONRTRIC_NAMESPACE autotest PA $data_json $output_yaml
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100575 echo ""
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100576}
577
578
579# Turn on debug level tracing in the agent
580# args: -
581# (Function for test scripts)
582set_agent_debug() {
583 echo -e $BOLD"Setting agent debug logging"$EBOLD
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100584 curlString="$PA_SERVICE_PATH$POLICY_AGENT_ACTUATOR -X POST -H Content-Type:application/json -d {\"configuredLevel\":\"debug\"}"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100585 result=$(__do_curl "$curlString")
586 if [ $? -ne 0 ]; then
587 __print_err "could not set debug mode" $@
588 ((RES_CONF_FAIL++))
589 return 1
590 fi
591 echo ""
592 return 0
593}
594
595# Turn on trace level tracing in the agent
596# args: -
597# (Function for test scripts)
598set_agent_trace() {
599 echo -e $BOLD"Setting agent trace logging"$EBOLD
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100600 curlString="$PA_SERVICE_PATH$POLICY_AGENT_ACTUATOR -X POST -H Content-Type:application/json -d {\"configuredLevel\":\"trace\"}"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100601 result=$(__do_curl "$curlString")
602 if [ $? -ne 0 ]; then
603 __print_err "could not set trace mode" $@
604 ((RES_CONF_FAIL++))
605 return 1
606 fi
607 echo ""
608 return 0
609}
610
611# Perform curl retries when making direct call to the agent for the specified http response codes
612# Speace separated list of http response codes
613# args: [<response-code>]*
614use_agent_retries() {
615 echo -e $BOLD"Do curl retries to the agent REST inteface for these response codes:$@"$EBOLD
616 AGENT_RETRY_CODES=$@
617 echo ""
618 return
619}
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100620
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100621# Check the agent logs for WARNINGs and ERRORs
622# args: -
623# (Function for test scripts)
624check_policy_agent_logs() {
625 __check_container_logs "Policy Agent" $POLICY_AGENT_APP_NAME $POLICY_AGENT_LOGPATH WARN ERR
626}
627
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100628#########################################################
629#### Test case functions A1 Policy management service
630#########################################################
631
632# This function compare the size, towards a target value, of a json array returned from <url> of the Policy Agent.
633# This is done immediately by setting PASS or FAIL or wait up to and optional timeout before setting PASS or FAIL
634# args: json:<url> <target-value> [<timeout-in-seconds]
635# (Function for test scripts)
636api_equal() {
637 echo "(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
638 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
639 if [[ $1 == "json:"* ]]; then
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100640 if [ "$PMS_VERSION" == "V2" ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100641 __var_test "Policy Agent" $PA_SERVICE_PATH$PMS_API_PREFIX"/v2/" $1 "=" $2 $3
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100642 else
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100643 __var_test "Policy Agent" $PA_SERVICE_PATH"/" $1 "=" $2 $3
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100644 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100645 return 0
646 fi
647 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100648 __print_err "needs two or three args: json:<json-array-param> <target-value> [ timeout ]" $@
649 return 1
650}
651
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100652# API Test function: GET /policies and V2 GET /v2/policy-instances
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100653# args: <response-code> <ric-id>|NORIC <service-id>|NOSERVICE <policy-type-id>|NOTYPE [ NOID | [<policy-id> <ric-id> <service-id> EMPTY|<policy-type-id> <template-file>]*]
654# args(V2): <response-code> <ric-id>|NORIC <service-id>|NOSERVICE <policy-type-id>|NOTYPE [ NOID | [<policy-id> <ric-id> <service-id> EMPTY|<policy-type-id> <transient> <notification-url> <template-file>]*]
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100655# (Function for test scripts)
656api_get_policies() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100657 __log_test_start $@
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100658
659 if [ "$PMS_VERSION" == "V2" ]; then
660 paramError=0
661 variableParams=$(($#-4))
662 if [ $# -lt 4 ]; then
663 paramError=1
664 elif [ $# -eq 5 ] && [ $5 != "NOID" ]; then
665 paramError=1
666 elif [ $# -gt 5 ] && [ $(($variableParams%7)) -ne 0 ]; then
667 paramError=1
668 fi
669
670 if [ $paramError -ne 0 ]; then
671 __print_err "<response-code> <ric-id>|NORIC <service-id>|NOSERVICE <policy-type-id>|NOTYPE [ NOID | [<policy-id> <ric-id> <service-id> EMPTY|<policy-type-id> <transient> <notification-url> <template-file>]*]" $@
672 return 1
673 fi
674 else
675 paramError=0
676 variableParams=$(($#-4))
677 if [ $# -lt 4 ]; then
678 paramError=1
679 elif [ $# -eq 5 ] && [ $5 != "NOID" ]; then
680 paramError=1
681 elif [ $# -gt 5 ] && [ $(($variableParams%5)) -ne 0 ]; then
682 paramError=1
683 fi
684
685 if [ $paramError -ne 0 ]; then
686 __print_err "<response-code> <ric-id>|NORIC <service-id>|NOSERVICE <policy-type-id>|NOTYPE [ NOID | [<policy-id> <ric-id> <service-id> EMPTY|<policy-type-id> <template-file>]*]" $@
687 return 1
688 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100689 fi
690
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100691 queryparams=""
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100692 if [ "$PMS_VERSION" == "V2" ]; then
693 if [ $2 != "NORIC" ]; then
694 queryparams="?ric_id="$2
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100695 fi
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100696 if [ $3 != "NOSERVICE" ]; then
697 if [ -z $queryparams ]; then
698 queryparams="?service_id="$3
699 else
700 queryparams=$queryparams"&service_id="$3
701 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100702 fi
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100703 if [ $4 != "NOTYPE" ]; then
704 if [ -z $queryparams ]; then
705 queryparams="?policytype_id="$4
706 else
707 queryparams=$queryparams"&policytype_id="$4
708 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100709 fi
710
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100711 query="/v2/policy-instances"$queryparams
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100712 res="$(__do_curl_to_api PA GET $query)"
713 status=${res:${#res}-3}
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100714
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100715 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100716 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100717 return 1
718 fi
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100719
720 if [ $# -gt 4 ]; then
721 body=${res:0:${#res}-3}
722 if [ $# -eq 5 ] && [ $5 == "NOID" ]; then
723 targetJson="["
724 else
725 targetJson="["
726 arr=(${@:5})
727
728 for ((i=0; i<$(($#-4)); i=i+7)); do
729
730 if [ "$targetJson" != "[" ]; then
731 targetJson=$targetJson","
732 fi
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100733 targetJson=$targetJson"{\"policy_id\":\"$UUID${arr[$i]}\",\"ric_id\":\"${arr[$i+1]}\",\"service_id\":\"${arr[$i+2]}\",\"policytype_id\":"
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100734 if [ "${arr[$i+3]}" == "EMPTY" ]; then
735 targetJson=$targetJson"\"\","
736 else
737 targetJson=$targetJson"\"${arr[$i+3]}\","
738 fi
739 targetJson=$targetJson"\"transient\":${arr[$i+4]},\"status_notification_uri\":\"${arr[$i+5]}\","
740 file="./tmp/.p.json"
741 sed 's/XXX/'${arr[$i]}'/g' ${arr[$i+6]} > $file
742 json=$(cat $file)
743 targetJson=$targetJson"\"policy_data\":"$json"}"
744 done
745 fi
746
747 targetJson=$targetJson"]"
748 targetJson="{\"policies\": $targetJson}"
749 echo "TARGET JSON: $targetJson" >> $HTTPLOG
750 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
751
752 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100753 __log_test_fail_body
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100754 return 1
755 fi
756 fi
757 else
758 if [ $2 != "NORIC" ]; then
759 queryparams="?ric="$2
760 fi
761 if [ $3 != "NOSERVICE" ]; then
762 if [ -z $queryparams ]; then
763 queryparams="?service="$3
764 else
765 queryparams=$queryparams"&service="$3
766 fi
767 fi
768 if [ $4 != "NOTYPE" ]; then
769 if [ -z $queryparams ]; then
770 queryparams="?type="$4
771 else
772 queryparams=$queryparams"&type="$4
773 fi
774 fi
775
776 query="/policies"$queryparams
777 res="$(__do_curl_to_api PA GET $query)"
778 status=${res:${#res}-3}
779
780 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100781 __log_test_fail_status_code $1 $status
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100782 return 1
783 fi
784
785 if [ $# -gt 4 ]; then
786 if [ $# -eq 5 ] && [ $5 == "NOID" ]; then
787 targetJson="["
788 else
789 body=${res:0:${#res}-3}
790 targetJson="["
791 arr=(${@:5})
792
793 for ((i=0; i<$(($#-4)); i=i+5)); do
794
795 if [ "$targetJson" != "[" ]; then
796 targetJson=$targetJson","
797 fi
798 targetJson=$targetJson"{\"id\":\"$UUID${arr[$i]}\",\"lastModified\":\"????\",\"ric\":\"${arr[$i+1]}\",\"service\":\"${arr[$i+2]}\",\"type\":"
799 if [ "${arr[$i+3]}" == "EMPTY" ]; then
800 targetJson=$targetJson"\"\","
801 else
802 targetJson=$targetJson"\"${arr[$i+3]}\","
803 fi
804 file="./tmp/.p.json"
805 sed 's/XXX/'${arr[$i]}'/g' ${arr[$i+4]} > $file
806 json=$(cat $file)
807 targetJson=$targetJson"\"json\":"$json"}"
808 done
809 fi
810
811 targetJson=$targetJson"]"
812 echo "TARGET JSON: $targetJson" >> $HTTPLOG
813 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
814
815 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100816 __log_test_fail_body
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100817 return 1
818 fi
819 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100820 fi
821
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100822 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100823 return 0
824
825}
826
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100827
828# API Test function: GET /policy and V2 GET /v2/policies/{policy_id}
829# args: <response-code> <policy-id> [<template-file>]
830# args(V2): <response-code> <policy-id> [ <template-file> <service-name> <ric-id> <policytype-id>|NOTYPE <transient> <notification-url>|NOURL ]
831
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100832# (Function for test scripts)
833api_get_policy() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100834 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100835
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100836
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100837 if [ "$PMS_VERSION" == "V2" ]; then
838 if [ $# -ne 2 ] && [ $# -ne 8 ]; then
839 __print_err "<response-code> <policy-id> [ <template-file> <service-name> <ric-id> <policytype-id>|NOTYPE <transient> <notification-url>|NOURL ]" $@
840 return 1
841 fi
842 query="/v2/policies/$UUID$2"
843 else
844 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
845 __print_err "<response-code> <policy-id> [<template-file>] " $@
846 return 1
847 fi
848 query="/policy?id=$UUID$2"
849 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200850 res="$(__do_curl_to_api PA GET $query)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100851 status=${res:${#res}-3}
852
853 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100854 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100855 return 1
856 fi
857
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100858 if [ "$PMS_VERSION" == "V2" ]; then
859 if [ $# -eq 8 ]; then
860
861 #Create a policy json to compare with
862 body=${res:0:${#res}-3}
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100863
864 targetJson="\"ric_id\":\"$5\",\"policy_id\":\"$UUID$2\",\"service_id\":\"$4\""
865 if [ $7 != "NOTRANSIENT" ]; then
866 targetJson=$targetJson", \"transient\":$7"
867 fi
868 if [ $6 != "NOTYPE" ]; then
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100869 targetJson=$targetJson", \"policytype_id\":\"$6\""
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100870 else
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100871 targetJson=$targetJson", \"policytype_id\":\"\""
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100872 fi
873 if [ $8 != "NOURL" ]; then
874 targetJson=$targetJson", \"status_notification_uri\":\"$8\""
875 fi
876
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100877 data=$(sed 's/XXX/'${2}'/g' $3)
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100878 targetJson=$targetJson", \"policy_data\":$data"
879 targetJson="{$targetJson}"
880
881 echo "TARGET JSON: $targetJson" >> $HTTPLOG
882 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
883 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100884 __log_test_fail_body
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100885 return 1
886 fi
887 fi
888 else
889 if [ $# -eq 3 ]; then
890 #Create a policy json to compare with
891 body=${res:0:${#res}-3}
892 file="./tmp/.p.json"
893 sed 's/XXX/'${2}'/g' $3 > $file
894 targetJson=$(< $file)
895 echo "TARGET JSON: $targetJson" >> $HTTPLOG
896 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
897 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100898 __log_test_fail_body
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100899 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100900 fi
901 fi
902
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100903 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100904 return 0
905}
906
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100907# API Test function: PUT /policy and V2 PUT /policies
908# args: <response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient>|NOTRANSIENT <template-file> [<count>]
909# args(V2): <response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient>|NOTRANSIENT <notification-url>|NOURL <template-file> [<count>]
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100910# (Function for test scripts)
911api_put_policy() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100912 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100913
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100914 if [ "$PMS_VERSION" == "V2" ]; then
915 if [ $# -lt 8 ] || [ $# -gt 9 ]; then
916 __print_err "<response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient>|NOTRANSIENT <notification-url>|NOURL <template-file> [<count>]" $@
917 return 1
918 fi
919 else
920 if [ $# -lt 7 ] || [ $# -gt 8 ]; then
921 __print_err "<response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient>|NOTRANSIENT <template-file> [<count>]" $@
922 return 1
923 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100924 fi
925
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100926 count=0
927 max=1
928 serv=$2
929 ric=$3
930 pt=$4
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100931 pid=$5
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100932 trans=$6
933
934 if [ "$PMS_VERSION" == "V2" ]; then
935 noti=$7
936 temp=$8
937 if [ $# -eq 9 ]; then
938 max=$9
939 fi
940 else
941 temp=$7
942 if [ $# -eq 8 ]; then
943 max=$8
944 fi
945 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100946
947 while [ $count -lt $max ]; do
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100948 if [ "$PMS_VERSION" == "V2" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100949
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100950 query="/v2/policies"
951
952 inputJson="\"ric_id\":\"$ric\",\"policy_id\":\"$UUID$pid\",\"service_id\":\"$serv\""
953 if [ $trans != "NOTRANSIENT" ]; then
954 inputJson=$inputJson", \"transient\":$trans"
955 fi
956 if [ $pt != "NOTYPE" ]; then
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100957 inputJson=$inputJson", \"policytype_id\":\"$pt\""
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100958 else
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100959 inputJson=$inputJson", \"policytype_id\":\"\""
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100960 fi
961 if [ $noti != "NOURL" ]; then
962 inputJson=$inputJson", \"status_notification_uri\":\"$noti\""
963 fi
964 file="./tmp/.p.json"
965 data=$(sed 's/XXX/'${pid}'/g' $temp)
966 inputJson=$inputJson", \"policy_data\":$data"
967 inputJson="{$inputJson}"
968 echo $inputJson > $file
969 else
970 query="/policy?id=$UUID$pid&ric=$ric&service=$serv"
971
972 if [ $pt != "NOTYPE" ]; then
973 query=$query"&type=$pt"
974 fi
975
976 if [ $trans != NOTRANSIENT ]; then
977 query=$query"&transient=$trans"
978 fi
979
980 file="./tmp/.p.json"
981 sed 's/XXX/'${pid}'/g' $temp > $file
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200982 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200983 res="$(__do_curl_to_api PA PUT $query $file)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100984 status=${res:${#res}-3}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200985 echo -ne " Executing "$count"("$max")${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100986 if [ $status -ne $1 ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200987 echo " Executed "$count"?("$max")"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100988 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100989 return 1
990 fi
991
992 let pid=$pid+1
993 let count=$count+1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200994 echo -ne " Executed "$count"("$max")${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100995 done
996 echo ""
997
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100998 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100999 return 0
1000}
1001
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001002# API Test function: PUT /policy and V2 PUT /policies, to run in batch
1003# args: <response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient> <template-file> [<count>]
1004# args(V2): <response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient> <notification-url>|NOURL <template-file> [<count>]
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001005# (Function for test scripts)
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001006
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001007api_put_policy_batch() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001008 __log_test_start $@
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001009
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001010 if [ "$PMS_VERSION" == "V2" ]; then
1011 if [ $# -lt 8 ] || [ $# -gt 9 ]; then
1012 __print_err "<response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient> <notification-url>|NOURL <template-file> [<count>]" $@
1013 return 1
1014 fi
1015 else
1016 if [ $# -lt 7 ] || [ $# -gt 8 ]; then
1017 __print_err "<response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient> <template-file> [<count>]" $@
1018 return 1
1019 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001020 fi
1021
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001022 count=0
1023 max=1
1024 serv=$2
1025 ric=$3
1026 pt=$4
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001027 pid=$5
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001028 trans=$6
1029 if [ "$PMS_VERSION" == "V2" ]; then
1030 noti=$7
1031 temp=$8
1032 if [ $# -eq 9 ]; then
1033 max=$9
1034 fi
1035 else
1036 temp=$7
1037 if [ $# -eq 8 ]; then
1038 max=$8
1039 fi
1040 fi
1041
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001042 ARR=""
1043 while [ $count -lt $max ]; do
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001044 if [ "$PMS_VERSION" == "V2" ]; then
1045 query="/v2/policies"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001046
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001047 inputJson="\"ric_id\":\"$ric\",\"policy_id\":\"$UUID$pid\",\"service_id\":\"$serv\""
1048 if [ $trans != "NOTRANSIENT" ]; then
1049 inputJson=$inputJson", \"transient\":$trans"
1050 fi
1051 if [ $pt != "NOTYPE" ]; then
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01001052 inputJson=$inputJson", \"policytype_id\":\"$pt\""
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001053 else
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01001054 inputJson=$inputJson", \"policytype_id\":\"\""
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001055 fi
1056 if [ $noti != "NOURL" ]; then
1057 inputJson=$inputJson", \"status_notification_uri\":\"$noti\""
1058 fi
1059 file="./tmp/.p.json"
1060 data=$(sed 's/XXX/'${pid}'/g' $temp)
1061 inputJson=$inputJson", \"policy_data\":$data"
1062 inputJson="{$inputJson}"
1063 echo $inputJson > $file
1064 else
1065 query="/policy?id=$UUID$pid&ric=$ric&service=$serv"
1066
1067 if [ $pt != "NOTYPE" ]; then
1068 query=$query"&type=$pt"
1069 fi
1070
1071 if [ $trans != NOTRANSIENT ]; then
1072 query=$query"&transient=$trans"
1073 fi
1074 file="./tmp/.p.json"
1075 sed 's/XXX/'${pid}'/g' $temp > $file
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001076 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001077 res="$(__do_curl_to_api PA PUT_BATCH $query $file)"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001078 status=${res:${#res}-3}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001079 echo -ne " Requesting(batch) "$count"("$max")${SAMELINE}"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001080
1081 if [ $status -ne 200 ]; then
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001082 echo " Requested(batch) "$count"?("$max")"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001083 __log_test_fail_status_code 200 $status
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001084 return 1
1085 fi
1086 cid=${res:0:${#res}-3}
1087 ARR=$ARR" "$cid
1088 let pid=$pid+1
1089 let count=$count+1
1090 echo -ne " Requested(batch) "$count"("$max")${SAMELINE}"
1091 done
1092
1093 echo ""
1094 count=0
1095 for cid in $ARR; do
1096
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001097 res="$(__do_curl_to_api PA RESPONSE $cid)"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001098 status=${res:${#res}-3}
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001099 echo -ne " Accepting(batch) "$count"("$max")${SAMELINE}"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001100
1101 if [ $status -ne $1 ]; then
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001102 echo " Accepted(batch) "$count"?("$max")"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001103 __log_test_fail_status_code $1 $status
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001104 return 1
1105 fi
1106
1107 let count=$count+1
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001108 echo -ne " Accepted(batch) "$count"("$max")${SAMELINE}"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001109 done
1110
1111 echo ""
1112
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001113 __log_test_pass
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001114 return 0
1115}
1116
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001117# API Test function: PUT /policy and V2 PUT /policies, to run in i parallel for a number of rics
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001118# args: <response-code> <service-name> <ric-id-base> <number-of-rics> <policytype-id> <policy-start-id> <transient> <template-file> <count-per-ric> <number-of-threads>
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001119# args(V2): <response-code> <service-name> <ric-id-base> <number-of-rics> <policytype-id> <policy-start-id> <transient> <notification-url>|NOURL <template-file> <count-per-ric> <number-of-threads>
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001120# (Function for test scripts)
1121api_put_policy_parallel() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001122 __log_test_start $@
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001123
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001124 if [ "$PMS_VERSION" == "V2" ]; then
1125 if [ $# -ne 11 ]; then
1126 __print_err "<response-code> <service-name> <ric-id-base> <number-of-rics> <policytype-id> <policy-start-id> <transient> <notification-url>|NOURL <template-file> <count-per-ric> <number-of-threads>" $@
1127 return 1
1128 fi
1129 else
1130 if [ $# -ne 10 ]; then
1131 __print_err " <response-code> <service-name> <ric-id-base> <number-of-rics> <policytype-id> <policy-start-id> <transient> <template-file> <count-per-ric> <number-of-threads>" $@
1132 return 1
1133 fi
1134 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001135 resp_code=$1; shift;
1136 serv=$1; shift
1137 ric_base=$1; shift;
1138 num_rics=$1; shift;
1139 type=$1; shift;
1140 start_id=$1; shift;
1141 transient=$1; shift;
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001142 if [ "$PMS_VERSION" == "V2" ]; then
1143 noti=$1; shift;
1144 else
1145 noti=""
1146 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001147 template=$1; shift;
1148 count=$1; shift;
1149 pids=$1; shift;
1150
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001151 #if [ $PA_ADAPTER != $RESTBASE ] && [ $PA_ADAPTER != $RESTBASE_SECURE ]; then
BjornMagnussonXAc963b732021-01-20 14:24:13 +01001152 if [ $PA_ADAPTER_TYPE != "REST" ]; then
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001153 echo " Info - api_put_policy_parallel uses only the agent REST interface - create over dmaap in parallel is not supported"
1154 echo " Info - will execute over agent REST"
1155 fi
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001156 if [ "$PMS_VERSION" == "V2" ]; then
1157 if [ $serv == "NOSERVICE" ]; then
1158 serv=""
1159 fi
BjornMagnussonXAc963b732021-01-20 14:24:13 +01001160 query="$PMS_API_PREFIX/v2/policies"
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001161 else
1162 if [ $serv == "NOSERVICE" ]; then
1163 serv=""
1164 fi
1165 query="/policy?service=$serv"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001166
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001167 if [ $type != "NOTYPE" ]; then
1168 query=$query"&type=$type"
1169 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001170
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001171 if [ $transient != NOTRANSIENT ]; then
1172 query=$query"&transient=$transient"
1173 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001174 fi
1175
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001176 urlbase=${PA_ADAPTER}${query}
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001177
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001178 httpproxy="NOPROXY"
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001179 if [ ! -z "$KUBE_PROXY_PATH" ]; then
1180 httpproxy=$KUBE_PROXY_PATH
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001181 fi
1182
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001183 for ((i=1; i<=$pids; i++))
1184 do
BjornMagnussonXAad047782020-06-08 15:54:11 +02001185 uuid=$UUID
1186 if [ -z "$uuid" ]; then
1187 uuid="NOUUID"
1188 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001189 echo "" > "./tmp/.pid${i}.res.txt"
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001190 if [ "$PMS_VERSION" == "V2" ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001191 echo $resp_code $urlbase $ric_base $num_rics $uuid $start_id $serv $type $transient $noti $template $count $pids $i $httpproxy > "./tmp/.pid${i}.txt"
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001192 else
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001193 echo $resp_code $urlbase $ric_base $num_rics $uuid $start_id $template $count $pids $i $httpproxy > "./tmp/.pid${i}.txt"
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001194 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001195 echo $i
1196 done | xargs -n 1 -I{} -P $pids bash -c '{
1197 arg=$(echo {})
1198 echo " Parallel process $arg started"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001199 tmp=$(< "./tmp/.pid${arg}.txt")
1200 python3 ../common/create_policies_process.py $tmp > ./tmp/.pid${arg}.res.txt
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001201 }'
1202 msg=""
1203 for ((i=1; i<=$pids; i++))
1204 do
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001205 file="./tmp/.pid${i}.res.txt"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001206 tmp=$(< $file)
1207 if [ -z "$tmp" ]; then
1208 echo " Process $i : unknown result (result file empty"
1209 msg="failed"
1210 else
1211 res=${tmp:0:1}
1212 if [ $res == "0" ]; then
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01001213 echo " Process $i : OK - "${tmp:1}
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001214 else
1215 echo " Process $i : failed - "${tmp:1}
1216 msg="failed"
1217 fi
1218 fi
1219 done
1220 if [ -z $msg ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001221 __log_test_pass " $(($count*$num_rics)) policy request(s) executed"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001222 return 0
1223 fi
1224
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001225 __log_test_fail_general "One of more processes failed to execute"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001226 return 1
1227}
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001228
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001229# API Test function: DELETE /policy and V2 DELETE /v2/policies/{policy_id}
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001230# args: <response-code> <policy-id> [count]
1231# (Function for test scripts)
1232api_delete_policy() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001233 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001234
1235 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
1236 __print_err "<response-code> <policy-id> [count]" $@
1237 return 1
1238 fi
1239
1240 count=0
1241 max=1
1242
1243 if [ $# -eq 3 ]; then
1244 max=$3
1245 fi
1246
1247 pid=$2
1248
1249 while [ $count -lt $max ]; do
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001250 if [ "$PMS_VERSION" == "V2" ]; then
1251 query="/v2/policies/"$UUID$pid
1252 else
1253 query="/policy?id="$UUID$pid
1254 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001255 res="$(__do_curl_to_api PA DELETE $query)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001256 status=${res:${#res}-3}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001257 echo -ne " Executing "$count"("$max")${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001258
1259 if [ $status -ne $1 ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001260 echo " Executed "$count"?("$max")"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001261 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001262 return 1
1263 fi
1264 let pid=$pid+1
1265 let count=$count+1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001266 echo -ne " Executed "$count"("$max")${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001267 done
1268 echo ""
1269
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001270 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001271 return 0
1272}
1273
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001274# API Test function: DELETE /policy and V2 DELETE /v2/policies/{policy_id}, to run in batch
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001275# args: <response-code> <policy-id> [count]
1276# (Function for test scripts)
1277api_delete_policy_batch() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001278 __log_test_start $@
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001279
1280 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
1281 __print_err "<response-code> <policy-id> [count]" $@
1282 return 1
1283 fi
1284
1285 count=0
1286 max=1
1287
1288 if [ $# -eq 3 ]; then
1289 max=$3
1290 fi
1291
1292 pid=$2
1293 ARR=""
1294 while [ $count -lt $max ]; do
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001295 if [ "$PMS_VERSION" == "V2" ]; then
1296 query="/v2/policies/"$UUID$pid
1297 else
1298 query="/policy?id="$UUID$pid
1299 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001300 res="$(__do_curl_to_api PA DELETE_BATCH $query)"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001301 status=${res:${#res}-3}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001302 echo -ne " Requesting(batch) "$count"("$max")${SAMELINE}"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001303
1304 if [ $status -ne 200 ]; then
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001305 echo " Requested(batch) "$count"?("$max")"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001306 __log_test_fail_status_code 200 $status
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001307 return 1
1308 fi
1309 cid=${res:0:${#res}-3}
1310 ARR=$ARR" "$cid
1311 let pid=$pid+1
1312 let count=$count+1
1313 echo -ne " Requested(batch) "$count"("$max")${SAMELINE}"
1314 done
1315
1316 echo ""
1317
1318 count=0
1319 for cid in $ARR; do
1320
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001321 res="$(__do_curl_to_api PA RESPONSE $cid)"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001322 status=${res:${#res}-3}
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001323 echo -ne " Deleting(batch) "$count"("$max")${SAMELINE}"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001324
1325 if [ $status -ne $1 ]; then
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001326 echo " Deleted(batch) "$count"?("$max")"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001327 __log_test_fail_status_code $1 $status
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001328 return 1
1329 fi
1330
1331 let count=$count+1
1332 echo -ne " Deleted(batch) "$count"("$max")${SAMELINE}"
1333 done
1334
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001335 echo ""
1336
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001337 __log_test_pass
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001338 return 0
1339}
1340
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001341# API Test function: DELETE /policy and V2 DELETE /v2/policies/{policy_id}, to run in i parallel for a number of rics
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001342# args: <response-code> <number-of-rics> <policy-start-id> <count-per-ric> <number-of-threads>
1343# (Function for test scripts)
1344api_delete_policy_parallel() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001345 __log_test_start $@
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001346
1347 if [ $# -ne 5 ]; then
1348 __print_err " <response-code> <ric-id-base> <number-of-rics> <policy-start-id> <count-per-ric> <number-of-threads>" $@
1349 return 1
1350 fi
1351 resp_code=$1; shift;
1352 num_rics=$1; shift;
1353 start_id=$1; shift;
1354 count=$1; shift;
1355 pids=$1; shift;
1356
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001357 #if [ $PA_ADAPTER != $RESTBASE ] && [ $PA_ADAPTER != $RESTBASE_SECURE ]; then
BjornMagnussonXAc963b732021-01-20 14:24:13 +01001358 if [ $PA_ADAPTER_TYPE != "REST" ]; then
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001359 echo " Info - api_delete_policy_parallel uses only the agent REST interface - create over dmaap in parallel is not supported"
1360 echo " Info - will execute over agent REST"
1361 fi
1362
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001363 if [ "$PMS_VERSION" == "V2" ]; then
BjornMagnussonXAc963b732021-01-20 14:24:13 +01001364 query="$PMS_API_PREFIX/v2/policies/"
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001365 else
1366 query="/policy"
1367 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001368
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001369 urlbase=${PA_ADAPTER}${query}
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001370
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001371 httpproxy="NOPROXY"
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001372 if [ ! -z "$KUBE_PROXY_PATH" ]; then
1373 httpproxy=$KUBE_PROXY_PATH
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001374 fi
1375
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001376 for ((i=1; i<=$pids; i++))
1377 do
BjornMagnussonXAad047782020-06-08 15:54:11 +02001378 uuid=$UUID
1379 if [ -z "$uuid" ]; then
1380 uuid="NOUUID"
1381 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001382 echo "" > "./tmp/.pid${i}.del.res.txt"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001383 echo $resp_code $urlbase $num_rics $uuid $start_id $count $pids $i $httpproxy> "./tmp/.pid${i}.del.txt"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001384 echo $i
1385 done | xargs -n 1 -I{} -P $pids bash -c '{
1386 arg=$(echo {})
1387 echo " Parallel process $arg started"
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001388 tmp=$(< "./tmp/.pid${arg}.del.txt")
1389 python3 ../common/delete_policies_process.py $tmp > ./tmp/.pid${arg}.del.res.txt
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001390 }'
1391 msg=""
1392 for ((i=1; i<=$pids; i++))
1393 do
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001394 file="./tmp/.pid${i}.del.res.txt"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001395 tmp=$(< $file)
1396 if [ -z "$tmp" ]; then
1397 echo " Process $i : unknown result (result file empty"
1398 msg="failed"
1399 else
1400 res=${tmp:0:1}
1401 if [ $res == "0" ]; then
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01001402 echo " Process $i : OK - "${tmp:1}
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001403 else
1404 echo " Process $i : failed - "${tmp:1}
1405 msg="failed"
1406 fi
1407 fi
1408 done
1409 if [ -z $msg ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001410 __log_test_pass " $(($count*$num_rics)) policy request(s) executed"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001411 return 0
1412 fi
1413
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001414 __log_test_fail_general "One of more processes failed to execute"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001415 return 1
1416}
1417
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001418# API Test function: GET /policy_ids and V2 GET /v2/policies
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001419# args: <response-code> <ric-id>|NORIC <service-id>|NOSERVICE <type-id>|NOTYPE ([<policy-instance-id]*|NOID)
1420# (Function for test scripts)
1421api_get_policy_ids() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001422 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001423
1424 if [ $# -lt 4 ]; then
1425 __print_err "<response-code> <ric-id>|NORIC <service-id>|NOSERVICE <type-id>|NOTYPE ([<policy-instance-id]*|NOID)" $@
1426 return 1
1427 fi
1428
1429 queryparams=""
1430
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001431 if [ "$PMS_VERSION" == "V2" ]; then
1432 if [ $2 != "NORIC" ]; then
1433 queryparams="?ric_id="$2
1434 fi
1435
1436 if [ $3 != "NOSERVICE" ]; then
1437 if [ -z $queryparams ]; then
1438 queryparams="?service_id="$3
1439 else
1440 queryparams=$queryparams"&service_id="$3
1441 fi
1442 fi
1443 if [ $4 != "NOTYPE" ]; then
1444 if [ -z $queryparams ]; then
1445 queryparams="?policytype_id="$4
1446 else
1447 queryparams=$queryparams"&policytype_id="$4
1448 fi
1449 fi
1450
1451 query="/v2/policies"$queryparams
1452 else
1453 if [ $2 != "NORIC" ]; then
1454 queryparams="?ric="$2
1455 fi
1456
1457 if [ $3 != "NOSERVICE" ]; then
1458 if [ -z $queryparams ]; then
1459 queryparams="?service="$3
1460 else
1461 queryparams=$queryparams"&service="$3
1462 fi
1463 fi
1464 if [ $4 != "NOTYPE" ]; then
1465 if [ -z $queryparams ]; then
1466 queryparams="?type="$4
1467 else
1468 queryparams=$queryparams"&type="$4
1469 fi
1470 fi
1471
1472 query="/policy_ids"$queryparams
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001473 fi
1474
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001475 res="$(__do_curl_to_api PA GET $query)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001476 status=${res:${#res}-3}
1477
1478 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001479 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001480 return 1
1481 fi
1482
1483 if [ $# -gt 4 ]; then
1484 body=${res:0:${#res}-3}
1485 targetJson="["
1486
1487 for pid in ${@:5} ; do
1488 if [ "$targetJson" != "[" ]; then
1489 targetJson=$targetJson","
1490 fi
1491 if [ $pid != "NOID" ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +02001492 targetJson=$targetJson"\"$UUID$pid\""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001493 fi
1494 done
1495
1496 targetJson=$targetJson"]"
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001497 if [ "$PMS_VERSION" == "V2" ]; then
1498 targetJson="{\"policy_ids\": $targetJson}"
1499 fi
1500 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1501 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1502
1503 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001504 __log_test_fail_body
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001505 return 1
1506 fi
1507 fi
1508
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001509 __log_test_pass
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001510 return 0
1511}
1512
1513# API Test function: V2 GET /v2/policy-types/{policyTypeId}
1514# args(V2): <response-code> <policy-type-id> [<schema-file>]
1515# (Function for test scripts)
1516api_get_policy_type() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001517 __log_test_start $@
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001518
1519 if [ "$PMS_VERSION" != "V2" ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001520 __log_test_fail_not_supported
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001521 return 1
1522 fi
1523
1524 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
1525 __print_err "<response-code> <policy-type-id> [<schema-file>]" $@
1526 return 1
1527 fi
1528 query="/v2/policy-types/$2"
1529
1530 res="$(__do_curl_to_api PA GET $query)"
1531 status=${res:${#res}-3}
1532
1533 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001534 __log_test_fail_status_code $1 $status
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001535 return 1
1536 fi
1537
1538 if [ $# -eq 3 ]; then
1539
1540 body=${res:0:${#res}-3}
1541
1542 targetJson=$(< $3)
1543 targetJson="{\"policy_schema\":$targetJson}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001544 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001545 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001546
1547 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001548 __log_test_fail_body
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001549 return 1
1550 fi
1551 fi
1552
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001553 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001554 return 0
1555}
1556
1557# API Test function: GET /policy_schema
1558# args: <response-code> <policy-type-id> [<schema-file>]
1559# (Function for test scripts)
1560api_get_policy_schema() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001561 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001562
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001563 if [ "$PMS_VERSION" == "V2" ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001564 __log_test_fail_not_supported
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001565 return 1
1566 fi
1567
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001568 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
1569 __print_err "<response-code> <policy-type-id> [<schema-file>]" $@
1570 return 1
1571 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001572 query="/policy_schema?id=$2"
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001573 res="$(__do_curl_to_api PA GET $query)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001574 status=${res:${#res}-3}
1575
1576 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001577 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001578 return 1
1579 fi
1580
1581 if [ $# -eq 3 ]; then
1582
1583 body=${res:0:${#res}-3}
1584
1585 targetJson=$(< $3)
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001586
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001587 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001588 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001589
1590 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001591 __log_test_fail_body
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001592 return 1
1593 fi
1594 fi
1595
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001596 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001597 return 0
1598}
1599
1600# API Test function: GET /policy_schemas
1601# args: <response-code> <ric-id>|NORIC [<schema-file>|NOFILE]*
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001602# args(V2): <response-code>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001603# (Function for test scripts)
1604api_get_policy_schemas() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001605 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001606
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001607 if [ "$PMS_VERSION" == "V2" ]; then
1608 if [ $# -ne 1 ]; then
1609 __print_err "<response-code>" $@
1610 return 1
1611 fi
1612 else
1613 if [ $# -lt 2 ]; then
1614 __print_err "<response-code> <ric-id>|NORIC [<schema-file>|NOFILE]*" $@
1615 return 1
1616 fi
1617 fi
1618 if [ "$PMS_VERSION" == "V2" ]; then
1619 query="/v2/policy-schemas"
1620 else
1621 query="/policy_schemas"
1622 if [ $2 != "NORIC" ]; then
1623 query=$query"?ric="$2
1624 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001625 fi
1626
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001627 res="$(__do_curl_to_api PA GET $query)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001628 status=${res:${#res}-3}
1629
1630 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001631 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001632 return 1
1633 fi
1634
1635 if [ $# -gt 2 ]; then
1636 body=${res:0:${#res}-3}
1637 targetJson="["
1638
1639 for file in ${@:3} ; do
1640 if [ "$targetJson" != "[" ]; then
1641 targetJson=$targetJson","
1642 fi
1643 if [ $file == "NOFILE" ]; then
1644 targetJson=$targetJson"{}"
1645 else
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001646 targetJson=$targetJson$(< $file)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001647 fi
1648 done
1649
1650 targetJson=$targetJson"]"
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001651 if [ "$PMS_VERSION" == "V2" ]; then
1652 targetJson="{\"policy_schemas\": $targetJson }"
1653 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001654 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001655 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001656
1657 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001658 __log_test_fail_body
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001659 return 1
1660 fi
1661 fi
1662
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001663 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001664 return 0
1665}
1666
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001667# API Test function: GET /policy_status and V2 GET /policies/{policy_id}/status
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001668# arg: <response-code> <policy-id> (STD|STD2 <enforce-status>|EMPTY [<reason>|EMPTY])|(OSC <instance-status> <has-been-deleted>)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001669# (Function for test scripts)
1670api_get_policy_status() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001671 __log_test_start $@
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001672
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001673 if [ $# -lt 4 ] || [ $# -gt 5 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001674 __print_err "<response-code> <policy-id> (STD <enforce-status>|EMPTY [<reason>|EMPTY])|(OSC <instance-status> <has-been-deleted>)" $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001675 return 1
1676 fi
1677
1678 targetJson=""
1679
1680 if [ $3 == "STD" ]; then
1681 targetJson="{\"enforceStatus\":\"$4\""
1682 if [ $# -eq 5 ]; then
1683 targetJson=$targetJson",\"reason\":\"$5\""
1684 fi
1685 targetJson=$targetJson"}"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001686 elif [ $3 == "STD2" ]; then
1687 if [ $4 == "EMPTY" ]; then
1688 targetJson="{\"enforceStatus\":\"\""
1689 else
1690 targetJson="{\"enforceStatus\":\"$4\""
1691 fi
1692 if [ $# -eq 5 ]; then
1693 if [ $5 == "EMPTY" ]; then
1694 targetJson=$targetJson",\"enforceReason\":\"\""
1695 else
1696 targetJson=$targetJson",\"enforceReason\":\"$5\""
1697 fi
1698 fi
1699 targetJson=$targetJson"}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001700 elif [ $3 == "OSC" ]; then
1701 targetJson="{\"instance_status\":\"$4\""
1702 if [ $# -eq 5 ]; then
1703 targetJson=$targetJson",\"has_been_deleted\":\"$5\""
1704 fi
1705 targetJson=$targetJson",\"created_at\":\"????\"}"
1706 else
1707 __print_err "<response-code> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)" $@
1708 return 1
1709 fi
1710
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001711 if [ "$PMS_VERSION" == "V2" ]; then
1712 query="/v2/policies/$UUID$2/status"
1713 targetJson="{\"last_modified\":\"????\",\"status\":$targetJson}"
1714 else
1715 query="/policy_status?id="$UUID$2
1716 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001717
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001718 res="$(__do_curl_to_api PA GET $query)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001719 status=${res:${#res}-3}
1720
1721 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001722 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001723 return 1
1724 fi
1725
1726 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1727 body=${res:0:${#res}-3}
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001728 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001729
1730 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001731 __log_test_fail_body
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001732 return 1
1733 fi
1734
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001735 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001736 return 0
1737}
1738
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001739# API Test function: GET /policy_types and V2 GET /v2/policy-types
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001740# args: <response-code> [<ric-id>|NORIC [<policy-type-id>|EMPTY [<policy-type-id>]*]]
1741# (Function for test scripts)
1742api_get_policy_types() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001743 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001744
1745 if [ $# -lt 1 ]; then
1746 __print_err "<response-code> [<ric-id>|NORIC [<policy-type-id>|EMPTY [<policy-type-id>]*]]" $@
1747 return 1
1748 fi
1749
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001750 if [ "$PMS_VERSION" == "V2" ]; then
1751 if [ $# -eq 1 ]; then
1752 query="/v2/policy-types"
1753 elif [ $2 == "NORIC" ]; then
1754 query="/v2/policy-types"
1755 else
1756 query="/v2/policy-types?ric_id=$2"
1757 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001758 else
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001759 if [ $# -eq 1 ]; then
1760 query="/policy_types"
1761 elif [ $2 == "NORIC" ]; then
1762 query="/policy_types"
1763 else
1764 query="/policy_types?ric=$2"
1765 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001766 fi
1767
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001768 res="$(__do_curl_to_api PA GET $query)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001769 status=${res:${#res}-3}
1770
1771 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001772 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001773 return 1
1774 fi
1775
1776 if [ $# -gt 2 ]; then
1777 body=${res:0:${#res}-3}
1778 targetJson="["
1779
1780 for pid in ${@:3} ; do
1781 if [ "$targetJson" != "[" ]; then
1782 targetJson=$targetJson","
1783 fi
1784 if [ $pid == "EMPTY" ]; then
1785 pid=""
1786 fi
1787 targetJson=$targetJson"\"$pid\""
1788 done
1789
1790 targetJson=$targetJson"]"
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001791 if [ "$PMS_VERSION" == "V2" ]; then
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01001792 targetJson="{\"policytype_ids\": $targetJson }"
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001793 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001794 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001795 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001796
1797 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001798 __log_test_fail_body
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001799 return 1
1800 fi
1801 fi
1802
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001803 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001804 return 0
1805}
1806
1807#########################################################
1808#### Test case functions Health check
1809#########################################################
1810
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001811# API Test function: GET /status and V2 GET /status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001812# args: <response-code>
1813# (Function for test scripts)
1814api_get_status() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001815 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001816 if [ $# -ne 1 ]; then
1817 __print_err "<response-code>" $@
1818 return 1
1819 fi
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001820 if [ "$PMS_VERSION" == "V2" ]; then
1821 query="/v2/status"
1822 else
1823 query="/status"
1824 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001825 res="$(__do_curl_to_api PA GET $query)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001826 status=${res:${#res}-3}
1827
1828 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001829 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001830 return 1
1831 fi
1832
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001833 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001834 return 0
1835}
1836
1837#########################################################
1838#### Test case functions RIC Repository
1839#########################################################
1840
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001841# API Test function: GET /ric and V2 GET /v2/rics/ric
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001842# args: <reponse-code> <management-element-id> [<ric-id>]
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001843# (V2) args: <reponse-code> <management-element-id>|NOME <ric-id>|<NORIC> [<string-of-ricinfo>]
1844# (V2) example of <string-of-ricinfo> = "ricsim_g1_1:me1_ricsim_g1_1,me2_ricsim_g1_1:1,2,4"
1845# (V2) format of ric-info: <ric-id>:<list-of-mes>:<list-of-policy-type-ids>
1846
1847
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001848# (Function for test scripts)
1849api_get_ric() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001850 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001851
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001852 if [ "$PMS_VERSION" == "V2" ]; then
1853 if [ $# -lt 3 ]; then
1854 __print_err "<reponse-code> <management-element-id>|NOME <ric-id>|<NORIC> [string-of-ricinfo>]" $@
1855 return 1
1856 fi
1857 search=""
1858 if [ $2 != "NOME" ]; then
1859 search="?managed_element_id="$2
1860 fi
1861 if [ $3 != "NORIC" ]; then
1862 if [ -z $search ]; then
1863 search="?ric_id="$3
1864 else
1865 search=$search"&ric_id="$3
1866 fi
1867 fi
1868 query="/v2/rics/ric"$search
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001869
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001870 res="$(__do_curl_to_api PA GET $query)"
1871 status=${res:${#res}-3}
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001872
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001873 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001874 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001875 return 1
1876 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001877
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001878 if [ $# -gt 3 ]; then
1879 body=${res:0:${#res}-3}
1880 res=$(python3 ../common/create_rics_json.py "./tmp/.tmp_rics.json" "V2" "$4" )
1881 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001882 __log_test_fail_general "Could not create target ric info json"
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001883 return 1
1884 fi
1885
1886 targetJson=$(<./tmp/.tmp_rics.json)
1887 targetJson=${targetJson:1:${#targetJson}-2} #remove array brackets
1888 echo " TARGET JSON: $targetJson" >> $HTTPLOG
1889 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1890 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001891 __log_test_fail_body
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001892 return 1
1893 fi
1894 fi
1895 else
1896 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
1897 __print_err "<reponse-code> <management-element-id> [<ric-id>]" $@
1898 return 1
1899 fi
1900
1901 query="/ric?managedElementId="$2
1902
1903 res="$(__do_curl_to_api PA GET $query)"
1904 status=${res:${#res}-3}
1905
1906 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001907 __log_test_fail_status_code $1 $status
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001908 return 1
1909 fi
1910
1911 if [ $# -eq 3 ]; then
1912 body=${res:0:${#res}-3}
1913 if [ "$body" != "$3" ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001914 __log_test_fail_body
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001915 return 1
1916 fi
1917 fi
1918 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001919 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001920 return 0
1921}
1922
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001923# API test function: GET /rics and V2 GET /v2/rics
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001924# args: <reponse-code> <policy-type-id>|NOTYPE [<space-separate-string-of-ricinfo>]
1925# example of <space-separate-string-of-ricinfo> = "ricsim_g1_1:me1_ricsim_g1_1,me2_ricsim_g1_1:1,2,4 ricsim_g1_1:me2_........."
1926# format of ric-info: <ric-id>:<list-of-mes>:<list-of-policy-type-ids>
1927# (Function for test scripts)
1928api_get_rics() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001929 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001930
1931 if [ $# -lt 2 ]; then
1932 __print_err "<reponse-code> <policy-type-id>|NOTYPE [<space-separate-string-of-ricinfo>]" $@
1933 return 1
1934 fi
1935
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001936 if [ "$PMS_VERSION" == "V2" ]; then
1937 query="/v2/rics"
1938 if [ $2 != "NOTYPE" ]; then
1939 query="/v2/rics?policytype_id="$2
1940 fi
1941 else
1942 query="/rics"
1943 if [ $2 != "NOTYPE" ]; then
1944 query="/rics?policyType="$2
1945 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001946 fi
1947
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001948 res="$(__do_curl_to_api PA GET $query)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001949 status=${res:${#res}-3}
1950
1951 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001952 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001953 return 1
1954 fi
1955
1956 if [ $# -gt 2 ]; then
1957 body=${res:0:${#res}-3}
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001958 if [ "$PMS_VERSION" == "V2" ]; then
1959 res=$(python3 ../common/create_rics_json.py "./tmp/.tmp_rics.json" "V2" "$3" )
1960 else
1961 res=$(python3 ../common/create_rics_json.py "./tmp/.tmp_rics.json" "V1" "$3" )
1962 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001963 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001964 __log_test_fail_general "Could not create target ric info json"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001965 return 1
1966 fi
1967
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001968 targetJson=$(<./tmp/.tmp_rics.json)
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001969 if [ "$PMS_VERSION" == "V2" ]; then
1970 targetJson="{\"rics\": $targetJson }"
1971 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001972 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001973 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001974 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001975 __log_test_fail_body
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001976 return 1
1977 fi
1978 fi
1979
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001980 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001981 return 0
1982}
1983
1984##################################################################
1985#### API Test case functions Service registry and supervision ####
1986##################################################################
1987
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001988# API test function: PUT /service and V2 PUT /service
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001989# args: <response-code> <service-name> <keepalive-timeout> <callbackurl>
1990# (Function for test scripts)
1991api_put_service() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001992 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001993 if [ $# -ne 4 ]; then
1994 __print_err "<response-code> <service-name> <keepalive-timeout> <callbackurl>" $@
1995 return 1
1996 fi
1997
BjornMagnussonXA4207b832020-11-03 09:52:49 +01001998 if [ "$PMS_VERSION" == "V2" ]; then
1999 query="/v2/services"
2000 json="{\"callback_url\": \""$4"\",\"keep_alive_interval_seconds\": \""$3"\",\"service_id\": \""$2"\"}"
2001 else
2002 query="/service"
2003 json="{\"callbackUrl\": \""$4"\",\"keepAliveIntervalSeconds\": \""$3"\",\"serviceName\": \""$2"\"}"
2004 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002005 file="./tmp/.tmp.json"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002006 echo "$json" > $file
2007
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002008 res="$(__do_curl_to_api PA PUT $query $file)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002009 status=${res:${#res}-3}
2010
2011 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002012 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002013 return 1
2014 fi
2015
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002016 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002017 return 0
2018}
2019
BjornMagnussonXA4207b832020-11-03 09:52:49 +01002020# API test function: GET /services and V2 GET /v2/services
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002021#args: <response-code> [ (<query-service-name> <target-service-name> <keepalive-timeout> <callbackurl>) | (NOSERVICE <target-service-name> <keepalive-timeout> <callbackurl> [<target-service-name> <keepalive-timeout> <callbackurl>]* )]
2022# (Function for test scripts)
2023api_get_services() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002024 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002025 #Number of accepted parameters: 1, 2, 4, 7, 10, 13,...
2026 paramError=1
2027 if [ $# -eq 1 ]; then
2028 paramError=0
2029 elif [ $# -eq 2 ] && [ $2 != "NOSERVICE" ]; then
2030 paramError=0
2031 elif [ $# -eq 5 ]; then
2032 paramError=0
2033 elif [ $# -gt 5 ] && [ $2 == "NOSERVICE" ]; then
2034 argLen=$(($#-2))
2035 if [ $(($argLen%3)) -eq 0 ]; then
2036 paramError=0
2037 fi
2038 fi
2039
2040 if [ $paramError -ne 0 ]; then
2041 __print_err "<response-code> [ (<query-service-name> <target-service-name> <keepalive-timeout> <callbackurl>) | (NOSERVICE <target-service-name> <keepalive-timeout> <callbackurl> [<target-service-name> <keepalive-timeout> <callbackurl>]* )]" $@
2042 return 1
2043 fi
2044
BjornMagnussonXA4207b832020-11-03 09:52:49 +01002045 if [ "$PMS_VERSION" == "V2" ]; then
2046 query="/v2/services"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002047
BjornMagnussonXA4207b832020-11-03 09:52:49 +01002048 if [ $# -gt 1 ] && [ $2 != "NOSERVICE" ]; then
2049 query="/v2/services?service_id="$2
2050 fi
2051 else
2052 query="/services"
2053
2054 if [ $# -gt 1 ] && [ $2 != "NOSERVICE" ]; then
2055 query="/services?name="$2
2056 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002057 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002058 res="$(__do_curl_to_api PA GET $query)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002059 status=${res:${#res}-3}
2060
2061 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002062 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002063 return 1
2064 fi
2065
2066 if [ $# -gt 2 ]; then
2067 variableArgCount=$(($#-2))
2068 body=${res:0:${#res}-3}
2069 targetJson="["
2070 shift; shift;
2071 cntr=0
2072 while [ $cntr -lt $variableArgCount ]; do
2073 servicename=$1; shift;
2074 timeout=$1; shift;
2075 callback=$1; shift;
2076 if [ $cntr -gt 0 ]; then
2077 targetJson=$targetJson","
2078 fi
2079 # timeSinceLastActivitySeconds value cannot be checked since value varies
BjornMagnussonXA4207b832020-11-03 09:52:49 +01002080 if [ "$PMS_VERSION" == "V2" ]; then
2081 targetJson=$targetJson"{\"service_id\": \""$servicename"\",\"keep_alive_interval_seconds\": "$timeout",\"time_since_last_activity_seconds\":\"????\",\"callback_url\": \""$callback"\"}"
2082 else
2083 targetJson=$targetJson"{\"serviceName\": \""$servicename"\",\"keepAliveIntervalSeconds\": "$timeout",\"timeSinceLastActivitySeconds\":\"????\",\"callbackUrl\": \""$callback"\"}"
2084 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002085 let cntr=cntr+3
2086 done
2087 targetJson=$targetJson"]"
BjornMagnussonXA4207b832020-11-03 09:52:49 +01002088 if [ "$PMS_VERSION" == "V2" ]; then
2089 targetJson="{\"service_list\": $targetJson }"
2090 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002091 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002092 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002093 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002094 __log_test_fail_body
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002095 return 1
2096 fi
2097 fi
2098
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002099 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002100 return 0
2101}
2102
BjornMagnussonXA4207b832020-11-03 09:52:49 +01002103# API test function: GET /services V2 GET /v2/services - (only checking service names)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002104# args: <response-code> [<service-name>]*"
2105# (Function for test scripts)
2106api_get_service_ids() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002107 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002108
2109 if [ $# -lt 1 ]; then
2110 __print_err "<response-code> [<service-name>]*" $@
2111 return 1
2112 fi
2113
BjornMagnussonXA4207b832020-11-03 09:52:49 +01002114 if [ "$PMS_VERSION" == "V2" ]; then
2115 query="/v2/services"
2116 else
2117 query="/services"
2118 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002119 res="$(__do_curl_to_api PA GET $query)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002120 status=${res:${#res}-3}
2121
2122 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002123 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002124 return 1
2125 fi
2126
2127 body=${res:0:${#res}-3}
2128 targetJson="["
2129 for rapp in ${@:2} ; do
2130 if [ "$targetJson" != "[" ]; then
2131 targetJson=$targetJson","
2132 fi
BjornMagnussonXA4207b832020-11-03 09:52:49 +01002133 if [ "$PMS_VERSION" == "V2" ]; then
2134 targetJson=$targetJson"{\"callback_url\":\"????\",\"keep_alive_interval_seconds\":\"????\",\"service_id\":\""$rapp"\",\"time_since_last_activity_seconds\":\"????\"}"
2135 else
2136 targetJson=$targetJson"{\"callbackUrl\":\"????\",\"keepAliveIntervalSeconds\":\"????\",\"serviceName\":\""$rapp"\",\"timeSinceLastActivitySeconds\":\"????\"}"
2137 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002138 done
2139
2140 targetJson=$targetJson"]"
BjornMagnussonXA4207b832020-11-03 09:52:49 +01002141 if [ "$PMS_VERSION" == "V2" ]; then
2142 targetJson="{\"service_list\": $targetJson }"
2143 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002144 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002145 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002146
2147 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002148 __log_test_fail_body
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002149 return 1
2150 fi
2151
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002152 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002153 return 0
2154}
2155
BjornMagnussonXA4207b832020-11-03 09:52:49 +01002156# API test function: DELETE /services and V2 DELETE /v2/services/{serviceId}
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002157# args: <response-code> <service-name>
2158# (Function for test scripts)
2159api_delete_services() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002160 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002161
2162 if [ $# -ne 2 ]; then
2163 __print_err "<response-code> <service-name>" $@
2164 return 1
2165 fi
BjornMagnussonXA4207b832020-11-03 09:52:49 +01002166 if [ "$PMS_VERSION" == "V2" ]; then
2167 query="/v2/services/"$2
2168 else
2169 query="/services?name="$2
2170 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002171 res="$(__do_curl_to_api PA DELETE $query)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002172 status=${res:${#res}-3}
2173
2174 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002175 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002176 return 1
2177 fi
2178
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002179 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002180 return 0
2181}
2182
BjornMagnussonXA4207b832020-11-03 09:52:49 +01002183# API test function: PUT /services/keepalive and V2 PUT /v2/services/{service_id}/keepalive
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002184# args: <response-code> <service-name>
2185# (Function for test scripts)
2186api_put_services_keepalive() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002187 __log_test_start $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002188
2189 if [ $# -ne 2 ]; then
2190 __print_err "<response-code> <service-name>" $@
2191 return 1
2192 fi
BjornMagnussonXA4207b832020-11-03 09:52:49 +01002193 if [ "$PMS_VERSION" == "V2" ]; then
2194 query="/v2/services/$2/keepalive"
2195 else
2196 query="/services/keepalive?name="$2
2197 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002198
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002199 res="$(__do_curl_to_api PA PUT $query)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002200 status=${res:${#res}-3}
2201
2202 if [ $status -ne $1 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002203 __log_test_fail_status_code $1 $status
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002204 return 1
2205 fi
2206
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002207 __log_test_pass
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002208 return 0
2209}
2210
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002211##################################################################
2212#### API Test case functions Configuration ####
2213##################################################################
2214
2215# API Test function: PUT /v2/configuration
2216# args: <response-code> <config-file>
2217# (Function for test scripts)
2218api_put_configuration() {
2219 __log_test_start $@
2220
2221 if [ "$PMS_VERSION" != "V2" ]; then
2222 __log_test_fail_not_supported
2223 return 1
2224 fi
2225
2226 if [ $# -ne 2 ]; then
2227 __print_err "<response-code> <config-file>" $@
2228 return 1
2229 fi
2230 if [ ! -f $2 ]; then
BjornMagnussonXA9d8fafb2021-05-10 11:11:49 +02002231 __log_test_fail_general "Config file "$2", does not exist"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002232 return 1
2233 fi
2234 inputJson=$(< $2)
2235 inputJson="{\"config\":"$inputJson"}"
2236 file="./tmp/.config.json"
2237 echo $inputJson > $file
2238 query="/v2/configuration"
2239 res="$(__do_curl_to_api PA PUT $query $file)"
2240 status=${res:${#res}-3}
2241
2242 if [ $status -ne $1 ]; then
2243 __log_test_fail_status_code $1 $status
2244 return 1
2245 fi
2246
2247 __log_test_pass
2248 return 0
2249}
2250
2251# API Test function: GET /v2/configuration
2252# args: <response-code> [<config-file>]
2253# (Function for test scripts)
2254api_get_configuration() {
2255 __log_test_start $@
2256
2257 if [ "$PMS_VERSION" != "V2" ]; then
2258 __log_test_fail_not_supported
2259 return 1
2260 fi
2261
2262 if [ $# -lt 1 ] || [ $# -gt 2 ]; then
2263 __print_err "<response-code> [<config-file>]" $@
2264 return 1
2265 fi
2266 if [ ! -f $2 ]; then
BjornMagnussonXA9d8fafb2021-05-10 11:11:49 +02002267 __log_test_fail_general "Config file "$2" for comparison, does not exist"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002268 return 1
2269 fi
2270
2271 query="/v2/configuration"
2272 res="$(__do_curl_to_api PA GET $query)"
2273 status=${res:${#res}-3}
2274
2275 if [ $status -ne $1 ]; then
2276 __log_test_fail_status_code $1 $status
2277 return 1
2278 fi
2279
2280 if [ $# -eq 2 ]; then
2281
2282 body=${res:0:${#res}-3}
2283
2284 targetJson=$(< $2)
2285 targetJson="{\"config\":"$targetJson"}"
2286 echo "TARGET JSON: $targetJson" >> $HTTPLOG
2287 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
2288
2289 if [ $res -ne 0 ]; then
2290 __log_test_fail_body
2291 return 1
2292 fi
2293 fi
2294
2295 __log_test_pass
2296 return 0
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002297}
2298
2299##########################################
2300#### Reset types and instances ####
2301##########################################
2302
2303# Admin reset to remove all policies and services
2304# All types and instances etc are removed - types and instances in a1 sims need to be removed separately
2305# NOTE - only works in kubernetes and the pod should not be running
2306# args: -
2307# (Function for test scripts)
2308
2309pms_kube_pvc_reset() {
2310 __log_test_start $@
2311
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002312 pvc_name=$(kubectl get pvc -n $KUBE_NONRTRIC_NAMESPACE --no-headers -o custom-columns=":metadata.name" | grep policy)
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02002313 if [ -z "$pvc_name" ]; then
2314 pvc_name=policymanagementservice-vardata-pvc
2315 fi
2316 echo " Trying to reset pvc: "$pvc_name
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002317 __kube_clean_pvc $POLICY_AGENT_APP_NAME $KUBE_NONRTRIC_NAMESPACE $pvc_name $POLICY_AGENT_CONTAINER_MNT_DIR
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002318
2319 __log_test_pass
2320 return 0
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002321}