def test_simple_target(self):
        """ test to make sure a simple target validates correctly.  No actual
        ZFS code is executed here.
        """
        # create a basic zpool object
        zpool = Zpool("rpool")
        zpool.action = "use_existing"

        # create one filesystem object
        fs = Filesystem("rpool/test1")
        fs.dataset_path = fs.name

        # create the DOC structure
        self.logical.insert_children(zpool)
        zpool.insert_children(fs)

        self.doc.volatile.insert_children(self.target)

        zpool_name, dataset, action, dataset_mp = dc.validate_target()
        self.assertTrue(zpool_name == zpool.name)
        self.assertTrue(dataset == "rpool/test1")
        self.assertTrue(action == "create")
        # the mountpoint will be None since the Filesystem.from_xml() method
        # is not called to determine the actual mountpoint
        self.assertFalse(dataset_mp)
    def test_simple_target(self):
        """ test to make sure a simple target validates correctly.  No actual
        ZFS code is executed here.
        """
        # create a basic zpool object
        zpool = Zpool("rpool")
        zpool.action = "use_existing"

        # create one filesystem object
        fs = Filesystem("rpool/test1")
        fs.dataset_path = fs.name

        # create the DOC structure
        self.logical.insert_children(zpool)
        zpool.insert_children(fs)

        self.doc.volatile.insert_children(self.target)

        zpool_name, dataset, action, dataset_mp = dc.validate_target()
        self.assertTrue(zpool_name == zpool.name)
        self.assertTrue(dataset == "rpool/test1")
        self.assertTrue(action == "create")
        # the mountpoint will be None since the Filesystem.from_xml() method
        # is not called to determine the actual mountpoint
        self.assertFalse(dataset_mp)
示例#3
0
class CreateSimpleDataObjectCache():
    '''A class that creates a simple data object'''
    def __init__(self, test_target=None):
        '''Initialize the class
           Parameters:
               -test_target - this arg is supplies the
                              test directory
        '''
        self.test_target = test_target
        self.engine = get_new_engine_instance()
        self.doc = self.engine.data_object_cache

        # Create the doc for finding the BE.
        self.desired_root = Target(Target.DESIRED)
        self.doc.persistent.insert_children(self.desired_root)
        self.logical = Logical(DEFAULT_LOGICAL_NAME)
        self.desired_root.insert_children(self.logical)
        self.zpool = Zpool(DEFAULT_ZPOOL_NAME)
        self.zpool.is_root = "true"
        self.logical.insert_children(self.zpool)
        self.vdev = Vdev(DEFAULT_VDEV_NAME)
        self.vdev.redundancy = "none"
        self.zpool.insert_children(self.vdev)
        self.be_obj = BE()
        self.zpool.insert_children(self.be_obj)
        self.be_obj.mountpoint = self.test_target
示例#4
0
    def test_no_filesystem(self):
        """ test to make sure validate target fails if no filesystem is
        specified.
        """
        # create a basic zpool object
        zpool = Zpool("rpool")
        zpool.action = "use_existing"

        # create the DOC structure
        self.logical.insert_children(zpool)

        self.doc.volatile.insert_children(self.target)

        self.assertRaises(RuntimeError, dc.validate_target)
示例#5
0
    def test_create_pool(self):
        """ test to make sure the create action on a zpool correctly errors
        """
        # create a basic zpool object with an action of create
        zpool = Zpool("rpool")
        zpool.action = "create"

        # create one filesystem object
        fs = Filesystem("rpool/test1")

        # create the DOC structure
        self.logical.insert_children(zpool)
        zpool.insert_children(fs)

        self.doc.volatile.insert_children(self.target)

        self.assertRaises(RuntimeError, dc.validate_target)
    def test_create_root_pool(self):
        """ test to make sure the create action on the bootfs correctly errors
        """
        # create a basic zpool object with an action of create
        zpool = Zpool("rpool")
        zpool.action = "create"

        # create one filesystem object
        fs = Filesystem("rpool/test1")

        # create the DOC structure
        self.logical.insert_children(zpool)
        zpool.insert_children(fs)

        self.doc.volatile.insert_children(self.target)

        self.assertRaises(RuntimeError, dc.validate_target)
示例#7
0
    def test_delete_filesystem(self):
        """ test to make sure the delete action correctly errors
        """
        # create a basic zpool object
        zpool = Zpool("rpool")
        zpool.action = "use_existing"

        # create one filesystem object with an action of delete
        fs = Filesystem("rpool/test1")
        fs.action = "delete"

        # create the DOC structure
        self.logical.insert_children(zpool)
        zpool.insert_children(fs)

        self.doc.volatile.insert_children(self.target)

        self.assertRaises(RuntimeError, dc.validate_target)
示例#8
0
    def test_two_filesystems(self):
        """ test to make sure two Filesystem objects correctly errors
        """
        # create a basic zpool object
        zpool = Zpool("rpool")
        zpool.action = "use_existing"

        # create two filesystem objects
        fs1 = Filesystem("rpool/test1")
        fs2 = Filesystem("rpool/test2")

        # create the DOC structure
        self.logical.insert_children(zpool)
        zpool.insert_children([fs1, fs2])

        self.doc.volatile.insert_children(self.target)

        self.assertRaises(RuntimeError, dc.validate_target)
    def test_delete_filesystem(self):
        """ test to make sure the delete action correctly errors
        """
        # create a basic zpool object
        zpool = Zpool("rpool")
        zpool.action = "use_existing"

        # create one filesystem object with an action of delete
        fs = Filesystem("rpool/test1")
        fs.action = "delete"

        # create the DOC structure
        self.logical.insert_children(zpool)
        zpool.insert_children(fs)

        self.doc.volatile.insert_children(self.target)

        self.assertRaises(RuntimeError, dc.validate_target)
    def test_two_filesystems(self):
        """ test to make sure two Filesystem objects correctly errors
        """
        # create a basic zpool object
        zpool = Zpool("rpool")
        zpool.action = "use_existing"

        # create two filesystem objects
        fs1 = Filesystem("rpool/test1")
        fs2 = Filesystem("rpool/test2")

        # create the DOC structure
        self.logical.insert_children(zpool)
        zpool.insert_children([fs1, fs2])

        self.doc.volatile.insert_children(self.target)

        self.assertRaises(RuntimeError, dc.validate_target)
示例#11
0
    def test_two_zpools(self):
        """ test to make sure two Zpool objects correctly errors
        """
        # create two zpool objects
        zpool1 = Zpool("rpool")
        zpool1.action = "use_existing"
        zpool2 = Zpool("rpool-two")
        zpool2.action = "use_existing"

        # create one filesystem object
        fs1 = Filesystem("rpool/test1")

        # create the DOC structure
        self.logical.insert_children([zpool1, zpool2])
        zpool1.insert_children(fs1)

        self.doc.volatile.insert_children(self.target)

        self.assertRaises(RuntimeError, dc.validate_target)
    def test_two_zpools(self):
        """ test to make sure two Zpool objects correctly errors
        """
        # create two zpool objects
        zpool1 = Zpool("rpool")
        zpool1.action = "use_existing"
        zpool2 = Zpool("rpool-two")
        zpool2.action = "use_existing"

        # create one filesystem object
        fs1 = Filesystem("rpool/test1")

        # create the DOC structure
        self.logical.insert_children([zpool1, zpool2])
        zpool1.insert_children(fs1)

        self.doc.volatile.insert_children(self.target)

        self.assertRaises(RuntimeError, dc.validate_target)
    def discover_zpools(self, search_name=""):
        """ discover_zpools - method to walk zpool list output to create Zpool
        objects.  Returns a logical DOC object with all zpools populated.
        """
        # create a logical element
        logical = Logical("logical")

        # set noswap and nodump to True until a zvol is found otherwise
        logical.noswap = True
        logical.nodump = True

        # retreive the list of zpools
        cmd = [ZPOOL, "list", "-H", "-o", "name"]
        p = run(cmd)

        # Get the list of zpools
        zpool_list = p.stdout.splitlines()

        # walk the list and populate the DOC
        for zpool_name in zpool_list:
            # if the user has specified a specific search name, only run
            # discovery on that particular pool name
            if search_name and zpool_name != search_name:
                continue

            self.logger.debug("Populating DOC for zpool:  %s", zpool_name)

            # create a new Zpool DOC object and insert it
            zpool = Zpool(zpool_name)
            zpool.action = "preserve"
            logical.insert_children(zpool)

            # check to see if the zpool is the boot pool
            cmd = [ZPOOL, "list", "-H", "-o", "bootfs", zpool_name]
            p = run(cmd)
            if p.stdout.rstrip() != "-":
                zpool.is_root = True

            # get the mountpoint of the zpool
            cmd = [ZFS, "get", "-H", "-o", "value", "mountpoint", zpool_name]
            p = run(cmd)
            zpool.mountpoint = p.stdout.strip()

            # set the vdev_mapping on each physical object in the DOC tree for
            # this zpool
            self.set_vdev_map(zpool)

            # for each zpool, get all of its datasets.  Switch to the C locale
            # so we don't have issues with LC_NUMERIC settings
            cmd = [
                ZFS, "list", "-r", "-H", "-o", "name,type,used,mountpoint",
                zpool_name
            ]
            p = run(cmd, env={"LC_ALL": "C"})

            # walk each dataset and create the appropriate DOC objects for
            # each.  Skip the first line of list output, as the top level
            # dataset (also the dataset with the same name as that of the
            # zpool) may have a different mountpoint than the zpool.
            for dataset in p.stdout.rstrip().split("\n")[1:]:
                try:
                    name, ds_type, ds_size, mountpoint = dataset.split(None, 3)
                except ValueError as err:
                    # trap on ValueError so any inconsistencies are captured
                    self.logger.debug("Unable to process dataset: %r" %
                                      dataset)
                    self.logger.debug(str(err))
                    continue

                # fix the name field to remove the name of the pool
                name = name.partition(zpool_name + "/")[2]

                if ds_type == "filesystem":
                    obj = Filesystem(name)
                    obj.mountpoint = mountpoint
                elif ds_type == "volume":
                    obj = Zvol(name)
                    obj.size = Size(ds_size)

                    # check for swap/dump.  If there's a match, set the zvol
                    # 'use' attribute and the noswap/nodump attribute of
                    # logical.  The zpool name needs to be re-attached to the
                    # zvol name to match what was already parsed
                    if os.path.join(zpool_name, name) in self.swap_list:
                        obj.use = "swap"
                        logical.noswap = False
                    if os.path.join(zpool_name, name) in self.dump_list:
                        obj.use = "dump"
                        logical.nodump = False

                obj.action = "preserve"
                zpool.insert_children(obj)

        return logical