Merge "refactor: Add setup_test_group to identity admin base test class"
diff --git a/tempest/api/identity/admin/v3/test_domains.py b/tempest/api/identity/admin/v3/test_domains.py
index 97a1f36..72b6be4 100644
--- a/tempest/api/identity/admin/v3/test_domains.py
+++ b/tempest/api/identity/admin/v3/test_domains.py
@@ -121,11 +121,7 @@
         # Create a domain with a user and a group in it
         domain = self.setup_test_domain()
         user = self.create_test_user(domain_id=domain['id'])
-        group = self.groups_client.create_group(
-            name=data_utils.rand_name('group'),
-            domain_id=domain['id'])['group']
-        self.addCleanup(test_utils.call_and_ignore_notfound_exc,
-                        self.groups_client.delete_group, group['id'])
+        group = self.setup_test_group(domain_id=domain['id'])
         # Delete the domain
         self.delete_domain(domain['id'])
         # Check the domain, its users and groups are gone
diff --git a/tempest/api/identity/admin/v3/test_groups.py b/tempest/api/identity/admin/v3/test_groups.py
index 507810b..752bc7c 100644
--- a/tempest/api/identity/admin/v3/test_groups.py
+++ b/tempest/api/identity/admin/v3/test_groups.py
@@ -32,10 +32,8 @@
     def test_group_create_update_get(self):
         name = data_utils.rand_name('Group')
         description = data_utils.rand_name('Description')
-        group = self.groups_client.create_group(
-            name=name, domain_id=self.domain['id'],
-            description=description)['group']
-        self.addCleanup(self.groups_client.delete_group, group['id'])
+        group = self.setup_test_group(name=name, domain_id=self.domain['id'],
+                                      description=description)
         self.assertEqual(group['name'], name)
         self.assertEqual(group['description'], description)
 
@@ -55,10 +53,8 @@
     def test_group_update_with_few_fields(self):
         name = data_utils.rand_name('Group')
         old_description = data_utils.rand_name('Description')
-        group = self.groups_client.create_group(
-            name=name, domain_id=self.domain['id'],
-            description=old_description)['group']
-        self.addCleanup(self.groups_client.delete_group, group['id'])
+        group = self.setup_test_group(name=name, domain_id=self.domain['id'],
+                                      description=old_description)
 
         new_name = data_utils.rand_name('UpdateGroup')
         updated_group = self.groups_client.update_group(
@@ -70,10 +66,7 @@
     @decorators.attr(type='smoke')
     @decorators.idempotent_id('1598521a-2f36-4606-8df9-30772bd51339')
     def test_group_users_add_list_delete(self):
-        name = data_utils.rand_name('Group')
-        group = self.groups_client.create_group(
-            name=name, domain_id=self.domain['id'])['group']
-        self.addCleanup(self.groups_client.delete_group, group['id'])
+        group = self.setup_test_group(domain_id=self.domain['id'])
         # add user into group
         users = []
         for _ in range(3):
@@ -100,11 +93,8 @@
         # create two groups, and add user into them
         groups = []
         for _ in range(2):
-            name = data_utils.rand_name('Group')
-            group = self.groups_client.create_group(
-                name=name, domain_id=self.domain['id'])['group']
+            group = self.setup_test_group(domain_id=self.domain['id'])
             groups.append(group)
-            self.addCleanup(self.groups_client.delete_group, group['id'])
             self.groups_client.add_group_user(group['id'], user['id'])
         # list groups which user belongs to
         user_groups = self.users_client.list_user_groups(user['id'])['groups']
@@ -118,12 +108,7 @@
         group_ids = list()
         fetched_ids = list()
         for _ in range(3):
-            name = data_utils.rand_name('Group')
-            description = data_utils.rand_name('Description')
-            group = self.groups_client.create_group(
-                name=name, domain_id=self.domain['id'],
-                description=description)['group']
-            self.addCleanup(self.groups_client.delete_group, group['id'])
+            group = self.setup_test_group(domain_id=self.domain['id'])
             group_ids.append(group['id'])
         # List and Verify Groups
         # When domain specific drivers are enabled the operations
diff --git a/tempest/api/identity/admin/v3/test_tokens.py b/tempest/api/identity/admin/v3/test_tokens.py
index 0845407..532f0d7 100644
--- a/tempest/api/identity/admin/v3/test_tokens.py
+++ b/tempest/api/identity/admin/v3/test_tokens.py
@@ -201,10 +201,7 @@
             role_id = self.setup_test_role()['id']
 
             # Create a group.
-            group_name = data_utils.rand_name('Group')
-            group_id = self.groups_client.create_group(
-                name=group_name, domain_id=domain_id)['group']['id']
-            self.addCleanup(self.groups_client.delete_group, group_id)
+            group_id = self.setup_test_group(domain_id=domain_id)['id']
 
             # Add the alt user to the group.
             self.groups_client.add_group_user(group_id, alt_user_id)
diff --git a/tempest/api/identity/base.py b/tempest/api/identity/base.py
index 68f2c07..282343c 100644
--- a/tempest/api/identity/base.py
+++ b/tempest/api/identity/base.py
@@ -292,6 +292,20 @@
             self.delete_domain, domain['id'])
         return domain
 
+    def setup_test_group(self, **kwargs):
+        """Set up a test group."""
+        if 'name' not in kwargs:
+            kwargs['name'] = data_utils.rand_name(
+                self.__class__.__name__ + '_test_project')
+        if 'description' not in kwargs:
+            kwargs['description'] = data_utils.rand_name(
+                self.__class__.__name__ + '_test_description')
+        group = self.groups_client.create_group(**kwargs)['group']
+        self.addCleanup(
+            test_utils.call_and_ignore_notfound_exc,
+            self.groups_client.delete_group, group['id'])
+        return group
+
 
 class BaseApplicationCredentialsV3Test(BaseIdentityV3Test):