Skip to content

Commit

Permalink
Change cwd when --local-checkout enabled
Browse files Browse the repository at this point in the history
Add new test file and adjust circleci
Add functions to testing lib: find_latest_sequence, check_file_startingwith_in_dir
  • Loading branch information
LegenJCdary committed Apr 21, 2022
1 parent 133378a commit 54d1bbd
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jobs:
command: |
./tests/01-test_argument_parsing.sh
./tests/02-checkrun.sh
mkdir -p /etc/alt-workdir
./tests/02a-checkrun_with_self_setup.sh
rm -rf /etc/alt-workdir
mv /etc/ansible-deployer /etc/alternate-deployer-dir
mkdir -p /etc/ansible-deployer/hooks
cp ./etc/hooks/setup_work_dir.sh /etc/ansible-deployer/hooks/
Expand Down
3 changes: 0 additions & 3 deletions source/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,12 @@ def main():
validators.validate_options(options)
selected_items = validators.validate_option_values_against_config(config, options)

<<<<<<< HEAD
=======
if options["subcommand"] in ("run", "verify"):
if options["self_setup"]:
os.chdir(options["self_setup"])
else:
os.chdir(workdir)

>>>>>>> ac1a0a0 (fixx)
user_groups = misc.get_all_user_groups(logger.logger)

if options["dry"]:
Expand Down
1 change: 0 additions & 1 deletion source/modules/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def create_workdir(timestamp: str, conf: dict, logger):

try:
os.mkdir(seq_path)
os.chdir(seq_path)
except Exception as e:
logger.critical("Failed to create work dir: %s error was: %s", seq_path, e, file=sys.stderr)
sys.exit(91)
Expand Down
4 changes: 2 additions & 2 deletions source/modules/runners/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def setup_ansible(self, setup_hooks: list, commit: str, workdir: str):
checkout the default repo.
"""
failed = False

workdir, commit = Runners.reassign_commit_and_workdir(commit, workdir)

for hook in setup_hooks:
if hook["module"] == "script":
try:
Expand Down Expand Up @@ -63,7 +63,7 @@ def setup_ansible(self, setup_hooks: list, commit: str, workdir: str):
self.logger.critical("Setup hook %s failed, cannot continue", hook["name"])
sys.exit(40)
else:
self.logger.info("Setup completed in %s", os.getcwd())
self.logger.info("Setup completed in %s", workdir)

else:
self.logger.error("Not supported")
Expand Down
2 changes: 1 addition & 1 deletion source/modules/validators/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def validate_options(self, options: dict):
required = ["task", "infra", "stage"]
elif options["subcommand"] == "verify":
required = ["task", "infra", "stage"]
notsupported = ["commit", "local_checkout"]
notsupported = ["commit"]
elif options["subcommand"] in ("lock", "unlock"):
required = ["infra", "stage"]
notsupported = ["task", "commit", "limit", "self_setup"]
Expand Down
16 changes: 16 additions & 0 deletions tests/02a-checkrun_with_self_setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash -l


source ./tests/testing_lib.sh

# Check if cwd is correctly switched when using --self-setup option
check_message_in_output "ansible-deployer run -t task_exec_bin_true -s testing -i testInfra -d --self-setup /etc/alt-workdir" "\[DEBUG\]: Successfully created workdir: /tmp/"
check_message_in_output "ansible-deployer run -t task_exec_bin_true -s testing -i testInfra -d --self-setup /etc/alt-workdir" "\[INFO\]: Setup completed in /etc/alt-workdir"
search_path=$(find_latest_sequence)
check_file_startingwith_in_dir "/etc/alt-workdir" "runBin.yaml"
check_file_startingwith_in_dir "$search_path" "ansible-deploy_execution_"
check_message_in_output "ansible-deployer verify --task=task_exec_bin_true -s testing -i testInfra -d --self-setup /etc/alt-workdir" "\[DEBUG\]: Successfully created workdir: /tmp/"
check_message_in_output "ansible-deployer verify --task=task_exec_bin_true -s testing -i testInfra -d --self-setup /etc/alt-workdir" "\[INFO\]: Setup completed in /etc/alt-workdir"
search_path=$(find_latest_sequence)
check_file_startingwith_in_dir "/etc/alt-workdir" "runBin.yaml"
check_file_startingwith_in_dir "$search_path" "ansible-deploy_execution_"
16 changes: 16 additions & 0 deletions tests/testing_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,19 @@ check_file_permissions() {
echo "FAILED: $1 has incorrect permissions $2"
fi
}

find_latest_sequence() {
last_seq="$(ls /tmp/$(date +%Y%m%d) | tail -n 1)"
echo "/tmp/$(date +%Y%m%d)/${last_seq}"
}

check_file_startingwith_in_dir() {
eval "find $1 -name "${2}*" 1>/dev/null"
if [ $? -eq 0 ]
then
echo "OK: $1 has file starting with pattern $2"
else
echo "FAILED: $1 does not contain file starting with pattern $2"
exit 1
fi
}

0 comments on commit 54d1bbd

Please # to comment.