Merge "Move connectionhandler.go into api package"
[multicloud/k8s.git] / kud / tests / _functions.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2018
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 ##############################################################################
10
11 set -o errexit
12 set -o nounset
13 set -o pipefail
14
15 FUNCTIONS_DIR="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")"
16
17 source /etc/environment
18
19 function print_msg {
20     local msg=$1
21     local RED='\033[0;31m'
22     local NC='\033[0m'
23
24     echo -e "${RED} $msg ---------------------------------------${NC}"
25 }
26
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"
31         exit
32     fi
33     echo "$(echo ${ansible_ifconfig#*>>} | tr '\n' ':')6641"
34 }
35
36 function call_api {
37     #Runs curl with passed flags and provides
38     #additional error handling and debug information
39
40     #Function outputs server response body
41     #and performs validation of http_code
42
43     local status
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[@]}" "$@")
47
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}"
53         return 2
54     else
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
59             return 1
60         else
61             return 0
62         fi
63     fi
64 }
65
66 function delete_resource {
67     #Issues DELETE http call to provided endpoint
68     #and further validates by following GET request
69
70     call_api -X DELETE "$1"
71     ! call_api -X GET "$1" >/dev/null
72 }
73
74 # init_network() - This function creates the OVN resouces required by the test
75 function init_network {
76     local fname=$1
77     local router_name="ovn4nfv-master"
78
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)
83
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\"
88 }
89
90 # cleanup_network() - This function removes the OVN resources created for the test
91 function cleanup_network {
92     local fname=$1
93
94     name=$(cat $fname | yq '.spec.name' | xargs)
95     ovn_central_address=$(get_ovn_central_address)
96
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
99     done
100 }
101
102 function _checks_args {
103     if [[ -z $1 ]]; then
104         echo "Missing CSAR ID argument"
105         exit 1
106     fi
107     if [[ -z $CSAR_DIR ]]; then
108         echo "CSAR_DIR global environment value is empty"
109         exit 1
110     fi
111     mkdir -p ${CSAR_DIR}/${1}
112 }
113
114 # destroy_deployment() - This function ensures that a specific deployment is
115 # destroyed in Kubernetes
116 function destroy_deployment {
117     local deployment_name=$1
118
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"
123     done
124 }
125
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
130
131     destroy_deployment $deployment_name
132     kubectl create -f $deployment_name.yaml
133 }
134
135 # wait_deployment() - Wait process to Running status on the Deployment's pods
136 function wait_deployment {
137     local deployment_name=$1
138
139     status_phase=""
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
145         fi
146         if [[ $new_phase == "Err"* ]]; then
147             exit 1
148         fi
149     done
150 }
151
152 # setup() - Base testing setup shared among functional tests
153 function setup {
154     if ! $(kubectl version &>/dev/null); then
155         echo "This funtional test requires kubectl client"
156         exit 1
157     fi
158     for deployment_name in $@; do
159         recreate_deployment $deployment_name
160     done
161     sleep 5
162     for deployment_name in $@; do
163         wait_deployment $deployment_name
164     done
165 }
166
167 # teardown() - Base testing teardown function
168 function teardown {
169     for deployment_name in $@; do
170         destroy_deployment $deployment_name
171     done
172 }
173 test_folder=${FUNCTIONS_DIR}