Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  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.  
  11. # Exit on any individual command failure.
  12. set -e
  13.  
  14. # Pretty colors.
  15. red='\033[0;31m'
  16. green='\033[0;32m'
  17. neutral='\033[0m'
  18.  
  19. timestamp=$(date +%s)
  20.  
  21. # Allow environment variables to override defaults.
  22. distro=${distro:-"ubuntu1404"}
  23. playbook=${playbook:-"test.yml"}
  24. cleanup=${cleanup:-"true"}
  25. container_id=${container_id:-$timestamp}
  26. playbook_opts=${playbook_opts:-""}
  27.  
  28. ## Set up vars for Docker setup.
  29. # CentOS 7
  30. if [ $distro = 'centos7' ]; then
  31. image="djx339/centos-ansible:7-systemd"
  32. opts="--privileged --tmpfs /run -v /sys/fs/cgroup:/sys/fs/cgroup:ro"
  33. # CentOS 6
  34. elif [ $distro = 'centos6' ]; then
  35. image="djx339/centos-ansible:6"
  36. opts="--privileged"
  37. # Ubuntu 16.04
  38. elif [ $distro = 'ubuntu1604' ]; then
  39. image="djx339/ubuntu-ansible:16.04"
  40. opts="--privileged --tmpfs /run --tmpfs /run/lock -v /sys/fs/cgroup:/sys/fs/cgroup:ro"
  41. # Ubuntu 14.04
  42. elif [ $distro = 'ubuntu1404' ]; then
  43. image="djx339/ubuntu-ansible:14.04"
  44. opts="--privileged"
  45. # Ubuntu 12.04
  46. elif [ $distro = 'ubuntu1204' ]; then
  47. image="djx339/ubuntu-ansible:12.04"
  48. opts="--privileged"
  49. # Debian 8
  50. elif [ $distro = 'debian8' ]; then
  51. image="djx339/debian-ansible:8"
  52. opts="--privileged"
  53. # Fedora 24
  54. elif [ $distro = 'fedora24' ]; then
  55. image="djx339/fedora-ansible:24"
  56. opts="--privileged"
  57. fi
  58.  
  59. # Run the container using the supplied OS.
  60. printf ${green}"Starting Docker container: ${image}."${neutral}"\n"
  61. docker pull ${image}
  62. docker run -itd --volume="$PWD":/etc/ansible/roles/role_under_test:rw --name $container_id $opts ${image}
  63.  
  64. printf "\n"
  65.  
  66. # Install requirements if `requirements.yml` is present.
  67. if [ -f "$PWD/tests/requirements.yml" ]; then
  68. printf ${green}"Requirements file detected; installing dependencies."${neutral}"\n"
  69. docker exec --tty $container_id env TERM=xterm ansible-galaxy install -r /etc/ansible/roles/role_under_test/tests/requirements.yml
  70. fi
  71.  
  72. printf "\n"
  73.  
  74. # Test Ansible syntax.
  75. printf ${green}"Checking Ansible playbook syntax."${neutral}
  76. docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook --syntax-check
  77.  
  78. printf "\n"
  79.  
  80. # Run Ansible playbook.
  81. printf ${green}"Running command: docker exec $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook"${neutral}
  82. docker exec $container_id env TERM=xterm env ANSIBLE_FORCE_COLOR=1 ansible-playbook $playbook_opts /etc/ansible/roles/role_under_test/tests/$playbook
  83.  
  84. # Run Ansible playbook again (idempotence test).
  85. printf ${green}"Running playbook again: idempotence test"${neutral}
  86. idempotence=$(mktemp)
  87. docker exec $container_id ansible-playbook $playbook_opts /etc/ansible/roles/role_under_test/tests/$playbook | tee -a $idempotence
  88. tail $idempotence \
  89. | grep -q 'changed=0.*failed=0' \
  90. && (printf ${green}'Idempotence test: pass'${neutral}"\n") \
  91. || (printf ${red}'Idempotence test: fail'${neutral}"\n" && exit 1)
  92.  
  93. # Remove the Docker container (if configured).
  94. if [ "$cleanup" = true ]; then
  95. printf "Removing Docker container...\n"
  96. docker rm -f $container_id
  97. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement