Merge "Cleanup: Add common "create_server" method"
diff --git a/tempest/scenario/manager.py b/tempest/scenario/manager.py
index d0f0127..d512ae8 100644
--- a/tempest/scenario/manager.py
+++ b/tempest/scenario/manager.py
@@ -258,6 +258,31 @@
             self.fail("Timed out waiting for thing %s to become %s"
                       % (thing_id, expected_status))
 
+    def create_server(self, client, name=None, image=None, flavor=None,
+                      create_kwargs={}):
+        if name is None:
+            name = rand_name('scenario-server-')
+        if image is None:
+            image = self.config.compute.image_ref
+        if flavor is None:
+            flavor = self.config.compute.flavor_ref
+        LOG.debug("Creating a server (name: %s, image: %s, flavor: %s)",
+                  name, image, flavor)
+        server = client.servers.create(name, image, flavor, **create_kwargs)
+        try:
+            self.assertEqual(server.name, name)
+            self.set_resource(name, server)
+        except AttributeError:
+            self.fail("Server not successfully created.")
+        self.status_timeout(client.servers, server.id, 'ACTIVE')
+        # The instance retrieved on creation is missing network
+        # details, necessitating retrieval after it becomes active to
+        # ensure correct details.
+        server = client.servers.get(server.id)
+        self.set_resource(name, server)
+        LOG.debug("Created server: %s", server)
+        return server
+
 
 class NetworkScenarioTest(OfficialClientTest):
     """
@@ -412,31 +437,6 @@
         self.set_resource(name, port)
         return port
 
-    def _create_server(self, client, network, name, key_name, security_groups):
-        flavor_id = self.config.compute.flavor_ref
-        base_image_id = self.config.compute.image_ref
-        create_kwargs = {
-            'nics': [
-                {'net-id': network.id},
-            ],
-            'key_name': key_name,
-            'security_groups': security_groups,
-        }
-        server = client.servers.create(name, base_image_id, flavor_id,
-                                       **create_kwargs)
-        try:
-            self.assertEqual(server.name, name)
-            self.set_resource(name, server)
-        except AttributeError:
-            self.fail("Server not successfully created.")
-        self.status_timeout(client.servers, server.id, 'ACTIVE')
-        # The instance retrieved on creation is missing network
-        # details, necessitating retrieval after it becomes active to
-        # ensure correct details.
-        server = client.servers.get(server.id)
-        self.set_resource(name, server)
-        return server
-
     def _create_floating_ip(self, server, external_network_id):
         result = self.network_client.list_ports(device_id=server.id)
         ports = result.get('ports', [])
diff --git a/tempest/scenario/test_minimum_basic.py b/tempest/scenario/test_minimum_basic.py
index 13b31ec..b789fa2 100644
--- a/tempest/scenario/test_minimum_basic.py
+++ b/tempest/scenario/test_minimum_basic.py
@@ -90,15 +90,10 @@
         self.assertEqual(name, self.keypair.name)
 
     def nova_boot(self):
-        name = rand_name('scenario-server-')
-        client = self.compute_client
-        flavor_id = self.config.compute.flavor_ref
-        self.server = client.servers.create(name=name, image=self.image,
-                                            flavor=flavor_id,
-                                            key_name=self.keypair.name)
-        self.addCleanup(self.compute_client.servers.delete, self.server)
-        self.assertEqual(name, self.server.name)
-        self._wait_for_server_status('ACTIVE')
+        create_kwargs = {'key_name': self.keypair.name}
+        self.server = self.create_server(self.compute_client,
+                                         image=self.image,
+                                         create_kwargs=create_kwargs)
 
     def nova_list(self):
         servers = self.compute_client.servers.list()
diff --git a/tempest/scenario/test_network_basic_ops.py b/tempest/scenario/test_network_basic_ops.py
index 5311eae..99b0071 100644
--- a/tempest/scenario/test_network_basic_ops.py
+++ b/tempest/scenario/test_network_basic_ops.py
@@ -213,8 +213,15 @@
             name = rand_name('server-smoke-%d-' % i)
             keypair_name = self.keypairs[tenant_id].name
             security_groups = [self.security_groups[tenant_id].name]
-            server = self._create_server(self.compute_client, network,
-                                         name, keypair_name, security_groups)
+            create_kwargs = {
+                'nics': [
+                    {'net-id': network.id},
+                ],
+                'key_name': keypair_name,
+                'security_groups': security_groups,
+            }
+            server = self.create_server(self.compute_client, name=name,
+                                        create_kwargs=create_kwargs)
             self.servers.append(server)
 
     @attr(type='smoke')
diff --git a/tempest/scenario/test_server_basic_ops.py b/tempest/scenario/test_server_basic_ops.py
index 0ec3a1d..43ac2d9 100644
--- a/tempest/scenario/test_server_basic_ops.py
+++ b/tempest/scenario/test_server_basic_ops.py
@@ -82,26 +82,12 @@
                 self.fail("Failed to create rule in security group.")
 
     def boot_instance(self):
-        i_name = rand_name('instance')
-        flavor_id = self.config.compute.flavor_ref
-        base_image_id = self.config.compute.image_ref
         create_kwargs = {
             'key_name': self.get_resource('keypair').id
         }
-        self.instance = self.compute_client.servers.create(
-            i_name, base_image_id, flavor_id, **create_kwargs)
-        try:
-            self.assertEqual(self.instance.name, i_name)
-            self.set_resource('instance', self.instance)
-        except AttributeError:
-            self.fail("Instance not successfully created.")
-
-        self.assertEqual(self.instance.status, 'BUILD')
-
-    def wait_on_active(self):
-        instance_id = self.get_resource('instance').id
-        self.status_timeout(
-            self.compute_client.servers, instance_id, 'ACTIVE')
+        instance = self.create_server(self.compute_client,
+                                      create_kwargs=create_kwargs)
+        self.set_resource('instance', instance)
 
     def pause_server(self):
         instance = self.get_resource('instance')
@@ -148,7 +134,6 @@
         self.create_keypair()
         self.create_security_group()
         self.boot_instance()
-        self.wait_on_active()
         self.pause_server()
         self.unpause_server()
         self.suspend_server()
diff --git a/tempest/scenario/test_snapshot_pattern.py b/tempest/scenario/test_snapshot_pattern.py
index 6e305c1..e8ce1bd 100644
--- a/tempest/scenario/test_snapshot_pattern.py
+++ b/tempest/scenario/test_snapshot_pattern.py
@@ -44,20 +44,11 @@
         self.status_timeout(self.image_client.images, image_id, status)
 
     def _boot_image(self, image_id):
-        name = rand_name('scenario-server-')
-        client = self.compute_client
-        flavor_id = self.config.compute.flavor_ref
-        LOG.debug("name:%s, image:%s" % (name, image_id))
-        server = client.servers.create(name=name,
-                                       image=image_id,
-                                       flavor=flavor_id,
-                                       key_name=self.keypair.name)
-        self.addCleanup(self.compute_client.servers.delete, server)
-        self.assertEqual(name, server.name)
-        self._wait_for_server_status(server, 'ACTIVE')
-        server = client.servers.get(server)  # getting network information
-        LOG.debug("server:%s" % server)
-        return server
+        create_kwargs = {
+            'key_name': self.keypair.name
+        }
+        return self.create_server(self.compute_client, image=image_id,
+                                  create_kwargs=create_kwargs)
 
     def _add_keypair(self):
         name = rand_name('scenario-keypair-')
diff --git a/tempest/scenario/test_stamp_pattern.py b/tempest/scenario/test_stamp_pattern.py
index 4434604..038d251 100644
--- a/tempest/scenario/test_stamp_pattern.py
+++ b/tempest/scenario/test_stamp_pattern.py
@@ -64,20 +64,11 @@
                             volume_snapshot.id, status)
 
     def _boot_image(self, image_id):
-        name = rand_name('scenario-server-')
-        client = self.compute_client
-        flavor_id = self.config.compute.flavor_ref
-        LOG.debug("name:%s, image:%s" % (name, image_id))
-        server = client.servers.create(name=name,
-                                       image=image_id,
-                                       flavor=flavor_id,
-                                       key_name=self.keypair.name)
-        self.addCleanup(self.compute_client.servers.delete, server)
-        self.assertEqual(name, server.name)
-        self._wait_for_server_status(server, 'ACTIVE')
-        server = client.servers.get(server)  # getting network information
-        LOG.debug("server:%s" % server)
-        return server
+        create_kwargs = {
+            'key_name': self.keypair.name
+        }
+        return self.create_server(self.compute_client, image=image_id,
+                                  create_kwargs=create_kwargs)
 
     def _add_keypair(self):
         name = rand_name('scenario-keypair-')
diff --git a/tempest/scenario/test_volume_snapshot_pattern.py b/tempest/scenario/test_volume_snapshot_pattern.py
index 4d8a400..95a30ed 100644
--- a/tempest/scenario/test_volume_snapshot_pattern.py
+++ b/tempest/scenario/test_volume_snapshot_pattern.py
@@ -44,11 +44,6 @@
         return vol
 
     def _boot_instance_from_volume(self, vol_id):
-        # NOTE(gfidente): the img_uuid here is only needed because
-        # the novaclient requires it to be passed as arg
-        img_uuid = self.config.compute.image_ref
-        i_name = rand_name('instance')
-        flavor_id = self.config.compute.flavor_ref
         # NOTE(gfidente): the syntax for block_device_mapping is
         # dev_name=id:type:size:delete_on_terminate
         # where type needs to be "snap" if the server is booted
@@ -59,15 +54,8 @@
         create_kwargs = {
             'block_device_mapping': bd_map
         }
-        i = self.compute_client.servers.create(name=i_name,
-                                               image=img_uuid,
-                                               flavor=flavor_id,
-                                               **create_kwargs)
-        self.set_resource(i.id, i)
-        self.status_timeout(self.compute_client.servers,
-                            i.id,
-                            'ACTIVE')
-        return i
+        return self.create_server(self.compute_client,
+                                  create_kwargs=create_kwargs)
 
     def _create_snapshot_from_volume(self, vol_id):
         volume_snapshots = self.volume_client.volume_snapshots