Exemplo n.º 1
0
    def test_match_or_die(self):
        """Test the match_or_die() function."""
        with self.assertRaises(ValueMismatchError) as catcher:
            match_or_die("input", "a", "output", "b")
        self.assertEqual(str(catcher.exception),
                         "input a does not match output b")

        match_or_die("input", "a", "output", "a")
Exemplo n.º 2
0
    def test_match_or_die(self):
        """Test the match_or_die() function."""
        with self.assertRaises(ValueMismatchError) as catcher:
            match_or_die("input", "a", "output", "b")
        self.assertEqual(str(catcher.exception),
                         "input a does not match output b")

        match_or_die("input", "a", "output", "a")
Exemplo n.º 3
0
    def run(self):
        """Do the actual work of this command.

        Raises:
          InvalidInputError: if :func:`ready_to_run` reports ``False``
        """
        super(COTRemoveFile, self).run()

        vm = self.vm

        # Find the existing file entry.
        # There may also be a disk entry for this file.
        # There may also be a disk device that maps this file to a drive.
        (file1, disk1, _, disk_dev1) = vm.search_from_filename(self.file_path)
        (file2, disk2, _, disk_dev2) = vm.search_from_file_id(self.file_id)
        file_obj = check_for_conflict("file to remove", [file1, file2])
        disk = check_for_conflict("disk associated with file to remove",
                                  [disk1, disk2])
        disk_drive = check_for_conflict("disk drive mapping this file",
                                        [disk_dev1, disk_dev2])

        if file_obj is None:
            raise InvalidInputError("No such file found")

        if self.file_id is None:
            self.file_id = vm.get_id_from_file(file_obj)
        else:
            match_or_die('--file-id', self.file_id,
                         'file id in OVF', vm.get_id_from_file(file_obj))

        if self.file_path is None:
            self.file_path = vm.get_path_from_file(file_obj)
        else:
            match_or_die('--file-path', self.file_path,
                         'file path in OVF', vm.get_path_from_file(file_obj))

        prompt_info = "file '{0}' (ID '{1}')".format(self.file_path,
                                                     self.file_id)
        if disk is not None:
            prompt_info += " and disk '{0}'".format(vm.get_id_from_disk(disk))
        if disk_drive is not None:
            prompt_info += " and device '{0}'".format(
                vm.device_info_str(disk_drive))

        self.ui.confirm_or_die("Remove {0}?".format(prompt_info))

        vm.remove_file(file_obj, disk=disk,
                       disk_drive=disk_drive)
Exemplo n.º 4
0
def validate_elements(vm, file_obj, disk_obj, disk_item, ctrl_item,
                      file_id, ctrl_type):
    """Validate any existing file, disk, controller item, and disk item.

    Raises:
      ValueMismatchError: if the search criteria select a non-unique set.

    Args:
      vm (VMDescription): Virtual machine object
      file_obj (object): Known file object
      disk_obj (object): Known disk object
      disk_item (object): Known disk device object
      ctrl_item (object): Known controller device object
      file_id (str): File identifier string
      ctrl_type (str): Controller type ("ide", "sata", or "scsi")
    """
    # Ok, we now have confirmed that we have at most one of each of these
    # four objects. Now it's time for some sanity checking...

    if file_obj is not None:
        if file_id is not None:
            match_or_die("File id", vm.get_id_from_file(file_obj),
                         "--file-id", file_id)
        # Should never fail this test if the above logic was sound...
        if disk_obj is not None:
            match_or_die("File id", vm.get_id_from_file(file_obj),
                         "Disk fileRef", vm.get_file_ref_from_disk(disk_obj))

    if disk_obj is not None:
        if file_id is not None:
            match_or_die("Disk fileRef", vm.get_file_ref_from_disk(disk_obj),
                         "--file-id", file_id)
        if file_obj is None:
            # This will happen if we're replacing a placeholder entry
            # (disk exists but has no associated file)
            logger.verbose("Found Disk but not File - maybe placeholder?")

    if disk_item is not None:
        vm.check_sanity_of_disk_device(disk_obj, file_obj,
                                       disk_item, ctrl_item)

    if ctrl_item is not None:
        match_or_die("controller type",
                     ctrl_item.hardware_type,
                     "--controller", ctrl_type)

    # Whew! Everything looks sane!
    logger.debug("Validation of existing data complete")
Exemplo n.º 5
0
def validate_elements(vm, file_obj, disk_obj, disk_item, ctrl_item,
                      file_id, ctrl_type):
    """Validate any existing file, disk, controller item, and disk item.

    Raises:
      ValueMismatchError: if the search criteria select a non-unique set.

    Args:
      vm (VMDescription): Virtual machine object
      file_obj (object): Known file object
      disk_obj (object): Known disk object
      disk_item (object): Known disk device object
      ctrl_item (object): Known controller device object
      file_id (str): File identifier string
      ctrl_type (str): Controller type ("ide" or "scsi")
    """
    # Ok, we now have confirmed that we have at most one of each of these
    # four objects. Now it's time for some sanity checking...

    if file_obj is not None:
        if file_id is not None:
            match_or_die("File id", vm.get_id_from_file(file_obj),
                         "--file-id", file_id)
        # Should never fail this test if the above logic was sound...
        if disk_obj is not None:
            match_or_die("File id", vm.get_id_from_file(file_obj),
                         "Disk fileRef", vm.get_file_ref_from_disk(disk_obj))

    if disk_obj is not None:
        if file_id is not None:
            match_or_die("Disk fileRef", vm.get_file_ref_from_disk(disk_obj),
                         "--file-id", file_id)
        if file_obj is None:
            # This will happen if we're replacing a placeholder entry
            # (disk exists but has no associated file)
            logger.verbose("Found Disk but not File - maybe placeholder?")

    if disk_item is not None:
        vm.check_sanity_of_disk_device(disk_obj, file_obj,
                                       disk_item, ctrl_item)

    if ctrl_item is not None:
        match_or_die("controller type",
                     ctrl_item.hardware_type,
                     "--controller", ctrl_type)

    # Whew! Everything looks sane!
    logger.debug("Validation of existing data complete")