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
21 local RED='\033[0;31m'
24 echo -e "${RED} $msg ---------------------------------------${NC}"
27 function get_ovn_central_address {
28 ansible_ifconfig=$(ansible ovn-central[0] -i ${FUNCTIONS_DIR}/../hosting_providers/vagrant/inventory/hosts.ini -m shell -a "ifconfig ${OVN_CENTRAL_INTERFACE} |grep \"inet addr\" |awk '{print \$2}' |awk -F: '{print \$2}'")
29 if [[ $ansible_ifconfig != *CHANGED* ]]; then
30 echo "Fail to get the OVN central IP address from ${OVN_CENTRAL_INTERFACE} nic"
33 echo "$(echo ${ansible_ifconfig#*>>} | tr '\n' ':')6641"
37 #Runs curl with passed flags and provides
38 #additional error handling and debug information
40 #Function outputs server response body
41 #and performs validation of http_code
44 local curl_response_file="$(mktemp -p /tmp)"
45 local curl_common_flags=(-s -w "%{http_code}" -o "${curl_response_file}")
46 local command=(curl "${curl_common_flags[@]}" "$@")
48 echo "[INFO] Running '${command[@]}'" >&2
49 if ! status="$("${command[@]}")"; then
50 echo "[ERROR] Internal curl error! '$status'" >&2
51 cat "${curl_response_file}"
52 rm "${curl_response_file}"
55 echo "[INFO] Server replied with status: ${status}" >&2
56 cat "${curl_response_file}"
57 rm "${curl_response_file}"
58 if [[ "${status:0:1}" =~ [45] ]]; then
66 function delete_resource {
67 #Issues DELETE http call to provided endpoint
68 #and further validates by following GET request
70 call_api -X DELETE "$1"
71 ! call_api -X GET "$1" >/dev/null
74 # init_network() - This function creates the OVN resouces required by the test
75 function init_network {
77 local router_name="ovn4nfv-master"
79 name=$(cat $fname | yq '.spec.name' | xargs)
80 subnet=$(cat $fname | yq '.spec.subnet' | xargs)
81 gateway=$(cat $fname | yq '.spec.gateway' | xargs)
82 ovn_central_address=$(get_ovn_central_address)
84 router_mac=$(printf '00:00:00:%02X:%02X:%02X' $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)))
85 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
86 ovn-nbctl --may-exist --db tcp:$ovn_central_address lrp-add $router_name rtos-$name $router_mac $gateway
87 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\"
90 # cleanup_network() - This function removes the OVN resources created for the test
91 function cleanup_network {
94 name=$(cat $fname | yq '.spec.name' | xargs)
95 ovn_central_address=$(get_ovn_central_address)
97 for cmd in "ls-del $name" "lrp-del rtos-$name" "lsp-del stor-$name"; do
98 ovn-nbctl --if-exist --db tcp:$ovn_central_address $cmd
102 function _checks_args {
104 echo "Missing CSAR ID argument"
107 if [[ -z $CSAR_DIR ]]; then
108 echo "CSAR_DIR global environment value is empty"
111 mkdir -p ${CSAR_DIR}/${1}
114 # destroy_deployment() - This function ensures that a specific deployment is
115 # destroyed in Kubernetes
116 function destroy_deployment {
117 local deployment_name=$1
119 echo "$(date +%H:%M:%S) - $deployment_name : Destroying deployment"
120 kubectl delete deployment $deployment_name --ignore-not-found=true --now
121 while kubectl get deployment $deployment_name &>/dev/null; do
122 echo "$(date +%H:%M:%S) - $deployment_name : Destroying deployment"
126 # recreate_deployment() - This function destroys an existing deployment and
127 # creates an new one based on its yaml file
128 function recreate_deployment {
129 local deployment_name=$1
131 destroy_deployment $deployment_name
132 kubectl create -f $deployment_name.yaml
135 # wait_deployment() - Wait process to Running status on the Deployment's pods
136 function wait_deployment {
137 local deployment_name=$1
140 while [[ $status_phase != "Running" ]]; do
141 new_phase=$(kubectl get pods | grep $deployment_name | awk '{print $3}')
142 if [[ $new_phase != $status_phase ]]; then
143 echo "$(date +%H:%M:%S) - $deployment_name : $new_phase"
144 status_phase=$new_phase
146 if [[ $new_phase == "Err"* ]]; then
152 # setup() - Base testing setup shared among functional tests
154 if ! $(kubectl version &>/dev/null); then
155 echo "This funtional test requires kubectl client"
158 for deployment_name in $@; do
159 recreate_deployment $deployment_name
162 for deployment_name in $@; do
163 wait_deployment $deployment_name
167 # teardown() - Base testing teardown function
169 for deployment_name in $@; do
170 destroy_deployment $deployment_name
173 test_folder=${FUNCTIONS_DIR}