Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing improvements #38

Merged
merged 2 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions tests/01-test_argument_parsing.sh
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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"
Expand All @@ -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'
65 changes: 16 additions & 49 deletions tests/02-checkrun.sh
Original file line number Diff line number Diff line change
@@ -1,58 +1,35 @@
#!/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"
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'

Expand All @@ -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 ]
Expand All @@ -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"
43 changes: 43 additions & 0 deletions tests/testing_lib.sh
Original file line number Diff line number Diff line change
@@ -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
}