Merge "libvirt: Add missing type hints"

This commit is contained in:
Zuul 2021-03-10 20:13:12 +00:00 committed by Gerrit Code Review
commit c5f2f71b2f
5 changed files with 37 additions and 31 deletions

View File

@ -165,7 +165,7 @@ class LibvirtReportNoSevTraitsTests(LibvirtReportTraitsTestBase):
# As we are changing the domain caps we need to clear the
# cache in the host object.
self.compute.driver._host._domain_caps = None
self.compute.driver._host._set_amd_sev_support()
self.compute.driver._host._supports_amd_sev = None
self.assertTrue(self.compute.driver._host.supports_amd_sev)
mock_exists.assert_has_calls([mock.call(SEV_KERNEL_PARAM_FILE)])
@ -235,7 +235,7 @@ class LibvirtReportSevTraitsTests(LibvirtReportTraitsTestBase):
# Retrigger the detection code. In the real world this
# would be a restart of the compute service.
self.compute.driver._host._domain_caps = None
self.compute.driver._host._set_amd_sev_support()
self.compute.driver._host._supports_amd_sev = None
self.assertFalse(self.compute.driver._host.supports_amd_sev)
mock_exists.assert_has_calls([mock.call(SEV_KERNEL_PARAM_FILE)])

View File

@ -27455,19 +27455,16 @@ class TestLibvirtSEV(test.NoDBTestCase):
@mock.patch.object(os.path, 'exists', new=mock.Mock(return_value=False))
class TestLibvirtSEVUnsupported(TestLibvirtSEV):
def test_get_mem_encrypted_slots_no_config(self):
self.driver._host._set_amd_sev_support()
self.assertEqual(0, self.driver._get_memory_encrypted_slots())
def test_get_mem_encrypted_slots_config_zero(self):
self.flags(num_memory_encrypted_guests=0, group='libvirt')
self.driver._host._set_amd_sev_support()
self.assertEqual(0, self.driver._get_memory_encrypted_slots())
@mock.patch.object(libvirt_driver.LOG, 'warning')
def test_get_mem_encrypted_slots_config_non_zero_unsupported(
self, mock_log):
self.flags(num_memory_encrypted_guests=16, group='libvirt')
self.driver._host._set_amd_sev_support()
# Still zero without mocked SEV support
self.assertEqual(0, self.driver._get_memory_encrypted_slots())
mock_log.assert_called_with(
@ -27475,7 +27472,6 @@ class TestLibvirtSEVUnsupported(TestLibvirtSEV):
'set to %d, but is not SEV-capable.', 16)
def test_get_mem_encrypted_slots_unsupported(self):
self.driver._host._set_amd_sev_support()
self.assertEqual(0, self.driver._get_memory_encrypted_slots())
@ -27486,7 +27482,6 @@ class TestLibvirtSEVSupported(TestLibvirtSEV):
@test.patch_exists(SEV_KERNEL_PARAM_FILE, True)
@test.patch_open(SEV_KERNEL_PARAM_FILE, "1\n")
def test_get_mem_encrypted_slots_unlimited(self):
self.driver._host._set_amd_sev_support()
self.assertEqual(db_const.MAX_INT,
self.driver._get_memory_encrypted_slots())
@ -27494,14 +27489,12 @@ class TestLibvirtSEVSupported(TestLibvirtSEV):
@test.patch_open(SEV_KERNEL_PARAM_FILE, "1\n")
def test_get_mem_encrypted_slots_config_non_zero_supported(self):
self.flags(num_memory_encrypted_guests=16, group='libvirt')
self.driver._host._set_amd_sev_support()
self.assertEqual(16, self.driver._get_memory_encrypted_slots())
@test.patch_exists(SEV_KERNEL_PARAM_FILE, True)
@test.patch_open(SEV_KERNEL_PARAM_FILE, "1\n")
def test_get_mem_encrypted_slots_config_zero_supported(self):
self.flags(num_memory_encrypted_guests=0, group='libvirt')
self.driver._host._set_amd_sev_support()
self.assertEqual(0, self.driver._get_memory_encrypted_slots())

View File

@ -1876,8 +1876,11 @@ def get_emulator_thread_policy_constraint(
return emu_threads_policy
def get_pci_numa_policy_constraint(flavor, image_meta):
"""Return pci numa affinity policy or None.
def get_pci_numa_policy_constraint(
flavor: 'objects.Flavor',
image_meta: 'objects.ImageMeta',
) -> str:
"""Validate and return the requested PCI NUMA affinity policy.
:param flavor: a flavor object to read extra specs from
:param image_meta: nova.objects.ImageMeta object instance

View File

@ -5678,12 +5678,12 @@ class LibvirtDriver(driver.ComputeDriver):
flavor: 'objects.Flavor',
instance: 'objects.Instance',
image_meta: 'objects.ImageMeta',
):
) -> None:
"""Add a vTPM device to the guest, if requested."""
# Enable virtual tpm support if required in the flavor or image.
vtpm_config = hardware.get_vtpm_constraint(flavor, image_meta)
if not vtpm_config:
return
return None
vtpm_secret_uuid = instance.system_metadata.get('vtpm_secret_uuid')
if not vtpm_secret_uuid:
@ -5871,8 +5871,12 @@ class LibvirtDriver(driver.ComputeDriver):
return supported_events
def _configure_guest_by_virt_type(
self, guest, instance, image_meta, flavor,
):
self,
guest: libvirt_guest.Guest,
instance: 'objects.Instance',
image_meta: 'objects.ImageMeta',
flavor: 'objects.Flavor',
) -> None:
if CONF.libvirt.virt_type in ("kvm", "qemu"):
caps = self._host.get_capabilities()
if caps.host.cpu.arch in (
@ -5930,9 +5934,17 @@ class LibvirtDriver(driver.ComputeDriver):
if guest.os_type == fields.VMMode.EXE:
guest.os_init_path = "/sbin/init"
return None
def _conf_non_lxc(
self, guest, root_device_name, rescue, instance, inst_path, image_meta,
disk_info,
self,
guest: libvirt_guest.Guest,
root_device_name: str,
rescue: bool,
instance: 'objects.Instance',
inst_path: str,
image_meta: 'objects.ImageMeta',
disk_info: ty.Dict[str, ty.Any],
):
if rescue:
self._set_guest_for_rescue(

View File

@ -121,11 +121,11 @@ class Host(object):
# A number of features are conditional on support in the hardware,
# kernel, QEMU, and/or libvirt. These are determined on demand and
# memoized by various properties below
self._supports_amd_sev = None
self._supports_amd_sev: ty.Optional[bool] = None
self._supports_uefi: ty.Optional[bool] = None
self._supports_secure_boot: ty.Optional[bool] = None
self._has_hyperthreading = None
self._has_hyperthreading: ty.Optional[bool] = None
@staticmethod
def _get_libvirt_proxy_classes(libvirt_module):
@ -1248,7 +1248,7 @@ class Host(object):
return False
@property
def has_hyperthreading(self):
def has_hyperthreading(self) -> bool:
"""Determine if host CPU has SMT, a.k.a. HyperThreading.
:return: True if the host has SMT enabled, else False.
@ -1316,7 +1316,7 @@ class Host(object):
self._supports_secure_boot = False
return False
def _kernel_supports_amd_sev(self):
def _kernel_supports_amd_sev(self) -> bool:
if not os.path.exists(SEV_KERNEL_PARAM_FILE):
LOG.debug("%s does not exist", SEV_KERNEL_PARAM_FILE)
return False
@ -1327,7 +1327,7 @@ class Host(object):
return contents == "1\n"
@property
def supports_amd_sev(self):
def supports_amd_sev(self) -> bool:
"""Returns a boolean indicating whether AMD SEV (Secure Encrypted
Virtualization) is supported. This is conditional on support
in the hardware, kernel, qemu, and libvirt.
@ -1338,21 +1338,18 @@ class Host(object):
would affect the support, nova-compute should be restarted
anyway.
"""
if self._supports_amd_sev is None:
self._set_amd_sev_support()
return self._supports_amd_sev
if self._supports_amd_sev is not None:
return self._supports_amd_sev
def _set_amd_sev_support(self):
self._supports_amd_sev = False
caps = self.get_capabilities()
if caps.host.cpu.arch != fields.Architecture.X86_64:
return
return self._supports_amd_sev
if not self._kernel_supports_amd_sev():
LOG.info("kernel doesn't support AMD SEV")
self._supports_amd_sev = False
return
return self._supports_amd_sev
domain_caps = self.get_domain_capabilities()
for arch in domain_caps:
@ -1362,9 +1359,10 @@ class Host(object):
for feature in domain_caps[arch][machine_type].features:
feature_is_sev = isinstance(
feature, vconfig.LibvirtConfigDomainCapsFeatureSev)
if (feature_is_sev and feature.supported):
if feature_is_sev and feature.supported:
LOG.info("AMD SEV support detected")
self._supports_amd_sev = True
return
return self._supports_amd_sev
LOG.debug("No AMD SEV support detected for any (arch, machine_type)")
return self._supports_amd_sev