mirror of
https://github.com/jcwimer/kubernetes-ansible
synced 2026-05-21 02:49:24 +00:00
Added vagrant tests
This commit is contained in:
21
tests/lib/test-function.sh
Normal file
21
tests/lib/test-function.sh
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
red=`tput setaf 1`
|
||||||
|
green=`tput setaf 2`
|
||||||
|
reset=`tput sgr0`
|
||||||
|
#echo "${red}red text ${green}green text${reset}"
|
||||||
|
|
||||||
|
function testbash() {
|
||||||
|
local name="${1}"
|
||||||
|
shift
|
||||||
|
local command="${@}"
|
||||||
|
eval $command
|
||||||
|
local return=$?
|
||||||
|
if [[ ! $return -eq 0 ]]; then
|
||||||
|
echo "${red}FAILED: ${name}${reset}"
|
||||||
|
return 1
|
||||||
|
else
|
||||||
|
echo "${green}PASSED: $name${reset}"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
}
|
||||||
67
tests/vagrant-tests.sh
Normal file
67
tests/vagrant-tests.sh
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
project_dir="$(dirname $( dirname $(readlink -f ${BASH_SOURCE[0]})))"
|
||||||
|
source ${project_dir}/tests/lib/test-function.sh
|
||||||
|
|
||||||
|
function main {
|
||||||
|
cd ${project_dir}
|
||||||
|
build-infrastructure
|
||||||
|
run-tests
|
||||||
|
echo Testing idempotency.
|
||||||
|
run-tests
|
||||||
|
destroy-infrastructure
|
||||||
|
}
|
||||||
|
|
||||||
|
function build-infrastructure {
|
||||||
|
trap "echo 'vagrant up failed'; destroy-infrastructure; exit 1" ERR
|
||||||
|
echo Building vagrant infrastructure
|
||||||
|
vagrant up > /dev/null
|
||||||
|
trap - ERR
|
||||||
|
}
|
||||||
|
|
||||||
|
function run-ssh-command {
|
||||||
|
local machine="${1}"
|
||||||
|
local command="${2}"
|
||||||
|
vagrant ssh "${machine}" -c "${command}"
|
||||||
|
}
|
||||||
|
|
||||||
|
function run-tests {
|
||||||
|
trap "destroy-infrastructure; exit 1" ERR
|
||||||
|
|
||||||
|
testbash "Running command on a vagrant node should not fail." \
|
||||||
|
"run-ssh-command client 'ls /vagrant'"
|
||||||
|
|
||||||
|
testbash "Client vagrant machine can ssh into bootstrap." \
|
||||||
|
"run-ssh-command client 'ssh -o StrictHostKeyChecking=no -i /home/vagrant/test_rsa vagrant@192.168.254.2 ls'"
|
||||||
|
|
||||||
|
testbash "Running deploy script should not fail." \
|
||||||
|
"run-ssh-command client 'bash /vagrant/tests/files/test-deploy.sh'"
|
||||||
|
|
||||||
|
echo Giving containers time to start up.
|
||||||
|
sleep 90s
|
||||||
|
|
||||||
|
local kubectl_config="export KUBECONFIG=/home/vagrant/admin.conf"
|
||||||
|
local curl_params="--silent --fail --max-time 10"
|
||||||
|
|
||||||
|
number_of_ready_nodes=$(run-ssh-command master "${kubectl_config}; kubectl get nodes | grep -v STATUS | grep Ready | wc -l")
|
||||||
|
testbash "There should be 4 nodes in Ready state." \
|
||||||
|
"test ${number_of_ready_nodes} -eq 4"
|
||||||
|
|
||||||
|
traefik_port=$(run-ssh-command master "${kubectl_config}; kubectl get service -n kube-system | grep traefik | awk {'print $5'} | cut -d , -f 2 | cut -d : -f 2 | cut -d / -f 1")
|
||||||
|
testbash "Traefik should be reachable from worker1 node." \
|
||||||
|
"run-ssh-command client 'curl ${curl_params} http://192.168.254.3:${traefik_port}/dashboard/ > /dev/null'"
|
||||||
|
testbash "Traefik should be reachable from worker2 node." \
|
||||||
|
"run-ssh-command client 'curl ${curl_params} http://192.168.254.3:${traefik_port}/dashboard/ > /dev/null'"
|
||||||
|
|
||||||
|
trap - ERR
|
||||||
|
}
|
||||||
|
|
||||||
|
function destroy-infrastructure {
|
||||||
|
trap "echo 'vagrant destroy failed'; exit 1" ERR
|
||||||
|
echo Tearing down vagrant infrastructure
|
||||||
|
vagrant destroy -f > /dev/null
|
||||||
|
trap - ERR
|
||||||
|
}
|
||||||
|
|
||||||
|
main
|
||||||
|
exit 0
|
||||||
Reference in New Issue
Block a user