Skip to content

Commit

Permalink
VMimage get --debug option
Browse files Browse the repository at this point in the history
This commit adds a new option to `vmimage get` feature called `debug`.
It brings possibility to run vmimage command in debug mode to get more
detailed information about possible failures. In debug mode, all
exceptions caused by vmimage utility will be raised to console. That
should provide enough information for further debugging of possible
issues.

Reference: #6051
Signed-off-by: Jan Richter <[email protected]>
  • Loading branch information
richtja committed Oct 31, 2024
1 parent 12863df commit 50527e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
20 changes: 16 additions & 4 deletions avocado/plugins/vmimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def list_downloaded_images():
return images


def download_image(distro, version=None, arch=None):
def download_image(distro, version=None, arch=None, debug=False):
"""
Downloads the vmimage to the cache directory if doesn't already exist
Expand All @@ -70,8 +70,8 @@ def download_image(distro, version=None, arch=None):
:rtype: dict
"""
cache_dir = settings.as_dict().get("datadir.paths.cache_dirs")[0]
image_info = vmimage.get(
name=distro, version=version, arch=arch, cache_dir=cache_dir
image_info = vmimage.Image.from_parameters(

Check warning on line 73 in avocado/plugins/vmimage.py

View check run for this annotation

Codecov / codecov/patch

avocado/plugins/vmimage.py#L73

Added line #L73 was not covered by tests
name=distro, version=version, arch=arch, cache_dir=cache_dir, debug=debug
)
file_path = image_info.base_image
image = {
Expand Down Expand Up @@ -155,6 +155,17 @@ def configure(self, parser):
long_arg="--arch",
)

help_msg = "More information about failures"
settings.register_option(

Check warning on line 159 in avocado/plugins/vmimage.py

View check run for this annotation

Codecov / codecov/patch

avocado/plugins/vmimage.py#L158-L159

Added lines #L158 - L159 were not covered by tests
section="vmimage.get",
key="debug",
default=False,
help_msg=help_msg,
key_type=bool,
parser=get_parser,
long_arg="--debug",
)

def run(self, config):
subcommand = config.get("vmimage_subcommand")
if subcommand == "list":
Expand All @@ -164,8 +175,9 @@ def run(self, config):
name = config.get("vmimage.get.distro")
version = config.get("vmimage.get.version")
arch = config.get("vmimage.get.arch")
debug = config.get("vmimage.get.debug")

Check warning on line 178 in avocado/plugins/vmimage.py

View check run for this annotation

Codecov / codecov/patch

avocado/plugins/vmimage.py#L178

Added line #L178 was not covered by tests
try:
image = download_image(name, version, arch)
image = download_image(name, version, arch, debug)

Check warning on line 180 in avocado/plugins/vmimage.py

View check run for this annotation

Codecov / codecov/patch

avocado/plugins/vmimage.py#L180

Added line #L180 was not covered by tests
except AttributeError:
LOG_UI.debug("The requested image could not be downloaded")
return exit_codes.AVOCADO_FAIL
Expand Down
15 changes: 11 additions & 4 deletions avocado/utils/vmimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ def from_parameters(
algorithm=None,
cache_dir=None,
snapshot_dir=None,
debug=False,
):
"""
Returns an Image, according to the parameters provided.
Expand All @@ -650,11 +651,13 @@ def from_parameters(
:param snapshot_dir: (optional) Local system path where the snapshot
images will be held. Defaults to cache_dir if
none is given.
:param debug: (optional) Whatever, it should raise ImageProviderError
with details about download failures. Default is False.
:returns: Image instance that can provide the image
according to the parameters.
"""
provider = get_best_provider(name, version, build, arch)
provider = get_best_provider(name, version, build, arch, debug=debug)

Check warning on line 660 in avocado/utils/vmimage.py

View check run for this annotation

Codecov / codecov/patch

avocado/utils/vmimage.py#L660

Added line #L660 was not covered by tests

if cache_dir is None:
cache_dir = tempfile.gettempdir()
Expand All @@ -671,7 +674,8 @@ def from_parameters(
snapshot_dir=snapshot_dir,
)
except ImageProviderError:
pass
if debug:
raise

Check warning on line 678 in avocado/utils/vmimage.py

View check run for this annotation

Codecov / codecov/patch

avocado/utils/vmimage.py#L677-L678

Added lines #L677 - L678 were not covered by tests

raise AttributeError("Provider not available")

Expand All @@ -695,7 +699,7 @@ def get(
)


def get_best_provider(name=None, version=None, build=None, arch=None):
def get_best_provider(name=None, version=None, build=None, arch=None, debug=False):
"""
Wrapper to get parameters of the best Image Provider, according to
the parameters provided.
Expand All @@ -705,6 +709,8 @@ def get_best_provider(name=None, version=None, build=None, arch=None):
:param version: (optional) Version of the system image.
:param build: (optional) Build number of the system image.
:param arch: (optional) Architecture of the system image.
:param debug: (optional) Whatever, it should raise ImageProviderError
with details about download failures. Default is False.
:returns: Image Provider
"""
Expand All @@ -727,7 +733,8 @@ def get_best_provider(name=None, version=None, build=None, arch=None):
try:
return provider(**provider_args)
except ImageProviderError:
pass
if debug:
raise

Check warning on line 737 in avocado/utils/vmimage.py

View check run for this annotation

Codecov / codecov/patch

avocado/utils/vmimage.py#L736-L737

Added lines #L736 - L737 were not covered by tests

raise AttributeError("Provider not available")

Expand Down

0 comments on commit 50527e8

Please sign in to comment.