avocado_qemu: Fix KNOWN_DISTROS map into the LinuxDistro class

As the KNOWN_DISTROS grows, more loosely methods will be created in
the avocado_qemu/__init__.py file.

Let's refactor the code so that KNOWN_DISTROS and related methods are
packaged in a class

Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
Signed-off-by: Eric Auger <eric.auger@redhat.com>
Message-Id: <20210706131729.30749-2-eric.auger@redhat.com>
[CR: moved aarch64 definition from patch 2 to 1]
[CR: protect get() when arch is not defined]
[CR: split long lines]
Acked-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
Signed-off-by: Cleber Rosa <crosa@redhat.com>
This commit is contained in:
Willian Rampazzo 2021-07-06 15:17:26 +02:00 committed by Cleber Rosa
parent 9f51934130
commit fb13040173

View File

@ -306,9 +306,13 @@ class LinuxSSHMixIn:
f'Guest command failed: {command}') f'Guest command failed: {command}')
return stdout_lines, stderr_lines return stdout_lines, stderr_lines
class LinuxDistro:
"""Represents a Linux distribution
#: A collection of known distros and their respective image checksum Holds information of known distros.
KNOWN_DISTROS = { """
#: A collection of known distros and their respective image checksum
KNOWN_DISTROS = {
'fedora': { 'fedora': {
'31': { '31': {
'x86_64': 'x86_64':
@ -327,13 +331,25 @@ KNOWN_DISTROS = {
} }
} }
def __init__(self, name, version, arch):
def get_known_distro_checksum(distro, distro_version, arch): self.name = name
self.version = version
self.arch = arch
try: try:
return KNOWN_DISTROS.get(distro).get(distro_version).\ info = self.KNOWN_DISTROS.get(name).get(version).get(arch)
get(arch).get('checksum')
except AttributeError: except AttributeError:
return None # Unknown distro
info = None
self._info = info or {}
@property
def checksum(self):
"""Gets the cloud-image file checksum"""
return self._info.get('checksum', None)
@checksum.setter
def checksum(self, value):
self._info['checksum'] = value
class LinuxTest(Test, LinuxSSHMixIn): class LinuxTest(Test, LinuxSSHMixIn):
@ -344,24 +360,24 @@ class LinuxTest(Test, LinuxSSHMixIn):
""" """
timeout = 900 timeout = 900
distro_checksum = None distro = None
username = 'root' username = 'root'
password = 'password' password = 'password'
def _set_distro(self): def _set_distro(self):
distro = self.params.get( distro_name = self.params.get(
'distro', 'distro',
default=self._get_unique_tag_val('distro')) default=self._get_unique_tag_val('distro'))
if not distro: if not distro_name:
distro = 'fedora' distro_name = 'fedora'
self.distro = distro
distro_version = self.params.get( distro_version = self.params.get(
'distro_version', 'distro_version',
default=self._get_unique_tag_val('distro_version')) default=self._get_unique_tag_val('distro_version'))
if not distro_version: if not distro_version:
distro_version = '31' distro_version = '31'
self.distro_version = distro_version
self.distro = LinuxDistro(distro_name, distro_version, self.arch)
# The distro checksum behaves differently than distro name and # The distro checksum behaves differently than distro name and
# version. First, it does not respect a tag with the same # version. First, it does not respect a tag with the same
@ -370,13 +386,9 @@ class LinuxTest(Test, LinuxSSHMixIn):
# order of precedence is: parameter, attribute and then value # order of precedence is: parameter, attribute and then value
# from KNOWN_DISTROS. # from KNOWN_DISTROS.
distro_checksum = self.params.get('distro_checksum', distro_checksum = self.params.get('distro_checksum',
default=self.distro_checksum) default=None)
if not distro_checksum:
distro_checksum = get_known_distro_checksum(self.distro,
self.distro_version,
self.arch)
if distro_checksum: if distro_checksum:
self.distro_checksum = distro_checksum self.distro.checksum = distro_checksum
def setUp(self, ssh_pubkey=None, network_device_type='virtio-net'): def setUp(self, ssh_pubkey=None, network_device_type='virtio-net'):
super(LinuxTest, self).setUp() super(LinuxTest, self).setUp()
@ -418,14 +430,14 @@ class LinuxTest(Test, LinuxSSHMixIn):
self.log.info('Downloading/preparing boot image') self.log.info('Downloading/preparing boot image')
# Fedora 31 only provides ppc64le images # Fedora 31 only provides ppc64le images
image_arch = self.arch image_arch = self.arch
if self.distro == 'fedora': if self.distro.name == 'fedora':
if image_arch == 'ppc64': if image_arch == 'ppc64':
image_arch = 'ppc64le' image_arch = 'ppc64le'
try: try:
boot = vmimage.get( boot = vmimage.get(
self.distro, arch=image_arch, version=self.distro_version, self.distro.name, arch=image_arch, version=self.distro.version,
checksum=self.distro_checksum, checksum=self.distro.checksum,
algorithm='sha256', algorithm='sha256',
cache_dir=self.cache_dirs[0], cache_dir=self.cache_dirs[0],
snapshot_dir=self.workdir) snapshot_dir=self.workdir)