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

Fetch and attach the manifests #56

Merged
merged 3 commits into from
Feb 25, 2021
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
build/
**/__pycache__/

.vscode/

.coverage
coverage.xml
28 changes: 28 additions & 0 deletions plugins/builder/osbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@ def compose_logs(self, compose_id: str):

return ComposeLogs.from_dict(res.json())

def compose_manifests(self, compose_id: str):
url = urllib.parse.urljoin(self.url, f"compose/{compose_id}/manifests")

res = self.http.get(url)

if res.status_code != 200:
body = res.content.decode("utf-8").strip()
msg = f"Failed to get the compose manifests: {body}"
raise koji.GenericError(msg) from None

return res.json()

def wait_for_compose(self, compose_id: str, *, sleep_time=2, callback=None):
while True:
status = self.compose_status(compose_id)
Expand Down Expand Up @@ -330,6 +342,21 @@ def attach_logs(self, compose_id: str, ireqs: ImageRequest):
self.logger.debug("Uploading logs: %s", name)
self.upload_json(log, name)

def attach_manifests(self, compose_id: str, ireqs: ImageRequest):
self.logger.debug("Fetching manifests")

try:
manifests = self.client.compose_manifests(compose_id)
except koji.GenericError as e:
self.logger.warning("Failed to fetch manifests: %s", str(e))
return

imanifests = zip(manifests, ireqs)
for manifest, ireq in imanifests:
name = "%s-%s.manifest" % (ireq.architecture, ireq.image_type)
self.logger.debug("Uploading manifest: %s", name)
self.upload_json(manifest, name)

@staticmethod
def arches_for_config(buildconfig: Dict):
archstr = buildconfig["arches"]
Expand Down Expand Up @@ -427,6 +454,7 @@ def handler(self, name, version, distro, image_types, target, arches, opts):
self.logger.debug("Compose finished: %s", str(status.as_dict()))
self.logger.info("Compose result: %s", status.status)

self.attach_manifests(cid, ireqs)
self.attach_logs(cid, ireqs)

if not status.is_success:
Expand Down
57 changes: 56 additions & 1 deletion test/unit/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def compose_create(self, request, _uri, response_headers):
"result": compose,
"status": self.status,
"routes": {
"logs": 200
"logs": 200,
"manifests": 200
}
}

Expand All @@ -86,6 +87,12 @@ def compose_create(self, request, _uri, response_headers):
body=self.compose_logs
)

httpretty.register_uri(
httpretty.GET,
urllib.parse.urljoin(self.url, "compose/" + compose_id + "/manifests"),
body=self.compose_manifests
)

return [201, response_headers, json.dumps(compose)]

def compose_status(self, _request, uri, response_headers):
Expand Down Expand Up @@ -125,6 +132,24 @@ def compose_logs(self, _request, uri, response_headers):
}
return [200, response_headers, json.dumps(result)]


def compose_manifests(self, _request, uri, response_headers):
route = self.routes.get("manifests")
if route and route["status"] != 200:
return [route["status"], response_headers, "Internal error"]

target = os.path.basename(os.path.dirname(uri))
compose = self.composes.get(target)
if not compose:
return [400, response_headers, f"Unknown compose: {target}"]

ireqs = compose["request"]["image_requests"]
result = [
{"sources": {}, "pipeline": {}} for _ in ireqs
]
return [200, response_headers, json.dumps(result)]


class UploadTracker:
"""Mock koji file uploading and keep track of uploaded files

Expand Down Expand Up @@ -481,6 +506,7 @@ def test_compose_success(self):
# check uploads: logs, compose request
for arch in arches:
self.uploads.assert_upload(f"{arch}-image_type.log.json")
self.uploads.assert_upload(f"{arch}-image_type.manifest.json")
self.uploads.assert_upload("compose-request.json")
self.uploads.assert_upload("compose-status.json")
self.uploads.assert_upload("koji-init.log.json")
Expand Down Expand Up @@ -513,6 +539,7 @@ def test_compose_failure(self):

self.uploads.assert_upload("compose-request.json")
self.uploads.assert_upload("x86_64-image_type.log.json")
self.uploads.assert_upload("x86_64-image_type.manifest.json")
self.uploads.assert_upload("compose-status.json")
# build must not have been tagged
self.assertEqual(len(session.host.tags), 0)
Expand Down Expand Up @@ -545,6 +572,34 @@ def test_compose_no_logs(self):
with self.assertRaises(AssertionError):
self.uploads.assert_upload("x86_64-image_type.log.json")

@httpretty.activate
def test_compose_no_manifest(self):
# Simulate fetching the manifests fails, a non-fatal issue
session = self.mock_session()
handler = self.make_handler(session=session)

url = self.plugin.DEFAULT_COMPOSER_URL
composer = MockComposer(url)
composer.httpretty_regsiter()

args = ["name", "version", "distro",
["image_type"],
"fedora-candidate",
composer.architectures,
{}]

composer.routes["manifests"] = {
"status": 500
}

res = handler.handler(*args)
assert res, "invalid compose result"

self.uploads.assert_upload("compose-request.json")

with self.assertRaises(AssertionError):
self.uploads.assert_upload("x86_64-image_type.manifest.json")

@httpretty.activate
def test_skip_tag(self):
# Simulate a successful compose, where the tagging
Expand Down