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

Plugin support to map image to downloadable url #1090

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
11 changes: 10 additions & 1 deletion tmt/steps/provision/testcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

import tmt
from tmt.steps.provision import ProvisionPlugin
from tmt.utils import WORKDIR_ROOT, ProvisionError, retry_session
from tmt.utils import (WORKDIR_ROOT, GeneralError, ProvisionError,
get_image_url, retry_session)


def import_testcloud():
Expand Down Expand Up @@ -368,6 +369,14 @@ def _guess_image_url(self, name):
url = testcloud.util.get_fedora_image_url("rawhide")

if not url:
try:
url = get_image_url(
name, arch='x86_64', format=[
'box', 'qcow2'])
if url is not None:
return url
Copy link
Collaborator

Choose a reason for hiding this comment

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

Condition not needed. The url would be returned even if None because of line 381.

except GeneralError as error:
self.fail(str(error))
raise ProvisionError(f"Could not map '{name}' to compose.")
return url

Expand Down
32 changes: 32 additions & 0 deletions tmt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1791,3 +1791,35 @@ def get_distgit_handler(remotes=None, usage_name=None):
def get_distgit_handler_names():
""" All known distgit handlers """
return [i.usage_name for i in DistGitHandler.__subclasses__()]


class Image2URLConvertor(object):
""" Common parent for image to url conversion plugins """
@staticmethod
def convert(value, arch=None, format=[]):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
def convert(value, arch=None, format=[]):
def convert(value, arch=None, format=None):

Using empty list as the default value is not recommended as it can cause problem with repeated calls. See also d4f41a5.

"""
Return url to download image from

Caller should set 'arch' and 'format'
If class can't convert value it should return 'None'
(e.g. class converts qcow2 images but asked to map podman)

Raises GeneralError when value was expected to be converted,
but couldn't be (e.g. old fedora release removed from download location)
"""
raise NotImplementedError()


def get_image_url(value, arch, format):
Copy link
Collaborator

Choose a reason for hiding this comment

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

The value sounds very generic. Is it rather an image name?

"""
Map image 'value' for 'arch' and one of 'format' into URL to download

First plugin which knows how to map the value wins

GeneralError might come from the plugin
"""
for candidate_class in Image2URLConvertor.__subclasses__():
url = candidate_class.convert(value, arch, format=format)
if url is not None:
log.debug(f"'{value}' mapped by '{candidate_class.__name__}'")
return url