daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #
  3. # Ansible role test shim.
  4. #
  5. # Usage: [OPTIONS] ./tests/test.sh
  6. #   - distro: a supported Docker distro version (default = "centos7")
  7. #   - playbook: a playbook in the tests directory (default = "test.yml")
  8. #   - cleanup: whether to remove the Docker container (default = true)
  9. #   - container_id: the --name to set for the container (default = timestamp)
  10. #   - test_idempotence: whether to test playbook's idempotence (default = true)
  11. #
  12. # License: MIT
  13.  
  14. # Exit on any individual command failure.
  15. set -e
  16.  
  17. # Pretty colors.
  18. red='\033[0;31m'
  19. green='\033[0;32m'
  20. neutral='\033[0m'
  21.  
  22. timestamp=$(date +%s)
  23.  
  24. # Allow environment variables to override defaults.
  25. distro=${distro:-"centos7"}
  26. playbook=${playbook:-"test.yml"}
  27. cleanup=${cleanup:-"true"}
  28. container_id=${container_id:-$timestamp}
  29. test_idempotence=${test_idempotence:-"true"}
  30.  
  31. ## Set up vars for Docker setup.
  32. # CentOS 7
  33. if [ $distro = 'centos7' ]; then
  34.   init="/usr/lib/systemd/systemd"
  35.   opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
  36. # CentOS 6
  37. elif [ $distro = 'centos6' ]; then
  38.   init="/sbin/init"
  39.   opts="--privileged"
  40. # Ubuntu 16.04
  41. elif [ $distro = 'ubuntu1604' ]; then
  42.   init="/lib/systemd/systemd"
  43.   opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
  44. # Ubuntu 14.04
  45. elif [ $distro = 'ubuntu1404' ]; then
  46.   init="/sbin/init"
  47.   opts="--privileged"
  48. # Ubuntu 12.04
  49. elif [ $distro = 'ubuntu1204' ]; then
  50.   init="/sbin/init"
  51.   opts="--privileged"
  52. # Debian 9
  53. elif [ $distro = 'debian9' ]; then
  54.   init="/lib/systemd/systemd"
  55.   opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
  56. # Debian 8
  57. elif [ $distro = 'debian8' ]; then
  58.   init="/lib/systemd/systemd"
  59.   opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
  60. # Fedora 24
  61. elif [ $distro = 'fedora24' ]; then
  62.   init="/usr/lib/systemd/systemd"
  63.   opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
  64. # Fedora 27
  65. elif [ $distro = 'fedora27' ]; then
  66.   init="/usr/lib/systemd/systemd"
  67.   opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
  68. fi
  69.  
  70. # Run the container using the supplied OS.
  71. printf ${green}"Starting Docker container: buluma/docker-$distro-ansible."${neutral}"\n"
  72. docker pull buluma/docker-$distro-ansible:latest
  73. docker run --detach --volume="$PWD":/etc/ansible/roles/role_under_test:rw --name $container_id $opts buluma/docker-$distro-ansible:latest $init
  74.  
  75. printf "\n"
  76.  
  77. # Install requirements if `requirements.yml` is present.
  78. if [ -f "$PWD/tests/requirements.yml" ]; then
  79.   printf ${green}"Requirements file detected; installing dependencies."${neutral}"\n"
  80.   docker exec --tty $container_id env TERM=xterm ansible-galaxy install -r /etc/ansible/roles/role_under_test/tests/requirements.yml
  81. fi
  82.  
  83. printf "\n"
  84.  
  85. # Test Ansible syntax.
  86. printf ${green}"Checking Ansible playbook syntax."${neutral}
  87. docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook --syntax-check
  88.  
  89. printf "\n"
  90.  
  91. # Run Ansible playbook.
  92. printf ${green}"Running command: docker exec $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook"${neutral}
  93. docker exec $container_id env TERM=xterm env ANSIBLE_FORCE_COLOR=1 ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook
  94.  
  95. if [ "$test_idempotence" = true ]; then
  96.   # Run Ansible playbook again (idempotence test).
  97.   printf ${green}"Running playbook again: idempotence test"${neutral}
  98.   idempotence=$(mktemp)
  99.   docker exec $container_id ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook | tee -a $idempotence
  100.   tail $idempotence \
  101.     | grep -q 'changed=0.*failed=0' \
  102.     && (printf ${green}'Idempotence test: pass'${neutral}"\n") \
  103.     || (printf ${red}'Idempotence test: fail'${neutral}"\n" && exit 1)
  104. fi
  105.  
  106. # Remove the Docker container (if configured).
  107. if [ "$cleanup" = true ]; then
  108.   printf "Removing Docker container...\n"
  109.   docker rm -f $container_id
  110. fi
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top