diff --git a/tests/01-test_argument_parsing.sh b/tests/01-test_argument_parsing.sh index f9cfbb56..a541db88 100755 --- a/tests/01-test_argument_parsing.sh +++ b/tests/01-test_argument_parsing.sh @@ -1,32 +1,9 @@ #!/bin/bash -l -check_message_in_output() { - CMD=$1 - EXPTEXT=$2 - echo "Check: $CMD" - eval "$CMD |& grep '$EXPTEXT'" - if [ $? -eq 0 ] - then - echo "OK: '${CMD}' returned '${EXPTEXT}'" - else - echo "FAILED: '${CMD}' didn't return '${EXPTEXT}'" - exit 1 - fi -} -check_run_ok() { - CMD=$1 - echo "Check: $CMD" - $CMD - if [ $? -ne 0 ] - then - echo "FAILED: ${CMD}" - exit 1 - else - echo "OK" - fi -} +source ./tests/testing_lib.sh +echo -e " ___ _ _ _\n / _ \/ | __ _ _ __ __ _ _ _ _ __ ___ ___ _ __ | |_ _ __ __ _ _ __ ___(_)_ __ __ _\n | | | | | _____ / _\` | '__/ _\` | | | | '_ \` _ \ / _ \ '_ \| __| | '_ \ / _\` | '__/ __| | '_ \ / _\` |\n | |_| | | |_____| | (_| | | | (_| | |_| | | | | | | __/ | | | |_ | |_) | (_| | | \__ \ | | | | (_| |\n \___/|_| \__,_|_| \__, |\__,_|_| |_| |_|\___|_| |_|\__| | .__/ \__,_|_| |___/_|_| |_|\__, |\n |___/ |_| |___/\n _ _ _ _\n __ ___ __ ___ _ __ __ _ ___ ___ _ __ ___ | |__ (_)_ __ __ _| |_(_) ___ _ __ ___\n \ \ /\ / / '__/ _ \| '_ \ / _\` | / __/ _ \| '_ \` _ \| '_ \| | '_ \ / _\` | __| |/ _ \| '_ \/ __|\n \ V V /| | | (_) | | | | (_| | | (_| (_) | | | | | | |_) | | | | | (_| | |_| | (_) | | | \__ \\n \_/\_/ |_| \___/|_| |_|\__, | \___\___/|_| |_| |_|_.__/|_|_| |_|\__,_|\__|_|\___/|_| |_|___/\n |___/\n \n" #Check wrong combinations check_message_in_output 'ansible-deployer' 'Too few arguments' check_message_in_output 'ansible-deployer run' '\[ERROR\]: task is required for run' @@ -51,6 +28,7 @@ check_message_in_output 'ansible-deployer unlock -t testTask -l test_hosts_1' '\ check_message_in_output 'ansible-deployer list --commit testTask' '\[ERROR\]: commit is not supported by list' check_message_in_output 'ansible-deployer list -l test_hosts_1' '\[ERROR\]: limit is not supported by list' +echo -e " ___ _ _ _\n / _ \/ | __ _ _ __ __ _ _ _ _ __ ___ ___ _ __ | |_ _ __ __ _ _ __ ___(_)_ __ __ _\n | | | | | _____ / _\` | '__/ _\` | | | | '_ \` _ \ / _ \ '_ \| __| | '_ \ / _\` | '__/ __| | '_ \ / _\` |\n | |_| | | |_____| | (_| | | | (_| | |_| | | | | | | __/ | | | |_ | |_) | (_| | | \__ \ | | | | (_| |\n \___/|_| \__,_|_| \__, |\__,_|_| |_| |_|\___|_| |_|\__| | .__/ \__,_|_| |___/_|_| |_|\__, |\n |___/ |_| |___/\n _ _ _ _ _\n ___ ___ _ __ _ __ ___ ___| |_ ___ ___ _ __ ___ | |__ (_)_ __ __ _| |_(_) ___ _ __ ___\n / __/ _ \| '__| '__/ _ \/ __| __| / __/ _ \| '_ \` _ \| '_ \| | '_ \ / _\` | __| |/ _ \| '_ \/ __|\n | (_| (_) | | | | | __/ (__| |_ | (_| (_) | | | | | | |_) | | | | | (_| | |_| | (_) | | | \__ \\n \___\___/|_| |_| \___|\___|\__| \___\___/|_| |_| |_|_.__/|_|_| |_|\__,_|\__|_|\___/|_| |_|___/\n \n" #Check if correct combinations are accepted check_run_ok "ansible-deployer run --dry -t task_exec_bin_true -s prod -i testInfra" check_run_ok "ansible-deployer run --dry -t task_with_limit -s testing -i testInfra -l testHost1" @@ -60,5 +38,6 @@ check_run_ok "ansible-deployer unlock --dry -s prod -i testInfra" check_run_ok "ansible-deployer list" check_run_ok "ansible-deployer list --task=task_exec_bin_true" +echo -e " ___ _ _ _\n / _ \/ | __ _ _ __ __ _ _ _ _ __ ___ ___ _ __ | |_ _ __ __ _ _ __ ___(_)_ __ __ _\n | | | | | _____ / _\` | '__/ _\` | | | | '_ \` _ \ / _ \ '_ \| __| | '_ \ / _\` | '__/ __| | '_ \ / _\` |\n | |_| | | |_____| | (_| | | | (_| | |_| | | | | | | __/ | | | |_ | |_) | (_| | | \__ \ | | | | (_| |\n \___/|_| \__,_|_| \__, |\__,_|_| |_| |_|\___|_| |_|\__| | .__/ \__,_|_| |___/_|_| |_|\__, |\n |___/ |_| |___/\n __ _\n __ ___ __ ___ _ __ __ _ ___ ___ _ __ / _(_) __ _\n \ \ /\ / / '__/ _ \| '_ \ / _\` | / __/ _ \| '_ \| |_| |/ _\` |\n \ V V /| | | (_) | | | | (_| | | (_| (_) | | | | _| | (_| |\n \_/\_/ |_| \___/|_| |_|\__, | \___\___/|_| |_|_| |_|\__, |\n |___/ |___/\n \n" #Check if wrong config is rejected check_message_in_output 'ansible-deployer run --dry -t task_exec_bin_true -i nonExistingInfra -s prod' '\[ERROR\]: nonExistingInfra not found in configuration file' diff --git a/tests/02-checkrun.sh b/tests/02-checkrun.sh index d2b1f8a1..37985532 100755 --- a/tests/02-checkrun.sh +++ b/tests/02-checkrun.sh @@ -1,46 +1,9 @@ #!/bin/bash -l -check_message_in_output() { - CMD=$1 - EXPTEXT=$2 - echo "Check: $CMD" - eval "$CMD |& grep '$EXPTEXT'" - if [ $? -eq 0 ] - then - echo "OK: '${CMD}' returned '${EXPTEXT}'" - else - echo "FAILED: '${CMD}' didn't return '${EXPTEXT}'" - exit 1 - fi -} -check_message_not_in_output() { - CMD=$1 - EXPTEXT=$2 - echo "Check: $CMD" - eval "$CMD |& grep '$EXPTEXT'" - if [ $? -eq 0 ] - then - echo "FAILED: '${CMD}' returned '${EXPTEXT}'" - exit 1 - else - echo "OK: '${CMD}' didn't return '${EXPTEXT}'" - fi -} - -check_run_ok() { - CMD=$1 - echo "Check: $CMD" - $CMD - if [ $? -ne 0 ] - then - echo "FAILED: ${CMD}" - exit 1 - else - echo "OK" - fi -} +source ./tests/testing_lib.sh +echo -e " ___ ____ _ _\n / _ \___ \ _____ _____ ___ _ _| |_(_) ___ _ __\n | | | |__) | _____ / _ \ \/ / _ \/ __| | | | __| |/ _ \| '_ \ \n | |_| / __/ |_____| | __/> < __/ (__| |_| | |_| | (_) | | | |\n \___/_____| \___/_/\_\___|\___|\__,_|\__|_|\___/|_| |_|\n \n _\n ___ ___ _ __ _ __ ___ ___| |_ _____ _____ ___\n / __/ _ \| '__| '__/ _ \/ __| __| / _ \ \/ / _ \/ __|\n | (_| (_) | | | | | __/ (__| |_ | __/> < __/ (__\n \___\___/|_| |_| \___|\___|\__| \___/_/\_\___|\___|\n \n" # Correct execution. check_run_ok "ansible-deployer run -t task_exec_bin_true -s prod -i testInfra" check_run_ok "ansible-deployer run -t task_with_limit -s testing -i testInfra -l testHost1" @@ -48,11 +11,25 @@ check_run_ok "ansible-deployer run -t tagged_task_true -s testing -i testInfra" # # multiple hosts in limit check_run_ok "ansible-deployer run -t task_with_limit -s testing -i testInfra2 -l xyzHosts" +echo -e " ___ ____ _ _\n / _ \___ \ _____ _____ ___ _ _| |_(_) ___ _ __\n | | | |__) | _____ / _ \ \/ / _ \/ __| | | | __| |/ _ \| '_ \ \n | |_| / __/ |_____| | __/> < __/ (__| |_| | |_| | (_) | | | |\n \___/_____| \___/_/\_\___|\___|\__,_|\__|_|\___/|_| |_|\n \n _ _ _ _ _ _\n (_)_ ____ ____ _| (_) __| | ___ _ __ | |_(_) ___ _ __ ___\n | | '_ \ \ / / _\` | | |/ _\` | / _ \| '_ \| __| |/ _ \| '_ \/ __|\n | | | | \ V / (_| | | | (_| | | (_) | |_) | |_| | (_) | | | \__ \\n |_|_| |_|\_/ \__,_|_|_|\__,_| \___/| .__/ \__|_|\___/|_| |_|___/\n |_|\n \n" # Non-existent option values check_message_in_output 'ansible-deployer run -t task_exec_bin_ERR -s prod -i testInfra' '\[ERROR\]: task_exec_bin_ERR not found in configuration file.' check_message_in_output 'ansible-deployer run -t task_exec_bin_true -s prod -i testInfra_ERR' '\[ERROR\]: testInfra_ERR not found in configuration file.' check_message_in_output 'ansible-deployer run -t task_exec_bin_true -s prod_ERR -i testInfra' '\[ERROR\]: prod_ERR not found in configuration file.' +echo -e " ___ ____ _ _\n / _ \___ \ _____ _____ ___ _ _| |_(_) ___ _ __\n | | | |__) | _____ / _ \ \/ / _ \/ __| | | | __| |/ _ \| '_ \ \n | |_| / __/ |_____| | __/> < __/ (__| |_| | |_| | (_) | | | |\n \___/_____| \___/_/\_\___|\___|\__,_|\__|_|\___/|_| |_|\n \n _ _ _\n ___| | _(_)_ __ _ __ (_)_ __ __ _\n / __| |/ / | '_ \| '_ \| | '_ \ / _\` |\n \__ \ <| | |_) | |_) | | | | | (_| |\n |___/_|\_\_| .__/| .__/|_|_| |_|\__, |\n |_| |_| |___/\n \n" +# Check infra/stage skipping +# # Sometimes skip (depending on stage) +check_message_in_output "ansible-deployer run -t task_skipping -s testing -i testInfra" "\[INFO\]: Skipping playbook" +check_message_in_output "ansible-deployer run -t task_skipping -s prod -i testInfra" "ran succesfully" +# # Always skip +check_message_not_in_output "ansible-deployer run -t task_skipping -s testing -i testInfra2" "ran succesfully"# # Never skip +check_message_not_in_output "ansible-deployer run -t task_skipping -s prod -i testInfra3" "\[INFO\]: Skipping playbook" +# # unlock infra for further tests +check_run_ok "ansible-deployer unlock -s testing -i testInfra" +check_run_ok "ansible-deployer unlock -s testing -i testInfra2" + +echo -e " ___ ____ _ _\n / _ \___ \ _____ _____ ___ _ _| |_(_) ___ _ __\n | | | |__) | _____ / _ \ \/ / _ \/ __| | | | __| |/ _ \| '_ \ \n | |_| / __/ |_____| | __/> < __/ (__| |_| | |_| | (_) | | | |\n \___/_____| \___/_/\_\___|\___|\__,_|\__|_|\___/|_| |_|\n \n _ _\n ___ | |_| |__ ___ _ __ ___\n / _ \| __| '_ \ / _ \ '__/ __|\n | (_) | |_| | | | __/ | \__ \\n \___/ \__|_| |_|\___|_| |___/\n \n" # misc check_message_in_output 'ansible-deployer run -t task_empty -s testing -i testInfra' '\[ERROR\]: No playbooks found for requested task' @@ -65,8 +42,6 @@ check_run_ok "ansible-deployer list --debug" "\[DEBUG\]: load_configuration call # Check --limit option check_message_in_output 'ansible-deployer run -t task_with_limit -s testing -i testInfra2 -l xyzHost4' 'ERROR\! Specified hosts and/or --limit does not match any hosts' -# # unlock infra for further tests -check_run_ok "ansible-deployer unlock -s testing -i testInfra" #Try execution of task without permissions if [ $UID -ne 0 ] @@ -76,11 +51,3 @@ else check_message_in_output "ansible-deployer run -t non_root_task -i testInfra -s testing" "\[ERROR\]: Task forbidden" fi -# Check infra/stage skipping -# # Sometimes skip (depending on stage) -check_message_in_output "ansible-deployer run -t task_skipping -s testing -i testInfra" "\[INFO\]: Skipping playbook" -check_message_in_output "ansible-deployer run -t task_skipping -s prod -i testInfra" "ran succesfully" -# # Always skip -check_message_not_in_output "ansible-deployer run -t task_skipping -s testing -i testInfra2" "ran succesfully" -# # Never skip -check_message_not_in_output "ansible-deployer run -t task_skipping -s prod -i testInfra3" "\[INFO\]: Skipping playbook" diff --git a/tests/testing_lib.sh b/tests/testing_lib.sh new file mode 100644 index 00000000..9d567a96 --- /dev/null +++ b/tests/testing_lib.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +check_run_ok() { + CMD=$1 + echo "Check: $CMD" + $CMD + if [ $? -ne 0 ] + then + echo "FAILED: ${CMD}" + exit 1 + else + echo "OK" + fi +} + +check_message_in_output() { + CMD=$1 + EXPTEXT=$2 + echo "Check: $CMD" + eval "$CMD |& grep '$EXPTEXT'" + if [ $? -eq 0 ] + then + echo "OK: '${CMD}' returned '${EXPTEXT}'" + else + echo "FAILED: '${CMD}' didn't return '${EXPTEXT}'" + exit 1 + fi +} + +check_message_not_in_output() { + CMD=$1 + EXPTEXT=$2 + echo "Check: $CMD" + eval "$CMD |& grep '$EXPTEXT'" + if [ $? -eq 0 ] + then + echo "FAILED: '${CMD}' returned '${EXPTEXT}'" + exit 1 + else + echo "OK: '${CMD}' didn't return '${EXPTEXT}'" + fi +} +