Merge "Remove -a build flag"
[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 function print_msg {
16     local msg=$1
17     local RED='\033[0;31m'
18     local NC='\033[0m'
19
20     echo -e "${RED} $msg ---------------------------------------${NC}"
21 }
22
23 function _get_ovn_central_address {
24     ansible_ifconfig=$(ansible ovn-central[0] -i $test_folder/../hosting_providers/vagrant/inventory/hosts.ini -m shell -a "ifconfig eth1 |grep \"inet addr\" |awk '{print \$2}' |awk -F: '{print \$2}'")
25     if [[ $ansible_ifconfig != *CHANGED* ]]; then
26         echo "Fail to get the OVN central IP address from eth1 nic"
27         exit
28     fi
29     echo "$(echo ${ansible_ifconfig#*>>} | tr '\n' ':')6641"
30 }
31
32 # install_ovn_deps() - Install dependencies required for tests that require OVN
33 function install_ovn_deps {
34     if ! $(yq --version &>/dev/null); then
35         sudo -E pip install yq
36     fi
37     if ! $(ovn-nbctl --version &>/dev/null); then
38         source /etc/os-release || source /usr/lib/os-release
39         case ${ID,,} in
40             *suse)
41             ;;
42             ubuntu|debian)
43                 sudo apt-get install -y apt-transport-https
44                 echo "deb https://packages.wand.net.nz $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/wand.list
45                 sudo curl https://packages.wand.net.nz/keyring.gpg -o /etc/apt/trusted.gpg.d/wand.gpg
46                 sudo apt-get update
47                 sudo apt install -y ovn-common
48             ;;
49             rhel|centos|fedora)
50             ;;
51         esac
52     fi
53 }
54
55 # init_network() - This function creates the OVN resouces required by the test
56 function init_network {
57     local fname=$1
58     local router_name="ovn4nfv-master"
59
60     name=$(cat $fname | yq '.spec.name' | xargs)
61     subnet=$(cat $fname  | yq '.spec.subnet' | xargs)
62     gateway=$(cat $fname  | yq '.spec.gateway' | xargs)
63     ovn_central_address=$(_get_ovn_central_address)
64
65     router_mac=$(printf '00:00:00:%02X:%02X:%02X' $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)))
66     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
67     ovn-nbctl --may-exist --db tcp:$ovn_central_address lrp-add $router_name rtos-$name $router_mac $gateway
68     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\"
69 }
70
71 # cleanup_network() - This function removes the OVN resources created for the test
72 function cleanup_network {
73     local fname=$1
74
75     name=$(cat $fname | yq '.spec.name' | xargs)
76     ovn_central_address=$(_get_ovn_central_address)
77
78     for cmd in "ls-del $name" "lrp-del rtos-$name" "lsp-del stor-$name"; do
79         ovn-nbctl --if-exist --db tcp:$ovn_central_address $cmd
80     done
81 }
82
83 function _checks_args {
84     if [[ -z $1 ]]; then
85         echo "Missing CSAR ID argument"
86         exit 1
87     fi
88     if [[ -z $CSAR_DIR ]]; then
89         echo "CSAR_DIR global environment value is empty"
90         exit 1
91     fi
92     mkdir -p ${CSAR_DIR}/${1}
93 }
94
95 # destroy_deployment() - This function ensures that a specific deployment is
96 # destroyed in Kubernetes
97 function destroy_deployment {
98     local deployment_name=$1
99
100     echo "$(date +%H:%M:%S) - $deployment_name : Destroying deployment"
101     kubectl delete deployment $deployment_name --ignore-not-found=true --now
102     while kubectl get deployment $deployment_name &>/dev/null; do
103         echo "$(date +%H:%M:%S) - $deployment_name : Destroying deployment"
104     done
105 }
106
107 # recreate_deployment() - This function destroys an existing deployment and
108 # creates an new one based on its yaml file
109 function recreate_deployment {
110     local deployment_name=$1
111
112     destroy_deployment $deployment_name
113     kubectl create -f $deployment_name.yaml
114 }
115
116 # wait_deployment() - Wait process to Running status on the Deployment's pods
117 function wait_deployment {
118     local deployment_name=$1
119
120     status_phase=""
121     while [[ $status_phase != "Running" ]]; do
122         new_phase=$(kubectl get pods | grep  $deployment_name | awk '{print $3}')
123         if [[ $new_phase != $status_phase ]]; then
124             echo "$(date +%H:%M:%S) - $deployment_name : $new_phase"
125             status_phase=$new_phase
126         fi
127         if [[ $new_phase == "Err"* ]]; then
128             exit 1
129         fi
130     done
131 }
132
133 # setup() - Base testing setup shared among functional tests
134 function setup {
135     for deployment_name in $@; do
136         recreate_deployment $deployment_name
137     done
138     sleep 5
139     for deployment_name in $@; do
140         wait_deployment $deployment_name
141     done
142 }
143
144 # teardown() - Base testing teardown function
145 function teardown {
146     for deployment_name in $@; do
147         destroy_deployment $deployment_name
148     done
149 }
150
151 if ! $(kubectl version &>/dev/null); then
152     echo "This funtional test requires kubectl client"
153     exit 1
154 fi
155 test_folder=$(pwd)