From 6e466cbe40142c916cad7ca20553897a24357f1e Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Thu, 15 Nov 2018 09:33:16 -0500 Subject: [PATCH] Added vagrant tests --- tests/lib/test-function.sh | 21 ++++++++++++ tests/vagrant-tests.sh | 67 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 tests/lib/test-function.sh create mode 100644 tests/vagrant-tests.sh diff --git a/tests/lib/test-function.sh b/tests/lib/test-function.sh new file mode 100644 index 0000000..b2aa6ba --- /dev/null +++ b/tests/lib/test-function.sh @@ -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 +} \ No newline at end of file diff --git a/tests/vagrant-tests.sh b/tests/vagrant-tests.sh new file mode 100644 index 0000000..f662eba --- /dev/null +++ b/tests/vagrant-tests.sh @@ -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 \ No newline at end of file