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

Fix all tests #269

Merged
Merged
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
5 changes: 5 additions & 0 deletions ansible_bender/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ def build(self, build: Build):
# commit the final image and apply all metadata
b.final_layer_id = builder.commit(build.target_image, final_image=True)

if b.squash:
logger.debug("Squashing metadata into a single layer")
# reset layers if squashing
b.wipe_layers()
self.record_progress(b, None, b.final_layer_id)
if not b.is_layering_on():
self.record_progress(b, None, b.final_layer_id)
else:
Expand Down
5 changes: 5 additions & 0 deletions ansible_bender/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ def record_layer(self, content, layer_id, base_image_id, cached=None):
self.layers.append(layer)
self.layer_index[layer_id] = layer

def wipe_layers(self):
""" remove all layers from the DB: used by squash """
self.layers = []
self.layer_index = {}

def get_top_layer_id(self):
"""
return id of the top layer, or None
Expand Down
4 changes: 4 additions & 0 deletions ansible_bender/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@
is_ansibles_python_2

logger = logging.getLogger(__name__)
# callback_whitelist got renamed to callbacks_enabled in ansible
# drop callback_whitelist once 2.15 is released
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a task/issue for this? Could be useful, as at least I, often forget notes like this in the source code 🙂

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Very good point! #272

# https://docs.ansible.com/ansible/latest/reference_appendices/config.html#callbacks-enabled
A_CFG_TEMPLATE = """\
[defaults]
# when user is changed, ansible might not be able to write to /.ansible
remote_tmp = /tmp
callback_plugins={0}
callback_whitelist=snapshoter\n
callbacks_enabled=snapshoter\n
"""


Expand Down
1 change: 0 additions & 1 deletion ansible_bender/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ def _get_and_bump_build_id(data):
else:
raise Exception(f'Database seems to be corrupted. Build {next_build_id} already exists.')


def record_build(self, build_i, build_id=None, build_state=None, set_finish_time=False):
"""
record build into database
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def ab(args, tmpdir_path, return_output=False, ignore_result=False, env=None):

:return:
"""
# put --debug in there for debugging
# put --debug in args for debugging
cmd = ["python3", "-m", "ansible_bender.cli", "--database-dir", tmpdir_path] + args
logger.debug("cmd = %s", cmd)
if ignore_result:
Expand Down
19 changes: 16 additions & 3 deletions tests/functional/test_buildah.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_build_basic_image_with_build_volumes(tmpdir, target_image):
with open(os.path.join(real_tmp, "file.txt"), "w") as fd:
fd.write("Hello, hello!")
container_mount = "/asdqwe"
vol_spec = "%s:%s" % (real_tmp, container_mount)
vol_spec = "%s:%s:Z" % (real_tmp, container_mount)
cmd = ["build", "--build-volumes", vol_spec, "--",
basic_playbook_path_w_bv, base_image, target_image]
ab(cmd, str(tmpdir))
Expand Down Expand Up @@ -126,7 +126,7 @@ def test_build_basic_image_with_all_params(tmpdir, target_image):
basic_playbook_path, base_image, target_image]
ab(cmd, str(tmpdir))
out = inspect_resource("image", target_image)
assert out['ImageAnnotations'] == {'bohemian': 'rhapsody'}
assert out['ImageAnnotations']['bohemian'] == 'rhapsody'
co = out["Docker"]["config"]
assert co["WorkingDir"] == workdir_path
assert co["Labels"]["A"] == "B"
Expand All @@ -149,7 +149,7 @@ def test_build_failure(tmpdir):
with pytest.raises(subprocess.CalledProcessError):
ab(cmd, str(tmpdir))
out = ab(["get-logs"], str(tmpdir), return_output=True).lstrip()
assert out.startswith("PLAY [registry")
assert "PLAY [registry" in out

# regex string for target image
image_name_regex = "%s+[-]+[0-9]+[-]+[0-9]+-failed" %(target_image)
Expand Down Expand Up @@ -296,3 +296,16 @@ def test_two_build_vols(tmpdir, target_image):

assert vol_spec1 in build_volumes
assert vol_spec2 in build_volumes


def test_squash(target_image, tmpdir):
cmd = ["build", "--squash", basic_playbook_path, base_image, target_image]

ab(cmd, str(tmpdir))

p_inspect_data = json.loads(subprocess.check_output(["podman", "inspect", "-t", "image", target_image]))[0]
assert len(p_inspect_data["RootFS"]["Layers"]) == 1

cmd = ["inspect", "--json"]
ab_inspect_data = json.loads(ab(cmd, str(tmpdir), return_output=True))
assert len(ab_inspect_data["layers"]) == 1
2 changes: 1 addition & 1 deletion tests/functional/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_get_logs(target_image, tmpdir):
cmd = ["build", basic_playbook_path, base_image, target_image]
ab(cmd, str(tmpdir))
out = ab(["get-logs"], str(tmpdir), return_output=True).lstrip()
assert out.startswith("PLAY [registry")
assert "PLAY [registry" in out
assert "TASK [Gathering Facts]" in out
assert "failed=0" in out
assert "TASK [print local env vars]" in out
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_build_db_metadata(application, build):
assert build.build_start_time is not None
assert build.log_lines is not None
logs = "\n".join([l for l in build.log_lines if l])
assert logs.startswith("PLAY [registry")
assert "PLAY [registry" in logs
assert "TASK [Gathering Facts]" in logs
assert "failed=0" in logs

Expand Down