Skip to content

Commit

Permalink
Merge 2cab57a into fb66957
Browse files Browse the repository at this point in the history
  • Loading branch information
chmeliik committed Feb 22, 2022
2 parents fb66957 + 2cab57a commit a0ef25a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 79 deletions.
44 changes: 19 additions & 25 deletions koji_containerbuild/plugins/builder_containerbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,16 @@
METADATA_TAG = "platform:_metadata_"

ANNOTATIONS_FILENAME = 'build_annotations.json'
ANNOTATIONS_FROM_BUILD_RESULTS = ['remote_sources']

DEFAULT_CONF_BINARY_SECTION = "default_binary"
DEFAULT_CONF_SOURCE_SECTION = "default_source"


def _concat(iterables):
return [x for iterable in iterables for x in iterable]


def create_task_response(osbs_result):
"""Create task response from an OSBS result"""
repositories = osbs_result.get('repositories', [])
Expand Down Expand Up @@ -448,22 +453,6 @@ def _read_user_warnings(self, logs_dir):
msg = "Exception ({}) while reading user warnings: {}".format(type(error), error)
raise ContainerError(msg)

def _get_repositories(self, annotations):
repositories = []
repo_str = annotations.get('repositories', '{}')
repo_dict = json.loads(repo_str)

if repo_dict:
for repos in repo_dict.values():
repositories.extend(repos)

return repositories

def _get_koji_build_id(self, labels):
koji_build_id = labels.get('koji-build-id')

return koji_build_id

def check_whitelist(self, name, target_info):
"""Check if container name is whitelisted in destination tag
Expand All @@ -480,10 +469,15 @@ def check_whitelist(self, name, target_info):
raise koji.BuildError("package (container) %s is blocked for tag %s" %
(name, target_info['dest_tag_name']))

def upload_build_annotations(self, annotations):
whitelist_str = annotations.get('koji_task_annotations_whitelist', "[]")
whitelist = json.loads(whitelist_str)
task_annotations = {k: v for k, v in annotations.items() if k in whitelist}
def upload_build_results_as_annotations(self, build_results):
task_annotations = {
# TODO: previously, remote_sources was double JSON-encoded, do we need to keep that?
# prev: {"remote_sources": "[{\\"name\\": null, \\"url\\": \\"some url\\"}]"}
# curr: {"remote_sources": [{"name": null, "url": "some url"}]}
name: build_results[name]
for name in ANNOTATIONS_FROM_BUILD_RESULTS
if name in build_results
}
if task_annotations:
f = StringIO()
json.dump(task_annotations, f, sort_keys=True, indent=4)
Expand Down Expand Up @@ -531,11 +525,10 @@ def sigint_handler(*args, **kwargs):
self.osbs().wait_for_build_to_finish(build_id)

has_succeeded = self.osbs().build_has_succeeded(build_id)
annotations = self.osbs().get_build_annotations(build_id)
labels = self.osbs().get_build_labels(build_id)
build_results = self.osbs().get_build_results(build_id)

if has_succeeded:
self.upload_build_annotations(annotations)
self.upload_build_results_as_annotations(build_results)

self.logger.debug("OSBS build finished with status: %s. Build "
"response: %s.", self.osbs().get_build_reason(build_id),
Expand Down Expand Up @@ -572,14 +565,15 @@ def sigint_handler(*args, **kwargs):

repositories = []
if has_succeeded:
repositories = self._get_repositories(annotations)
repositories = _concat(build_results['repositories'].values())

self.logger.info("Image available in the following repositories: %r",
repositories)

koji_build_id = None
if has_succeeded:
koji_build_id = self._get_koji_build_id(labels)
# For backward compatibility reasons, koji_build_id has to be a string
koji_build_id = str(build_results['koji-build-id'])

self.logger.info("Koji content generator build ID: %s", koji_build_id)

Expand Down
4 changes: 2 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ function setup_kojic() {
# from specified git source (default: upstream master)
$RUN rm -rf /tmp/osbs-client
$RUN git clone --depth 1 --single-branch \
https://github.com/projectatomic/osbs-client --branch master /tmp/osbs-client
https://github.com/chmeliik/osbs-client --branch tekton-results /tmp/osbs-client
# RPM install build dependencies for osbs-client
$RUN "${BUILDDEP[@]}" --define "with_python3 ${WITH_PY3}" -y /tmp/osbs-client/osbs-client.spec

# Run pip install with '--no-deps' to avoid compilation.
# This will also ensure all the deps are specified in the spec
# Pip install osbs-client from git master
$RUN "${PIP_INST[@]}" --upgrade --no-deps --force-reinstall \
git+https://github.com/projectatomic/osbs-client
git+https://github.com/chmeliik/osbs-client@tekton-results
# Pip install dockerfile-parse from git master
$RUN "${PIP_INST[@]}" --upgrade --force-reinstall \
git+https://github.com/containerbuildsystem/dockerfile-parse
Expand Down
74 changes: 22 additions & 52 deletions tests/test_builder_containerbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,6 @@ def test_osbs(self, task_method, method, scratch):
assert isinstance(osbs_obj, osbs.api.OSBS)
assert osbs_obj.os_conf.conf_section == expected_conf_section

@pytest.mark.parametrize("repos", [{'repo1': 'test1'}, {'repo2': 'test2'}])
def test_get_repositories(self, repos):
cct = builder_containerbuild.BuildContainerTask(id=1,
method='buildContainer',
params='params',
session='session',
options='options',
workdir='workdir')
repositories = []
for repo in repos.values():
repositories.extend(repo)

assert set(cct._get_repositories({'repositories': json.dumps(repos)})) == set(repositories)

def _check_logfiles(self, log_entries, logs_dir):
def check_meta_entry(filename):
source_file = os.path.join(koji.pathinfo.work(), filename)
Expand Down Expand Up @@ -356,13 +342,10 @@ def _mock_osbs(self, koji_build_id, src, koji_task_id, source=False,
(flexmock(osbs.api.OSBS).should_receive('get_build_reason').and_return('Succeeded'))
(flexmock(osbs.api.OSBS).should_receive('build_has_succeeded').and_return(True))
(flexmock(osbs.api.OSBS).should_receive('build_was_cancelled').and_return(False))
repos_str = '{"unique": ["unique-repo"], "primary": ["primary-repo"]}'
repos = {"unique": ["unique-repo"], "primary": ["primary-repo"]}
(flexmock(osbs.api.OSBS)
.should_receive('get_build_annotations')
.and_return({'repositories': repos_str}))
(flexmock(osbs.api.OSBS)
.should_receive('get_build_labels')
.and_return({'koji-build-id': koji_build_id}))
.should_receive('get_build_results')
.and_return({'repositories': repos, 'koji-build-id': koji_build_id}))
(flexmock(osbs.api.OSBS).should_receive('build_not_finished').and_return(False))
(flexmock(osbs.api.OSBS).should_receive('cancel_build').never())
(flexmock(osbs.api.OSBS)
Expand Down Expand Up @@ -557,7 +540,7 @@ def test_osbs_build(self, tmpdir, pkg_info, failure):

assert task_response == {
'repositories': ['unique-repo', 'primary-repo'],
'koji_builds': [koji_build_id]
'koji_builds': [str(koji_build_id)]
}

@pytest.mark.parametrize(('pkg_info', 'failure'), (
Expand Down Expand Up @@ -613,7 +596,7 @@ def test_osbs_build_source(self, pkg_info, failure):

assert task_response == {
'repositories': ['unique-repo', 'primary-repo'],
'koji_builds': [koji_build_id]
'koji_builds': [str(koji_build_id)]
}

@pytest.mark.parametrize('reason, expected_exc_type, build_finished', [
Expand Down Expand Up @@ -917,7 +900,7 @@ def test_additional_args(self, tmpdir, log_upload_raises, additional_args):

assert task_response == {
'repositories': ['unique-repo', 'primary-repo'],
'koji_builds': [koji_build_id]
'koji_builds': [str(koji_build_id)]
}

@pytest.mark.parametrize('log_upload_raises', (True, False))
Expand Down Expand Up @@ -982,7 +965,7 @@ def test_additional_args_source(self, log_upload_raises, additional_args):

assert task_response == {
'repositories': ['unique-repo', 'primary-repo'],
'koji_builds': [koji_build_id]
'koji_builds': [str(koji_build_id)]
}

@pytest.mark.parametrize(('isolated', 'release', 'koji_parent_build'), (
Expand Down Expand Up @@ -1035,7 +1018,7 @@ def test_flatpak_build(self, tmpdir, isolated, release, koji_parent_build):
task_response = task.handler(src['src'], 'target', opts=additional_args)
assert task_response == {
'repositories': ['unique-repo', 'primary-repo'],
'koji_builds': [koji_build_id]
'koji_builds': [str(koji_build_id)]
}

@pytest.mark.parametrize(('tag', 'release', 'is_oversized'), (
Expand Down Expand Up @@ -1091,7 +1074,7 @@ def test_oversized_tags(self, tmpdir, tag, release, is_oversized):

assert task_response == {
'repositories': ['unique-repo', 'primary-repo'],
'koji_builds': [koji_build_id]
'koji_builds': [str(koji_build_id)]
}

@pytest.mark.parametrize(('build_state', 'build_fails'), (
Expand Down Expand Up @@ -1302,7 +1285,7 @@ def test_compose_ids_and_signing_intent(self, tmpdir, additional_args, raises):

assert task_response == {
'repositories': ['unique-repo', 'primary-repo'],
'koji_builds': [koji_build_id]
'koji_builds': [str(koji_build_id)]
}

@pytest.mark.parametrize(('additional_args', 'raises'), (
Expand Down Expand Up @@ -1365,7 +1348,7 @@ def test_arch_override(self, tmpdir, additional_args, raises):

assert task_response == {
'repositories': ['unique-repo', 'primary-repo'],
'koji_builds': [koji_build_id]
'koji_builds': [str(koji_build_id)]
}

@pytest.mark.parametrize('arg_name, arg_value, expected_types', [
Expand Down Expand Up @@ -1616,18 +1599,15 @@ def test_schema_validation_valid_options_sourcecontainer(self, tmpdir, scratch,

task.handler('target', build_opts)

@pytest.mark.parametrize('annotations', (
@pytest.mark.parametrize('build_results', (
{},
{'koji_task_annotations_whitelist': '[]'},
{'koji_task_annotations_whitelist': '[]', 'remote_source_url': 'stub_url'},
{'koji_task_annotations_whitelist': '["remote_source_url"]',
'remote_source_url': 'stub_url'},
{'remote_source_url': 'stub_url'},
{'a': '1', 'b': '2'},
{'koji_task_annotations_whitelist': '["a", "b"]', 'a': '1', 'b': '2', 'c': '3'},
{'koji_task_annotations_whitelist': '["a", "b"]', 'a': '1', 'c': '3'}
))
def test_upload_annotations(self, tmpdir, annotations):
{
'remote_sources': [{'name': None, 'url': 'stub_url'}],
'repositories': {'unique': [], 'primary': [], 'floating': []},
'koji-build-id': 1,
},
))
def test_upload_build_results_as_annotations(self, tmpdir, build_results):
def mock_incremental_upload(session, fname, fd, uploadpath, logger=None):
with open(os.path.join(uploadpath, fname), 'w') as f:
data = fd.read()
Expand All @@ -1644,25 +1624,15 @@ def mock_incremental_upload(session, fname, fd, uploadpath, logger=None):
options='options')
flexmock(cct).should_receive('getUploadPath').and_return(tmpdir.strpath)

cct.upload_build_annotations(annotations)
whitelist = annotations.get('koji_task_annotations_whitelist')
if whitelist:
whitelist = json.loads(whitelist)
cct.upload_build_results_as_annotations(build_results)

if not whitelist or len(annotations) < 2:
if not build_results:
assert not os.path.exists(annotations_file)
else:
assert os.path.exists(annotations_file)
with open(annotations_file) as f:
build_annotations = json.load(f)
for key, value in build_annotations.items():
assert key in whitelist
assert value == annotations[key]
for item in whitelist:
if item in annotations:
assert item in build_annotations
else:
assert item not in build_annotations
assert build_annotations == {'remote_sources': [{'name': None, 'url': 'stub_url'}]}

def test_raise_OsbsValidationException(self, tmpdir):
df_content = """\
Expand Down

0 comments on commit a0ef25a

Please sign in to comment.