Merge "Add cinder volume instancelocality filter test" into mcp/caracal
diff --git a/tempest/api/volume/test_volumes_filters.py b/tempest/api/volume/test_volumes_filters.py
new file mode 100644
index 0000000..ac164eb
--- /dev/null
+++ b/tempest/api/volume/test_volumes_filters.py
@@ -0,0 +1,46 @@
+# Copyright 2021 Mirantis 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 testtools
+
+from tempest.api.volume import base
+from tempest import config
+from tempest.lib import decorators
+
+
+CONF = config.CONF
+
+
+class VolumesFilter(base.BaseVolumeAdminTest):
+    @testtools.skipUnless(
+        "InstanceLocalityFilter" in CONF.volume.scheduler_default_filters,
+        "Cinder InstanceLocalityFilter is disabled",
+    )
+    @decorators.idempotent_id("5c13f4f7-5add-4fad-8ef7-dccca0f76295")
+    def test_instancelocalityfilter(self):
+        # 1. Create instance
+        # 2. Create volume by using local_to_instance hint
+        # 3. Compare server host and volume host are the same.
+        server = self.create_server()
+        server_host = self.admin_manager.servers_client.show_server(
+            server["id"])["server"]["OS-EXT-SRV-ATTR:host"]
+        volume = self.create_volume(hints={"local_to_instance": server["id"]})
+        fetched_volume = self.admin_volume_client.show_volume(volume["id"])[
+            "volume"]
+        self.assertEqual(
+            server_host, fetched_volume["os-vol-host-attr:host"].split("@")
+            [0],
+            "The fetched Volume host is different "
+            "from the created instance",)
diff --git a/tempest/config.py b/tempest/config.py
index a9d042d..af02edd 100644
--- a/tempest/config.py
+++ b/tempest/config.py
@@ -1046,6 +1046,9 @@
                     "If both values are not specified, Tempest avoids tests "
                     "which require a microversion. Valid values are string "
                     "with format 'X.Y' or string 'latest'",),
+    cfg.ListOpt('scheduler_default_filters',
+                default=[],
+                help="The list of enabled scheduler filters.",),
 ]
 
 volume_feature_group = cfg.OptGroup(name='volume-feature-enabled',
diff --git a/tempest/lib/services/volume/v3/volumes_client.py b/tempest/lib/services/volume/v3/volumes_client.py
index c6f8973..65dc258 100644
--- a/tempest/lib/services/volume/v3/volumes_client.py
+++ b/tempest/lib/services/volume/v3/volumes_client.py
@@ -105,14 +105,17 @@
         self.validate_response(schema.show_volume, resp, body)
         return rest_client.ResponseBody(resp, body)
 
-    def create_volume(self, **kwargs):
+    def create_volume(self, hints=None, **kwargs):
         """Creates a new Volume.
 
         For a full list of available parameters, please refer to the official
         API reference:
         https://docs.openstack.org/api-ref/block-storage/v3/index.html#create-a-volume
         """
-        post_body = json.dumps({'volume': kwargs})
+        obj = {'volume': kwargs}
+        if hints is not None:
+            obj['OS-SCH-HNT:scheduler_hints'] = hints
+        post_body = json.dumps(obj)
         resp, body = self.post('volumes', post_body)
         body = json.loads(body)
         schema = self.get_schema(self.schema_versions_info)