Merge "Refactor Functional tests"
[multicloud/k8s.git] / vagrant / 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 _checks_args {
16     if [[ -z $1 ]]; then
17         echo "Missing CSAR ID argument"
18         exit 1
19     fi
20     if [[ -z $CSAR_DIR ]]; then
21         echo "CSAR_DIR global environment value is empty"
22         exit 1
23     fi
24     mkdir -p ${CSAR_DIR}/${1}
25 }
26
27 # destroy_deployment() - This function ensures that a specific deployment is
28 # destroyed in Kubernetes
29 function destroy_deployment {
30     local deployment_name=$1
31
32     kubectl delete deployment $deployment_name --ignore-not-found=true --now
33     while kubectl get deployment $deployment_name &>/dev/null; do
34         echo "$(date +%H:%M:%S) - $deployment_name : Destroying deployment"
35     done
36 }
37
38 # recreate_deployment() - This function destroys an existing deployment and
39 # creates an new one based on its yaml file
40 function recreate_deployment {
41     local deployment_name=$1
42
43     destroy_deployment $deployment_name
44     kubectl create -f $deployment_name.yaml
45 }
46
47 # wait_deployment() - Wait process to Running status on the Deployment's pods
48 function wait_deployment {
49     local deployment_name=$1
50
51     status_phase=""
52     while [[ $status_phase != "Running" ]]; do
53         new_phase=$(kubectl get pods | grep  $deployment_name | awk '{print $3}')
54         if [[ $new_phase != $status_phase ]]; then
55             echo "$(date +%H:%M:%S) - $deployment_name : $new_phase"
56             status_phase=$new_phase
57         fi
58         if [[ $new_phase == "Err"* ]]; then
59             exit 1
60         fi
61     done
62 }
63
64 # setup() - Base testing setup shared among functional tests
65 function setup {
66     for deployment_name in $@; do
67         recreate_deployment $deployment_name
68     done
69
70     for deployment_name in $@; do
71         wait_deployment $deployment_name
72     done
73 }
74
75 # teardown() - Base testing teardown function
76 function teardown {
77     for deployment_name in $@; do
78         destroy_deployment $deployment_name
79     done
80 }
81
82 if ! $(kubectl version &>/dev/null); then
83     echo "This funtional test requires kubectl client"
84     exit 1
85 fi