2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
15 FUNCTIONS_DIR="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")"
17 source /etc/environment
18 source $FUNCTIONS_DIR/_common_test.sh
22 local RED='\033[0;31m'
25 echo -e "${RED} $msg ---------------------------------------${NC}"
28 function ssh_cluster {
29 master_ip=$(kubectl cluster-info | grep "Kubernetes master" | awk -F '[:/]' '{print $4}')
30 ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${master_ip} -- "$@"
33 function get_ovn_central_address {
34 #Reuse OVN_CENTRAL_ADDRESS if available (bypassable by --force flag)
35 if [[ "${1:-}" != "--force" ]] && [[ -n "${OVN_CENTRAL_ADDRESS:-}" ]]; then
36 echo "${OVN_CENTRAL_ADDRESS}"
40 local remote_command="ip address show dev $OVN_CENTRAL_INTERFACE primary"
41 declare -a ansible_command=(ansible ovn-central[0] -i \
42 "${FUNCTIONS_DIR}/../hosting_providers/vagrant/inventory/hosts.ini")
43 declare -a filter=(awk -F '[ \t/]+' \
44 'BEGIN {r=1} {for (i=1; i<=NF; i++) if ($i == "inet") {print $(i+1); r=0}} END {exit r}')
47 #Determine OVN_CENTRAL_INTERFACE address
48 if ! result="$("${ansible_command[@]}" -a "${remote_command}")"; then
49 echo "Ansible error for remote host ovn-central[0]" >&2
52 if [[ "${result}" != *CHANGED* ]]; then
53 echo "Failed to execute command on remote host ovn-central[0]" >&2
56 if ! result="$("${filter[@]}" <<< "${result}")"; then
57 echo "Failed to retrieve interface address from command output" >&2
67 #Runs curl with passed flags and provides
68 #additional error handling and debug information
70 #Function outputs server response body
71 #and performs validation of http_code
74 local curl_response_file="$(mktemp -p /tmp)"
75 local curl_common_flags=(-s -w "%{http_code}" -o "${curl_response_file}")
76 local command=(curl "${curl_common_flags[@]}" "$@")
78 echo "[INFO] Running '${command[@]}'" >&2
79 if ! status="$("${command[@]}")"; then
80 echo "[ERROR] Internal curl error! '$status'" >&2
81 cat "${curl_response_file}"
82 rm "${curl_response_file}"
85 echo "[INFO] Server replied with status: ${status}" >&2
86 cat "${curl_response_file}"
87 rm "${curl_response_file}"
88 if [[ "${status:0:1}" =~ [45] ]]; then
96 function call_api_nox {
97 # this version doesn't exit the script if there's
100 #Runs curl with passed flags and provides
101 #additional error handling and debug information
103 #Function outputs server response body
104 #and performs validation of http_code
107 local curl_response_file="$(mktemp -p /tmp)"
108 local curl_common_flags=(-s -w "%{http_code}" -o "${curl_response_file}")
109 local command=(curl "${curl_common_flags[@]}" "$@")
111 echo "[INFO] Running '${command[@]}'" >&2
112 if ! status="$("${command[@]}")"; then
113 echo "[ERROR] Internal curl error! '$status'" >&2
114 cat "${curl_response_file}"
115 rm "${curl_response_file}"
117 echo "[INFO] Server replied with status: ${status}" >&2
118 if [[ "${status:0:1}" =~ [45] ]]; then
119 cat "${curl_response_file}"
121 cat "${curl_response_file}" | jq .
123 rm "${curl_response_file}"
127 function delete_resource {
128 #Issues DELETE http call to provided endpoint
129 #and further validates by following GET request
131 call_api -X DELETE "$1"
132 ! call_api -X GET "$1" >/dev/null
135 # init_network() - This function creates the OVN resouces required by the test
136 function init_network {
138 local router_name="ovn4nfv-master"
140 name=$(cat $fname | yq '.spec.name' | xargs)
141 subnet=$(cat $fname | yq '.spec.subnet' | xargs)
142 gateway=$(cat $fname | yq '.spec.gateway' | xargs)
143 ovn_central_address=$(get_ovn_central_address)
145 router_mac=$(printf '00:00:00:%02X:%02X:%02X' $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)))
146 ovn-nbctl --may-exist --db tcp:$ovn_central_address ls-add $name -- set logical_switch $name other-config:subnet=$subnet external-ids:gateway_ip=$gateway
147 ovn-nbctl --may-exist --db tcp:$ovn_central_address lrp-add $router_name rtos-$name $router_mac $gateway
148 ovn-nbctl --may-exist --db tcp:$ovn_central_address lsp-add $name stor-$name -- set logical_switch_port stor-$name type=router options:router-port=rtos-$name addresses=\"$router_mac\"
151 # cleanup_network() - This function removes the OVN resources created for the test
152 function cleanup_network {
155 name=$(cat $fname | yq '.spec.name' | xargs)
156 ovn_central_address=$(get_ovn_central_address)
158 for cmd in "ls-del $name" "lrp-del rtos-$name" "lsp-del stor-$name"; do
159 ovn-nbctl --if-exist --db tcp:$ovn_central_address $cmd
163 function _checks_args {
165 echo "Missing CSAR ID argument"
168 if [[ -z $CSAR_DIR ]]; then
169 echo "CSAR_DIR global environment value is empty"
172 mkdir -p ${CSAR_DIR}/${1}
179 echo "$(date +%H:%M:%S) - $name : Destroying $type"
180 kubectl delete $type $name --ignore-not-found=true --now
181 while kubectl get $type $name &>/dev/null; do
182 echo "$(date +%H:%M:%S) - $name : Destroying $type"
186 # destroy_deployment() - This function ensures that a specific deployment is
187 # destroyed in Kubernetes
188 function destroy_deployment {
189 local deployment_name=$1
191 _destroy "deployment" $deployment_name
199 kubectl create -f $name.yaml
202 # wait_deployment() - Wait process to Running status on the Deployment's pods
203 function wait_deployment {
204 local deployment_name=$1
207 while [[ $status_phase != "Running" ]]; do
208 new_phase=$(kubectl get pods | grep $deployment_name | awk '{print $3}')
209 if [[ $new_phase != $status_phase ]]; then
210 echo "$(date +%H:%M:%S) - $deployment_name : $new_phase"
211 status_phase=$new_phase
213 if [[ $new_phase == "Err"* ]]; then
219 # wait_for_pod() - Wait until first pod matched by kubectl filters is in running status
220 function wait_for_pod {
222 # wait_for_pods example_pod
223 # wait_for_pods --namespace test different_pod
224 # wait_for_pods -n test -l app=plugin_test
227 while [[ "$status_phase" != "Running" ]]; do
228 new_phase="$(kubectl get pods -o 'go-template={{ index .items 0 "status" "phase" }}' "$@" )"
229 if [[ "$new_phase" != "$status_phase" ]]; then
230 echo "$(date +%H:%M:%S) - Filter=[$*] : $new_phase"
231 status_phase="$new_phase"
233 if [[ "$new_phase" == "Err"* ]]; then
239 # wait_for_deployment() - Wait until the deployment is ready
240 function wait_for_deployment {
242 # wait_for_deployment $DEPLOYMENT_NAME $REPLICAS
243 # wait_for_deployment example_deployment 2
247 while [[ "$status" != $2* ]]; do
248 new_status=`kubectl get deployment -A | grep $1 | awk '{print $3}'`
249 if [[ "$new_status" != "$status" ]]; then
253 pod_status=`kubectl get pods -A | grep $1 | awk '{print $4}'`
254 if [[ $pod_status =~ "Err" ]]; then
255 echo "Deployment $1 error"
261 # wait_for_deployment_status() - Wait until the deployment intent group is the specified status
262 function wait_for_deployment_status {
264 # wait_for_deployment_status {base-url-orchestrator}/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/deployment-intent-groups/{deployment-intent-group-name}/status Instantiated
265 if [ "$#" -ne 2 ]; then
266 echo "Usage: wait_for_deployment_status URL STATUS"
269 for try in {0..59}; do
271 new_phase="$(call_api $1 | jq -r .status)"
272 echo "$(date +%H:%M:%S) - Filter=[$*] : $new_phase"
273 if [[ "$new_phase" == "$2" ]]; then
280 function setup_type {
284 if ! $(kubectl version &>/dev/null); then
285 echo "This funtional test requires kubectl client"
289 _recreate $type $name
293 wait_deployment $name
297 function teardown_type {
306 # setup() - Base testing setup shared among functional tests
308 setup_type "deployment" $@
311 # teardown() - Base testing teardown function
313 teardown_type "deployment" $@
316 # check_ip_range() - Verifying IP address in address range
317 function check_ip_range {
323 if [[ ! -e /usr/bin/ipcalc ]]; then
324 echo -e "Command 'ipcalc' not found"
328 if [[ -z ${IP} ]] || [[ -z ${MASK} ]]; then
331 min=`/usr/bin/ipcalc $MASK|awk '/HostMin:/{print $2}'`
332 max=`/usr/bin/ipcalc $MASK|awk '/HostMax:/{print $2}'`
333 MIN=`echo $min|awk -F"." '{printf"%.0f\n",$1*256*256*256+$2*256*256+$3*256+$4}'`
334 MAX=`echo $max|awk -F"." '{printf"%.0f\n",$1*256*256*256+$2*256*256+$3*256+$4}'`
335 IPvalue=`echo $IP|awk -F"." '{printf"%.0f\n",$1*256*256*256+$2*256*256+$3*256+$4}'`
336 if [[ "$IPvalue" -gt "$MIN" ]] && [[ "$IPvalue" -lt "$MAX" ]]; then
337 echo -e "$IP in ipset $MASK"
340 echo -e "$IP not in ipset $MASK"
344 test_folder=${FUNCTIONS_DIR}