Merge "Add service tags to api.volume"
diff --git a/tempest/api/compute/admin/test_flavors_extra_specs.py b/tempest/api/compute/admin/test_flavors_extra_specs.py
index f2f82b5..ace77a6 100644
--- a/tempest/api/compute/admin/test_flavors_extra_specs.py
+++ b/tempest/api/compute/admin/test_flavors_extra_specs.py
@@ -17,6 +17,8 @@
 
 from tempest.api import compute
 from tempest.api.compute import base
+from tempest.common.utils.data_utils import rand_int_id
+from tempest.common.utils.data_utils import rand_name
 from tempest import exceptions
 from tempest.test import attr
 
@@ -39,12 +41,12 @@
             raise cls.skipException(msg)
 
         cls.client = cls.os_adm.flavors_client
-        flavor_name = 'test_flavor2'
+        flavor_name = rand_name('test_flavor')
         ram = 512
         vcpus = 1
         disk = 10
         ephemeral = 10
-        cls.new_flavor_id = 12345
+        cls.new_flavor_id = rand_int_id(start=1000)
         swap = 1024
         rxtx = 1
         # Create a flavor so as to set/get/unset extra specs
diff --git a/tempest/scenario/manager.py b/tempest/scenario/manager.py
index 90b3fca..d3c2a18 100644
--- a/tempest/scenario/manager.py
+++ b/tempest/scenario/manager.py
@@ -28,6 +28,7 @@
 from neutronclient.common import exceptions as exc
 import neutronclient.v2_0.client
 import novaclient.client
+from novaclient import exceptions as nova_exceptions
 
 from tempest.api.network import common as net_common
 from tempest.common import isolated_creds
@@ -276,27 +277,57 @@
         expected status to show. At any time, if the returned
         status of the thing is ERROR, fail out.
         """
+        self._status_timeout(things, thing_id, expected_status=expected_status)
+
+    def delete_timeout(self, things, thing_id):
+        """
+        Given a thing, do a loop, sleeping
+        for a configurable amount of time, checking for the
+        deleted status to show. At any time, if the returned
+        status of the thing is ERROR, fail out.
+        """
+        self._status_timeout(things,
+                             thing_id,
+                             allow_notfound=True)
+
+    def _status_timeout(self,
+                        things,
+                        thing_id,
+                        expected_status=None,
+                        allow_notfound=False):
+
+        log_status = expected_status if expected_status else ''
+        if allow_notfound:
+            log_status += ' or NotFound' if log_status != '' else 'NotFound'
+
         def check_status():
             # python-novaclient has resources available to its client
             # that all implement a get() method taking an identifier
             # for the singular resource to retrieve.
-            thing = things.get(thing_id)
+            try:
+                thing = things.get(thing_id)
+            except nova_exceptions.NotFound:
+                if allow_notfound:
+                    return True
+                else:
+                    raise
+
             new_status = thing.status
             if new_status == 'ERROR':
                 message = "%s failed to get to expected status. \
                           In ERROR state." % (thing)
                 raise exceptions.BuildErrorException(message)
-            elif new_status == expected_status:
+            elif new_status == expected_status and expected_status is not None:
                 return True  # All good.
             LOG.debug("Waiting for %s to get to %s status. "
                       "Currently in %s status",
-                      thing, expected_status, new_status)
+                      thing, log_status, new_status)
         if not tempest.test.call_until_true(
             check_status,
             self.config.compute.build_timeout,
             self.config.compute.build_interval):
             message = "Timed out waiting for thing %s \
-                      to become %s" % (thing_id, expected_status)
+                      to become %s" % (thing_id, log_status)
             raise exceptions.TimeoutException(message)
 
     def create_loginable_secgroup_rule(self, client=None, secgroup_id=None):
diff --git a/tempest/scenario/test_volume_boot_pattern.py b/tempest/scenario/test_volume_boot_pattern.py
new file mode 100644
index 0000000..09c1cb7
--- /dev/null
+++ b/tempest/scenario/test_volume_boot_pattern.py
@@ -0,0 +1,161 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+from tempest.common.utils.data_utils import rand_name
+from tempest.scenario import manager
+
+
+class TestVolumeBootPattern(manager.OfficialClientTest):
+
+    """
+    This test case attempts to reproduce the following steps:
+
+     * Create in Cinder some bootable volume importing a Glance image
+     * Boot an instance from the bootable volume
+     * Write content to the volume
+     * Delete an instance and Boot a new instance from the volume
+     * Check written content in the instance
+     * Create a volume snapshot while the instance is running
+     * Boot an additional instance from the new snapshot based volume
+     * Check written content in the instance booted from snapshot
+    """
+
+    def _create_volume_from_image(self):
+        img_uuid = self.config.compute.image_ref
+        vol_name = rand_name('volume-origin')
+        return self.create_volume(name=vol_name, imageRef=img_uuid)
+
+    def _boot_instance_from_volume(self, vol_id, keypair):
+        # 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
+        # from a snapshot, size instead can be safely left empty
+        bd_map = {
+            'vda': vol_id + ':::0'
+        }
+        create_kwargs = {
+            'block_device_mapping': bd_map,
+            'key_name': keypair.name
+        }
+        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
+        snap_name = rand_name('snapshot')
+        snap = volume_snapshots.create(volume_id=vol_id,
+                                       force=True,
+                                       display_name=snap_name)
+        self.set_resource(snap.id, snap)
+        self.status_timeout(volume_snapshots,
+                            snap.id,
+                            'available')
+        return snap
+
+    def _create_volume_from_snapshot(self, snap_id):
+        vol_name = rand_name('volume')
+        return self.create_volume(name=vol_name, snapshot_id=snap_id)
+
+    def _stop_instances(self, instances):
+        # NOTE(gfidente): two loops so we do not wait for the status twice
+        for i in instances:
+            self.compute_client.servers.stop(i)
+        for i in instances:
+            self.status_timeout(self.compute_client.servers,
+                                i.id,
+                                'SHUTOFF')
+
+    def _detach_volumes(self, volumes):
+        # NOTE(gfidente): two loops so we do not wait for the status twice
+        for v in volumes:
+            self.volume_client.volumes.detach(v)
+        for v in volumes:
+            self.status_timeout(self.volume_client.volumes,
+                                v.id,
+                                'available')
+
+    def _ssh_to_server(self, server, keypair):
+        if self.config.compute.use_floatingip_for_ssh:
+            floating_ip = self.compute_client.floating_ips.create()
+            fip_name = rand_name('scenario-fip')
+            self.set_resource(fip_name, floating_ip)
+            server.add_floating_ip(floating_ip)
+            ip = floating_ip.ip
+        else:
+            network_name_for_ssh = self.config.compute.network_for_ssh
+            ip = server.networks[network_name_for_ssh][0]
+
+        client = self.get_remote_client(ip,
+                                        private_key=keypair.private_key)
+        return client.ssh_client
+
+    def _get_content(self, ssh_client):
+        return ssh_client.exec_command('cat /tmp/text')
+
+    def _write_text(self, ssh_client):
+        text = rand_name('text-')
+        ssh_client.exec_command('echo "%s" > /tmp/text; sync' % (text))
+
+        return self._get_content(ssh_client)
+
+    def _delete_server(self, server):
+        self.compute_client.servers.delete(server)
+        self.delete_timeout(self.compute_client.servers, server.id)
+
+    def _check_content_of_written_file(self, ssh_client, expected):
+        actual = self._get_content(ssh_client)
+        self.assertEqual(expected, actual)
+
+    def test_volume_boot_pattern(self):
+        keypair = self.create_keypair()
+        self.create_loginable_secgroup_rule()
+
+        # create an instance from volume
+        volume_origin = self._create_volume_from_image()
+        instance_1st = self._boot_instance_from_volume(volume_origin.id,
+                                                       keypair)
+
+        # write content to volume on instance
+        ssh_client_for_instance_1st = self._ssh_to_server(instance_1st,
+                                                          keypair)
+        text = self._write_text(ssh_client_for_instance_1st)
+
+        # delete instance
+        self._delete_server(instance_1st)
+
+        # create a 2nd instance from volume
+        instance_2nd = self._boot_instance_from_volume(volume_origin.id,
+                                                       keypair)
+
+        # check the content of written file
+        ssh_client_for_instance_2nd = self._ssh_to_server(instance_2nd,
+                                                          keypair)
+        self._check_content_of_written_file(ssh_client_for_instance_2nd, text)
+
+        # snapshot a volume
+        snapshot = self._create_snapshot_from_volume(volume_origin.id)
+
+        # create a 3rd instance from snapshot
+        volume = self._create_volume_from_snapshot(snapshot.id)
+        instance_from_snapshot = self._boot_instance_from_volume(volume.id,
+                                                                 keypair)
+
+        # check the content of written file
+        ssh_client = self._ssh_to_server(instance_from_snapshot, keypair)
+        self._check_content_of_written_file(ssh_client, text)
+
+        # NOTE(gfidente): ensure resources are in clean state for
+        # deletion operations to succeed
+        self._stop_instances([instance_2nd, instance_from_snapshot])
+        self._detach_volumes([volume_origin, volume])
diff --git a/tempest/scenario/test_volume_snapshot_pattern.py b/tempest/scenario/test_volume_snapshot_pattern.py
deleted file mode 100644
index d873d30..0000000
--- a/tempest/scenario/test_volume_snapshot_pattern.py
+++ /dev/null
@@ -1,92 +0,0 @@
-# vim: tabstop=4 shiftwidth=4 softtabstop=4
-
-#    Licensed under the Apache License, Version 2.0 (the "License"); you may
-#    not use this file except in compliance with the License. You may obtain
-#    a copy of the License at
-#
-#         http://www.apache.org/licenses/LICENSE-2.0
-#
-#    Unless required by applicable law or agreed to in writing, software
-#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-#    License for the specific language governing permissions and limitations
-#    under the License.
-
-from tempest.common.utils.data_utils import rand_name
-from tempest.scenario import manager
-
-
-class TestVolumeSnapshotPattern(manager.OfficialClientTest):
-
-    """
-    This test case attempts to reproduce the following steps:
-
-     * Create in Cinder some bootable volume importing a Glance image
-     * Boot an instance from the bootable volume
-     * Create a volume snapshot while the instance is running
-     * Boot an additional instance from the new snapshot based volume
-    """
-
-    def _create_volume_from_image(self):
-        img_uuid = self.config.compute.image_ref
-        vol_name = rand_name('volume-origin')
-        return self.create_volume(name=vol_name, imageRef=img_uuid)
-
-    def _boot_instance_from_volume(self, vol_id):
-        # 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
-        # from a snapshot, size instead can be safely left empty
-        bd_map = {
-            'vda': vol_id + ':::0'
-        }
-        create_kwargs = {
-            'block_device_mapping': bd_map
-        }
-        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
-        snap_name = rand_name('snapshot')
-        snap = volume_snapshots.create(volume_id=vol_id,
-                                       force=True,
-                                       display_name=snap_name)
-        self.set_resource(snap.id, snap)
-        self.status_timeout(volume_snapshots,
-                            snap.id,
-                            'available')
-        return snap
-
-    def _create_volume_from_snapshot(self, snap_id):
-        vol_name = rand_name('volume')
-        return self.create_volume(name=vol_name, snapshot_id=snap_id)
-
-    def _stop_instances(self, instances):
-        # NOTE(gfidente): two loops so we do not wait for the status twice
-        for i in instances:
-            self.compute_client.servers.stop(i)
-        for i in instances:
-            self.status_timeout(self.compute_client.servers,
-                                i.id,
-                                'SHUTOFF')
-
-    def _detach_volumes(self, volumes):
-        # NOTE(gfidente): two loops so we do not wait for the status twice
-        for v in volumes:
-            self.volume_client.volumes.detach(v)
-        for v in volumes:
-            self.status_timeout(self.volume_client.volumes,
-                                v.id,
-                                'available')
-
-    def test_volume_snapshot_pattern(self):
-        volume_origin = self._create_volume_from_image()
-        i_origin = self._boot_instance_from_volume(volume_origin.id)
-        snapshot = self._create_snapshot_from_volume(volume_origin.id)
-        volume = self._create_volume_from_snapshot(snapshot.id)
-        i = self._boot_instance_from_volume(volume.id)
-        # NOTE(gfidente): ensure resources are in clean state for
-        # deletion operations to succeed
-        self._stop_instances([i_origin, i])
-        self._detach_volumes([volume_origin, volume])
diff --git a/tempest/services/compute/xml/servers_client.py b/tempest/services/compute/xml/servers_client.py
index ec9464a..6f17611 100644
--- a/tempest/services/compute/xml/servers_client.py
+++ b/tempest/services/compute/xml/servers_client.py
@@ -46,9 +46,14 @@
     # expanded xml namespace.
     type_ns_prefix = ('{http://docs.openstack.org/compute/ext/extended_ips/'
                       'api/v1.1}type')
+    mac_ns_prefix = ('{http://docs.openstack.org/compute/ext/extended_ips_mac'
+                     '/api/v1.1}mac_addr')
+
     if type_ns_prefix in ip:
-        ip['OS-EXT-IPS:type'] = ip[type_ns_prefix]
-        ip.pop(type_ns_prefix)
+        ip['OS-EXT-IPS:type'] = ip.pop(type_ns_prefix)
+
+    if mac_ns_prefix in ip:
+        ip['OS-EXT-IPS-MAC:mac_addr'] = ip.pop(mac_ns_prefix)
     return ip
 
 
@@ -101,11 +106,35 @@
         json['addresses'] = json_addresses
     else:
         json = xml_to_json(xml_dom)
-    diskConfig = '{http://docs.openstack.org/compute/ext/disk_config/api/v1.1'\
-                 '}diskConfig'
+    diskConfig = ('{http://docs.openstack.org'
+                  '/compute/ext/disk_config/api/v1.1}diskConfig')
+    terminated_at = ('{http://docs.openstack.org/'
+                     'compute/ext/server_usage/api/v1.1}terminated_at')
+    launched_at = ('{http://docs.openstack.org'
+                   '/compute/ext/server_usage/api/v1.1}launched_at')
+    power_state = ('{http://docs.openstack.org'
+                   '/compute/ext/extended_status/api/v1.1}power_state')
+    availability_zone = ('{http://docs.openstack.org'
+                         '/compute/ext/extended_availability_zone/api/v2}'
+                         'availability_zone')
+    vm_state = ('{http://docs.openstack.org'
+                '/compute/ext/extended_status/api/v1.1}vm_state')
+    task_state = ('{http://docs.openstack.org'
+                  '/compute/ext/extended_status/api/v1.1}task_state')
     if diskConfig in json:
-        json['OS-DCF:diskConfig'] = json[diskConfig]
-        del json[diskConfig]
+        json['OS-DCF:diskConfig'] = json.pop(diskConfig)
+    if terminated_at in json:
+        json['OS-SRV-USG:terminated_at'] = json.pop(terminated_at)
+    if launched_at in json:
+        json['OS-SRV-USG:launched_at'] = json.pop(launched_at)
+    if power_state in json:
+        json['OS-EXT-STS:power_state'] = json.pop(power_state)
+    if availability_zone in json:
+        json['OS-EXT-AZ:availability_zone'] = json.pop(availability_zone)
+    if vm_state in json:
+        json['OS-EXT-STS:vm_state'] = json.pop(vm_state)
+    if task_state in json:
+        json['OS-EXT-STS:task_state'] = json.pop(task_state)
     return json