Skip to content

Commit

Permalink
Merge remote-tracking branch 'richtja/vmimage_debug'
Browse files Browse the repository at this point in the history
Signed-off-by: Cleber Rosa <[email protected]>
  • Loading branch information
clebergnu committed Nov 22, 2024
2 parents 659af3a + 7420e5f commit 26441ef
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
18 changes: 16 additions & 2 deletions avocado/plugins/vmimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re

from avocado.core import exit_codes, output
from avocado.core.output import LOG_UI
from avocado.core.output import LOG_UI, add_log_handler
from avocado.core.plugin_interfaces import CLICmd
from avocado.core.settings import settings
from avocado.utils import astring, vmimage
Expand Down Expand Up @@ -70,7 +70,7 @@ 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(
image_info = vmimage.Image.from_parameters(
name=distro, version=version, arch=arch, cache_dir=cache_dir
)
file_path = image_info.base_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(
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,6 +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")
if debug:
add_log_handler(vmimage.LOG)
try:
image = download_image(name, version, arch)
except AttributeError:
Expand Down
14 changes: 9 additions & 5 deletions avocado/utils/vmimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Provides VM images acquired from official repositories
"""

import logging
import os
import re
import tempfile
Expand All @@ -29,6 +30,8 @@
from avocado.utils import path as utils_path
from avocado.utils import process

LOG = logging.getLogger(__name__)

# pylint: disable=C0401
#: The "qemu-img" binary used when creating the snapshot images. If
#: set to None (the default), it will attempt to find a suitable binary
Expand Down Expand Up @@ -114,7 +117,7 @@ def _feed_html_parser(self, url, parser):
data = urlopen(url).read()
parser.feed(astring.to_text(data, self.HTML_ENCODING))
except HTTPError as exc:
raise ImageProviderError(f"Cannot open {self.url_versions}") from exc
raise ImageProviderError(f"Cannot open {url}") from exc

@staticmethod
def get_best_version(versions):
Expand Down Expand Up @@ -670,8 +673,8 @@ def from_parameters(
cache_dir=cache_dir,
snapshot_dir=snapshot_dir,
)
except ImageProviderError:
pass
except ImageProviderError as e:
LOG.debug(e)

raise AttributeError("Provider not available")

Expand Down Expand Up @@ -726,9 +729,10 @@ def get_best_provider(name=None, version=None, build=None, arch=None):
if name is None or name == provider.name.lower():
try:
return provider(**provider_args)
except ImageProviderError:
pass
except ImageProviderError as e:
LOG.debug(e)

LOG.debug("Provider for %s not available", name)
raise AttributeError("Provider not available")


Expand Down
2 changes: 1 addition & 1 deletion selftests/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"nrunner-requirement": 28,
"unit": 678,
"jobs": 11,
"functional-parallel": 313,
"functional-parallel": 314,
"functional-serial": 7,
"optional-plugins": 0,
"optional-plugins-golang": 2,
Expand Down
11 changes: 11 additions & 0 deletions selftests/functional/plugin/vmimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ def test_list_images(self):
result = process.run(cmd_line)
self.assertIn(expected_output, result.stdout_text)

def test_get_debug(self):
cmd_line = (
f"{AVOCADO} --config {self.config_file.name} vmimage "
f"get --debug --distro=SHOULD_NEVER_EXIST --arch zzz_64"
)
result = process.run(cmd_line, ignore_status=True)
self.assertEqual(result.exit_status, exit_codes.AVOCADO_FAIL)
self.assertIn(
"Provider for should_never_exist not available", result.stdout_text
)

def tearDown(self):
self.base_dir.cleanup()

Expand Down

0 comments on commit 26441ef

Please sign in to comment.