|
| 1 | +#!/usr/bin/env roundup |
| 2 | +# |
| 3 | + |
| 4 | +: ${RUNDECK_USER?"environment variable not set."} |
| 5 | +: ${RUNDECK_PROJECT?"environment variable not set."} |
| 6 | +: ${REMOTE_NODE?"environment variable not set."} |
| 7 | + |
| 8 | +# Let's get started |
| 9 | +# ----------------- |
| 10 | + |
| 11 | +# Helpers |
| 12 | +# ------------ |
| 13 | + |
| 14 | +#. ./include.sh |
| 15 | + |
| 16 | + |
| 17 | +# The Plan |
| 18 | +# -------- |
| 19 | +describe "load jobs: valid use of ansible plugin" |
| 20 | +JOBID="4fd7f51b-ed22-479e-8785-8393411c0106" |
| 21 | +RUN_MODULE_JOB="$JOBID" |
| 22 | +JOBID2="72d84cce-0eac-49dc-9a16-1515453e3604" |
| 23 | +RUN_PLAYBOOK_JOB="$JOBID2" |
| 24 | +JOBID3="df5fa3e8-5891-456e-a8da-7d3f81f7b644" |
| 25 | +RUN_SCRIPT_JOB="$JOBID3" |
| 26 | + |
| 27 | +it_should_list_ansible_nodes() { |
| 28 | + |
| 29 | + |
| 30 | + bash -c "rd nodes list -p $RUNDECK_PROJECT " |
| 31 | + |
| 32 | +} |
| 33 | +it_should_have_test_ansible_node() { |
| 34 | + |
| 35 | + # Parse lines into array elements. |
| 36 | + IFS=$'\n\t' |
| 37 | + |
| 38 | + cmdout=($(bash -c "rd nodes list -p $RUNDECK_PROJECT -F test-ansible-node | grep -v '^#' ")) |
| 39 | + expout=( test-ansible-node ) |
| 40 | + echo "${cmdout[@]}" |
| 41 | + if ! test ${#expout[*]} = ${#cmdout[*]} |
| 42 | + then |
| 43 | + echo "FAIL: command output did not contain ${#expout[*]} lines. Contained ${#cmdout[*]}: ${cmdout[*]}" |
| 44 | + echo cmdout was: ${cmdout[@]} |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | + test "${cmdout[@]}" = "${expout[@]}" |
| 48 | +} |
| 49 | +it_should_have_two_ansible_nodes() { |
| 50 | + |
| 51 | + # Parse lines into array elements. |
| 52 | + IFS=$'\n\t' |
| 53 | + |
| 54 | + cmdout=($(bash -c "rd nodes list -p $RUNDECK_PROJECT -F tags:ansible-nodes | grep -v '^#' ")) |
| 55 | + expout=( $RUNDECK_NODE test-ansible-node ) |
| 56 | + echo "${cmdout[@]}" |
| 57 | + if ! test ${#expout[*]} = ${#cmdout[*]} |
| 58 | + then |
| 59 | + echo "FAIL: command output did not contain ${#expout[*]} lines. Contained ${#cmdout[*]}: ${cmdout[*]}" |
| 60 | + echo cmdout was: ${cmdout[@]} |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + let max=$(( ${#cmdout[*]} - 1 )) |
| 64 | + for i in $(seq 0 $max) ; do |
| 65 | + test "${cmdout[$i]}" = "${expout[$i]}" |
| 66 | + done |
| 67 | +} |
| 68 | +it_should_load_ansible_jobs() { |
| 69 | + |
| 70 | + # load job file |
| 71 | + bash -c "rd jobs load -p $RUNDECK_PROJECT --format xml -f $HOME/atest/ansible-jobs.xml" |
| 72 | + |
| 73 | + |
| 74 | + # verify jobs exist |
| 75 | + |
| 76 | + bash -c "rd jobs list -p $RUNDECK_PROJECT -i $JOBID -% '%id'" |
| 77 | + bash -c "rd jobs list -p $RUNDECK_PROJECT -i $JOBID2 -% '%id'" |
| 78 | + bash -c "rd jobs list -p $RUNDECK_PROJECT -i $JOBID3 -% '%id'" |
| 79 | +} |
| 80 | + |
| 81 | +it_should_run_ansible_module_step(){ |
| 82 | + bash -c "rd run -p $RUNDECK_PROJECT -i $RUN_MODULE_JOB -f | grep -v ^#" > test.output |
| 83 | + # diff with expected |
| 84 | + cat >expected.output <<END |
| 85 | +
|
| 86 | +Using /etc/ansible/ansible.cfg as config file |
| 87 | +test-ansible-node | SUCCESS => { |
| 88 | + "changed": false, |
| 89 | + "ping": "pong" |
| 90 | +} |
| 91 | +rundeck1 | SUCCESS => { |
| 92 | + "changed": false, |
| 93 | + "ping": "pong" |
| 94 | +} |
| 95 | +END |
| 96 | + # set +e |
| 97 | + # diff expected.output test.output |
| 98 | + # result=$? |
| 99 | + # set -e |
| 100 | + cat test.output |
| 101 | + grep 'test-ansible-node | SUCCESS => {' test.output || ( echo "Expected output not seen" && exit 2 ) |
| 102 | + grep "$RUNDECK_NODE | SUCCESS => {" test.output || ( echo "Expected output not seen" && exit 2 ) |
| 103 | +} |
| 104 | +it_should_run_ansible_playbook_step(){ |
| 105 | + bash -c "rd run -p $RUNDECK_PROJECT -i $RUN_PLAYBOOK_JOB -f | grep -v ^#" > test.output |
| 106 | + # diff with expected |
| 107 | + |
| 108 | + cat test.output |
| 109 | + grep 'PLAY \[An example playbook\]' test.output || ( echo "Expected output not seen" && exit 2 ) |
| 110 | + grep 'ok: \[test-ansible-node\]' test.output || ( echo "Expected output not seen" && exit 2 ) |
| 111 | + grep "ok: \[$RUNDECK_NODE\]" test.output || ( echo "Expected output not seen" && exit 2 ) |
| 112 | + grep "TASK \[print the current date and time\]" test.output || ( echo "Expected output not seen" && exit 2 ) |
| 113 | + grep "changed: \[$RUNDECK_NODE\] =>" test.output || ( echo "Expected output not seen" && exit 2 ) |
| 114 | + grep "changed: \[test-ansible-node\] =>" test.output || ( echo "Expected output not seen" && exit 2 ) |
| 115 | + |
| 116 | +} |
| 117 | +it_should_run_ansible_script_step(){ |
| 118 | + bash -c "rd run -p $RUNDECK_PROJECT -i $RUN_SCRIPT_JOB -f --outformat '%node: %log' | grep -v ^#" > test.output |
| 119 | + # diff with expected |
| 120 | + |
| 121 | + grep "$RUNDECK_NODE: DATE" test.output || ( echo "Expected output not seen" && exit 2 ) |
| 122 | + grep "$RUNDECK_NODE: UNAME" test.output || ( echo "Expected output not seen" && exit 2 ) |
| 123 | + grep "test-ansible-node: DATE" test.output || ( echo "Expected output not seen" && exit 2 ) |
| 124 | + grep "test-ansible-node: UNAME" test.output || ( echo "Expected output not seen" && exit 2 ) |
| 125 | + |
| 126 | +} |
0 commit comments