def test_bootloader_in_kickstart(self): ''' test that a bootloader such as prepboot/biosboot shows up in the kickstart data ''' # prepboot test case with patch( 'pyanaconda.storage.osinstall.InstallerStorage.bootloader_device', new_callable=PropertyMock) as mock_bootloader_device: with patch( 'pyanaconda.storage.osinstall.InstallerStorage.mountpoints', new_callable=PropertyMock) as mock_mountpoints: # set up prepboot partition bootloader_device_obj = PartitionDevice( "test_partition_device") bootloader_device_obj.size = Size('5 MiB') bootloader_device_obj.format = formats.get_format("prepboot") prepboot_blivet_obj = InstallerStorage() # mountpoints must exist for update_ksdata to run mock_bootloader_device.return_value = bootloader_device_obj mock_mountpoints.values.return_value = [] # initialize ksdata prepboot_ksdata = returnClassForVersion()() prepboot_blivet_obj.ksdata = prepboot_ksdata prepboot_blivet_obj.update_ksdata() self.assertIn("part prepboot", str(prepboot_blivet_obj.ksdata)) # biosboot test case with patch('pyanaconda.storage.osinstall.InstallerStorage.devices', new_callable=PropertyMock) as mock_devices: with patch( 'pyanaconda.storage.osinstall.InstallerStorage.mountpoints', new_callable=PropertyMock) as mock_mountpoints: # set up biosboot partition biosboot_device_obj = PartitionDevice( "biosboot_partition_device") biosboot_device_obj.size = Size('1MiB') biosboot_device_obj.format = formats.get_format("biosboot") biosboot_blivet_obj = InstallerStorage() # mountpoints must exist for updateKSData to run mock_devices.return_value = [biosboot_device_obj] mock_mountpoints.values.return_value = [] # initialize ksdata biosboot_ksdata = returnClassForVersion()() biosboot_blivet_obj.ksdata = biosboot_ksdata biosboot_blivet_obj.update_ksdata() self.assertIn("part biosboot", str(biosboot_blivet_obj.ksdata))
def test_biosboot_bootloader_in_kickstart(self, mock_mountpoints, mock_devices, dbus): """Test that a biosboot bootloader shows up in the ks data.""" # set up biosboot partition biosboot_device_obj = PartitionDevice("biosboot_partition_device") biosboot_device_obj.size = Size('1MiB') biosboot_device_obj.format = formats.get_format("biosboot") # mountpoints must exist for updateKSData to run mock_devices.return_value = [biosboot_device_obj] mock_mountpoints.values.return_value = [] # initialize ksdata biosboot_blivet_obj = InstallerStorage(makeVersion()) biosboot_blivet_obj.update_ksdata() self.assertIn("part biosboot", str(biosboot_blivet_obj.ksdata))
class TestCaseComponent(object): """A TestCaseComponent is one individual test that runs as part of a TestCase. It consists of a set of disk images provided by self.disksToCreate, a kickstart storage snippet provided by self.ks, and an expected error condition provided by self.expectedExceptionType and self.expectedExceptionText. If the TestCaseComponent is expected to succeed, these latter two should just be set to None. A TestCaseComponent succeeds in the following cases: * No exception is encountered, and self.expectedExceptionType is None. A TestCaseComponent fails in all other cases. Class attributes: name -- An identifying string given to this TestCaseComponent. """ name = "" def __init__(self): """Create a new TestCaseComponent instance. This __init__ method should typically do very little. However, subclasses must be sure to set self.disksToCreate. This attribute is a list of (disk name, blivet.Size) tuples that will be used in this test. Disks given in this list will be automatically created by setupDisks and destroyed by tearDownDisks. """ self._disks = {} self._storage = None self.disksToCreate = [] @property def ks(self): """Return the storage-specific portion of a kickstart file used for performing this test. The kickstart snippet must be provided as text. Only storage commands will be tested. No bootloader actions will be performed. """ return "" def setupDisks(self, ksdata): """Create all disk images given by self.disksToCreate and initialize the storage module. Subclasses may override this method, but they should be sure to call the base method as well. """ self._storage = InstallerStorage(ksdata=ksdata) # blivet only sets up the bootloader in installer_mode. We don't # want installer_mode, though, because that involves running lots # of programs on the host and setting up all sorts of other things. # Thus, we set it up manually. from pyanaconda.bootloader import get_bootloader self._storage._bootloader = get_bootloader() for (name, size) in self.disksToCreate: self._disks[name] = blivet.util.create_sparse_tempfile(name, size) self._storage.disk_images[name] = self._disks[name] self._storage.reset() def tearDownDisks(self): """Disable any disk images used by this test component and remove their image files from the host system. Subclasses may override this method, but they should call the base method as well to make sure the images get destroyed. """ # pylint: disable=undefined-variable self._storage.devicetree.teardown_disk_images() for d in self._disks.values(): os.unlink(d) @property def expectedExceptionType(self): """Should this test component be expected to fail, this property returns the exception type. If this component is not expected to fail, this property returns None. All components that are expected to fail should override this property. """ return None @property def expectedExceptionText(self): """Should this test component be expected to fail, this property returns a regular expression that the raised exception's text must match. Otherwise, this property returns None. All components that are expected to fail should override this property. """ return None def _text_matches(self, s): prog = re.compile(self.expectedExceptionText) for line in s.splitlines(): match = prog.match(line) if match: return match return None def _run(self): from blivet.errors import StorageError # Set up disks/blivet. try: # Parse the kickstart using anaconda's parser, since it has more # advanced error detection. This also requires having storage set # up first. parser = AnacondaKSParser(AnacondaKSHandler()) parser.readKickstartFromString(self.ks) self.setupDisks(parser.handler) doKickstartStorage(self._storage, parser.handler) self._storage.update_ksdata() self._storage.devicetree.teardown_all() self._storage.do_it() except (BootLoaderError, KickstartError, StorageError) as e: # anaconda handles expected kickstart errors (like parsing busted # input files) by printing the error and quitting. For testing, an # error might be expected so we should compare the result here with # what is expected. if self.expectedExceptionType and isinstance( e, self.expectedExceptionType): # We expected an exception, and we got one of the correct type. # If it also contains the string we were expecting, then the # test case passes. Otherwise, it's a failure. if self.expectedExceptionText and self._text_matches(str(e)): return else: raise FailedTest(str(e), self.expectedExceptionText) else: # We either got an exception when we were not expecting one, # or we got one of a type other than what we were expecting. # Either of these cases indicates a failure of the test case. raise FailedTest(e, self.expectedExceptionType) finally: self.tearDownDisks() if self.expectedExceptionType: raise FailedTest(None, self.expectedExceptionType)