Skip to content

Commit

Permalink
Add tests for better hook output
Browse files Browse the repository at this point in the history
  • Loading branch information
LegenJCdary committed Mar 31, 2022
1 parent a640cc8 commit 2c436f9
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ jobs:
./tests/01-test_argument_parsing.sh
./tests/02-checkrun.sh
./tests/05-permission_checks.sh
- run:
name: "Copy failing hooks"
command: cp ./tests/files/etc/hooks/* /etc/ansible-deploy/hooks
- run:
name: "Run shell script for error returning hook"
command: |
cp ./tests/files/etc/tasks_error_hook.yaml /etc/ansible-deploy/tasks.yaml
./tests/04-error_hook.sh
- run:
name: "Run shell script for error returning hook with exit"
command: |
cp ./tests/files/etc/tasks_exit_hook.yaml /etc/ansible-deploy/tasks.yaml
./tests/04-exit_hook.sh
- run:
name: "Run shell script for critical returning hook"
command: |
chmod 0644 /etc/ansible-deploy/hooks/exit_hook.sh
./tests/04-critical_hook.sh
- run:
name: "Create incompatible config file"
command: cp ./tests/files/incompatible_config.yml /etc/ansible-deploy
Expand Down
1 change: 1 addition & 0 deletions etc/hooks/setup_work_dir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ cat << END > ./prod_infra3_inv.yaml
localhost
END

echo "setup_work_dir finished succesfully"
1 change: 1 addition & 0 deletions tests/02-checkrun.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ check_message_in_output "ansible-deployer run -t task_with_commit -s testing -i
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' '\[CRITICAL\]: No playbooks found for requested task'
check_message_in_output 'ansible-deployer run -t task_exec_bin_true -s prod -i testInfra' '\[INFO\]: setup_work_dir finished succesfully'

#Artificially generate lock
check_run_ok "ansible-deployer lock -s locked -i testInfra"
Expand Down
6 changes: 6 additions & 0 deletions tests/04-critical_hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash -l


source ./tests/testing_lib.sh

check_message_in_output 'ansible-deployer run -t task_exec_bin_true -s prod -i testInfra' '\[CRITICAL\]: Failed executing /etc/ansible-deploy/hooks/exit_hook.sh'
7 changes: 7 additions & 0 deletions tests/04-error_hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash -l


source ./tests/testing_lib.sh

check_message_in_output 'ansible-deployer run -t task_exec_bin_true -s prod -i testInfra' '\[ERROR\]: /etc/ansible-deploy/hooks/error_hook.sh: line 3: dos2uni: command not found'
check_run_ok 'ansible-deployer run -t task_exec_bin_true -s prod -i testInfra'
6 changes: 6 additions & 0 deletions tests/04-exit_hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash -l


source ./tests/testing_lib.sh

check_message_in_output 'ansible-deployer run -t task_exec_bin_true -s prod -i testInfra' '\[CRITICAL\]: Setup hook exit_hook failed, cannot continue'
5 changes: 5 additions & 0 deletions tests/files/etc/hooks/error_hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash -l

dos2uni /etc/ansible-deploy/ansible-deploy.yaml

ls -l /etc/ansible-deploy/ansible-deploy.yaml
6 changes: 6 additions & 0 deletions tests/files/etc/hooks/exit_hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash -l

dos2uni /etc/ansible-deploy/ansible-deploy.yaml
ls -l /etc/ansible-deploy/ansible-deploy.yaml

exit 1
36 changes: 36 additions & 0 deletions tests/files/etc/tasks_error_hook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
setup_hooks:
- name: setup_ansible
module: script
opts:
file: /etc/ansible-deploy/hooks/setup_work_dir.sh
- name: error_hook
module: script
opts:
file: /etc/ansible-deploy/hooks/error_hook.sh

play_items:
- name: run_bin_true
file: runBinTrue.yaml

tasks:
- name: task_exec_bin_true
play_items:
- run_bin_true
allowed_for:
- group: test_group
infra:
- name: testInfra
stages:
- testing
commit:
- test_version
- group: root
infra:
- name: testInfra
stages:
- testing
- prod
- locked
commit:
- test_version
allow_limit: False
36 changes: 36 additions & 0 deletions tests/files/etc/tasks_exit_hook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
setup_hooks:
- name: setup_ansible
module: script
opts:
file: /etc/ansible-deploy/hooks/setup_work_dir.sh
- name: exit_hook
module: script
opts:
file: /etc/ansible-deploy/hooks/exit_hook.sh

play_items:
- name: run_bin_true
file: runBinTrue.yaml

tasks:
- name: task_exec_bin_true
play_items:
- run_bin_true
allowed_for:
- group: test_group
infra:
- name: testInfra
stages:
- testing
commit:
- test_version
- group: root
infra:
- name: testInfra
stages:
- testing
- prod
- locked
commit:
- test_version
allow_limit: False

0 comments on commit 2c436f9

Please sign in to comment.