Merge "Remove unnessary assertIsNotNone"
diff --git a/doc/source/microversion_testing.rst b/doc/source/microversion_testing.rst
index dc73ef2..6b87b4d 100644
--- a/doc/source/microversion_testing.rst
+++ b/doc/source/microversion_testing.rst
@@ -236,6 +236,10 @@
 
  .. _2.25: http://docs.openstack.org/developer/nova/api_microversion_history.html#maximum-in-mitaka
 
+ * `2.32`_
+
+ .. _2.32: http://docs.openstack.org/developer/nova/api_microversion_history.html#id29
+
  * `2.37`_
 
  .. _2.37: http://docs.openstack.org/developer/nova/api_microversion_history.html#id34
diff --git a/tempest/api/compute/admin/test_auto_allocate_network.py b/tempest/api/compute/admin/test_auto_allocate_network.py
index 4eb3376..8e481fd 100644
--- a/tempest/api/compute/admin/test_auto_allocate_network.py
+++ b/tempest/api/compute/admin/test_auto_allocate_network.py
@@ -19,8 +19,8 @@
 from tempest.common import credentials_factory as credentials
 from tempest.common import waiters
 from tempest import config
-from tempest import exceptions
 from tempest.lib.common.utils import test_utils
+from tempest.lib import exceptions as lib_excs
 from tempest import test
 
 CONF = config.CONF
@@ -82,14 +82,14 @@
         nets = cls.networks_client.list_networks(
             **search_opts).get('networks', [])
         if nets:
-            raise exceptions.TempestException(
+            raise lib_excs.TempestException(
                 'Found tenant networks: %s' % nets)
         # (2) Retrieve shared network list.
         search_opts = {'shared': True}
         nets = cls.networks_client.list_networks(
             **search_opts).get('networks', [])
         if nets:
-            raise exceptions.TempestException(
+            raise lib_excs.TempestException(
                 'Found shared networks: %s' % nets)
 
     @classmethod
diff --git a/tempest/api/compute/admin/test_migrations.py b/tempest/api/compute/admin/test_migrations.py
index 4f075eb..c9ba730 100644
--- a/tempest/api/compute/admin/test_migrations.py
+++ b/tempest/api/compute/admin/test_migrations.py
@@ -47,12 +47,7 @@
         server = self.create_test_server(wait_until="ACTIVE")
         server_id = server['id']
 
-        self.servers_client.resize_server(server_id, self.flavor_ref_alt)
-        waiters.wait_for_server_status(self.servers_client,
-                                       server_id, 'VERIFY_RESIZE')
-        self.servers_client.confirm_resize_server(server_id)
-        waiters.wait_for_server_status(self.servers_client,
-                                       server_id, 'ACTIVE')
+        self.resize_server(server_id, self.flavor_ref_alt)
 
         body = self.client.list_migrations()['migrations']
 
diff --git a/tempest/api/compute/base.py b/tempest/api/compute/base.py
index d8294f7..096941f 100644
--- a/tempest/api/compute/base.py
+++ b/tempest/api/compute/base.py
@@ -345,6 +345,15 @@
             LOG.exception('Failed to delete server %s' % server_id)
 
     @classmethod
+    def resize_server(cls, server_id, new_flavor_id, **kwargs):
+        """resize and confirm_resize an server, waits for it to be ACTIVE."""
+        cls.servers_client.resize_server(server_id, new_flavor_id, **kwargs)
+        waiters.wait_for_server_status(cls.servers_client, server_id,
+                                       'VERIFY_RESIZE')
+        cls.servers_client.confirm_resize_server(server_id)
+        waiters.wait_for_server_status(cls.servers_client, server_id, 'ACTIVE')
+
+    @classmethod
     def delete_volume(cls, volume_id):
         """Deletes the given volume and waits for it to be gone."""
         cls._delete_volume(cls.volumes_extensions_client, volume_id)
@@ -373,14 +382,18 @@
             self.request_microversion))
 
     @classmethod
-    def create_volume(cls):
+    def create_volume(cls, image_ref=None):
         """Create a volume and wait for it to become 'available'.
 
+        :param image_ref: Specify an image id to create a bootable volume.
         :returns: The available volume.
         """
         vol_name = data_utils.rand_name(cls.__name__ + '-volume')
-        volume = cls.volumes_client.create_volume(
-            size=CONF.volume.volume_size, display_name=vol_name)['volume']
+        create_params = dict(size=CONF.volume.volume_size,
+                             display_name=vol_name)
+        if image_ref is not None:
+            create_params['imageRef'] = image_ref
+        volume = cls.volumes_client.create_volume(**create_params)['volume']
         cls.volumes.append(volume)
         waiters.wait_for_volume_status(cls.volumes_client,
                                        volume['id'], 'available')
diff --git a/tempest/api/compute/servers/test_device_tagging.py b/tempest/api/compute/servers/test_device_tagging.py
new file mode 100644
index 0000000..9acc2b1
--- /dev/null
+++ b/tempest/api/compute/servers/test_device_tagging.py
@@ -0,0 +1,268 @@
+# Copyright (C) 2016, Red Hat, Inc.
+#
+#    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.
+
+import json
+
+from oslo_log import log as logging
+
+from tempest.api.compute import base
+from tempest.common.utils import data_utils
+from tempest.common.utils.linux import remote_client
+from tempest.common import waiters
+from tempest import config
+from tempest import exceptions
+from tempest import test
+
+
+CONF = config.CONF
+
+LOG = logging.getLogger(__name__)
+
+
+class DeviceTaggingTest(base.BaseV2ComputeTest):
+
+    min_microversion = '2.32'
+    max_microversion = 'latest'
+
+    @classmethod
+    def skip_checks(cls):
+        super(DeviceTaggingTest, cls).skip_checks()
+        if not CONF.service_available.neutron:
+            raise cls.skipException('Neutron is required')
+        if not CONF.validation.run_validation:
+            raise cls.skipException('Validation must be enabled')
+        if (not CONF.compute_feature_enabled.config_drive
+            and not CONF.compute_feature_enabled.metadata_service):
+            raise cls.skipException('One of metadata or config drive must be '
+                                    'enabled')
+
+    @classmethod
+    def setup_clients(cls):
+        super(DeviceTaggingTest, cls).setup_clients()
+        cls.networks_client = cls.os.networks_client
+        cls.ports_client = cls.os.ports_client
+        cls.volumes_client = cls.os.volumes_client
+        cls.subnets_client = cls.os.subnets_client
+        cls.routers_client = cls.os.routers_client
+        cls.interfaces_client = cls.os.interfaces_client
+
+    @classmethod
+    def setup_credentials(cls):
+        cls.set_network_resources(network=True, subnet=True, router=True,
+                                  dhcp=True)
+        super(DeviceTaggingTest, cls).setup_credentials()
+
+    @classmethod
+    def resource_setup(cls):
+        cls.set_validation_resources()
+        super(DeviceTaggingTest, cls).resource_setup()
+
+    def verify_device_metadata(self, md_json):
+        md_dict = json.loads(md_json)
+        for d in md_dict['devices']:
+            if d['type'] == 'nic':
+                if d['mac'] == self.port1['mac_address']:
+                    self.assertEqual(d['tags'], ['port-1'])
+                if d['mac'] == self.port2['mac_address']:
+                    self.assertEqual(d['tags'], ['port-2'])
+                if d['mac'] == self.net_2_100_mac:
+                    self.assertEqual(d['tags'], ['net-2-100'])
+                if d['mac'] == self.net_2_200_mac:
+                    self.assertEqual(d['tags'], ['net-2-200'])
+
+        found_devices = [d['tags'][0] for d in md_dict['devices']]
+        self.assertItemsEqual(found_devices, ['port-1', 'port-2', 'net-1',
+                                              'net-2-100', 'net-2-200',
+                                              'boot', 'other'])
+
+    @test.idempotent_id('a2e65a6c-66f1-4442-aaa8-498c31778d96')
+    @test.services('network', 'volume', 'image')
+    def test_device_tagging(self):
+        # Create volumes
+        # The create_volume methods waits for the volumes to be available and
+        # the base class will clean them up on tearDown.
+        boot_volume = self.create_volume(CONF.compute.image_ref)
+        other_volume = self.create_volume()
+        untagged_volume = self.create_volume()
+
+        # Create networks
+        net1 = self.networks_client.create_network(
+            name=data_utils.rand_name('device-tagging-net1'))['network']
+        self.addCleanup(self.networks_client.delete_network, net1['id'])
+
+        net2 = self.networks_client.create_network(
+            name=data_utils.rand_name('device-tagging-net2'))['network']
+        self.addCleanup(self.networks_client.delete_network, net2['id'])
+
+        # Create subnets
+        subnet1 = self.subnets_client.create_subnet(
+            network_id=net1['id'],
+            cidr='10.1.1.0/24',
+            ip_version=4)['subnet']
+        self.addCleanup(self.subnets_client.delete_subnet, subnet1['id'])
+
+        subnet2 = self.subnets_client.create_subnet(
+            network_id=net2['id'],
+            cidr='10.2.2.0/24',
+            ip_version=4)['subnet']
+        self.addCleanup(self.subnets_client.delete_subnet, subnet2['id'])
+
+        # Create ports
+        self.port1 = self.ports_client.create_port(
+            network_id=net1['id'],
+            fixed_ips=[{'subnet_id': subnet1['id']}])['port']
+        self.addCleanup(self.ports_client.delete_port, self.port1['id'])
+
+        self.port2 = self.ports_client.create_port(
+            network_id=net1['id'],
+            fixed_ips=[{'subnet_id': subnet1['id']}])['port']
+        self.addCleanup(self.ports_client.delete_port, self.port2['id'])
+
+        # Create server
+        admin_pass = data_utils.rand_password()
+        config_drive_enabled = CONF.compute_feature_enabled.config_drive
+
+        server = self.create_test_server(
+            validatable=True,
+            config_drive=config_drive_enabled,
+            adminPass=admin_pass,
+            name=data_utils.rand_name('device-tagging-server'),
+            networks=[
+                # Validation network for ssh
+                {
+                    'uuid': self.get_tenant_network()['id']
+                },
+                # Different tags for different ports
+                {
+                    'port': self.port1['id'],
+                    'tag': 'port-1'
+                },
+                {
+                    'port': self.port2['id'],
+                    'tag': 'port-2'
+                },
+                # Two nics on same net, one tagged one not
+                {
+                    'uuid': net1['id'],
+                    'tag': 'net-1'
+                },
+                {
+                    'uuid': net1['id']
+                },
+                # Two nics on same net, different IP
+                {
+                    'uuid': net2['id'],
+                    'fixed_ip': '10.2.2.100',
+                    'tag': 'net-2-100'
+                },
+                {
+                    'uuid': net2['id'],
+                    'fixed_ip': '10.2.2.200',
+                    'tag': 'net-2-200'
+                }
+            ],
+            block_device_mapping_v2=[
+                # Boot volume
+                {
+                    'uuid': boot_volume['id'],
+                    'source_type': 'volume',
+                    'destination_type': 'volume',
+                    'boot_index': 0,
+                    'tag': 'boot'
+                },
+                # Other volume
+                {
+                    'uuid': other_volume['id'],
+                    'source_type': 'volume',
+                    'destination_type': 'volume',
+                    'boot_index': 1,
+                    'tag': 'other'
+                },
+                # Untagged volume
+                {
+                    'uuid': untagged_volume['id'],
+                    'source_type': 'volume',
+                    'destination_type': 'volume',
+                    'boot_index': 2
+                }
+            ])
+
+        self.addCleanup(waiters.wait_for_server_termination,
+                        self.servers_client, server['id'])
+        self.addCleanup(self.servers_client.delete_server, server['id'])
+
+        self.ssh_client = remote_client.RemoteClient(
+            self.get_server_ip(server),
+            CONF.validation.image_ssh_user,
+            admin_pass,
+            self.validation_resources['keypair']['private_key'],
+            server=server,
+            servers_client=self.servers_client)
+
+        # Find the MAC addresses of our fixed IPs
+        self.net_2_100_mac = None
+        self.net_2_200_mac = None
+        ifaces = self.interfaces_client.list_interfaces(server['id'])
+        for iface in ifaces['interfaceAttachments']:
+            if 'fixed_ips' in iface:
+                for ip in iface['fixed_ips']:
+                    if ip['ip_address'] == '10.2.2.100':
+                        self.net_2_100_mac = iface['mac_addr']
+                    if ip['ip_address'] == '10.2.2.200':
+                        self.net_2_200_mac = iface['mac_addr']
+        # Make sure we have the MACs we need, there's no reason for some to be
+        # missing
+        self.assertTrue(self.net_2_100_mac)
+        self.assertTrue(self.net_2_200_mac)
+
+        # Verify metadata from metadata service
+        if CONF.compute_feature_enabled.metadata_service:
+            md_url = 'http://169.254.169.254/openstack/latest/meta_data.json'
+            LOG.info('Attempting to verify tagged devices in server %s via '
+                     'the metadata service: %s', server['id'], md_url)
+
+            def get_and_verify_metadata():
+                try:
+                    self.ssh_client.exec_command('curl -V')
+                except exceptions.SSHExecCommandFailed:
+                    if not CONF.compute_feature_enabled.config_drive:
+                        raise self.skipException('curl not found in guest '
+                                                 'and config drive is '
+                                                 'disabled')
+                    LOG.warning('curl was not found in the guest, device '
+                                'tagging metadata was not checked in the '
+                                'metadata API')
+                    return True
+                cmd = 'curl %s' % md_url
+                md_json = self.ssh_client.exec_command(cmd)
+                self.verify_device_metadata(md_json)
+                return True
+
+            if not test.call_until_true(get_and_verify_metadata,
+                                        CONF.compute.build_timeout,
+                                        CONF.compute.build_interval):
+                raise exceptions.TimeoutException('Timeout while verifying '
+                                                  'metadata on server.')
+
+        # Verify metadata on config drive
+        if CONF.compute_feature_enabled.config_drive:
+            cmd_blkid = 'blkid -t LABEL=config-2 -o device'
+            LOG.info('Attempting to verify tagged devices in server %s via '
+                     'the config drive.', server['id'])
+            dev_name = self.ssh_client.exec_command(cmd_blkid)
+            dev_name = dev_name.rstrip()
+            self.ssh_client.exec_command('sudo mount %s /mnt' % dev_name)
+            cmd_md = 'sudo cat /mnt/openstack/latest/meta_data.json'
+            md_json = self.ssh_client.exec_command(cmd_md)
+            self.verify_device_metadata(md_json)
diff --git a/tempest/api/compute/servers/test_disk_config.py b/tempest/api/compute/servers/test_disk_config.py
index aba0240..ff8ea6e 100644
--- a/tempest/api/compute/servers/test_disk_config.py
+++ b/tempest/api/compute/servers/test_disk_config.py
@@ -94,12 +94,8 @@
         self._update_server_with_disk_config(server['id'],
                                              disk_config='MANUAL')
         # Resize with auto option
-        self.client.resize_server(server['id'], self.flavor_ref_alt,
-                                  disk_config='AUTO')
-        waiters.wait_for_server_status(self.client, server['id'],
-                                       'VERIFY_RESIZE')
-        self.client.confirm_resize_server(server['id'])
-        waiters.wait_for_server_status(self.client, server['id'], 'ACTIVE')
+        self.resize_server(server['id'], self.flavor_ref_alt,
+                           disk_config='AUTO')
 
         server = self.client.show_server(server['id'])['server']
         self.assertEqual('AUTO', server['OS-DCF:diskConfig'])
@@ -114,12 +110,8 @@
         self._update_server_with_disk_config(server['id'],
                                              disk_config='AUTO')
         # Resize with manual option
-        self.client.resize_server(server['id'], self.flavor_ref_alt,
-                                  disk_config='MANUAL')
-        waiters.wait_for_server_status(self.client, server['id'],
-                                       'VERIFY_RESIZE')
-        self.client.confirm_resize_server(server['id'])
-        waiters.wait_for_server_status(self.client, server['id'], 'ACTIVE')
+        self.resize_server(server['id'], self.flavor_ref_alt,
+                           disk_config='MANUAL')
 
         server = self.client.show_server(server['id'])['server']
         self.assertEqual('MANUAL', server['OS-DCF:diskConfig'])
diff --git a/tempest/api/compute/volumes/test_attach_volume.py b/tempest/api/compute/volumes/test_attach_volume.py
index ee6ac25..56ec8c6 100644
--- a/tempest/api/compute/volumes/test_attach_volume.py
+++ b/tempest/api/compute/volumes/test_attach_volume.py
@@ -156,6 +156,36 @@
         self.assertEqual(volume['id'], body['volumeId'])
         self.assertEqual(attachment['id'], body['id'])
 
+    @test.idempotent_id('757d488b-a951-4bc7-b3cd-f417028da08a')
+    def test_list_get_two_volume_attachments(self):
+        # NOTE: This test is using the volume device auto-assignment
+        # without specifying the device ("/dev/sdb", etc). The feature
+        # is supported since Nova Liberty release or later. So this should
+        # be skipped on older clouds.
+        server = self._create_server()
+        volume_1st = self.create_volume()
+        volume_2nd = self.create_volume()
+        attachment_1st = self._attach_volume(server['id'], volume_1st['id'])
+        attachment_2nd = self._attach_volume(server['id'], volume_2nd['id'])
+
+        body = self.servers_client.list_volume_attachments(
+            server['id'])['volumeAttachments']
+        self.assertEqual(2, len(body))
+
+        body = self.servers_client.show_volume_attachment(
+            server['id'],
+            attachment_1st['id'])['volumeAttachment']
+        self.assertEqual(server['id'], body['serverId'])
+        self.assertEqual(attachment_1st['volumeId'], body['volumeId'])
+        self.assertEqual(attachment_1st['id'], body['id'])
+
+        body = self.servers_client.show_volume_attachment(
+            server['id'],
+            attachment_2nd['id'])['volumeAttachment']
+        self.assertEqual(server['id'], body['serverId'])
+        self.assertEqual(attachment_2nd['volumeId'], body['volumeId'])
+        self.assertEqual(attachment_2nd['id'], body['id'])
+
 
 class AttachVolumeShelveTestJSON(AttachVolumeTestJSON):
     """Testing volume with shelved instance.
diff --git a/tempest/api/volume/test_volumes_actions.py b/tempest/api/volume/test_volumes_actions.py
index 38f1082..d8d6b9a 100644
--- a/tempest/api/volume/test_volumes_actions.py
+++ b/tempest/api/volume/test_volumes_actions.py
@@ -74,8 +74,11 @@
             fetched_volume = self.client.show_volume(
                 self.volume['id'])['volume']
             # Get Volume information
-            bool_flag = self._is_true(fetched_volume['bootable'])
-            self.assertEqual(bool_bootable, bool_flag)
+            # NOTE(masayukig): 'bootable' is "true" or "false" in the current
+            # cinder implementation. So we need to cast boolean values to str
+            # and make it lower to compare here.
+            self.assertEqual(str(bool_bootable).lower(),
+                             fetched_volume['bootable'])
 
     @test.idempotent_id('9516a2c8-9135-488c-8dd6-5677a7e5f371')
     @test.services('compute')
@@ -136,9 +139,6 @@
         body = self.client.show_volume(self.volume['id'])['volume']
         self.assertIn('available', body['status'])
 
-    def _is_true(self, val):
-        return val in ['true', 'True', True]
-
     @test.idempotent_id('fff74e1e-5bd3-4b33-9ea9-24c103bc3f59')
     def test_volume_readonly_update(self):
         for readonly in [True, False]:
@@ -148,8 +148,11 @@
             # Get Volume information
             fetched_volume = self.client.show_volume(
                 self.volume['id'])['volume']
-            bool_flag = self._is_true(fetched_volume['metadata']['readonly'])
-            self.assertEqual(readonly, bool_flag)
+            # NOTE(masayukig): 'readonly' is "True" or "False" in the current
+            # cinder implementation. So we need to cast boolean values to str
+            # to compare here.
+            self.assertEqual(str(readonly),
+                             fetched_volume['metadata']['readonly'])
 
 
 class VolumesV1ActionsTest(VolumesV2ActionsTest):
diff --git a/tempest/tests/lib/services/volume/v3/test_user_messages.py b/tempest/tests/lib/services/volume/v3/test_user_messages_client.py
similarity index 100%
rename from tempest/tests/lib/services/volume/v3/test_user_messages.py
rename to tempest/tests/lib/services/volume/v3/test_user_messages_client.py