def delete_snapshot(snapshot_id):
     waiters.wait_for_snapshot_status(self.snapshots_client,
                                      snapshot_id,
                                      'available')
     # Delete snapshot
     self.snapshots_client.delete_snapshot(snapshot_id)
     self.snapshots_client.wait_for_resource_deletion(snapshot_id)
    def test_consistencygroup_cgsnapshot_create_delete(self):
        # Create volume type
        name = data_utils.rand_name("volume-type")
        volume_type = self.admin_volume_types_client.create_volume_type(
            name=name)['volume_type']

        # Create CG
        cg_name = data_utils.rand_name('CG')
        create_consistencygroup = (
            self.consistencygroups_adm_client.create_consistencygroup)
        cg = create_consistencygroup(volume_type['id'],
                                     name=cg_name)['consistencygroup']
        vol_name = data_utils.rand_name("volume")
        self.name_field = self.special_fields['name_field']
        params = {
            self.name_field: vol_name,
            'volume_type': volume_type['id'],
            'consistencygroup_id': cg['id'],
            'size': CONF.volume.volume_size
        }

        # Create volume
        volume = self.admin_volume_client.create_volume(**params)['volume']
        waiters.wait_for_volume_status(self.admin_volume_client, volume['id'],
                                       'available')
        self.consistencygroups_adm_client.wait_for_consistencygroup_status(
            cg['id'], 'available')
        self.assertEqual(cg_name, cg['name'])

        # Create cgsnapshot
        cgsnapshot_name = data_utils.rand_name('cgsnapshot')
        create_cgsnapshot = (
            self.consistencygroups_adm_client.create_cgsnapshot)
        cgsnapshot = create_cgsnapshot(cg['id'],
                                       name=cgsnapshot_name)['cgsnapshot']
        snapshots = self.admin_snapshots_client.list_snapshots(
            detail=True)['snapshots']
        for snap in snapshots:
            if volume['id'] == snap['volume_id']:
                waiters.wait_for_snapshot_status(self.admin_snapshots_client,
                                                 snap['id'], 'available')
        self.consistencygroups_adm_client.wait_for_cgsnapshot_status(
            cgsnapshot['id'], 'available')
        self.assertEqual(cgsnapshot_name, cgsnapshot['name'])

        # Get a given CG snapshot
        cgsnapshot = self.consistencygroups_adm_client.show_cgsnapshot(
            cgsnapshot['id'])['cgsnapshot']
        self.assertEqual(cgsnapshot_name, cgsnapshot['name'])

        # Get all CG snapshots with detail
        cgsnapshots = self.consistencygroups_adm_client.list_cgsnapshots(
            detail=True)['cgsnapshots']
        self.assertIn((cgsnapshot['name'], cgsnapshot['id']),
                      [(m['name'], m['id']) for m in cgsnapshots])

        # Clean up
        self._delete_cgsnapshot(cgsnapshot['id'], cg['id'])
        self._delete_consistencygroup(cg['id'])
        self.admin_volume_types_client.delete_volume_type(volume_type['id'])
Example #3
0
 def create_snapshot(cls, volume_id=1, **kwargs):
     """Wrapper utility that returns a test snapshot."""
     snapshot = cls.snapshots_client.create_snapshot(
         volume_id=volume_id, **kwargs)['snapshot']
     cls.snapshots.append(snapshot)
     waiters.wait_for_snapshot_status(cls.snapshots_client, snapshot['id'], 'available')
     return snapshot
Example #4
0
 def create_snapshot(cls, volume_id=1, **kwargs):
     """Wrapper utility that returns a test snapshot."""
     snapshot = cls.snapshots_client.create_snapshot(volume_id=volume_id,
                                                     **kwargs)['snapshot']
     cls.snapshots.append(snapshot)
     waiters.wait_for_snapshot_status(cls.snapshots_client, snapshot['id'],
                                      'available')
     return snapshot
Example #5
0
    def test_consistencygroup_cgsnapshot_create_delete(self):
        # Create volume type
        name = data_utils.rand_name("volume-type")
        volume_type = self.admin_volume_types_client.create_volume_type(
            name=name)['volume_type']

        # Create CG
        cg_name = data_utils.rand_name('CG')
        create_consistencygroup = (
            self.consistencygroups_adm_client.create_consistencygroup)
        cg = create_consistencygroup(volume_type['id'],
                                     name=cg_name)['consistencygroup']
        vol_name = data_utils.rand_name("volume")
        self.name_field = self.special_fields['name_field']
        params = {self.name_field: vol_name,
                  'volume_type': volume_type['id'],
                  'consistencygroup_id': cg['id'],
                  'size': CONF.volume.volume_size}

        # Create volume
        volume = self.admin_volume_client.create_volume(**params)['volume']
        waiters.wait_for_volume_status(self.admin_volume_client,
                                       volume['id'], 'available')
        self.consistencygroups_adm_client.wait_for_consistencygroup_status(
            cg['id'], 'available')
        self.assertEqual(cg_name, cg['name'])

        # Create cgsnapshot
        cgsnapshot_name = data_utils.rand_name('cgsnapshot')
        create_cgsnapshot = (
            self.consistencygroups_adm_client.create_cgsnapshot)
        cgsnapshot = create_cgsnapshot(cg['id'],
                                       name=cgsnapshot_name)['cgsnapshot']
        snapshots = self.admin_snapshots_client.list_snapshots(
            detail=True)['snapshots']
        for snap in snapshots:
            if volume['id'] == snap['volume_id']:
                waiters.wait_for_snapshot_status(self.admin_snapshots_client,
                                                 snap['id'], 'available')
        self.consistencygroups_adm_client.wait_for_cgsnapshot_status(
            cgsnapshot['id'], 'available')
        self.assertEqual(cgsnapshot_name, cgsnapshot['name'])

        # Get a given CG snapshot
        cgsnapshot = self.consistencygroups_adm_client.show_cgsnapshot(
            cgsnapshot['id'])['cgsnapshot']
        self.assertEqual(cgsnapshot_name, cgsnapshot['name'])

        # Get all CG snapshots with detail
        cgsnapshots = self.consistencygroups_adm_client.list_cgsnapshots(
            detail=True)['cgsnapshots']
        self.assertIn((cgsnapshot['name'], cgsnapshot['id']),
                      [(m['name'], m['id']) for m in cgsnapshots])

        # Clean up
        self._delete_cgsnapshot(cgsnapshot['id'], cg['id'])
        self._delete_consistencygroup(cg['id'])
        self.admin_volume_types_client.delete_volume_type(volume_type['id'])
 def test_create_delete_snapshot(self):
     for volume_id in self.local_volid_list:
         LOG.info("@@@create snapshot vol_id:%s" % volume_id)
         snap = self.client.create_snapshot(volume_id)['snapshot']
         waiters.wait_for_snapshot_status(self.client,
                                          snap['id'],
                                          'available')
         LOG.info("@@@delete snapshot id:%s" % (snap['id']))
         self.cleanup_snapshot(snap)
Example #7
0
    def create_snapshot(cls, volume_id=1, **kwargs):
        """Wrapper utility that returns a test snapshot."""
        name_field = cls.special_fields['name_field']
        if name_field not in kwargs:
            name = data_utils.rand_name(cls.__name__ + '-Snapshot')
            kwargs[name_field] = name

        snapshot = cls.snapshots_client.create_snapshot(
            volume_id=volume_id, **kwargs)['snapshot']
        cls.snapshots.append(snapshot)
        waiters.wait_for_snapshot_status(cls.snapshots_client,
                                         snapshot['id'], 'available')
        return snapshot
Example #8
0
    def create_snapshot(cls, volume_id=1, **kwargs):
        """Wrapper utility that returns a test snapshot."""
        name_field = cls.special_fields['name_field']
        if name_field not in kwargs:
            name = data_utils.rand_name(cls.__name__ + '-Snapshot')
            kwargs[name_field] = name

        snapshot = cls.snapshots_client.create_snapshot(volume_id=volume_id,
                                                        **kwargs)['snapshot']
        cls.snapshots.append(snapshot)
        waiters.wait_for_snapshot_status(cls.snapshots_client, snapshot['id'],
                                         'available')
        return snapshot
    def _create_snapshot_from_volume(self, vol_id):
        snap_name = data_utils.rand_name(self.__class__.__name__ + "-snapshot")
        snap = self.snapshots_client.create_snapshot(volume_id=vol_id, force=True, display_name=snap_name)["snapshot"]
        self.addCleanup(self.snapshots_client.wait_for_resource_deletion, snap["id"])
        self.addCleanup(self.snapshots_client.delete_snapshot, snap["id"])
        waiters.wait_for_snapshot_status(self.snapshots_client, snap["id"], "available")

        # NOTE(e0ne): Cinder API v2 uses name instead of display_name
        if "display_name" in snap:
            self.assertEqual(snap_name, snap["display_name"])
        else:
            self.assertEqual(snap_name, snap["name"])

        return snap
 def _create_volume_snapshot(self, volume):
     snapshot_name = data_utils.rand_name('scenario-snapshot')
     snapshot = self.snapshots_client.create_snapshot(
         volume_id=volume['id'], display_name=snapshot_name)['snapshot']
     self.addCleanup(self.snapshots_client.wait_for_resource_deletion,
                     snapshot['id'])
     self.addCleanup(self.snapshots_client.delete_snapshot, snapshot['id'])
     waiters.wait_for_volume_status(self.volumes_client, volume['id'],
                                    'available')
     waiters.wait_for_snapshot_status(self.snapshots_client, snapshot['id'],
                                      'available')
     if 'display_name' in snapshot:
         self.assertEqual(snapshot_name, snapshot['display_name'])
     else:
         self.assertEqual(snapshot_name, snapshot['name'])
     return snapshot
    def _create_snapshot_from_volume(self, vol_id):
        snap_name = data_utils.rand_name(self.__class__.__name__ + '-snapshot')
        snap = self.snapshots_client.create_snapshot(
            volume_id=vol_id, force=True, display_name=snap_name)['snapshot']
        self.addCleanup(self.snapshots_client.wait_for_resource_deletion,
                        snap['id'])
        self.addCleanup(self.snapshots_client.delete_snapshot, snap['id'])
        waiters.wait_for_snapshot_status(self.snapshots_client, snap['id'],
                                         'available')

        # NOTE(e0ne): Cinder API v2 uses name instead of display_name
        if 'display_name' in snap:
            self.assertEqual(snap_name, snap['display_name'])
        else:
            self.assertEqual(snap_name, snap['name'])

        return snap
Example #12
0
    def resource_setup(cls):
        super(SnapshotsActionsV2Test, cls).resource_setup()

        # Create a test shared volume for tests
        vol_name = data_utils.rand_name(cls.__name__ + '-Volume')
        cls.name_field = cls.special_fields['name_field']
        params = {cls.name_field: vol_name}
        cls.volume = cls.volumes_client.create_volume(**params)['volume']
        waiters.wait_for_volume_status(cls.volumes_client, cls.volume['id'],
                                       'available')

        # Create a test shared snapshot for tests
        snap_name = data_utils.rand_name(cls.__name__ + '-Snapshot')
        params = {cls.name_field: snap_name}
        cls.snapshot = cls.client.create_snapshot(volume_id=cls.volume['id'],
                                                  **params)['snapshot']
        waiters.wait_for_snapshot_status(cls.client, cls.snapshot['id'],
                                         'available')
    def resource_setup(cls):
        super(SnapshotsActionsV2Test, cls).resource_setup()

        # Create a test shared volume for tests
        vol_name = data_utils.rand_name(cls.__name__ + '-Volume')
        cls.name_field = cls.special_fields['name_field']
        params = {cls.name_field: vol_name}
        cls.volume = cls.volumes_client.create_volume(**params)['volume']
        waiters.wait_for_volume_status(cls.volumes_client,
                                       cls.volume['id'], 'available')

        # Create a test shared snapshot for tests
        snap_name = data_utils.rand_name(cls.__name__ + '-Snapshot')
        params = {cls.name_field: snap_name}
        cls.snapshot = cls.client.create_snapshot(
            volume_id=cls.volume['id'], **params)['snapshot']
        waiters.wait_for_snapshot_status(cls.client,
                                         cls.snapshot['id'], 'available')
    def _create_snapshot_from_volume(self, vol_id):
        snap_name = data_utils.rand_name('snapshot')
        snap = self.snapshots_client.create_snapshot(
            volume_id=vol_id,
            force=True,
            display_name=snap_name)['snapshot']
        self.addCleanup(
            self.snapshots_client.wait_for_resource_deletion, snap['id'])
        self.addCleanup(self.snapshots_client.delete_snapshot, snap['id'])
        waiters.wait_for_snapshot_status(self.snapshots_client,
                                         snap['id'], 'available')

        # NOTE(e0ne): Cinder API v2 uses name instead of display_name
        if 'display_name' in snap:
            self.assertEqual(snap_name, snap['display_name'])
        else:
            self.assertEqual(snap_name, snap['name'])

        return snap
    def _create_volume_snapshot(self, volume):
        snapshot_name = data_utils.rand_name('scenario-snapshot')
        snapshot = self.snapshots_client.create_snapshot(
            volume_id=volume['id'], display_name=snapshot_name)['snapshot']

        def cleaner():
            self.snapshots_client.delete_snapshot(snapshot['id'])
            try:
                while self.snapshots_client.show_snapshot(
                        snapshot['id'])['snapshot']:
                    time.sleep(1)
            except lib_exc.NotFound:
                pass
        self.addCleanup(cleaner)
        waiters.wait_for_volume_status(self.volumes_client,
                                       volume['id'], 'available')
        waiters.wait_for_snapshot_status(self.snapshots_client,
                                         snapshot['id'], 'available')
        self.assertEqual(snapshot_name, snapshot['display_name'])
        return snapshot
    def _create_volume_snapshot(self, volume):
        snapshot_name = data_utils.rand_name('scenario-snapshot')
        snapshot = self.snapshots_client.create_snapshot(
            volume_id=volume['id'], display_name=snapshot_name)['snapshot']

        def cleaner():
            self.snapshots_client.delete_snapshot(snapshot['id'])
            try:
                while self.snapshots_client.show_snapshot(
                        snapshot['id'])['snapshot']:
                    time.sleep(1)
            except lib_exc.NotFound:
                pass
        self.addCleanup(cleaner)
        waiters.wait_for_volume_status(self.volumes_client,
                                       volume['id'], 'available')
        waiters.wait_for_snapshot_status(self.snapshots_client,
                                         snapshot['id'], 'available')
        self.assertEqual(snapshot_name, snapshot['display_name'])
        return snapshot
Example #17
0
    def test_unmanage_manage_snapshot(self):
        # Create a volume
        volume = self.create_volume()

        # Create a snapshot
        snapshot = self.create_snapshot(volume_id=volume['id'])

        # Unmanage the snapshot
        # Unmanage snapshot function works almost the same as delete snapshot,
        # but it does not delete the snapshot data
        self.admin_snapshots_client.unmanage_snapshot(snapshot['id'])
        self.admin_snapshots_client.wait_for_resource_deletion(snapshot['id'])

        # Fetch snapshot ids
        snapshot_list = [
            snap['id']
            for snap in self.snapshots_client.list_snapshots()['snapshots']
        ]

        # Verify snapshot does not exist in snapshot list
        self.assertNotIn(snapshot['id'], snapshot_list)

        # Manage the snapshot
        snapshot_ref = '_snapshot-%s' % snapshot['id']
        new_snapshot = self.admin_snapshot_manage_client.manage_snapshot(
            volume_id=volume['id'], ref={'source-name':
                                         snapshot_ref})['snapshot']
        self.addCleanup(self.delete_snapshot, self.admin_snapshots_client,
                        new_snapshot['id'])

        # Wait for the snapshot to be available after manage operation
        waiters.wait_for_snapshot_status(self.admin_snapshots_client,
                                         new_snapshot['id'], 'available')

        # Verify the managed snapshot has the expected parent volume
        self.assertEqual(new_snapshot['volume_id'], volume['id'])
Example #18
0
def cleanup():
    admin_manager = credentials.AdminManager()

    body = admin_manager.servers_client.list_servers(all_tenants=True)
    LOG.info("Cleanup::remove %s servers" % len(body['servers']))
    for s in body['servers']:
        try:
            admin_manager.servers_client.delete_server(s['id'])
        except Exception:
            pass

    for s in body['servers']:
        try:
            waiters.wait_for_server_termination(admin_manager.servers_client,
                                                s['id'])
        except Exception:
            pass

    keypairs = admin_manager.keypairs_client.list_keypairs()['keypairs']
    LOG.info("Cleanup::remove %s keypairs" % len(keypairs))
    for k in keypairs:
        try:
            admin_manager.keypairs_client.delete_keypair(k['name'])
        except Exception:
            pass

    secgrp_client = admin_manager.compute_security_groups_client
    secgrp = (secgrp_client.list_security_groups(all_tenants=True)
              ['security_groups'])
    secgrp_del = [grp for grp in secgrp if grp['name'] != 'default']
    LOG.info("Cleanup::remove %s Security Group" % len(secgrp_del))
    for g in secgrp_del:
        try:
            secgrp_client.delete_security_group(g['id'])
        except Exception:
            pass

    admin_floating_ips_client = admin_manager.compute_floating_ips_client
    floating_ips = (admin_floating_ips_client.list_floating_ips()
                    ['floating_ips'])
    LOG.info("Cleanup::remove %s floating ips" % len(floating_ips))
    for f in floating_ips:
        try:
            admin_floating_ips_client.delete_floating_ip(f['id'])
        except Exception:
            pass

    users = admin_manager.users_client.list_users()['users']
    LOG.info("Cleanup::remove %s users" % len(users))
    for user in users:
        if user['name'].startswith("stress_user"):
            admin_manager.users_client.delete_user(user['id'])
    tenants = admin_manager.tenants_client.list_tenants()['tenants']
    LOG.info("Cleanup::remove %s tenants" % len(tenants))
    for tenant in tenants:
        if tenant['name'].startswith("stress_tenant"):
            admin_manager.tenants_client.delete_tenant(tenant['id'])

    # We have to delete snapshots first or
    # volume deletion may block

    _, snaps = admin_manager.snapshots_client.list_snapshots(
        all_tenants=True)['snapshots']
    LOG.info("Cleanup::remove %s snapshots" % len(snaps))
    for v in snaps:
        try:
            waiters.wait_for_snapshot_status(
                admin_manager.snapshots_client, v['id'], 'available')
            admin_manager.snapshots_client.delete_snapshot(v['id'])
        except Exception:
            pass

    for v in snaps:
        try:
            admin_manager.snapshots_client.wait_for_resource_deletion(v['id'])
        except Exception:
            pass

    vols = admin_manager.volumes_client.list_volumes(
        params={"all_tenants": True})
    LOG.info("Cleanup::remove %s volumes" % len(vols))
    for v in vols:
        try:
            waiters.wait_for_volume_status(
                admin_manager.volumes_client, v['id'], 'available')
            admin_manager.volumes_client.delete_volume(v['id'])
        except Exception:
            pass

    for v in vols:
        try:
            admin_manager.volumes_client.wait_for_resource_deletion(v['id'])
        except Exception:
            pass
    def resource_setup(cls):
        super(UserIsolationSetup, cls).resource_setup()

        # Prepare an array to store information
        fileinfo = {}
        if os.path.exists(file_path):
            LOG.info("/!\\ deleting previous file /!\\")
            os.remove(file_path)

        # Create a server
        LOG.info("setting up server...")
        name = data_utils.rand_name("VM_Setup")
        server = cls.create_test_server(name=name, wait_until="ACTIVE")
        cls.server = cls.client.show_server(server["id"])["server"]
        fileinfo["server"] = cls.server
        LOG.info("VM_Setup created and active")

        # Create an image
        name = data_utils.rand_name("image")
        body = cls.compute_images_client.create_image(cls.server["id"], name=name)
        image_id = data_utils.parse_image_id(body.response["location"])
        waiters.wait_for_image_status(cls.compute_images_client, image_id, "ACTIVE")
        cls.image = cls.compute_images_client.show_image(image_id)["image"]
        fileinfo["image"] = cls.image
        LOG.info("image created and active")

        # Create a keypair
        cls.keypairname = data_utils.rand_name("keypair")
        cls.keypairs_client.create_keypair(name=cls.keypairname)
        fileinfo["keypairname"] = cls.keypairname
        LOG.info("keypair created")

        # Create a security group
        name = data_utils.rand_name("security")
        description = data_utils.rand_name("description")
        cls.security_group = cls.security_client.create_security_group(name=name, description=description)[
            "security_group"
        ]
        fileinfo["security_group"] = cls.security_group
        LOG.info("security group created")

        # Create a security group rule
        cls.rule = cls.rule_client.create_security_group_rule(
            parent_group_id=cls.security_group["id"], ip_protocol="tcp", from_port=22, to_port=22
        )["security_group_rule"]
        fileinfo["rule"] = cls.rule
        LOG.info("security rule created")

        # Create two volumes
        name = data_utils.rand_name("volume1")
        cls.metadata = {"vol_metadata": data_utils.rand_name("vol_metadata")}
        cls.volume1 = cls.volumes_client.create_volume(size=1, display_name=name, metadata=cls.metadata)["volume"]
        name = data_utils.rand_name("volume2")
        cls.volume2 = cls.volumes_client.create_volume(size=1, display_name=name)["volume"]
        waiters.wait_for_volume_status(cls.volumes_client, cls.volume1["id"], "available")
        waiters.wait_for_volume_status(cls.volumes_client, cls.volume2["id"], "available")
        fileinfo["volume1"] = cls.volume1
        fileinfo["metadata"] = cls.metadata
        fileinfo["volume2"] = cls.volume2
        LOG.info("volumes created")

        # Create a snapshot from volume1
        name = data_utils.rand_name("snapshot")
        cls.snapshot = cls.snapshots_client.create_snapshot(volume_id=cls.volume1["id"], display_name=name)["snapshot"]
        waiters.wait_for_snapshot_status(cls.snapshots_client, cls.snapshot["id"], "available")
        fileinfo["snapshot"] = cls.snapshot
        LOG.info("snapshot created")

        # Attach volume2 to the server
        cls.attachment = cls.servers_client.attach_volume(cls.server["id"], volumeId=cls.volume2["id"])[
            "volumeAttachment"
        ]
        waiters.wait_for_volume_status(cls.volumes_client, cls.volume2["id"], "in-use")
        fileinfo["attachment"] = cls.attachment
        LOG.info("volume attached to server")

        # Save array information to file
        f = open(file_path, "w")
        json.dump(fileinfo, f)
        f.close()
        LOG.info("file created with ids, waiting...")
Example #20
0
 def delete_snapshot(snapshot_id):
     waiters.wait_for_snapshot_status(self.snapshots_client,
                                      snapshot_id, 'available')
     # Delete snapshot
     self.snapshots_client.delete_snapshot(snapshot_id)
     self.snapshots_client.wait_for_resource_deletion(snapshot_id)
Example #21
0
 def create_snapshot(self, volume_id):
     snapshot = self.snapshots_client.\
         create_snapshot(volume_id=volume_id)['snapshot']
     waiters.wait_for_snapshot_status(self.snapshots_client, snapshot['id'],
                                      'available')
     return snapshot['id']
Example #22
0
def cleanup():
    admin_manager = credentials.AdminManager()

    body = admin_manager.servers_client.list_servers(all_tenants=True)
    LOG.info("Cleanup::remove %s servers" % len(body['servers']))
    for s in body['servers']:
        try:
            admin_manager.servers_client.delete_server(s['id'])
        except Exception:
            pass

    for s in body['servers']:
        try:
            waiters.wait_for_server_termination(admin_manager.servers_client,
                                                s['id'])
        except Exception:
            pass

    keypairs = admin_manager.keypairs_client.list_keypairs()['keypairs']
    LOG.info("Cleanup::remove %s keypairs" % len(keypairs))
    for k in keypairs:
        try:
            admin_manager.keypairs_client.delete_keypair(k['name'])
        except Exception:
            pass

    secgrp_client = admin_manager.compute_security_groups_client
    secgrp = (secgrp_client.list_security_groups(
        all_tenants=True)['security_groups'])
    secgrp_del = [grp for grp in secgrp if grp['name'] != 'default']
    LOG.info("Cleanup::remove %s Security Group" % len(secgrp_del))
    for g in secgrp_del:
        try:
            secgrp_client.delete_security_group(g['id'])
        except Exception:
            pass

    admin_floating_ips_client = admin_manager.compute_floating_ips_client
    floating_ips = (
        admin_floating_ips_client.list_floating_ips()['floating_ips'])
    LOG.info("Cleanup::remove %s floating ips" % len(floating_ips))
    for f in floating_ips:
        try:
            admin_floating_ips_client.delete_floating_ip(f['id'])
        except Exception:
            pass

    users = admin_manager.users_client.list_users()['users']
    LOG.info("Cleanup::remove %s users" % len(users))
    for user in users:
        if user['name'].startswith("stress_user"):
            admin_manager.users_client.delete_user(user['id'])
    tenants = admin_manager.tenants_client.list_tenants()['tenants']
    LOG.info("Cleanup::remove %s tenants" % len(tenants))
    for tenant in tenants:
        if tenant['name'].startswith("stress_tenant"):
            admin_manager.tenants_client.delete_tenant(tenant['id'])

    # We have to delete snapshots first or
    # volume deletion may block

    _, snaps = admin_manager.snapshots_client.list_snapshots(
        all_tenants=True)['snapshots']
    LOG.info("Cleanup::remove %s snapshots" % len(snaps))
    for v in snaps:
        try:
            waiters.wait_for_snapshot_status(admin_manager.snapshots_client,
                                             v['id'], 'available')
            admin_manager.snapshots_client.delete_snapshot(v['id'])
        except Exception:
            pass

    for v in snaps:
        try:
            admin_manager.snapshots_client.wait_for_resource_deletion(v['id'])
        except Exception:
            pass

    vols = admin_manager.volumes_client.list_volumes(
        params={"all_tenants": True})
    LOG.info("Cleanup::remove %s volumes" % len(vols))
    for v in vols:
        try:
            waiters.wait_for_volume_status(admin_manager.volumes_client,
                                           v['id'], 'available')
            admin_manager.volumes_client.delete_volume(v['id'])
        except Exception:
            pass

    for v in vols:
        try:
            admin_manager.volumes_client.wait_for_resource_deletion(v['id'])
        except Exception:
            pass
    def test_create_consistencygroup_from_cgsnapshot(self):
        # Create volume type
        name = data_utils.rand_name("volume-type")
        volume_type = self.admin_volume_types_client.create_volume_type(
            name=name)['volume_type']

        # Create CG
        cg_name = data_utils.rand_name('CG')
        create_consistencygroup = (
            self.consistencygroups_adm_client.create_consistencygroup)
        cg = create_consistencygroup(volume_type['id'],
                                     name=cg_name)['consistencygroup']
        vol_name = data_utils.rand_name("volume")
        self.name_field = self.special_fields['name_field']
        params = {self.name_field: vol_name,
                  'volume_type': volume_type['id'],
                  'consistencygroup_id': cg['id'],
                  'size': CONF.volume.volume_size}

        # Create volume
        volume = self.admin_volume_client.create_volume(**params)['volume']
        waiters.wait_for_volume_status(self.admin_volume_client,
                                       volume['id'], 'available')
        self.consistencygroups_adm_client.wait_for_consistencygroup_status(
            cg['id'], 'available')
        self.assertEqual(cg_name, cg['name'])

        # Create cgsnapshot
        cgsnapshot_name = data_utils.rand_name('cgsnapshot')
        create_cgsnapshot = (
            self.consistencygroups_adm_client.create_cgsnapshot)
        cgsnapshot = create_cgsnapshot(cg['id'],
                                       name=cgsnapshot_name)['cgsnapshot']
        snapshots = self.snapshots_client.list_snapshots(
            detail=True)['snapshots']
        for snap in snapshots:
            if volume['id'] == snap['volume_id']:
                waiters.wait_for_snapshot_status(self.admin_snapshots_client,
                                                 snap['id'], 'available')
        self.consistencygroups_adm_client.wait_for_cgsnapshot_status(
            cgsnapshot['id'], 'available')
        self.assertEqual(cgsnapshot_name, cgsnapshot['name'])

        # Create CG from CG snapshot
        cg_name2 = data_utils.rand_name('CG_from_snap')
        create_consistencygroup2 = (
            self.consistencygroups_adm_client.create_consistencygroup_from_src)
        cg2 = create_consistencygroup2(cgsnapshot_id=cgsnapshot['id'],
                                       name=cg_name2)['consistencygroup']
        vols = self.admin_volume_client.list_volumes(
            detail=True)['volumes']
        for vol in vols:
            if vol['consistencygroup_id'] == cg2['id']:
                waiters.wait_for_volume_status(self.admin_volume_client,
                                               vol['id'], 'available')
        self.consistencygroups_adm_client.wait_for_consistencygroup_status(
            cg2['id'], 'available')
        self.assertEqual(cg_name2, cg2['name'])

        # Clean up
        self._delete_consistencygroup(cg2['id'])
        self._delete_cgsnapshot(cgsnapshot['id'], cg['id'])
        self._delete_consistencygroup(cg['id'])
        self.admin_volume_types_client.delete_volume_type(volume_type['id'])