def test_04_snapshot_limit(self):
        """Test snapshot limit in snapshot policies
        """
        # Validate the following
        # 1. Perform hourly recurring snapshot on the root disk of VM and keep
        #    the maxsnapshots as 1
        # 2. listSnapshots should list the snapshot that was created
        #    snapshot folder in secondary storage should contain only one
        #    snapshot image(/secondary/snapshots/$accountid/$volumeid/)

        # Get the Root disk of VM
        volumes = list_volumes(self.apiclient, virtualmachineid=self.virtual_machine.id, type="ROOT", listall=True)
        self.assertEqual(isinstance(volumes, list), True, "Check list response returns a valid list")
        volume = volumes[0]

        # Create a snapshot policy
        recurring_snapshot = SnapshotPolicy.create(self.apiclient, volume.id, self.services["recurring_snapshot"])
        self.cleanup.append(recurring_snapshot)

        snapshot_policy = list_snapshot_policy(self.apiclient, id=recurring_snapshot.id, volumeid=volume.id)
        self.assertEqual(isinstance(snapshot_policy, list), True, "Check list response returns a valid list")

        self.assertNotEqual(snapshot_policy, None, "Check if result exists in list item call")

        self.assertEqual(
            snapshot_policy[0].id, recurring_snapshot.id, "Check recurring snapshot id in list resources call"
        )
        self.assertEqual(
            snapshot_policy[0].maxsnaps,
            self.services["recurring_snapshot"]["maxsnaps"],
            "Check interval type in list resources call",
        )
        # Sleep for (maxsnaps+1) hours to verify
        # only maxsnaps snapshots are retained
        time.sleep((int(self.services["recurring_snapshot"]["maxsnaps"]) + 1) * 3600)

        # Verify the snapshot was created or not
        snapshots = list_snapshots(
            self.apiclient,
            volumeid=volume.id,
            intervaltype=self.services["recurring_snapshot"]["intervaltype"],
            snapshottype="RECURRING",
            listall=True,
        )

        self.assertEqual(isinstance(snapshots, list), True, "Check list response returns a valid list")
        self.assertEqual(
            len(snapshots),
            self.services["recurring_snapshot"]["maxsnaps"],
            "Check maximum number of recurring snapshots retained",
        )
        snapshot = snapshots[0]
        # Sleep to ensure that snapshot is reflected in sec storage
        time.sleep(self.services["sleep"])
        self.assertTrue(is_snapshot_on_nfs(self.apiclient, self.dbclient, self.config, self.zone.id, snapshot.id))
        return
    def test_01_concurrent_snapshots(self):
        """Concurrent Snapshots
            1. Create snapshot on 2 new VMs in parallel and check
                    1. all snapshot jobs are running
                    2. listSnapshots should list all the snapshots
                    3. Verify secondary_storage NFS share
                       contains the required volume under
                       /secondary/snapshots/$accountid/$volumeid/$snapshot_uuid.
                    4. Verify backup_snap_id was non null in "snapshots"table
            2. Perform step 1 for all the 4 VM's.
            3. Verify that VM gets migrated when snapshot
                is in pregress for the VM.
            4. Verify that snapshots get created when
                VM's are stoped in between snapshot creation.
            5. Perform live Migration then stop all the
                VM's after that verify that snapshot creation success .
            6. Verify success of snapshots creation in case:
                Stop the running VM while performing
                concurrent snapshot on volumes
            7. Verify success of snapshots creation in case:
                Start Migration of VM's and then Stop the running VM then
                performing concurrent snapshot on volumes
        """
        # Step 1
        try:
            create_snapshot_thread_1 = Thread(
                target=CreateSnapshot,
                args=(
                    self,
                    self.root_pool[0],
                    False))
            create_snapshot_thread_2 = Thread(
                target=CreateSnapshot,
                args=(
                    self,
                    self.root_pool[1],
                    False))
            create_snapshot_thread_1.start()
            create_snapshot_thread_2.start()
            create_snapshot_thread_1.join()
            create_snapshot_thread_2.join()

        except:
            self.debug("Error: unable to start thread")

        snapshots = list_snapshots(
            self.apiclient,
            account=self.account.name,
            domainid=self.account.domainid,
            listall=True
        )

        for snapshot in self.snapshot_pool:
            self.assertTrue(snapshot.id in any(
                s.id) for s in snapshots)

        for snapshot in self.snapshot_pool:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        for snapshot in self.snapshot_pool:
            snapshot.delete(self.apiclient)

        self.snapshot_pool = []
        # Step 2
        thread_pool = []
        for i in range(4):
            try:
                create_snapshot_thread_1 = Thread(
                    target=CreateSnapshot,
                    args=(
                        self,
                        self.root_pool[i],
                        False))
                thread_pool.append(create_snapshot_thread_1)

            except Exception as e:
                raise Exception(
                    "Warning: Exception unable to start thread : %s" %
                    e)

        for thread in thread_pool:
            thread.start()

        for thread in thread_pool:
            thread.join()

        snapshots = list_snapshots(
            self.apiclient,
            account=self.account.name,
            domainid=self.account.domainid,
            listall=True
        )

        for snapshot in self.snapshot_pool:
            self.assertTrue(snapshot.id in any(
                s.id) for s in snapshots)

        for snapshot in self.snapshot_pool:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        for snapshot in self.snapshot_pool:
            snapshot.delete(self.apiclient)

        self.snapshot_pool = []

        # Step 3
        # Recurring snapshot
        try:
            create_snapshot_thread_1 = Thread(
                target=CreateSnapshot,
                args=(
                    self,
                    self.root_pool[0],
                    True))
            create_snapshot_thread_2 = Thread(
                target=CreateSnapshot,
                args=(
                    self,
                    self.root_pool[1],
                    True))
            create_snapshot_thread_1.start()
            create_snapshot_thread_2.start()
            create_snapshot_thread_1.join()
            create_snapshot_thread_2.join()

        except:
            self.debug("Error: unable to start thread")

        for rec_snap in self.rec_policy_pool:
            list_snapshots_policy_1 = list_snapshot_policy(
                self.apiclient,
                id=rec_snap.id,
            )

            list_validation = validateList(list_snapshots_policy_1)

            self.assertEqual(
                list_validation[0],
                PASS,
                "snapshot list validation failed due to %s" %
                list_validation[2])

        time.sleep(self.sleep_time_for_hourly_policy)

        snapshots = list_snapshots(
            self.apiclient,
            account=self.account.name,
            domainid=self.account.domainid,
            listall=True
        )

        for snapshot in self.snapshot_pool:
            self.assertTrue(snapshot.id in any(
                s.id) for s in snapshots)

        for snapshot in self.snapshot_pool:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        for snapshot in self.snapshot_pool:
            snapshot.delete(self.apiclient)

        self.snapshot_pool = []

        for rec_snap_pol in self.rec_policy_pool:
            rec_snap_pol.delete(self.apiclient)

        self.rec_policy_pool = []

        # Step 4
        thread_pool = []
        for i in range(4):
            try:
                create_snapshot_thread_1 = Thread(
                    target=CreateSnapshot,
                    args=(
                        self,
                        self.root_pool[i],
                        True))
                thread_pool.append(create_snapshot_thread_1)

            except Exception as e:
                raise Exception(
                    "Warning: Exception unable to start thread : %s" %
                    e)

        for thread in thread_pool:
            thread.start()

        for thread in thread_pool:
            thread.join()

        for rec_snap in self.rec_policy_pool:
            list_snapshots_policy_1 = list_snapshot_policy(
                self.apiclient,
                id=rec_snap.id,
            )

            list_validation_1 = validateList(list_snapshots_policy_1)

            self.assertEqual(
                list_validation[0],
                PASS,
                "snapshot list validation failed due to %s" %
                list_validation_1[2])

        time.sleep(self.sleep_time_for_hourly_policy)

        for snapshot in self.snapshot_pool:
            self.assertTrue(snapshot.id in any(
                s.id) for s in snapshots)

        for snapshot in self.snapshot_pool:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        for snapshot in self.snapshot_pool:
            snapshot.delete(self.apiclient)

        self.snapshot_pool = []

        for rec_snap_pol in self.rec_policy_pool:
            rec_snap_pol.delete(self.apiclient)

        self.rec_policy_pool = []

        # Step 5

        try:
            thread_pool = []
            for i in range(4):
                create_snapshot_thread_1 = Thread(
                    target=CreateSnapshot,
                    args=(
                        self,
                        self.root_pool[i],
                        False))
                thread_pool.append(create_snapshot_thread_1)

            destinationHost = Host.listForMigration(
                self.apiclient,
                virtualmachineid=self.vm_pool[3].id)
            migrate_volume_thread_1 = Thread(target=MigrateRootVolume,
                                             args=(self,
                                                   self.vm_pool[3],
                                                   destinationHost[0]))

            thread_pool.append(migrate_volume_thread_1)

            for thread in thread_pool:
                thread.start()

            for thread in thread_pool:
                thread.join()

        except Exception as e:
            raise Exception(
                "Warning: Exception unable to start thread : %s" %
                e)

        snapshots = list_snapshots(
            self.apiclient,
            account=self.account.name,
            domainid=self.account.domainid,
            listall=True
        )

        for snapshot in self.snapshot_pool:
            self.assertTrue(snapshot.id in any(
                s.id) for s in snapshots)

        for snapshot in self.snapshot_pool:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        for snapshot in self.snapshot_pool:
            snapshot.delete(self.apiclient)

        self.snapshot_pool = []

        # Step 6
        try:
            thread_pool = []
            for i in range(4):
                create_snapshot_thread_1 = Thread(
                    target=CreateSnapshot,
                    args=(
                        self,
                        self.root_pool[i],
                        False))
                thread_pool.append(create_snapshot_thread_1)

            stop_vm_thread_1 = Thread(target=self.StopVM,
                                      args=(self.vm_pool,
                                            ))

            thread_pool.append(stop_vm_thread_1)

            for thread in thread_pool:
                thread.start()

            for thread in thread_pool:
                thread.join()

        except Exception as e:
            raise Exception(
                "Warning: Exception unable to start thread : %s" %
                e)

        snapshots = list_snapshots(
            self.apiclient,
            account=self.account.name,
            domainid=self.account.domainid,
            listall=True
        )

        for snapshot in self.snapshot_pool:
            self.assertTrue(snapshot.id in any(
                s.id) for s in snapshots)

        for snapshot in self.snapshot_pool:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        for snapshot in self.snapshot_pool:
            snapshot.delete(self.apiclient)

        self.snapshot_pool = []

        # Step 7
        thread_pool = []
        try:
            for i in range(2):
                destinationHost = Host.listForMigration(
                    self.apiclient,
                    virtualmachineid=self.vm_pool[i].id)
                migrate_volume_thread_1 = Thread(target=MigrateRootVolume,
                                                 args=(self,
                                                       self.vm_pool[i],
                                                       destinationHost[0]))

                thread_pool.append(migrate_volume_thread_1)

            for thread in thread_pool:
                thread.start()

            for thread in thread_pool:
                thread.join()

            for vm in self.vm_pool:
                if vm.state != "Stopped":
                    vm.stop(self.apiclient)

            thread_pool = []
            for vm in self.vm_pool[:2]:
                create_snapshot_thread_1 = Thread(
                    target=CreateSnapshot,
                    args=(
                        self,
                        self.root_pool[0],
                        False))
                thread_pool.append(create_snapshot_thread_1)

            for thread in thread_pool:
                thread.start()

            for thread in thread_pool:
                thread.join()

        except:
            self.debug("Error: unable to start thread")

        snapshots = list_snapshots(
            self.apiclient,
            account=self.account.name,
            domainid=self.account.domainid,
            listall=True
        )

        for snapshot in self.snapshot_pool:
            self.assertTrue(snapshot.id in any(
                s.id) for s in snapshots)

        for snapshot in self.snapshot_pool:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        for snapshot in self.snapshot_pool:
            snapshot.delete(self.apiclient)

        self.snapshot_pool = []
    def test_03_volume_rec_snapshot(self):
        """ Test Volume (root) Snapshot
        # 1. For snapshot.delta.max > maxsnaps verify that when number of snapshot exceeds
                maxsnaps value previous snapshot should get deleted from database but remain
                on secondary storage and when the value exceeds snapshot.delta.max the
                snapshot should get deleted from secondary storage
        """

        if self.hypervisor.lower() != "xenserver":
            self.skipTest("Skip test for hypervisor other than Xenserver")

        # Step 1
        self.testdata["recurring_snapshot"]["intervaltype"] = 'HOURLY'
        self.testdata["recurring_snapshot"]["schedule"] = 1
        recurring_snapshot_root = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])

        Configurations.update(self.apiclient,
                              name="snapshot.delta.max",
                              value="3")

        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_root.id,
            volumeid=self.volume[0].id)
        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        timeout = self.testdata["timeout"]
        while True:
            snapshots = list_snapshots(
                self.apiclient,
                volumeid=self.volume[0].id,
                intervaltype=self.testdata["recurring_snapshot"]
                ["intervaltype"],
                snapshottype='RECURRING',
                listall=True)

            if isinstance(snapshots, list):
                break

            elif timeout == 0:
                raise Exception("List snapshots API call failed.")

        time.sleep(3600 * 2)

        snapshots_2 = list_snapshots(
            self.apiclient,
            volumeid=self.volume[0].id,
            intervaltype=self.testdata["recurring_snapshot"]["intervaltype"],
            snapshottype='RECURRING',
            listall=True)

        self.assertTrue(snapshots[0] not in snapshots_2)

        for snapshot in snapshots_2:
            snapshots.append(snapshot)

        time.sleep(360)
        self.assertEqual(
            self.dbclient.execute(
                "select status  from snapshots where uuid='%s'" %
                snapshots[0].id)[0][0], "Destroyed")

        self.assertTrue(
            is_snapshot_on_nfs(self.apiclient, self.dbclient, self.config,
                               self.zone.id, snapshots[0].id))

        time.sleep(3600)

        snapshots_3 = list_snapshots(
            self.apiclient,
            volumeid=self.volume[0].id,
            intervaltype=self.testdata["recurring_snapshot"]["intervaltype"],
            snapshottype='RECURRING',
            listall=True)

        self.assertTrue(snapshots[1] not in snapshots_3)
        snapshots.append(snapshots_3[1])
        time.sleep(180)

        self.assertEqual(
            self.dbclient.execute(
                "select status  from snapshots where uuid='%s'" %
                snapshots[1].id)[0][0], "Destroyed")

        for snapshot in [snapshots[0], snapshots[1]]:
            self.assertTrue(
                is_snapshot_on_nfs(self.apiclient, self.dbclient, self.config,
                                   self.zone.id, snapshot.id))

        time.sleep(3600)

        snapshots_4 = list_snapshots(
            self.apiclient,
            volumeid=self.volume[0].id,
            intervaltype=self.testdata["recurring_snapshot"]["intervaltype"],
            snapshottype='RECURRING',
            listall=True)

        self.assertTrue(snapshots[2] not in snapshots_4)

        snapshots.append(snapshots_4[1])
        time.sleep(180)

        self.assertEqual(
            self.dbclient.execute(
                "select status  from snapshots where uuid='%s'" %
                snapshots[2].id)[0][0], "Destroyed")

        for snapshot in [snapshots[0], snapshots[1], snapshots[2]]:
            self.assertFalse(
                is_snapshot_on_nfs(self.apiclient, self.dbclient, self.config,
                                   self.zone.id, snapshot.id))

        return
    def test_02_volume_max_snapshot(self):
        """ Test Volume Snapshot
        # 1. Create Hourly reccuring snapshot policy with maxsnaps=2
                verify that when 3rd snapshot is taken first snapshot gets deleted
        """

        if self.hypervisor.lower() not in ["kvm", "vmware"]:
            self.skipTest("Skip test for hypervisor other than KVM and VMWare")

        # Step 1
        self.testdata["recurring_snapshot"]["intervaltype"] = 'HOURLY'
        self.testdata["recurring_snapshot"]["schedule"] = 1
        recurring_snapshot_1 = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])
        # ListSnapshotPolicy should return newly created policy
        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_1.id,
            volumeid=self.volume[0].id)
        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        timeout = self.testdata["timeout"]
        while True:
            snapshots = list_snapshots(
                self.apiclient,
                volumeid=self.volume[0].id,
                intervaltype=self.testdata["recurring_snapshot"]
                ["intervaltype"],
                snapshottype='RECURRING',
                listall=True)

            if isinstance(snapshots, list):
                break

            elif timeout == 0:
                raise Exception("List snapshots API call failed.")

        snap_to_delete = snapshots[0]

        time.sleep((self.testdata["recurring_snapshot"]["maxsnaps"]) * 3600)

        snapshots_1 = list_snapshots(
            self.apiclient,
            volumeid=self.volume[0].id,
            intervaltype=self.testdata["recurring_snapshot"]["intervaltype"],
            snapshottype='RECURRING',
            listall=True)

        self.assertTrue(snap_to_delete not in snapshots_1)

        time.sleep(360)

        self.assertEqual(
            self.dbclient.execute(
                "select status  from snapshots where uuid='%s'" %
                snap_to_delete.id)[0][0], "Destroyed")

        self.assertFalse(
            is_snapshot_on_nfs(self.apiclient, self.dbclient, self.config,
                               self.zone.id, snap_to_delete.id))

        # DATA DISK
        recurring_snapshot_data = SnapshotPolicy.create(
            self.apiclient, self.data_volume[0].id,
            self.testdata["recurring_snapshot"])
        # ListSnapshotPolicy should return newly created policy
        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_data.id,
            volumeid=self.data_volume[0].id)

        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        timeout = self.testdata["timeout"]
        while True:
            snapshots = list_snapshots(
                self.apiclient,
                volumeid=self.volume[0].id,
                intervaltype=self.testdata["recurring_snapshot"]
                ["intervaltype"],
                snapshottype='RECURRING',
                listall=True)

            if isinstance(snapshots, list):
                break

            elif timeout == 0:
                raise Exception("List snapshots API call failed.")

        data_snap_to_delete = snapshots[0]

        time.sleep((self.testdata["recurring_snapshot"]["maxsnaps"]) * 3600)

        data_snapshots_1 = list_snapshots(
            self.apiclient,
            volumeid=self.volume[0].id,
            intervaltype=self.testdata["recurring_snapshot"]["intervaltype"],
            snapshottype='RECURRING',
            listall=True)

        self.assertTrue(data_snap_to_delete not in data_snapshots_1)

        time.sleep(360)

        self.assertEqual(
            self.dbclient.execute(
                "select status  from snapshots where uuid='%s'" %
                snap_to_delete.id)[0][0], "Destroyed")

        self.assertFalse(
            is_snapshot_on_nfs(self.apiclient, self.dbclient, self.config,
                               self.zone.id, data_snap_to_delete.id))
    def test_01_volume_snapshot(self):
        """ Test Volume (root) Snapshot
        # 1. Create Hourly, Daily,Weekly recurring snapshot policy for ROOT disk and
                    Verify the presence of the corresponding snapshots on the Secondary Storage
        # 2. Delete the snapshot policy and verify the entry as Destroyed in snapshot_schedule
        # 3. Verify that maxsnaps should not consider manual snapshots for deletion
        # 4. Snapshot policy should reflect the correct timezone
        # 5. Verify that listSnapshotPolicies() should return all snapshot policies
                that belong to the account (both manual and recurring snapshots)
        # 6. Verify that listSnapshotPolicies() should not return snapshot
                policies that have been deleted
        # 7. Verify that snapshot should not be created for VM in Destroyed state
        # 8. Verify that snapshot should get created after resuming the VM
        # 9. Verify that All the recurring policies associated with the VM should be
                deleted after VM get destroyed.
        """
        # Step 1
        self.testdata["recurring_snapshot"]["intervaltype"] = 'HOURLY'

        recurring_snapshot = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])
        # ListSnapshotPolicy should return newly created policy
        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot.id,
            volumeid=self.volume[0].id)
        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        timeout = self.testdata["timeout"]
        while True:
            snapshots = list_snapshots(
                self.apiclient,
                volumeid=self.volume[0].id,
                intervaltype=self.testdata["recurring_snapshot"]
                ["intervaltype"],
                snapshottype='RECURRING',
                listall=True)

            if isinstance(snapshots, list):
                break

            elif timeout == 0:
                raise Exception("List snapshots API call failed.")

        for snapshot in snapshots:
            self.assertEqual(
                self.dbclient.execute(
                    "select type_description from snapshots where name='%s'" %
                    snapshot.name)[0][0], "HOURLY")

        time.sleep(180)

        for snapshot in snapshots:
            self.assertTrue(
                is_snapshot_on_nfs(self.apiclient, self.dbclient, self.config,
                                   self.zone.id, snapshot.id))

        recurring_snapshot.delete(self.apiclient)

        self.assertEqual(
            self.dbclient.execute(
                "select * from snapshot_policy where uuid='%s'" %
                recurring_snapshot.id), [])

        self.testdata["recurring_snapshot"]["intervaltype"] = 'DAILY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00'
        recurring_snapshot_daily = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])

        list_snapshots_policy_daily = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_daily.id,
            volumeid=self.volume[0].id)

        list_validation = validateList(list_snapshots_policy_daily)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        snap_db_daily = self.dbclient.execute(
            "select * from snapshot_policy where uuid='%s'" %
            recurring_snapshot_daily.id)

        validation_result_1 = validateList(snap_db_daily)

        self.assertEqual(
            validation_result_1[0], PASS,
            "snapshot_policy list validation failed due to %s" %
            validation_result_1[2])

        self.assertNotEqual(len(snap_db_daily), 0, "Check DB Query result set")

        recurring_snapshot_daily.delete(self.apiclient)

        self.assertEqual(
            self.dbclient.execute(
                "select * from snapshot_policy where uuid='%s'" %
                recurring_snapshot_daily.id), [])

        self.testdata["recurring_snapshot"]["intervaltype"] = 'WEEKLY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00:1'
        recurring_snapshot_weekly = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])

        list_snapshots_policy_weekly = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_weekly.id,
            volumeid=self.volume[0].id)

        list_validation = validateList(list_snapshots_policy_weekly)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        snap_sch_2 = self.dbclient.execute(
            "select * from snapshot_policy where uuid='%s'" %
            recurring_snapshot_weekly.id)

        validation_result_2 = validateList(snap_sch_2)

        self.assertEqual(
            validation_result_2[0], PASS,
            "snapshot_policy list validation failed due to %s" %
            validation_result_2[2])

        self.assertNotEqual(len(snap_sch_2), 0, "Check DB Query result set")

        recurring_snapshot_weekly.delete(self.apiclient)

        self.assertEqual(
            self.dbclient.execute(
                "select * from snapshot_policy where uuid='%s'" %
                recurring_snapshot_weekly.id), [])

        self.testdata["recurring_snapshot"]["intervaltype"] = 'MONTHLY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00:1'
        recurring_snapshot_monthly = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])

        list_snapshots_policy_monthly = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_monthly.id,
            volumeid=self.volume[0].id)

        list_validation = validateList(list_snapshots_policy_monthly)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        snap_sch_3 = self.dbclient.execute(
            "select * from snapshot_policy where uuid='%s'" %
            recurring_snapshot_monthly.id)

        validation_result = validateList(snap_sch_3)

        self.assertEqual(
            validation_result[0], PASS,
            "snapshot_policy list validation failed due to %s" %
            validation_result[2])

        self.assertNotEqual(len(snap_sch_3), 0, "Check DB Query result set")

        recurring_snapshot_monthly.delete(self.apiclient)

        self.assertEqual(
            self.dbclient.execute(
                "select * from snapshot_policy where uuid='%s'" %
                recurring_snapshot_weekly.id), [])

        snapshots = list_snapshots(
            self.apiclient,
            volumeid=self.volume[0].id,
            intervaltype=self.testdata["recurring_snapshot"]["intervaltype"],
            snapshottype='RECURRING',
            listall=True)

        # Step 3
        self.testdata["recurring_snapshot"]["intervaltype"] = 'HOURLY'
        self.testdata["recurring_snapshot"]["schedule"] = 1
        recurring_snapshot_1 = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])
        # ListSnapshotPolicy should return newly created policy
        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_1.id,
            volumeid=self.volume[0].id)
        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        timeout = self.testdata["timeout"]
        while True:
            snapshots = list_snapshots(
                self.apiclient,
                volumeid=self.volume[0].id,
                intervaltype=self.testdata["recurring_snapshot"]
                ["intervaltype"],
                snapshottype='RECURRING',
                listall=True)

            if isinstance(snapshots, list):
                break

            elif timeout == 0:
                raise Exception("List snapshots API call failed.")

        snap_to_delete = snapshots[0]

        time.sleep((self.testdata["recurring_snapshot"]["maxsnaps"]) * 3600)

        snapshots_1 = list_snapshots(
            self.apiclient,
            volumeid=self.volume[0].id,
            intervaltype=self.testdata["recurring_snapshot"]["intervaltype"],
            snapshottype='RECURRING',
            listall=True)

        self.assertTrue(snap_to_delete not in snapshots_1)

        time.sleep(360)

        self.assertEqual(
            self.dbclient.execute(
                "select status  from snapshots where uuid='%s'" %
                snap_to_delete.id)[0][0], "Destroyed")

        self.assertFalse(
            is_snapshot_on_nfs(self.apiclient, self.dbclient, self.config,
                               self.zone.id, snap_to_delete.id))

        # Step 4
        recurring_snapshot = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])
        # ListSnapshotPolicy should return newly created policy
        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot.id,
            volumeid=self.volume[0].id)
        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        time.sleep(180)
        snap_time_hourly = self.dbclient.execute(
            "select scheduled_timestamp from \
            snapshot_schedule where uuid='%s'" % recurring_snapshot.id)

        self.debug("Timestamp for hourly snapshot %s" % snap_time_hourly)
        recurring_snapshot.delete(self.apiclient)

        self.testdata["recurring_snapshot"]["intervaltype"] = 'DAILY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00'
        recurring_snapshot_daily = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])

        list_snapshots_policy_daily = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_daily.id,
            volumeid=self.volume[0].id)

        list_validation = validateList(list_snapshots_policy_daily)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        time.sleep(180)
        snap_time_daily = self.dbclient.execute(
            "select scheduled_timestamp from \
                    snapshot_schedule where uuid='%s'" %
            recurring_snapshot_daily.id)

        self.debug("Timestamp for daily snapshot %s" % snap_time_daily)
        recurring_snapshot_daily.delete(self.apiclient)

        self.testdata["recurring_snapshot"]["intervaltype"] = 'WEEKLY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00:1'
        recurring_snapshot_weekly = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])

        list_snapshots_policy_weekly = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_weekly.id,
            volumeid=self.volume[0].id)

        list_validation = validateList(list_snapshots_policy_weekly)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        time.sleep(180)
        snap_time_weekly = self.dbclient.execute(
            "select scheduled_timestamp from \
                    snapshot_schedule where uuid='%s'" %
            recurring_snapshot_weekly.id)

        self.debug("Timestamp for monthly snapshot %s" % snap_time_weekly)
        recurring_snapshot_weekly.delete(self.apiclient)

        self.testdata["recurring_snapshot"]["intervaltype"] = 'MONTHLY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00:1'
        recurring_snapshot_monthly = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])

        list_snapshots_policy_monthly = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_monthly.id,
            volumeid=self.volume[0].id)

        list_validation = validateList(list_snapshots_policy_monthly)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        time.sleep(180)
        snap_time_monthly = self.dbclient.execute(
            "select scheduled_timestamp from \
                    snapshot_schedule where uuid='%s'" %
            recurring_snapshot_monthly.id)

        self.debug("Timestamp for monthly snapshot %s" % snap_time_monthly)

        recurring_snapshot_monthly.delete(self.apiclient)

        # Step 5
        self.testdata["recurring_snapshot"]["intervaltype"] = 'HOURLY'
        self.testdata["recurring_snapshot"]["schedule"] = 1
        recurring_snapshot_hourly = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])
        self.testdata["recurring_snapshot"]["intervaltype"] = 'MONTHLY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00:1'
        recurring_snapshot_monthly = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])

        list_snapshots_policy = list_snapshot_policy(
            self.apiclient, volumeid=self.volume[0].id)

        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        for rec in [recurring_snapshot_hourly, recurring_snapshot_monthly]:
            self.assertTrue(rec.id in any(policy['id'])
                            for policy in list_snapshots_policy)

        recurring_snapshot_hourly.delete(self.apiclient)
        recurring_snapshot_monthly.delete(self.apiclient)

        # Step 6
        self.testdata["recurring_snapshot"]["intervaltype"] = 'HOURLY'
        self.testdata["recurring_snapshot"]["schedule"] = 1
        recurring_snapshot_hourly = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])

        self.testdata["recurring_snapshot"]["intervaltype"] = 'MONTHLY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00:1'
        recurring_snapshot_monthly = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])

        recurring_snapshot_monthly.delete(self.apiclient)

        list_snapshots_policy = list_snapshot_policy(
            self.apiclient, volumeid=self.volume[0].id)

        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        self.assertTrue(recurring_snapshot_hourly.id in any(policy['id'])
                        for policy in list_snapshots_policy)

        self.assertTrue(recurring_snapshot_monthly.id not in any(policy['id'])
                        for policy in list_snapshots_policy)

        # Step 7
        self.testdata["recurring_snapshot"]["intervaltype"] = 'HOURLY'
        self.testdata["recurring_snapshot"]["schedule"] = 1
        recurring_snapshot = SnapshotPolicy.create(
            self.apiclient, self.volume[0].id,
            self.testdata["recurring_snapshot"])
        # ListSnapshotPolicy should return newly created policy
        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot.id,
            volumeid=self.volume[0].id)
        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        timeout = self.testdata["timeout"]
        while True:
            snapshots = list_snapshots(
                self.apiclient,
                volumeid=self.volume[0].id,
                intervaltype=self.testdata["recurring_snapshot"]
                ["intervaltype"],
                snapshottype='RECURRING',
                listall=True)

            if isinstance(snapshots, list):
                break

            elif timeout == 0:
                raise Exception("List snapshots API call failed.")

        self.vm_1.delete(self.apiclient, expunge=False)

        time.sleep(3600)
        snapshot_list = Snapshot.list(self.apiclient,
                                      volumeid=self.volume[0].id)

        list_validation = validateList(snapshot_list)

        self.assertEqual(
            list_validation[0], PASS,
            "snapshot list validation failed due to %s" % list_validation[2])

        self.assertEqual(
            len(snapshot_list), 1,
            "Verify that snapsot is not created after VM deletion")
        # Step 8
        self.vm_1.recover(self.apiclient)
        time.sleep(3600)

        snapshot_list = Snapshot.list(self.apiclient,
                                      volumeid=self.volume[0].id)

        self.assertEqual(
            len(snapshot_list), 2,
            "Verify that snapsot is not created after VM deletion")
        # Step 9
        self.vm_1.delete(self.apiclient)
        time.sleep(180)
        with self.assertRaises(Exception):
            list_snapshots_policy = list_snapshot_policy(
                self.apiclient, volumeid=self.volume[0].id)
    def test_01_concurrent_snapshots(self):
        """Concurrent Snapshots
            1. Create snapshot on 2 new VMs in parallel and check
                    1. all snapshot jobs are running
                    2. listSnapshots should list all the snapshots
                    3. Verify secondary_storage NFS share
                       contains the required volume under
                       /secondary/snapshots/$accountid/$volumeid/$snapshot_uuid.
                    4. Verify backup_snap_id was non null in "snapshots"table
            2. Perform step 1 for all the 4 VM's.
            3. Verify that VM gets migrated when snapshot
                is in pregress for the VM.
            4. Verify that snapshots get created when
                VM's are stoped in between snapshot creation.
            5. Perform live Migration then stop all the
                VM's after that verify that snapshot creation success .
            6. Verify success of snapshots creation in case:
                Stop the running VM while performing
                concurrent snapshot on volumes
            7. Verify success of snapshots creation in case:
                Start Migration of VM's and then Stop the running VM then
                performing concurrent snapshot on volumes
        """
        # Step 1
        try:
            create_snapshot_thread_1 = Thread(
                target=CreateSnapshot,
                args=(
                    self,
                    self.root_pool[0],
                    False))
            create_snapshot_thread_2 = Thread(
                target=CreateSnapshot,
                args=(
                    self,
                    self.root_pool[1],
                    False))
            create_snapshot_thread_1.start()
            create_snapshot_thread_2.start()
            create_snapshot_thread_1.join()
            create_snapshot_thread_2.join()

        except:
            self.debug("Error: unable to start thread")

        snapshots = list_snapshots(
            self.apiclient,
            account=self.account.name,
            domainid=self.account.domainid,
            listall=True
        )

        for snapshot in self.snapshot_pool:
            self.assertTrue(snapshot.id in any(
                s.id) for s in snapshots)

        for snapshot in self.snapshot_pool:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        for snapshot in self.snapshot_pool:
            snapshot.delete(self.apiclient)

        self.snapshot_pool = []
        # Step 2
        thread_pool = []
        for i in range(4):
            try:
                create_snapshot_thread_1 = Thread(
                    target=CreateSnapshot,
                    args=(
                        self,
                        self.root_pool[i],
                        False))
                thread_pool.append(create_snapshot_thread_1)

            except Exception as e:
                raise Exception(
                    "Warning: Exception unable to start thread : %s" %
                    e)

        for thread in thread_pool:
            thread.start()

        for thread in thread_pool:
            thread.join()

        snapshots = list_snapshots(
            self.apiclient,
            account=self.account.name,
            domainid=self.account.domainid,
            listall=True
        )

        for snapshot in self.snapshot_pool:
            self.assertTrue(snapshot.id in any(
                s.id) for s in snapshots)

        for snapshot in self.snapshot_pool:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        for snapshot in self.snapshot_pool:
            snapshot.delete(self.apiclient)

        self.snapshot_pool = []

        # Step 3
        # Recurring snapshot
        try:
            create_snapshot_thread_1 = Thread(
                target=CreateSnapshot,
                args=(
                    self,
                    self.root_pool[0],
                    True))
            create_snapshot_thread_2 = Thread(
                target=CreateSnapshot,
                args=(
                    self,
                    self.root_pool[1],
                    True))
            create_snapshot_thread_1.start()
            create_snapshot_thread_2.start()
            create_snapshot_thread_1.join()
            create_snapshot_thread_2.join()

        except:
            self.debug("Error: unable to start thread")

        for rec_snap in self.rec_policy_pool:
            list_snapshots_policy_1 = list_snapshot_policy(
                self.apiclient,
                id=rec_snap.id,
            )

            list_validation = validateList(list_snapshots_policy_1)

            self.assertEqual(
                list_validation[0],
                PASS,
                "snapshot list validation failed due to %s" %
                list_validation[2])

        time.sleep(self.sleep_time_for_hourly_policy)

        snapshots = list_snapshots(
            self.apiclient,
            account=self.account.name,
            domainid=self.account.domainid,
            listall=True
        )

        for snapshot in self.snapshot_pool:
            self.assertTrue(snapshot.id in any(
                s.id) for s in snapshots)

        for snapshot in self.snapshot_pool:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        for snapshot in self.snapshot_pool:
            snapshot.delete(self.apiclient)

        self.snapshot_pool = []

        for rec_snap_pol in self.rec_policy_pool:
            rec_snap_pol.delete(self.apiclient)

        self.rec_policy_pool = []

        # Step 4
        thread_pool = []
        for i in range(4):
            try:
                create_snapshot_thread_1 = Thread(
                    target=CreateSnapshot,
                    args=(
                        self,
                        self.root_pool[i],
                        True))
                thread_pool.append(create_snapshot_thread_1)

            except Exception as e:
                raise Exception(
                    "Warning: Exception unable to start thread : %s" %
                    e)

        for thread in thread_pool:
            thread.start()

        for thread in thread_pool:
            thread.join()

        for rec_snap in self.rec_policy_pool:
            list_snapshots_policy_1 = list_snapshot_policy(
                self.apiclient,
                id=rec_snap.id,
            )

            list_validation_1 = validateList(list_snapshots_policy_1)

            self.assertEqual(
                list_validation[0],
                PASS,
                "snapshot list validation failed due to %s" %
                list_validation_1[2])

        time.sleep(self.sleep_time_for_hourly_policy)

        for snapshot in self.snapshot_pool:
            self.assertTrue(snapshot.id in any(
                s.id) for s in snapshots)

        for snapshot in self.snapshot_pool:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        for snapshot in self.snapshot_pool:
            snapshot.delete(self.apiclient)

        self.snapshot_pool = []

        for rec_snap_pol in self.rec_policy_pool:
            rec_snap_pol.delete(self.apiclient)

        self.rec_policy_pool = []

        # Step 5

        try:
            thread_pool = []
            for i in range(4):
                create_snapshot_thread_1 = Thread(
                    target=CreateSnapshot,
                    args=(
                        self,
                        self.root_pool[i],
                        False))
                thread_pool.append(create_snapshot_thread_1)

            destinationHost = Host.listForMigration(
                self.apiclient,
                virtualmachineid=self.vm_pool[3].id)
            migrate_volume_thread_1 = Thread(target=MigrateRootVolume,
                                             args=(self,
                                                   self.vm_pool[3],
                                                   destinationHost[0]))

            thread_pool.append(migrate_volume_thread_1)

            for thread in thread_pool:
                thread.start()

            for thread in thread_pool:
                thread.join()

        except Exception as e:
            raise Exception(
                "Warning: Exception unable to start thread : %s" %
                e)

        snapshots = list_snapshots(
            self.apiclient,
            account=self.account.name,
            domainid=self.account.domainid,
            listall=True
        )

        for snapshot in self.snapshot_pool:
            self.assertTrue(snapshot.id in any(
                s.id) for s in snapshots)

        for snapshot in self.snapshot_pool:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        for snapshot in self.snapshot_pool:
            snapshot.delete(self.apiclient)

        self.snapshot_pool = []

        # Step 6
        try:
            thread_pool = []
            for i in range(4):
                create_snapshot_thread_1 = Thread(
                    target=CreateSnapshot,
                    args=(
                        self,
                        self.root_pool[i],
                        False))
                thread_pool.append(create_snapshot_thread_1)

            stop_vm_thread_1 = Thread(target=self.StopVM,
                                      args=(self.vm_pool,
                                            ))

            thread_pool.append(stop_vm_thread_1)

            for thread in thread_pool:
                thread.start()

            for thread in thread_pool:
                thread.join()

        except Exception as e:
            raise Exception(
                "Warning: Exception unable to start thread : %s" %
                e)

        snapshots = list_snapshots(
            self.apiclient,
            account=self.account.name,
            domainid=self.account.domainid,
            listall=True
        )

        for snapshot in self.snapshot_pool:
            self.assertTrue(snapshot.id in any(
                s.id) for s in snapshots)

        for snapshot in self.snapshot_pool:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        for snapshot in self.snapshot_pool:
            snapshot.delete(self.apiclient)

        self.snapshot_pool = []

        # Step 7
        thread_pool = []
        try:
            for i in range(2):
                destinationHost = Host.listForMigration(
                    self.apiclient,
                    virtualmachineid=self.vm_pool[i].id)
                migrate_volume_thread_1 = Thread(target=MigrateRootVolume,
                                                 args=(self,
                                                       self.vm_pool[i],
                                                       destinationHost[0]))

                thread_pool.append(migrate_volume_thread_1)

            for thread in thread_pool:
                thread.start()

            for thread in thread_pool:
                thread.join()

            for vm in self.vm_pool:
                if vm.state != "Stopped":
                    vm.stop(self.apiclient)

            thread_pool = []
            for vm in self.vm_pool[:2]:
                create_snapshot_thread_1 = Thread(
                    target=CreateSnapshot,
                    args=(
                        self,
                        self.root_pool[0],
                        False))
                thread_pool.append(create_snapshot_thread_1)

            for thread in thread_pool:
                thread.start()

            for thread in thread_pool:
                thread.join()

        except:
            self.debug("Error: unable to start thread")

        snapshots = list_snapshots(
            self.apiclient,
            account=self.account.name,
            domainid=self.account.domainid,
            listall=True
        )

        for snapshot in self.snapshot_pool:
            self.assertTrue(snapshot.id in any(
                s.id) for s in snapshots)

        for snapshot in self.snapshot_pool:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        for snapshot in self.snapshot_pool:
            snapshot.delete(self.apiclient)

        self.snapshot_pool = []
Пример #7
0
    def test_04_snapshot_limit(self):
        """Test snapshot limit in snapshot policies
        """
        # Validate the following
        # 1. Perform hourly recurring snapshot on the root disk of VM and keep
        #    the maxsnapshots as 1
        # 2. listSnapshots should list the snapshot that was created
        #    snapshot folder in secondary storage should contain only one
        #    snapshot image(/secondary/snapshots/$accountid/$volumeid/)

        # Get the Root disk of VM
        volumes = list_volumes(self.apiclient,
                               virtualmachineid=self.virtual_machine.id,
                               type='ROOT',
                               listall=True)
        self.assertEqual(isinstance(volumes, list), True,
                         "Check list response returns a valid list")
        volume = volumes[0]

        # Create a snapshot policy
        recurring_snapshot = SnapshotPolicy.create(
            self.apiclient, volume.id, self.services["recurring_snapshot"])
        self.cleanup.append(recurring_snapshot)

        snapshot_policy = list_snapshot_policy(self.apiclient,
                                               id=recurring_snapshot.id,
                                               volumeid=volume.id)
        self.assertEqual(isinstance(snapshot_policy, list), True,
                         "Check list response returns a valid list")

        self.assertNotEqual(snapshot_policy, None,
                            "Check if result exists in list item call")

        self.assertEqual(snapshot_policy[0].id, recurring_snapshot.id,
                         "Check recurring snapshot id in list resources call")
        self.assertEqual(snapshot_policy[0].maxsnaps,
                         self.services["recurring_snapshot"]["maxsnaps"],
                         "Check interval type in list resources call")
        # Sleep for (maxsnaps+1) hours to verify
        # only maxsnaps snapshots are retained
        time.sleep(
            (int(self.services["recurring_snapshot"]["maxsnaps"]) + 1) * 3600)

        # Verify the snapshot was created or not
        snapshots = list_snapshots(
                        self.apiclient,
                        volumeid=volume.id,
                        intervaltype=\
                        self.services["recurring_snapshot"]["intervaltype"],
                        snapshottype='RECURRING',
                        listall=True
                        )

        self.assertEqual(isinstance(snapshots, list), True,
                         "Check list response returns a valid list")
        self.assertEqual(
            len(snapshots), self.services["recurring_snapshot"]["maxsnaps"],
            "Check maximum number of recurring snapshots retained")
        snapshot = snapshots[0]
        # Sleep to ensure that snapshot is reflected in sec storage
        time.sleep(self.services["sleep"])
        self.assertTrue(
            is_snapshot_on_nfs(self.apiclient, self.dbclient, self.config,
                               self.zone.id, snapshot.id))
        return
Пример #8
0
    def test_recurring_snapshot_data_disk(self):
        """Test Recurring Snapshot data Disk
        """
        # 1. Create snapshot policy for data disk
        # 2. ListSnapshot policy should return newly created policy
        # 3. Verify only most recent number (maxsnaps) snapshots retailed

        volume = list_volumes(
            self.apiclient,
            virtualmachineid=self.virtual_machine_with_disk.id,
            type='DATADISK',
            listall=True)

        self.assertEqual(isinstance(volume, list), True,
                         "Check list response returns a valid list")

        recurring_snapshot = SnapshotPolicy.create(
            self.apiclient, volume[0].id, self.services["recurring_snapshot"])
        self.cleanup.append(recurring_snapshot)
        # ListSnapshotPolicy should return newly created policy
        list_snapshots_policy = list_snapshot_policy(self.apiclient,
                                                     id=recurring_snapshot.id,
                                                     volumeid=volume[0].id)

        self.assertEqual(isinstance(list_snapshots_policy, list), True,
                         "Check list response returns a valid list")

        self.assertNotEqual(list_snapshots_policy, None,
                            "Check if result exists in list item call")
        snapshots_policy = list_snapshots_policy[0]
        self.assertEqual(snapshots_policy.id, recurring_snapshot.id,
                         "Check recurring snapshot id in list resources call")
        self.assertEqual(snapshots_policy.maxsnaps,
                         self.services["recurring_snapshot"]["maxsnaps"],
                         "Check interval type in list resources call")

        max_snapshots = self.services["recurring_snapshot"]["maxsnaps"]
        # Sleep for (maxsnaps) hours to verify only maxsnaps snapshots are
        # retained
        time.sleep((max_snapshots * 2) * 3600)

        timeout = self.services["timeout"]
        while True:
            snapshots = list_snapshots(
                self.apiclient,
                volumeid=volume[0].id,
                intervaltype=self.services["recurring_snapshot"]
                ["intervaltype"],
                snapshottype='RECURRING',
                listall=True)

            if isinstance(snapshots, list):
                break

            elif timeout == 0:
                raise Exception("List snapshots API call failed.")

            time.sleep(1)
            timeout = timeout - 1

        self.assertEqual(isinstance(snapshots, list), True,
                         "Check list response returns a valid list")
        self.assertEqual(
            len(snapshots), max_snapshots,
            "Check maximum number of recurring snapshots retained")
        return
    def test_03_volume_rec_snapshot(self):
        """ Test Volume (root) Snapshot
        # 1. For snapshot.delta.max > maxsnaps verify that when number of snapshot exceeds 
                maxsnaps value previous snapshot should get deleted from database but remain 
                on secondary storage and when the value exceeds snapshot.delta.max the 
                snapshot should get deleted from secondary storage
        """

        if self.hypervisor.lower() != "xenserver":
            self.skipTest("Skip test for hypervisor other than Xenserver")

        # Step 1
        self.testdata["recurring_snapshot"]["intervaltype"] = 'HOURLY'
        self.testdata["recurring_snapshot"]["schedule"] = 1
        recurring_snapshot_root = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )

        Configurations.update(self.apiclient,
                              name="snapshot.delta.max",
                              value="3"
                              )

        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_root.id,
            volumeid=self.volume[0].id
        )
        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        timeout = self.testdata["timeout"]
        while True:
            snapshots = list_snapshots(
                self.apiclient,
                volumeid=self.volume[0].id,
                intervaltype=self.testdata[
                    "recurring_snapshot"]["intervaltype"],
                snapshottype='RECURRING',
                listall=True
            )

            if isinstance(snapshots, list):
                break

            elif timeout == 0:
                raise Exception("List snapshots API call failed.")

        time.sleep(3600 * 2)

        snapshots_2 = list_snapshots(
            self.apiclient,
            volumeid=self.volume[0].id,
            intervaltype=self.testdata["recurring_snapshot"]["intervaltype"],
            snapshottype='RECURRING',
            listall=True
        )

        self.assertTrue(snapshots[0] not in snapshots_2)

        for snapshot in snapshots_2:
            snapshots.append(snapshot)

        time.sleep(360)
        self.assertEqual(
            self.dbclient.execute(
                "select status  from snapshots where uuid='%s'" %
                snapshots[0].id)[0][0],
            "Destroyed"
        )

        self.assertTrue(
            is_snapshot_on_nfs(
                self.apiclient,
                self.dbclient,
                self.config,
                self.zone.id,
                snapshots[0].id))

        time.sleep(3600)

        snapshots_3 = list_snapshots(
            self.apiclient,
            volumeid=self.volume[0].id,
            intervaltype=self.testdata["recurring_snapshot"]["intervaltype"],
            snapshottype='RECURRING',
            listall=True
        )

        self.assertTrue(snapshots[1] not in snapshots_3)
        snapshots.append(snapshots_3[1])
        time.sleep(180)

        self.assertEqual(
            self.dbclient.execute(
                "select status  from snapshots where uuid='%s'" %
                snapshots[1].id)[0][0],
            "Destroyed"
        )

        for snapshot in [snapshots[0], snapshots[1]]:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        time.sleep(3600)

        snapshots_4 = list_snapshots(
            self.apiclient,
            volumeid=self.volume[0].id,
            intervaltype=self.testdata["recurring_snapshot"]["intervaltype"],
            snapshottype='RECURRING',
            listall=True
        )

        self.assertTrue(snapshots[2] not in snapshots_4)

        snapshots.append(snapshots_4[1])
        time.sleep(180)

        self.assertEqual(
            self.dbclient.execute(
                "select status  from snapshots where uuid='%s'" %
                snapshots[2].id)[0][0],
            "Destroyed"
        )

        for snapshot in [snapshots[0], snapshots[1], snapshots[2]]:
            self.assertFalse(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        return
    def test_02_volume_max_snapshot(self):
        """ Test Volume Snapshot
        # 1. Create Hourly reccuring snapshot policy with maxsnaps=2 
                verify that when 3rd snapshot is taken first snapshot gets deleted
        """

        if self.hypervisor.lower() not in ["kvm", "vmware"]:
            self.skipTest("Skip test for hypervisor other than KVM and VMWare")

        # Step 1
        self.testdata["recurring_snapshot"]["intervaltype"] = 'HOURLY'
        self.testdata["recurring_snapshot"]["schedule"] = 1
        recurring_snapshot_1 = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )
        # ListSnapshotPolicy should return newly created policy
        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_1.id,
            volumeid=self.volume[0].id
        )
        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        timeout = self.testdata["timeout"]
        while True:
            snapshots = list_snapshots(
                self.apiclient,
                volumeid=self.volume[0].id,
                intervaltype=self.testdata[
                    "recurring_snapshot"]["intervaltype"],
                snapshottype='RECURRING',
                listall=True
            )

            if isinstance(snapshots, list):
                break

            elif timeout == 0:
                raise Exception("List snapshots API call failed.")

        snap_to_delete = snapshots[0]

        time.sleep(
            (self.testdata["recurring_snapshot"]["maxsnaps"]) * 3600
        )
        
        snapshots_1 = list_snapshots(
            self.apiclient,
            volumeid=self.volume[0].id,
            intervaltype=self.testdata["recurring_snapshot"]["intervaltype"],
            snapshottype='RECURRING',
            listall=True
        )

        self.assertTrue(snap_to_delete not in snapshots_1)

        time.sleep(360)

        self.assertEqual(
            self.dbclient.execute(
                "select status  from snapshots where uuid='%s'" %
                snap_to_delete.id)[0][0],
            "Destroyed"
        )

        self.assertFalse(
            is_snapshot_on_nfs(
                self.apiclient,
                self.dbclient,
                self.config,
                self.zone.id,
                snap_to_delete.id))

        # DATA DISK
        recurring_snapshot_data = SnapshotPolicy.create(
            self.apiclient,
            self.data_volume[0].id,
            self.testdata["recurring_snapshot"]
        )
        # ListSnapshotPolicy should return newly created policy
        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_data.id,
            volumeid=self.data_volume[0].id
        )

        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        timeout = self.testdata["timeout"]
        while True:
            snapshots = list_snapshots(
                self.apiclient,
                volumeid=self.volume[0].id,
                intervaltype=self.testdata[
                    "recurring_snapshot"]["intervaltype"],
                snapshottype='RECURRING',
                listall=True
            )

            if isinstance(snapshots, list):
                break

            elif timeout == 0:
                raise Exception("List snapshots API call failed.")

        data_snap_to_delete = snapshots[0]

        time.sleep(
            (self.testdata["recurring_snapshot"]["maxsnaps"]) * 3600
        )

        data_snapshots_1 = list_snapshots(
            self.apiclient,
            volumeid=self.volume[0].id,
            intervaltype=self.testdata["recurring_snapshot"]["intervaltype"],
            snapshottype='RECURRING',
            listall=True
        )

        self.assertTrue(data_snap_to_delete not in data_snapshots_1)

        time.sleep(360)

        self.assertEqual(
            self.dbclient.execute(
                "select status  from snapshots where uuid='%s'" %
                snap_to_delete.id)[0][0],
            "Destroyed"
        )

        self.assertFalse(
            is_snapshot_on_nfs(
                self.apiclient,
                self.dbclient,
                self.config,
                self.zone.id,
                data_snap_to_delete.id))
    def test_01_volume_snapshot(self):
        """ Test Volume (root) Snapshot
        # 1. Create Hourly, Daily,Weekly recurring snapshot policy for ROOT disk and 
                    Verify the presence of the corresponding snapshots on the Secondary Storage
        # 2. Delete the snapshot policy and verify the entry as Destroyed in snapshot_schedule
        # 3. Verify that maxsnaps should not consider manual snapshots for deletion
        # 4. Snapshot policy should reflect the correct timezone
        # 5. Verify that listSnapshotPolicies() should return all snapshot policies
                that belong to the account (both manual and recurring snapshots)
        # 6. Verify that listSnapshotPolicies() should not return snapshot 
                policies that have been deleted
        # 7. Verify that snapshot should not be created for VM in Destroyed state
        # 8. Verify that snapshot should get created after resuming the VM
        # 9. Verify that All the recurring policies associated with the VM should be 
                deleted after VM get destroyed.
        """
        # Step 1
        self.testdata["recurring_snapshot"]["intervaltype"] = 'HOURLY'

        recurring_snapshot = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )
        # ListSnapshotPolicy should return newly created policy
        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot.id,
            volumeid=self.volume[0].id
        )
        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        timeout = self.testdata["timeout"]
        while True:
            snapshots = list_snapshots(
                self.apiclient,
                volumeid=self.volume[0].id,
                intervaltype=self.testdata[
                    "recurring_snapshot"]["intervaltype"],
                snapshottype='RECURRING',
                listall=True
            )

            if isinstance(snapshots, list):
                break

            elif timeout == 0:
                raise Exception("List snapshots API call failed.")

        for snapshot in snapshots:
            self.assertEqual(
                self.dbclient.execute(
                    "select type_description from snapshots where name='%s'" %
                    snapshot.name)[0][0],
                "HOURLY"
            )

        time.sleep(180)

        for snapshot in snapshots:
            self.assertTrue(
                is_snapshot_on_nfs(
                    self.apiclient,
                    self.dbclient,
                    self.config,
                    self.zone.id,
                    snapshot.id))

        recurring_snapshot.delete(self.apiclient)

        self.assertEqual(
            self.dbclient.execute(
                "select * from snapshot_policy where uuid='%s'" %
                recurring_snapshot.id),
            []
        )

        self.testdata["recurring_snapshot"]["intervaltype"] = 'DAILY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00'
        recurring_snapshot_daily = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )

        list_snapshots_policy_daily = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_daily.id,
            volumeid=self.volume[0].id
        )

        list_validation = validateList(list_snapshots_policy_daily)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        snap_db_daily = self.dbclient.execute(
            "select * from snapshot_policy where uuid='%s'" %
            recurring_snapshot_daily.id)

        validation_result_1 = validateList(snap_db_daily)

        self.assertEqual(
            validation_result_1[0],
            PASS,
            "snapshot_policy list validation failed due to %s" %
            validation_result_1[2])

        self.assertNotEqual(
            len(snap_db_daily),
            0,
            "Check DB Query result set"
        )

        recurring_snapshot_daily.delete(self.apiclient)

        self.assertEqual(
            self.dbclient.execute(
                "select * from snapshot_policy where uuid='%s'" %
                recurring_snapshot_daily.id),
            []
        )
    
        self.testdata["recurring_snapshot"]["intervaltype"] = 'WEEKLY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00:1'
        recurring_snapshot_weekly = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )

        list_snapshots_policy_weekly = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_weekly.id,
            volumeid=self.volume[0].id
        )

        list_validation = validateList(list_snapshots_policy_weekly)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        snap_sch_2 = self.dbclient.execute(
            "select * from snapshot_policy where uuid='%s'" %
            recurring_snapshot_weekly.id)

        validation_result_2 = validateList(snap_sch_2)

        self.assertEqual(
            validation_result_2[0],
            PASS,
            "snapshot_policy list validation failed due to %s" %
            validation_result_2[2])

        self.assertNotEqual(
            len(snap_sch_2),
            0,
            "Check DB Query result set"
        )

        recurring_snapshot_weekly.delete(self.apiclient)

        self.assertEqual(
            self.dbclient.execute(
                "select * from snapshot_policy where uuid='%s'" %
                recurring_snapshot_weekly.id),
            []
        )

        self.testdata["recurring_snapshot"]["intervaltype"] = 'MONTHLY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00:1'
        recurring_snapshot_monthly = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )

        list_snapshots_policy_monthly = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_monthly.id,
            volumeid=self.volume[0].id
        )

        list_validation = validateList(list_snapshots_policy_monthly)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        snap_sch_3 = self.dbclient.execute(
            "select * from snapshot_policy where uuid='%s'" %
            recurring_snapshot_monthly.id)

        validation_result = validateList(snap_sch_3)

        self.assertEqual(
            validation_result[0],
            PASS,
            "snapshot_policy list validation failed due to %s" %
            validation_result[2])

        self.assertNotEqual(
            len(snap_sch_3),
            0,
            "Check DB Query result set"
        )

        recurring_snapshot_monthly.delete(self.apiclient)

        self.assertEqual(
            self.dbclient.execute(
                "select * from snapshot_policy where uuid='%s'" %
                recurring_snapshot_weekly.id),
            []
        )

        snapshots = list_snapshots(
            self.apiclient,
            volumeid=self.volume[0].id,
            intervaltype=self.testdata["recurring_snapshot"]["intervaltype"],
            snapshottype='RECURRING',
            listall=True
        )

        # Step 3
        self.testdata["recurring_snapshot"]["intervaltype"] = 'HOURLY'
        self.testdata["recurring_snapshot"]["schedule"] = 1
        recurring_snapshot_1 = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )
        # ListSnapshotPolicy should return newly created policy
        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_1.id,
            volumeid=self.volume[0].id
        )
        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        timeout = self.testdata["timeout"]
        while True:
            snapshots = list_snapshots(
                self.apiclient,
                volumeid=self.volume[0].id,
                intervaltype=self.testdata[
                    "recurring_snapshot"]["intervaltype"],
                snapshottype='RECURRING',
                listall=True
            )

            if isinstance(snapshots, list):
                break

            elif timeout == 0:
                raise Exception("List snapshots API call failed.")

        snap_to_delete = snapshots[0]

        time.sleep(
            (self.testdata["recurring_snapshot"]["maxsnaps"]) * 3600
        )

        snapshots_1 = list_snapshots(
            self.apiclient,
            volumeid=self.volume[0].id,
            intervaltype=self.testdata["recurring_snapshot"]["intervaltype"],
            snapshottype='RECURRING',
            listall=True
        )

        self.assertTrue(snap_to_delete not in snapshots_1)

        time.sleep(360)

        self.assertEqual(
            self.dbclient.execute(
                "select status  from snapshots where uuid='%s'" %
                snap_to_delete.id)[0][0],
            "Destroyed"
        )

        self.assertFalse(
            is_snapshot_on_nfs(
                self.apiclient,
                self.dbclient,
                self.config,
                self.zone.id,
                snap_to_delete.id))

        # Step 4
        recurring_snapshot = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )
        # ListSnapshotPolicy should return newly created policy
        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot.id,
            volumeid=self.volume[0].id
        )
        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        time.sleep(180)
        snap_time_hourly = self.dbclient.execute(
            "select scheduled_timestamp from \
            snapshot_schedule where uuid='%s'" %
            recurring_snapshot.id)

        self.debug("Timestamp for hourly snapshot %s" % snap_time_hourly)
        recurring_snapshot.delete(self.apiclient)

        self.testdata["recurring_snapshot"]["intervaltype"] = 'DAILY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00'
        recurring_snapshot_daily = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )

        list_snapshots_policy_daily = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_daily.id,
            volumeid=self.volume[0].id
        )

        list_validation = validateList(list_snapshots_policy_daily)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        time.sleep(180)
        snap_time_daily = self.dbclient.execute(
            "select scheduled_timestamp from \
                    snapshot_schedule where uuid='%s'" %
            recurring_snapshot_daily.id)

        self.debug("Timestamp for daily snapshot %s" % snap_time_daily)
        recurring_snapshot_daily.delete(self.apiclient)

        self.testdata["recurring_snapshot"]["intervaltype"] = 'WEEKLY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00:1'
        recurring_snapshot_weekly = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )

        list_snapshots_policy_weekly = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_weekly.id,
            volumeid=self.volume[0].id
        )

        list_validation = validateList(list_snapshots_policy_weekly)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        time.sleep(180)
        snap_time_weekly = self.dbclient.execute(
            "select scheduled_timestamp from \
                    snapshot_schedule where uuid='%s'" %
            recurring_snapshot_weekly.id)

        self.debug("Timestamp for monthly snapshot %s" % snap_time_weekly)
        recurring_snapshot_weekly.delete(self.apiclient)

        self.testdata["recurring_snapshot"]["intervaltype"] = 'MONTHLY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00:1'
        recurring_snapshot_monthly = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )

        list_snapshots_policy_monthly = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot_monthly.id,
            volumeid=self.volume[0].id
        )

        list_validation = validateList(list_snapshots_policy_monthly)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        time.sleep(180)
        snap_time_monthly = self.dbclient.execute(
            "select scheduled_timestamp from \
                    snapshot_schedule where uuid='%s'" %
            recurring_snapshot_monthly.id)

        self.debug("Timestamp for monthly snapshot %s" % snap_time_monthly)

        recurring_snapshot_monthly.delete(self.apiclient)

        # Step 5
        self.testdata["recurring_snapshot"]["intervaltype"] = 'HOURLY'
        self.testdata["recurring_snapshot"]["schedule"] = 1
        recurring_snapshot_hourly = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )
        self.testdata["recurring_snapshot"]["intervaltype"] = 'MONTHLY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00:1'
        recurring_snapshot_monthly = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )

        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            volumeid=self.volume[0].id
        )

        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        for rec in [recurring_snapshot_hourly, recurring_snapshot_monthly]:
            self.assertTrue(
                rec.id in any(
                    policy['id']) for policy in list_snapshots_policy)

        recurring_snapshot_hourly.delete(self.apiclient)
        recurring_snapshot_monthly.delete(self.apiclient)

        # Step 6
        self.testdata["recurring_snapshot"]["intervaltype"] = 'HOURLY'
        self.testdata["recurring_snapshot"]["schedule"] = 1
        recurring_snapshot_hourly = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )

        self.testdata["recurring_snapshot"]["intervaltype"] = 'MONTHLY'
        self.testdata["recurring_snapshot"]["schedule"] = '00:00:1'
        recurring_snapshot_monthly = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )

        recurring_snapshot_monthly.delete(self.apiclient)

        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            volumeid=self.volume[0].id
        )

        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        self.assertTrue(
            recurring_snapshot_hourly.id in any(
                policy['id']) for policy in list_snapshots_policy)

        self.assertTrue(
            recurring_snapshot_monthly.id not in any(
                policy['id']) for policy in list_snapshots_policy)

        # Step 7
        self.testdata["recurring_snapshot"]["intervaltype"] = 'HOURLY'
        self.testdata["recurring_snapshot"]["schedule"] = 1
        recurring_snapshot = SnapshotPolicy.create(
            self.apiclient,
            self.volume[0].id,
            self.testdata["recurring_snapshot"]
        )
        # ListSnapshotPolicy should return newly created policy
        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot.id,
            volumeid=self.volume[0].id
        )
        list_validation = validateList(list_snapshots_policy)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        timeout = self.testdata["timeout"]
        while True:
            snapshots = list_snapshots(
                self.apiclient,
                volumeid=self.volume[0].id,
                intervaltype=self.testdata[
                    "recurring_snapshot"]["intervaltype"],
                snapshottype='RECURRING',
                listall=True
            )

            if isinstance(snapshots, list):
                break

            elif timeout == 0:
                raise Exception("List snapshots API call failed.")

        self.vm_1.delete(self.apiclient, expunge=False)

        time.sleep(3600)
        snapshot_list = Snapshot.list(
            self.apiclient,
            volumeid=self.volume[0].id
        )

        list_validation = validateList(snapshot_list)

        self.assertEqual(
            list_validation[0],
            PASS,
            "snapshot list validation failed due to %s" %
            list_validation[2])

        self.assertEqual(len(snapshot_list),
                         1,
                         "Verify that snapsot is not created after VM deletion"
                         )
        # Step 8
        self.vm_1.recover(self.apiclient)
        time.sleep(3600)

        snapshot_list = Snapshot.list(
            self.apiclient,
            volumeid=self.volume[0].id
        )

        self.assertEqual(len(snapshot_list),
                         2,
                         "Verify that snapsot is not created after VM deletion"
                         )
        # Step 9
        self.vm_1.delete(self.apiclient)
        time.sleep(180)
        with self.assertRaises(Exception):
            list_snapshots_policy = list_snapshot_policy(
                self.apiclient,
                volumeid=self.volume[0].id
            )
    def test_recurring_snapshot_data_disk(self):
        """Test Recurring Snapshot data Disk
        """
        # 1. Create snapshot policy for data disk
        # 2. ListSnapshot policy should return newly created policy
        # 3. Verify only most recent number (maxsnaps) snapshots retailed

        volume = list_volumes(
            self.apiclient,
            virtualmachineid=self.virtual_machine_with_disk.id,
            type='DATADISK',
            listall=True
        )

        self.assertEqual(
            isinstance(volume, list),
            True,
            "Check list response returns a valid list"
        )

        recurring_snapshot = SnapshotPolicy.create(
            self.apiclient,
            volume[0].id,
            self.services["recurring_snapshot"]
        )
        self.cleanup.append(recurring_snapshot)
        # ListSnapshotPolicy should return newly created policy
        list_snapshots_policy = list_snapshot_policy(
            self.apiclient,
            id=recurring_snapshot.id,
            volumeid=volume[0].id
        )

        self.assertEqual(
            isinstance(list_snapshots_policy, list),
            True,
            "Check list response returns a valid list"
        )

        self.assertNotEqual(
            list_snapshots_policy,
            None,
            "Check if result exists in list item call"
        )
        snapshots_policy = list_snapshots_policy[0]
        self.assertEqual(
            snapshots_policy.id,
            recurring_snapshot.id,
            "Check recurring snapshot id in list resources call"
        )
        self.assertEqual(
            snapshots_policy.maxsnaps,
            self.services["recurring_snapshot"]["maxsnaps"],
            "Check interval type in list resources call"
        )

        max_snapshots = self.services["recurring_snapshot"]["maxsnaps"]
        # Sleep for (maxsnaps) hours to verify only maxsnaps snapshots are
        # retained
        time.sleep(
            (max_snapshots * 2) * 3600
        )

        timeout = self.services["timeout"]
        while True:
            snapshots = list_snapshots(
                self.apiclient,
                volumeid=volume[0].id,
                intervaltype=self.services["recurring_snapshot"]
                ["intervaltype"],
                snapshottype='RECURRING',
                listall=True)

            if isinstance(snapshots, list):
                break

            elif timeout == 0:
                raise Exception("List snapshots API call failed.")

            time.sleep(1)
            timeout = timeout - 1

        self.assertEqual(
            isinstance(snapshots, list),
            True,
            "Check list response returns a valid list"
        )
        self.assertEqual(
            len(snapshots),
            max_snapshots,
            "Check maximum number of recurring snapshots retained"
        )
        return