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

Modify scripts/check_galaxy to return more descriptive error message(s) #695

Merged
merged 11 commits into from
Aug 15, 2024
10 changes: 6 additions & 4 deletions src/ansible_builder/_target_scripts/check_galaxy
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ if [ $? -ne 0 ]
then
cat<<EOF
**********************************************************************
ERROR - Missing Ansible installation
ERROR - 'ansible-galaxy' command not functioning as expected

The 'ansible-galaxy' command is not found in the base image. This
image is used to create the intermediary image that performs the
Galaxy collection and role installation process.
There was an error running the 'ansible-galaxy' command. Some
possible causes are 'ansible-core' missing from the base image, an
incorrect 'ansible.cfg', or 'ansible-galaxy' not being available to
the selected Python interpreter. Please use the -vvv option to
examine the build output for any errors.

Ansible must be installed in the base image. If you are using a
recent enough version of the execution environment file, you may
Expand Down
25 changes: 25 additions & 0 deletions test/data/v3/check_galaxy/ee-bad-ansible-cfg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 3

images:
base_image:
name: quay.io/centos/centos:stream9

dependencies:
ansible_core:
package_pip: ansible-core
ansible_runner:
package_pip: ansible-runner
galaxy:
collections:
- containers.podman

options:
package_manager_path: '/bin/true'

additional_build_files:
- src: ./files/bad_ansible.cfg
dest: configs

additional_build_steps:
prepend_galaxy:
- COPY _build/configs/bad_ansible.cfg /etc/ansible/ansible.cfg
3 changes: 3 additions & 0 deletions test/data/v3/check_galaxy/files/bad_ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Bad ansible.cfg
[defaults]
==?k,sm../a
19 changes: 19 additions & 0 deletions test/integration/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,22 @@ def test_missing_runner(cli, runtime, ee_tag, data_dir, tmp_path):
)

assert "ERROR - Missing Ansible Runner installation" in einfo.value.stdout


@pytest.mark.test_all_runtimes
def test_bad_ansible_cfg(cli, runtime, ee_tag, data_dir, tmp_path):
"""
Test that the check_galaxy script will cause build failure with
incorrect ansible.cfg
"""
ee_def = data_dir / 'v3' / 'check_galaxy' / 'ee-bad-ansible-cfg.yml'

with pytest.raises(subprocess.CalledProcessError) as einfo:
cli(
f'ansible-builder build -c {tmp_path} -f {ee_def} -t {ee_tag} '
f'--container-runtime={runtime} -v3'
)

assert "ERROR - 'ansible-galaxy' command not functioning as expected" in einfo.value.stdout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to this assert, we should add another that checks for the actual error emitted from ansible-galaxy (visible in the output thanks to using -v3) to make sure it is the faulty ansible.cfg causing the error and not something else. In this case, looks like the error string we want begins with:

ERROR: Error reading config file (/etc/ansible/ansible.cfg): Source contains parsing errors:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for getting the exact line I needed!

assert ("ERROR: Error reading config file (/etc/ansible/ansible.cfg): "
"Source contains parsing errors:") in einfo.value.stdout
Loading