Skip to content

Commit

Permalink
Retry buildah if incorrect arch created
Browse files Browse the repository at this point in the history
  • Loading branch information
Tulsi Chandwani committed Jun 27, 2023
1 parent 0937a41 commit ed4d377
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
13 changes: 12 additions & 1 deletion iib/workers/tasks/build.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-3.0-or-later
import json
import logging
import os
import shutil
Expand Down Expand Up @@ -66,7 +67,7 @@
@retry(
before_sleep=before_sleep_log(log, logging.WARNING),
reraise=True,
retry=retry_if_exception_type(ExternalServiceError),
retry=(retry_if_exception_type(ExternalServiceError) | retry_if_exception_type(IIBError)),
stop=stop_after_attempt(worker_config.iib_total_attempts),
wait=wait_incrementing(
start=worker_config.iib_retry_delay,
Expand Down Expand Up @@ -117,6 +118,16 @@ def _build_image(dockerfile_dir: str, dockerfile_name: str, request_id: int, arc
{'cwd': dockerfile_dir},
exc_msg=f'Failed to build the container image on the arch {arch}',
)
log.debug('Verifying that %s was built with expected arch %s', destination, arch)
buildah_inspect = json.loads(run_cmd(['buildah', 'inspect', destination]))
archmap = {"amd64": "x86_64", "arm64": "aarch64", "s390x": "s390x", "ppc64le": "ppc64le"}
destination_arch = buildah_inspect['Docker']['config']['Labels']['architecture']

if destination_arch not in archmap.values() or destination_arch != archmap.get(arch, None):
log.warning("Wrong arch created for %s", destination)
exp_message = f'Wrong arch created, for image {destination} expected arch {arch}, \
found {destination_arch}'
raise IIBError(exp_message)


def _cleanup() -> None:
Expand Down
62 changes: 44 additions & 18 deletions tests/test_workers/test_tasks/test_build.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-3.0-or-later
import copy
import json
import os
import re
import stat
Expand All @@ -16,29 +17,54 @@
worker_config = get_worker_config()


@pytest.mark.parametrize('arch', ('amd64', 'ppc64le', 's390x', 'spam'))
@pytest.mark.parametrize('arch', ('amd64', 'ppc64le', 's390x', 'arm64'))
@mock.patch('iib.workers.tasks.build.run_cmd')
def test_build_image(mock_run_cmd, arch):
archmap = {"amd64": "x86_64", "arm64": "aarch64", "s390x": "s390x", "ppc64le": "ppc64le"}
buildah_inspect_output = json.dumps(
{"Docker": {"config": {"Labels": {"architecture": archmap.get(arch)}}}}
)
mock_run_cmd.side_effect = [
None,
buildah_inspect_output,
None,
buildah_inspect_output,
None,
buildah_inspect_output,
None,
buildah_inspect_output,
]
build._build_image('/some/dir', 'some.Dockerfile', 3, arch)

mock_run_cmd.assert_called_once_with(
mock_run_cmd.assert_has_calls(
[
'buildah',
'bud',
'--no-cache',
'--format',
'docker',
'--override-arch',
arch,
'--arch',
arch,
'-t',
f'iib-build:3-{arch}',
'-f',
'/some/dir/some.Dockerfile',
],
{'cwd': '/some/dir'},
exc_msg=mock.ANY,
mock.call(
[
'buildah',
'bud',
'--no-cache',
'--format',
'docker',
'--override-arch',
arch,
'--arch',
arch,
'-t',
f'iib-build:3-{arch}',
'-f',
'/some/dir/some.Dockerfile',
],
{'cwd': '/some/dir'},
exc_msg=f"Failed to build the container image on the arch {arch}",
),
mock.call(
[
'buildah',
'inspect',
f'iib-build:3-{arch}',
]
),
]
)


Expand Down

0 comments on commit ed4d377

Please sign in to comment.