Skip to content

Commit

Permalink
Increase test coverage of containerfile.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Shrews committed Oct 16, 2023
1 parent d250606 commit 629f0e9
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
19 changes: 19 additions & 0 deletions test/integration/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,22 @@ def test_v3_set_user_id(cli, build_dir_and_ee_yml):
text = containerfile.read_text()

assert "USER bob" in text


def test_v3_additional_build_files(cli, build_dir_and_ee_yml):
"""
Test functionality of addition_build_files.
"""
tmpdir, eeyml = build_dir_and_ee_yml(
"""
version: 3
additional_build_files:
- src: ansible.cfg
dest: configs
"""
)

cfg = tmpdir / 'ansible.cfg'
cfg.touch()

cli(f'ansible-builder create -c {tmpdir} -f {eeyml} --output-filename Containerfile')
90 changes: 87 additions & 3 deletions test/unit/test_containerfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
# pylint: disable=W0212


def make_containerfile(tmpdir, ee_path, **cf_kwargs):
def make_containerfile(tmpdir, ee_path, run_validate=False, **cf_kwargs):
definition = UserDefinition(ee_path)
build_context = str(tmpdir / '_build')
c = Containerfile(definition, build_context=build_context, container_runtime='podman', **cf_kwargs)
if run_validate:
definition.validate()
c = Containerfile(definition, build_context=str(tmpdir), container_runtime='podman', **cf_kwargs)
return c


Expand Down Expand Up @@ -119,3 +120,86 @@ def test_prepare_galaxy_install_steps_with_ignore_code(build_dir_and_ee_yml):
f"--keyring \"{constants.default_keyring_name}\""
]
assert c.steps == expected


def test_v2_custom_builder_image(build_dir_and_ee_yml):
"""
Test that a customer builder image in the v2 schema is used (not valid for v3).
"""
ee_data = [
'version: 2',
'images:',
' base_image:',
' name: quay.io/user/mycustombaseimage:latest',
' builder_image:',
' name: quay.io/user/mycustombuilderimage:latest',
]
tmpdir, ee_path = build_dir_and_ee_yml("\n".join(ee_data))
c = make_containerfile(tmpdir, ee_path, run_validate=True)
c.prepare()

assert 'ARG EE_BUILDER_IMAGE="quay.io/user/mycustombuilderimage:latest"' in c.steps
assert "FROM $EE_BUILDER_IMAGE as builder" in c.steps


def test_v3_various(build_dir_and_ee_yml):
"""
Test various v3 expected outputs appear in the Containerfile as they
happen during the prepare() phase. These could be broken down into
individual tests, but for now we'll just use this catch-all.
"""
ee_data = [
'version: 3',
'images:',
' base_image:',
' name: quay.io/user/mycustombaseimage:latest',
'options:',
' skip_ansible_check: False',
' relax_passwd_permissions: True',
' workdir: /myworkdir',
' container_init:',
' package_pip: dumb-init==x.y.z',
' user: myuser',
'dependencies:',
' ansible_core:',
' package_pip: ansible-core',
' python_interpreter:',
' package_system: python310',
]
tmpdir, ee_path = build_dir_and_ee_yml("\n".join(ee_data))
c = make_containerfile(tmpdir, ee_path, run_validate=True)
c.prepare()

assert "RUN /output/scripts/check_ansible $PYCMD" in c.steps
assert "RUN chmod ug+rw /etc/passwd" in c.steps
assert "RUN mkdir -p /myworkdir && chgrp 0 /myworkdir && chmod -R ug+rwx /myworkdir" in c.steps
assert "RUN $PYCMD -m pip install --no-cache-dir 'dumb-init==x.y.z'" in c.steps
assert "USER myuser" in c.steps
assert "RUN $PKGMGR install $PYPKG -y ; if [ -z $PKGMGR_PRESERVE_CACHE ]; then $PKGMGR clean all; fi" in c.steps
assert "RUN $PYCMD -m pip install --no-cache-dir $ANSIBLE_INSTALL_REFS" in c.steps


def test__handle_additional_build_files(build_dir_and_ee_yml):
"""
Test additional build file handling works as expected.
"""
ee_data = [
'version: 3',
'images:',
' base_image:',
' name: quay.io/user/mycustombaseimage:latest',
'additional_build_files:',
' - src: ansible.cfg',
' dest: configs',
]
tmpdir, ee_path = build_dir_and_ee_yml("\n".join(ee_data))

cfg = tmpdir / 'ansible.cfg'
cfg.touch()

c = make_containerfile(tmpdir, ee_path, run_validate=True)
c._handle_additional_build_files()

config_dir = tmpdir / '_build' / 'configs'
assert config_dir.exists()
assert (config_dir / 'ansible.cfg').exists()

0 comments on commit 629f0e9

Please sign in to comment.