Use create_test_server for attach volume server test

This commut uses create_test_server to clean up the
test test_create_server_attach_volume, which currently
suffers from a higher-than-usual failure rate. Example:

This commit also replaces manual volume creation with
``create_volume`` helper function defined in Tempest's
base compute class.

http://logs.openstack.org/47/496047/1/check/gate-tempest-dsvm-patrole-admin-ubuntu-xenial/2901c0f/console.html#_2017-08-22_01_40_22_359577

Change-Id: If85279ff2f4ee7fad2b3d4c665398f699afc7b38
diff --git a/patrole_tempest_plugin/tests/api/compute/test_server_rbac.py b/patrole_tempest_plugin/tests/api/compute/test_server_rbac.py
index 10ea801..4a105d7 100644
--- a/patrole_tempest_plugin/tests/api/compute/test_server_rbac.py
+++ b/patrole_tempest_plugin/tests/api/compute/test_server_rbac.py
@@ -41,69 +41,12 @@
         cls.networks_client = cls.os_primary.networks_client
         cls.ports_client = cls.os_primary.ports_client
         cls.subnets_client = cls.os_primary.subnets_client
+        cls.admin_servers_client = cls.os_admin.servers_client
 
     @classmethod
     def resource_setup(cls):
         super(ComputeServersRbacTest, cls).resource_setup()
         cls.server = cls.create_test_server(wait_until='ACTIVE')
-        # Create a volume
-        volume_name = data_utils.rand_name(cls.__name__ + '-volume')
-        name_field = 'name'
-        if not CONF.volume_feature_enabled.api_v2:
-            name_field = 'display_name'
-
-        params = {name_field: volume_name,
-                  'imageRef': CONF.compute.image_ref,
-                  'size': CONF.volume.volume_size}
-
-        volume = cls.volumes_client.create_volume(**params)['volume']
-        waiters.wait_for_volume_resource_status(cls.volumes_client,
-                                                volume['id'], 'available')
-        cls.volumes.append(volume)
-        cls.volume_id = volume['id']
-
-    def _create_network_resources(self):
-        # Create network
-        network_name = data_utils.rand_name(
-            self.__class__.__name__ + '-network')
-
-        network = self.networks_client.create_network(
-            name=network_name, port_security_enabled=True)['network']
-        self.addCleanup(self.networks_client.delete_network, network['id'])
-
-        # Create subnet for the network
-        subnet_name = data_utils.rand_name(self.__class__.__name__ + '-subnet')
-        subnet = self.subnets_client.create_subnet(
-            name=subnet_name,
-            network_id=network['id'],
-            cidr=CONF.network.project_network_cidr,
-            ip_version=4)['subnet']
-        self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
-
-        return network
-
-    def _create_test_server_with_volume(self, volume_id):
-        # Create a server with the volume created earlier
-        server_name = data_utils.rand_name(self.__class__.__name__ + "-server")
-        bd_map_v2 = [{'uuid': volume_id,
-                      'source_type': 'volume',
-                      'destination_type': 'volume',
-                      'boot_index': 0,
-                      'delete_on_termination': True}]
-        device_mapping = {'block_device_mapping_v2': bd_map_v2}
-
-        # Since the server is booted from volume, the imageRef does not need
-        # to be specified.
-        server = self.servers_client.create_server(
-            name=server_name, imageRef='',
-            flavorRef=CONF.compute.flavor_ref,
-            **device_mapping)['server']
-
-        waiters.wait_for_server_status(
-            self.os_admin.servers_client, server['id'], 'ACTIVE')
-
-        self.servers.append(server)
-        return server
 
     @rbac_rule_validation.action(
         service="nova",
@@ -139,8 +82,25 @@
         rule="os_compute_api:servers:create:attach_volume")
     @decorators.idempotent_id('eeddac5e-15aa-454f-838d-db608aae4dd8')
     def test_create_server_attach_volume(self):
+        # To create a bootable volume, the UUID of the image from which
+        # to create the volume must be included as the imageRef attribute in
+        # the request body.
+        volume_id = self.create_volume(
+            imageRef=CONF.compute.image_ref,
+            size=CONF.volume.volume_size)['id']
+
+        server_name = data_utils.rand_name(self.__class__.__name__ + "-server")
+        bd_map_v2 = [{'uuid': volume_id,
+                      'source_type': 'volume',
+                      'destination_type': 'volume',
+                      'boot_index': 0,
+                      'delete_on_termination': True}]
+        device_mapping = {'block_device_mapping_v2': bd_map_v2}
+
         self.rbac_utils.switch_role(self, toggle_rbac_role=True)
-        self._create_test_server_with_volume(self.volume_id)
+        # Use image_id='' to avoid using the default image in tempest.conf.
+        self.create_test_server(name=server_name, image_id='',
+                                **device_mapping)
 
     @test.services('network')
     @rbac_rule_validation.action(
@@ -148,12 +108,33 @@
         rule="os_compute_api:servers:create:attach_network")
     @decorators.idempotent_id('b44cd4ff-50a4-42ce-ada3-724e213cd540')
     def test_create_server_attach_network(self):
-        network = self._create_network_resources()
-        self.rbac_utils.switch_role(self, toggle_rbac_role=True)
+        def _create_network_resources():
+            # Create network
+            network_name = data_utils.rand_name(
+                self.__class__.__name__ + '-network')
+
+            network = self.networks_client.create_network(
+                name=network_name, port_security_enabled=True)['network']
+            self.addCleanup(self.networks_client.delete_network, network['id'])
+
+            # Create subnet for the network
+            subnet_name = data_utils.rand_name(
+                self.__class__.__name__ + '-subnet')
+            subnet = self.subnets_client.create_subnet(
+                name=subnet_name,
+                network_id=network['id'],
+                cidr=CONF.network.project_network_cidr,
+                ip_version=4)['subnet']
+            self.addCleanup(self.subnets_client.delete_subnet, subnet['id'])
+
+            return network
+
+        network = _create_network_resources()
         network_id = {'uuid': network['id']}
+
+        self.rbac_utils.switch_role(self, toggle_rbac_role=True)
         server = self.create_test_server(wait_until='ACTIVE',
                                          networks=[network_id])
-
         self.addCleanup(waiters.wait_for_server_termination,
                         self.servers_client, server['id'])
         self.addCleanup(self.servers_client.delete_server, server['id'])
@@ -164,10 +145,11 @@
     @decorators.idempotent_id('062e3440-e873-4b41-9317-bf6d8be50c12')
     def test_delete_server(self):
         server = self.create_test_server(wait_until='ACTIVE')
+
         self.rbac_utils.switch_role(self, toggle_rbac_role=True)
         self.servers_client.delete_server(server['id'])
         waiters.wait_for_server_termination(
-            self.os_admin.servers_client, server['id'])
+            self.admin_servers_client, server['id'])
 
     @rbac_rule_validation.action(
         service="nova",
@@ -178,7 +160,7 @@
         self.rbac_utils.switch_role(self, toggle_rbac_role=True)
         try:
             self.servers_client.update_server(self.server['id'], name=new_name)
-            waiters.wait_for_server_status(self.os_admin.servers_client,
+            waiters.wait_for_server_status(self.admin_servers_client,
                                            self.server['id'], 'ACTIVE')
         except exceptions.ServerFault as e:
             # Some other policy may have blocked it.