class TestBE(unittest.TestCase):
    def setUp(self):
        self.engine = engine_test_utils.get_new_engine_instance()
        self.doc = self.engine.data_object_cache.volatile

        # create DOC objects
        self.target = Target(Target.DESIRED)
        self.doc.insert_children(self.target)

        self.logical = Logical("logical")

    def tearDown(self):
        self.target.delete_children()
        self.target.delete()
        engine_test_utils.reset_engine()

    def test_one_be_create(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        be1 = BE("be1")
        zpool.insert_children(be1)
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_two_be_create(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        be1 = BE("be1")
        be2 = BE("be2")
        zpool.insert_children([be1, be2])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_default_be_create(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        be1 = BE()
        zpool.insert_children(be1)
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))
class TestBE(unittest.TestCase):
    def setUp(self):
        self.engine = engine_test_utils.get_new_engine_instance()
        self.doc = self.engine.data_object_cache.volatile

        # create DOC objects
        self.target = Target(Target.DESIRED)
        self.doc.insert_children(self.target)

        self.logical = Logical("logical")

    def tearDown(self):
        self.target.delete_children()
        self.target.delete()
        engine_test_utils.reset_engine()

    def test_one_be_create(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        be1 = BE("be1")
        zpool.insert_children(be1)
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_two_be_create(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        be1 = BE("be1")
        be2 = BE("be2")
        zpool.insert_children([be1, be2])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_default_be_create(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        be1 = BE()
        zpool.insert_children(be1)
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))
class TestZpool(unittest.TestCase):
    def setUp(self):
        self.engine = engine_test_utils.get_new_engine_instance()
        self.doc = self.engine.data_object_cache.volatile

        # create DOC objects
        self.target = Target(Target.DESIRED)
        self.doc.insert_children(self.target)

        self.logical = Logical("logical")

        self.disk1 = Disk("disk")
        self.disk1.ctd = "c8t1d0"
        self.disk1.disk_prop = DiskProp()
        self.disk1.disk_prop.blocksize = 512

        self.disk2 = Disk("disk2")
        self.disk2.ctd = "c8t2d0"
        self.disk2.disk_prop = DiskProp()
        self.disk2.disk_prop.blocksize = 512

    def tearDown(self):
        self.target.delete_children()
        self.target.delete()
        engine_test_utils.reset_engine()

    def test_one_disk_zpool_create(self):
        self.disk1.in_zpool = "test_zpool"
        zpool = Zpool("test_zpool")
        zpool.action = "create"
        self.logical.insert_children(zpool)
        self.target.insert_children([self.disk1, self.logical])
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_two_disk_zpool_create(self):
        self.disk1.in_zpool = "test_zpool"
        self.disk2.in_zpool = "test_zpool"
        zpool = Zpool("test_zpool")
        zpool.action = "create"
        self.logical.insert_children(zpool)
        self.target.insert_children([self.disk1, self.disk2, self.logical])
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_whole_disk_zpool_create(self):
        self.disk1.in_zpool = "test_zpool"
        self.disk1.whole_disk = True
        self.disk2.in_zpool = "test_zpool"
        self.disk2.whole_disk = True
        zpool = Zpool("test_zpool")
        zpool.action = "create"
        self.logical.insert_children(zpool)
        self.target.insert_children([self.disk1, self.disk2, self.logical])
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_empty_zpool_delete(self):
        zpool = Zpool("test_zpool")
        zpool.action = "delete"
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_full_zpool_delete(self):
        zpool = Zpool("test_zpool")
        zpool.action = "delete"
        fs1 = Filesystem("test_zpool/fs1")
        fs2 = Filesystem("test_zpool/fs2")
        fs3 = Filesystem("test_zpool/fs3")
        zpool.insert_children([fs1, fs2, fs3])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_empty_zpool_preserve(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_full_zpool_preserve(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        fs1 = Filesystem("test_zpool/fs1")
        fs2 = Filesystem("test_zpool/fs2")
        fs3 = Filesystem("test_zpool/fs3")
        zpool.insert_children([fs1, fs2, fs3])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        self.doc.insert_children(self.target)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_filesystem_create(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        fs1 = Filesystem("test_zpool/fs1")
        fs1.action = "create"
        fs2 = Filesystem("test_zpool/fs2")
        fs2.action = "create"
        fs3 = Filesystem("test_zpool/fs3")
        fs3.action = "create"
        zpool.insert_children([fs1, fs2, fs3])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        self.doc.insert_children(self.target)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_filesystem_create_and_delete(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        fs1 = Filesystem("test_zpool/fs1")
        fs1.action = "create"
        fs2 = Filesystem("test_zpool/fs2")
        fs2.action = "delete"
        fs3 = Filesystem("test_zpool/fs3")
        fs3.action = "create"
        zpool.insert_children([fs1, fs2, fs3])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        self.doc.insert_children(self.target)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_zvol_create(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        fs1 = Filesystem("test_zpool/swap")
        fs1.action = "create"
        fs2 = Filesystem("test_zpool/dump")
        fs2.action = "create"
        zpool.insert_children([fs1, fs2])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        self.doc.insert_children(self.target)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_zvol_create_and_delete(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        fs1 = Filesystem("test_zpool/swap")
        fs1.action = "delete"
        fs2 = Filesystem("test_zpool/dump")
        fs2.action = "create"
        zpool.insert_children([fs1, fs2])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        self.doc.insert_children(self.target)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_full_zpool_create(self):
        zpool = Zpool("test_zpool")
        zpool.action = "create"
        fs1 = Filesystem("test_zpool/swap")
        fs1.action = "create"
        fs2 = Filesystem("test_zpool/dump")
        fs2.action = "create"
        zpool.insert_children([fs1, fs2])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_pool_and_dataset_options(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        fs1 = Filesystem("test_zpool/fs1")
        fs1.action = "create"
        fs2 = Filesystem("test_zpool/fs2")
        fs2.action = "create"
        fs3 = Filesystem("test_zpool/fs3")
        fs3.action = "create"
        pool_options = PoolOptions("pool options", dict())
        fs_options = DatasetOptions("fs options", dict())
        pool_options.data_dict["autoexpand"] = "off"
        fs_options.data_dict["atime"] = "on"
        zpool.insert_children([fs1, fs2, fs3, pool_options, fs_options])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        self.doc.insert_children(self.target)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_two_disk_vdev_create(self):
        self.disk1.in_zpool = "test_zpool"
        self.disk1.in_vdev = "test_mirror"
        self.disk2.in_zpool = "test_zpool"
        self.disk2.in_vdev = "test_mirror"
        zpool = Zpool("test_zpool")
        zpool.action = "create"
        vdev = Vdev("test_mirror")
        vdev.redundancy = "mirror"
        zpool.insert_children(vdev)
        self.logical.insert_children(zpool)
        self.target.insert_children([self.disk1, self.disk2, self.logical])
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_multi_disk_multi_vdev_create(self):
        self.disk1.in_zpool = "test_zpool"
        self.disk1.in_vdev = "test_mirror"
        self.disk2.in_zpool = "test_zpool"
        self.disk2.in_vdev = "test_mirror"
        self.disk3 = Disk("disk3")
        self.disk3.ctd = "c8t3d0"
        self.disk3.in_zpool = "test_zpool"
        self.disk3.in_vdev = "test_raidz"
        self.disk4 = Disk("disk4")
        self.disk4.ctd = "c8t4d0"
        self.disk4.in_zpool = "test_zpool"
        self.disk4.in_vdev = "test_raidz"
        self.disk5 = Disk("disk5")
        self.disk5.ctd = "c8t5d0"
        self.disk5.in_zpool = "test_zpool"
        self.disk5.in_vdev = "test_raidz"
        self.disk6 = Disk("disk6")
        self.disk6.ctd = "c8t6d0"
        self.disk6.in_zpool = "test_zpool"
        self.disk6.in_vdev = "test_spare"
        self.disk7 = Disk("disk7")
        self.disk7.ctd = "c8t7d0"
        self.disk7.in_zpool = "test_zpool"
        self.disk7.in_vdev = "test_log"
        zpool1 = Zpool("test_zpool")
        zpool1.action = "create"
        vdev1 = Vdev("test_mirror")
        vdev1.redundancy = "mirror"
        vdev2 = Vdev("test_raidz")
        vdev2.redundancy = "raidz"
        vdev3 = Vdev("test_log")
        vdev3.redundancy = "log"
        vdev4 = Vdev("test_spare")
        vdev4.redundancy = "spare"
        zpool1.insert_children([vdev1, vdev2, vdev3, vdev4])
        self.logical.insert_children(zpool1)
        self.target.insert_children([self.disk1, self.disk2, self.disk3, \
            self.disk4, self.disk5, self.disk6, self.disk7, self.logical])
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))
class TestZpool(unittest.TestCase):
    def setUp(self):
        self.engine = engine_test_utils.get_new_engine_instance()
        self.doc = self.engine.data_object_cache.volatile

        # create DOC objects
        self.target = Target(Target.DESIRED)
        self.doc.insert_children(self.target)

        self.logical = Logical("logical")

        self.disk1 = Disk("disk")
        self.disk1.ctd = "c8t1d0"
        self.disk1.disk_prop = DiskProp()
        self.disk1.disk_prop.blocksize = 512

        self.disk2 = Disk("disk2")
        self.disk2.ctd = "c8t2d0"
        self.disk2.disk_prop = DiskProp()
        self.disk2.disk_prop.blocksize = 512

    def tearDown(self):
        self.target.delete_children()
        self.target.delete()
        engine_test_utils.reset_engine()

    def test_one_disk_zpool_create(self):
        self.disk1.in_zpool = "test_zpool"
        zpool = Zpool("test_zpool")
        zpool.action = "create"
        self.logical.insert_children(zpool)
        self.target.insert_children([self.disk1, self.logical])
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_two_disk_zpool_create(self):
        self.disk1.in_zpool = "test_zpool"
        self.disk2.in_zpool = "test_zpool"
        zpool = Zpool("test_zpool")
        zpool.action = "create"
        self.logical.insert_children(zpool)
        self.target.insert_children([self.disk1, self.disk2, self.logical])
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_whole_disk_zpool_create(self):
        self.disk1.in_zpool = "test_zpool"
        self.disk1.whole_disk = True
        self.disk2.in_zpool = "test_zpool"
        self.disk2.whole_disk = True
        zpool = Zpool("test_zpool")
        zpool.action = "create"
        self.logical.insert_children(zpool)
        self.target.insert_children([self.disk1, self.disk2, self.logical])
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_empty_zpool_delete(self):
        zpool = Zpool("test_zpool")
        zpool.action = "delete"
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_full_zpool_delete(self):
        zpool = Zpool("test_zpool")
        zpool.action = "delete"
        fs1 = Filesystem("test_zpool/fs1")
        fs2 = Filesystem("test_zpool/fs2")
        fs3 = Filesystem("test_zpool/fs3")
        zpool.insert_children([fs1, fs2, fs3])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_empty_zpool_preserve(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_full_zpool_preserve(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        fs1 = Filesystem("test_zpool/fs1")
        fs2 = Filesystem("test_zpool/fs2")
        fs3 = Filesystem("test_zpool/fs3")
        zpool.insert_children([fs1, fs2, fs3])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        self.doc.insert_children(self.target)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_filesystem_create(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        fs1 = Filesystem("test_zpool/fs1")
        fs1.action = "create"
        fs2 = Filesystem("test_zpool/fs2")
        fs2.action = "create"
        fs3 = Filesystem("test_zpool/fs3")
        fs3.action = "create"
        zpool.insert_children([fs1, fs2, fs3])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        self.doc.insert_children(self.target)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_filesystem_create_and_delete(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        fs1 = Filesystem("test_zpool/fs1")
        fs1.action = "create"
        fs2 = Filesystem("test_zpool/fs2")
        fs2.action = "delete"
        fs3 = Filesystem("test_zpool/fs3")
        fs3.action = "create"
        zpool.insert_children([fs1, fs2, fs3])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        self.doc.insert_children(self.target)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_zvol_create(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        fs1 = Filesystem("test_zpool/swap")
        fs1.action = "create"
        fs2 = Filesystem("test_zpool/dump")
        fs2.action = "create"
        zpool.insert_children([fs1, fs2])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        self.doc.insert_children(self.target)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_zvol_create_and_delete(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        fs1 = Filesystem("test_zpool/swap")
        fs1.action = "delete"
        fs2 = Filesystem("test_zpool/dump")
        fs2.action = "create"
        zpool.insert_children([fs1, fs2])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        self.doc.insert_children(self.target)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_full_zpool_create(self):
        zpool = Zpool("test_zpool")
        zpool.action = "create"
        fs1 = Filesystem("test_zpool/swap")
        fs1.action = "create"
        fs2 = Filesystem("test_zpool/dump")
        fs2.action = "create"
        zpool.insert_children([fs1, fs2])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_pool_and_dataset_options(self):
        zpool = Zpool("test_zpool")
        zpool.action = "preserve"
        fs1 = Filesystem("test_zpool/fs1")
        fs1.action = "create"
        fs2 = Filesystem("test_zpool/fs2")
        fs2.action = "create"
        fs3 = Filesystem("test_zpool/fs3")
        fs3.action = "create"
        pool_options = PoolOptions("pool options", dict())
        fs_options = DatasetOptions("fs options", dict())
        pool_options.data_dict["autoexpand"] = "off"
        fs_options.data_dict["atime"] = "on"
        zpool.insert_children([fs1, fs2, fs3, pool_options, fs_options])
        self.logical.insert_children(zpool)
        self.target.insert_children(self.logical)
        self.doc.insert_children(self.target)
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_two_disk_vdev_create(self):
        self.disk1.in_zpool = "test_zpool"
        self.disk1.in_vdev = "test_mirror"
        self.disk2.in_zpool = "test_zpool"
        self.disk2.in_vdev = "test_mirror"
        zpool = Zpool("test_zpool")
        zpool.action = "create"
        vdev = Vdev("test_mirror")
        vdev.redundancy = "mirror"
        zpool.insert_children(vdev)
        self.logical.insert_children(zpool)
        self.target.insert_children([self.disk1, self.disk2, self.logical])
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))

    def test_multi_disk_multi_vdev_create(self):
        self.disk1.in_zpool = "test_zpool"
        self.disk1.in_vdev = "test_mirror"
        self.disk2.in_zpool = "test_zpool"
        self.disk2.in_vdev = "test_mirror"
        self.disk3 = Disk("disk3")
        self.disk3.ctd = "c8t3d0"
        self.disk3.in_zpool = "test_zpool"
        self.disk3.in_vdev = "test_raidz"
        self.disk4 = Disk("disk4")
        self.disk4.ctd = "c8t4d0"
        self.disk4.in_zpool = "test_zpool"
        self.disk4.in_vdev = "test_raidz"
        self.disk5 = Disk("disk5")
        self.disk5.ctd = "c8t5d0"
        self.disk5.in_zpool = "test_zpool"
        self.disk5.in_vdev = "test_raidz"
        self.disk6 = Disk("disk6")
        self.disk6.ctd = "c8t6d0"
        self.disk6.in_zpool = "test_zpool"
        self.disk6.in_vdev = "test_spare"
        self.disk7 = Disk("disk7")
        self.disk7.ctd = "c8t7d0"
        self.disk7.in_zpool = "test_zpool"
        self.disk7.in_vdev = "test_log"
        zpool1 = Zpool("test_zpool")
        zpool1.action = "create"
        vdev1 = Vdev("test_mirror")
        vdev1.redundancy = "mirror"
        vdev2 = Vdev("test_raidz")
        vdev2.redundancy = "raidz"
        vdev3 = Vdev("test_log")
        vdev3.redundancy = "log"
        vdev4 = Vdev("test_spare")
        vdev4.redundancy = "spare"
        zpool1.insert_children([vdev1, vdev2, vdev3, vdev4])
        self.logical.insert_children(zpool1)
        self.target.insert_children([self.disk1, self.disk2, self.disk3, \
            self.disk4, self.disk5, self.disk6, self.disk7, self.logical])
        t = instantiation.TargetInstantiation("test_ti")
        try:
            t.execute(dry_run=True)
        except Exception as err:
            self.fail(str(err))