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

test: check if layers are cached when invoked role fails #241

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 6 additions & 4 deletions .packit.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
downstream_package_name: ansible-bender
specfile_path: ansible-bender.spec
upstream_package_name: ansible-bender
current_version_command: ["python3", "setup.py", "--version"]
create_tarball_command: ["python3", "setup.py", "sdist", "--dist-dir", "."]
actions:
create-archive:
- "python3 setup.py sdist --dist-dir ."
- "sh -c 'echo ansible-bender-$(python3 setup.py --version).tar.gz'"
get-current-version:
- "python3 setup.py --version"
jobs:
- job: copr_build
metadata:
Expand Down
44 changes: 44 additions & 0 deletions tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import os
import shutil
import subprocess
from pathlib import Path

import pytest
import yaml
from ansible_bender.exceptions import ABBuildUnsuccesful
from flexmock import flexmock

from ansible_bender.api import Application
Expand Down Expand Up @@ -256,3 +259,44 @@ def test_pb_with_role(build, application):
assert len(build.layers) == 8
finally:
run_cmd(["buildah", "rmi", im], ignore_status=True, print_output=True)


@pytest.mark.parametrize(
"playbook_content",
(
"- hosts: all\n"
" roles:\n"
" - train\n",

"- hosts: all\n"
" tasks:\n"
" - import_role:\n"
" name: train\n",

"- hosts: all\n"
" tasks:\n"
" - include_role:\n"
" name: train\n"
)
)
def test_pb_with_role_fail(build, application, tmp_path, playbook_content):
test_roles_path = tmp_path / "roles"
shutil.copytree(roles_dir, test_roles_path)
test_pb_path: Path = tmp_path / Path(role_pb_path).name
test_pb_path.write_text(playbook_content)
im = "image-built-with-role-should-fail"
build.playbook_path = str(test_pb_path)
build.target_image = im
os.environ["ANSIBLE_ROLES_PATH"] = str(test_roles_path)

# this is the test scenario: make the build fail
os.unlink(test_roles_path / "train" / "files" / "ticket")

with pytest.raises(ABBuildUnsuccesful):
application.build(build)

for layer in build.layers:
if layer.base_image_id:
assert not layer.cached # when the task failed, all layers must not be cached
else:
assert layer.cached # the is base image