enable volumes v2 snapshot tests by sharing codes

1. create volumes v2 tests using the existent test class.
2. create a new test class for v1, which inherits v2.

This patch includes the following volumes tests:
- test_snapshot_metadata.py
- test_volumes_snapshots.py
- test_volumes_snapshots_negative.py

Partially implements: blueprint cinder-v2-api-tests

Change-Id: I5e8a8c71d8c3a3b330a60ab0ea9603f8864518bd
diff --git a/tempest/api/volume/base.py b/tempest/api/volume/base.py
index 43f48ff..8aad058 100644
--- a/tempest/api/volume/base.py
+++ b/tempest/api/volume/base.py
@@ -69,6 +69,7 @@
             if not CONF.volume_feature_enabled.api_v2:
                 msg = "Volume API v2 is disabled"
                 raise cls.skipException(msg)
+            cls.snapshots_client = cls.os.snapshots_v2_client
             cls.volumes_client = cls.os.volumes_v2_client
             cls.volumes_extension_client = cls.os.volumes_v2_extension_client
             cls.availability_zone_client = (
diff --git a/tempest/api/volume/test_snapshot_metadata.py b/tempest/api/volume/test_snapshot_metadata.py
index 94ba095..7040891 100644
--- a/tempest/api/volume/test_snapshot_metadata.py
+++ b/tempest/api/volume/test_snapshot_metadata.py
@@ -17,13 +17,12 @@
 from tempest import test
 
 
-class SnapshotMetadataTest(base.BaseVolumeV1Test):
-    _interface = "json"
+class SnapshotV2MetadataTestJSON(base.BaseVolumeTest):
 
     @classmethod
     @test.safe_setup
     def setUpClass(cls):
-        super(SnapshotMetadataTest, cls).setUpClass()
+        super(SnapshotV2MetadataTestJSON, cls).setUpClass()
         cls.client = cls.snapshots_client
         # Create a volume
         cls.volume = cls.create_volume()
@@ -34,7 +33,7 @@
     def tearDown(self):
         # Update the metadata to {}
         self.client.update_snapshot_metadata(self.snapshot_id, {})
-        super(SnapshotMetadataTest, self).tearDown()
+        super(SnapshotV2MetadataTestJSON, self).tearDown()
 
     @test.attr(type='gate')
     def test_create_get_delete_snapshot_metadata(self):
@@ -100,5 +99,13 @@
         self.assertEqual(expect, body)
 
 
-class SnapshotMetadataTestXML(SnapshotMetadataTest):
+class SnapshotV2MetadataTestXML(SnapshotV2MetadataTestJSON):
+    _interface = "xml"
+
+
+class SnapshotV1MetadataTestJSON(SnapshotV2MetadataTestJSON):
+    _api_version = 1
+
+
+class SnapshotV1MetadataTestXML(SnapshotV1MetadataTestJSON):
     _interface = "xml"
diff --git a/tempest/api/volume/test_volumes_snapshots.py b/tempest/api/volume/test_volumes_snapshots.py
index 7db1ef1..8390f03 100644
--- a/tempest/api/volume/test_volumes_snapshots.py
+++ b/tempest/api/volume/test_volumes_snapshots.py
@@ -20,21 +20,19 @@
 CONF = config.CONF
 
 
-class VolumesSnapshotTest(base.BaseVolumeV1Test):
-    _interface = "json"
+class VolumesV2SnapshotTestJSON(base.BaseVolumeTest):
 
     @classmethod
     @test.safe_setup
     def setUpClass(cls):
-        super(VolumesSnapshotTest, cls).setUpClass()
+        super(VolumesV2SnapshotTestJSON, cls).setUpClass()
         cls.volume_origin = cls.create_volume()
 
         if not CONF.volume_feature_enabled.snapshot:
             raise cls.skipException("Cinder volume snapshots are disabled")
 
-    @classmethod
-    def tearDownClass(cls):
-        super(VolumesSnapshotTest, cls).tearDownClass()
+        cls.name_field = cls.special_fields['name_field']
+        cls.descrip_field = cls.special_fields['descrip_field']
 
     def _detach(self, volume_id):
         """Detach volume."""
@@ -90,8 +88,8 @@
     def test_snapshot_create_get_list_update_delete(self):
         # Create a snapshot
         s_name = data_utils.rand_name('snap')
-        snapshot = self.create_snapshot(self.volume_origin['id'],
-                                        display_name=s_name)
+        params = {self.name_field: s_name}
+        snapshot = self.create_snapshot(self.volume_origin['id'], **params)
 
         # Get the snap and check for some of its details
         _, snap_get = self.snapshots_client.get_snapshot(snapshot['id'])
@@ -100,26 +98,26 @@
                          "Referred volume origin mismatch")
 
         # Compare also with the output from the list action
-        tracking_data = (snapshot['id'], snapshot['display_name'])
+        tracking_data = (snapshot['id'], snapshot[self.name_field])
         _, snaps_list = self.snapshots_client.list_snapshots()
-        snaps_data = [(f['id'], f['display_name']) for f in snaps_list]
+        snaps_data = [(f['id'], f[self.name_field]) for f in snaps_list]
         self.assertIn(tracking_data, snaps_data)
 
         # Updates snapshot with new values
         new_s_name = data_utils.rand_name('new-snap')
         new_desc = 'This is the new description of snapshot.'
+        params = {self.name_field: new_s_name,
+                  self.descrip_field: new_desc}
         _, update_snapshot = \
-            self.snapshots_client.update_snapshot(snapshot['id'],
-                                                  display_name=new_s_name,
-                                                  display_description=new_desc)
+            self.snapshots_client.update_snapshot(snapshot['id'], **params)
         # Assert response body for update_snapshot method
-        self.assertEqual(new_s_name, update_snapshot['display_name'])
-        self.assertEqual(new_desc, update_snapshot['display_description'])
+        self.assertEqual(new_s_name, update_snapshot[self.name_field])
+        self.assertEqual(new_desc, update_snapshot[self.descrip_field])
         # Assert response body for get_snapshot method
         _, updated_snapshot = \
             self.snapshots_client.get_snapshot(snapshot['id'])
-        self.assertEqual(new_s_name, updated_snapshot['display_name'])
-        self.assertEqual(new_desc, updated_snapshot['display_description'])
+        self.assertEqual(new_s_name, updated_snapshot[self.name_field])
+        self.assertEqual(new_desc, updated_snapshot[self.descrip_field])
 
         # Delete the snapshot
         self.snapshots_client.delete_snapshot(snapshot['id'])
@@ -131,11 +129,11 @@
         """list snapshots with params."""
         # Create a snapshot
         display_name = data_utils.rand_name('snap')
-        snapshot = self.create_snapshot(self.volume_origin['id'],
-                                        display_name=display_name)
+        params = {self.name_field: display_name}
+        snapshot = self.create_snapshot(self.volume_origin['id'], **params)
 
         # Verify list snapshots by display_name filter
-        params = {'display_name': snapshot['display_name']}
+        params = {self.name_field: snapshot[self.name_field]}
         self._list_by_param_values_and_assert(params)
 
         # Verify list snapshots by status filter
@@ -144,7 +142,7 @@
 
         # Verify list snapshots by status and display name filter
         params = {'status': 'available',
-                  'display_name': snapshot['display_name']}
+                  self.name_field: snapshot[self.name_field]}
         self._list_by_param_values_and_assert(params)
 
     @test.attr(type='gate')
@@ -152,18 +150,18 @@
         """list snapshot details with params."""
         # Create a snapshot
         display_name = data_utils.rand_name('snap')
-        snapshot = self.create_snapshot(self.volume_origin['id'],
-                                        display_name=display_name)
+        params = {self.name_field: display_name}
+        snapshot = self.create_snapshot(self.volume_origin['id'], **params)
 
         # Verify list snapshot details by display_name filter
-        params = {'display_name': snapshot['display_name']}
+        params = {self.name_field: snapshot[self.name_field]}
         self._list_by_param_values_and_assert(params, with_detail=True)
         # Verify list snapshot details by status filter
         params = {'status': 'available'}
         self._list_by_param_values_and_assert(params, with_detail=True)
         # Verify list snapshot details by status and display name filter
         params = {'status': 'available',
-                  'display_name': snapshot['display_name']}
+                  self.name_field: snapshot[self.name_field]}
         self._list_by_param_values_and_assert(params, with_detail=True)
 
     @test.attr(type='gate')
@@ -181,5 +179,13 @@
         self.clear_snapshots()
 
 
-class VolumesSnapshotTestXML(VolumesSnapshotTest):
+class VolumesV2SnapshotTestXML(VolumesV2SnapshotTestJSON):
+    _interface = "xml"
+
+
+class VolumesV1SnapshotTestJSON(VolumesV2SnapshotTestJSON):
+    _api_version = 1
+
+
+class VolumesV1SnapshotTestXML(VolumesV1SnapshotTestJSON):
     _interface = "xml"
diff --git a/tempest/api/volume/test_volumes_snapshots_negative.py b/tempest/api/volume/test_volumes_snapshots_negative.py
index 61aa307..ddecda8 100644
--- a/tempest/api/volume/test_volumes_snapshots_negative.py
+++ b/tempest/api/volume/test_volumes_snapshots_negative.py
@@ -21,12 +21,11 @@
 CONF = config.CONF
 
 
-class VolumesSnapshotNegativeTest(base.BaseVolumeV1Test):
-    _interface = "json"
+class VolumesV2SnapshotNegativeTestJSON(base.BaseVolumeTest):
 
     @classmethod
     def setUpClass(cls):
-        super(VolumesSnapshotNegativeTest, cls).setUpClass()
+        super(VolumesV2SnapshotNegativeTestJSON, cls).setUpClass()
 
         if not CONF.volume_feature_enabled.snapshot:
             raise cls.skipException("Cinder volume snapshots are disabled")
@@ -48,5 +47,13 @@
                           None, display_name=s_name)
 
 
-class VolumesSnapshotNegativeTestXML(VolumesSnapshotNegativeTest):
+class VolumesV2SnapshotNegativeTestXML(VolumesV2SnapshotNegativeTestJSON):
+    _interface = "xml"
+
+
+class VolumesV1SnapshotNegativeTestJSON(VolumesV2SnapshotNegativeTestJSON):
+    _api_version = 1
+
+
+class VolumesV1SnapshotNegativeTestXML(VolumesV1SnapshotNegativeTestJSON):
     _interface = "xml"
diff --git a/tempest/clients.py b/tempest/clients.py
index eab496e..fa7a193 100644
--- a/tempest/clients.py
+++ b/tempest/clients.py
@@ -188,11 +188,15 @@
 from tempest.services.volume.v2.json.extensions_client import \
     ExtensionsV2ClientJSON as VolumeV2ExtensionClientJSON
 from tempest.services.volume.v2.json.qos_client import QosSpecsV2ClientJSON
+from tempest.services.volume.v2.json.snapshots_client import \
+    SnapshotsV2ClientJSON
 from tempest.services.volume.v2.json.volumes_client import VolumesV2ClientJSON
 from tempest.services.volume.v2.xml.availability_zone_client import \
     VolumeV2AvailabilityZoneClientXML
 from tempest.services.volume.v2.xml.extensions_client import \
     ExtensionsV2ClientXML as VolumeV2ExtensionClientXML
+from tempest.services.volume.v2.xml.snapshots_client import \
+    SnapshotsV2ClientXML
 from tempest.services.volume.v2.xml.volumes_client import VolumesV2ClientXML
 from tempest.services.volume.xml.admin.volume_hosts_client import \
     VolumeHostsClientXML
@@ -245,6 +249,7 @@
                 self.auth_provider)
             self.backups_client = BackupsClientXML(self.auth_provider)
             self.snapshots_client = SnapshotsClientXML(self.auth_provider)
+            self.snapshots_v2_client = SnapshotsV2ClientXML(self.auth_provider)
             self.volumes_client = VolumesClientXML(self.auth_provider)
             self.volumes_v2_client = VolumesV2ClientXML(self.auth_provider)
             self.volume_types_client = VolumeTypesClientXML(
@@ -324,6 +329,8 @@
                 self.auth_provider)
             self.backups_client = BackupsClientJSON(self.auth_provider)
             self.snapshots_client = SnapshotsClientJSON(self.auth_provider)
+            self.snapshots_v2_client = SnapshotsV2ClientJSON(
+                self.auth_provider)
             self.volumes_client = VolumesClientJSON(self.auth_provider)
             self.volumes_v2_client = VolumesV2ClientJSON(self.auth_provider)
             self.volume_types_client = VolumeTypesClientJSON(
diff --git a/tempest/services/volume/json/snapshots_client.py b/tempest/services/volume/json/snapshots_client.py
index f50ba2f..1f8065b 100644
--- a/tempest/services/volume/json/snapshots_client.py
+++ b/tempest/services/volume/json/snapshots_client.py
@@ -24,15 +24,16 @@
 LOG = logging.getLogger(__name__)
 
 
-class SnapshotsClientJSON(rest_client.RestClient):
-    """Client class to send CRUD Volume API requests."""
+class BaseSnapshotsClientJSON(rest_client.RestClient):
+    """Base Client class to send CRUD Volume API requests."""
 
     def __init__(self, auth_provider):
-        super(SnapshotsClientJSON, self).__init__(auth_provider)
+        super(BaseSnapshotsClientJSON, self).__init__(auth_provider)
 
         self.service = CONF.volume.catalog_type
         self.build_interval = CONF.volume.build_interval
         self.build_timeout = CONF.volume.build_timeout
+        self.create_resp = 200
 
     def list_snapshots(self, params=None):
         """List all the snapshot."""
@@ -77,7 +78,7 @@
         post_body = json.dumps({'snapshot': post_body})
         resp, body = self.post('snapshots', post_body)
         body = json.loads(body)
-        self.expected_success(200, resp.status)
+        self.expected_success(self.create_resp, resp.status)
         return resp, body['snapshot']
 
     def update_snapshot(self, snapshot_id, **kwargs):
@@ -203,3 +204,7 @@
         resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body)
         self.expected_success(202, resp.status)
         return resp, body
+
+
+class SnapshotsClientJSON(BaseSnapshotsClientJSON):
+    """Client class to send CRUD Volume V1 API requests."""
diff --git a/tempest/services/volume/v2/json/snapshots_client.py b/tempest/services/volume/v2/json/snapshots_client.py
new file mode 100644
index 0000000..553176b
--- /dev/null
+++ b/tempest/services/volume/v2/json/snapshots_client.py
@@ -0,0 +1,23 @@
+#    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.services.volume.json import snapshots_client
+
+
+class SnapshotsV2ClientJSON(snapshots_client.BaseSnapshotsClientJSON):
+    """Client class to send CRUD Volume V2 API requests."""
+
+    def __init__(self, auth_provider):
+        super(SnapshotsV2ClientJSON, self).__init__(auth_provider)
+
+        self.api_version = "v2"
+        self.create_resp = 202
diff --git a/tempest/services/volume/v2/xml/snapshots_client.py b/tempest/services/volume/v2/xml/snapshots_client.py
new file mode 100644
index 0000000..b29d86c
--- /dev/null
+++ b/tempest/services/volume/v2/xml/snapshots_client.py
@@ -0,0 +1,23 @@
+#    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.services.volume.xml import snapshots_client
+
+
+class SnapshotsV2ClientXML(snapshots_client.BaseSnapshotsClientXML):
+    """Client class to send CRUD Volume V2 API requests."""
+
+    def __init__(self, auth_provider):
+        super(SnapshotsV2ClientXML, self).__init__(auth_provider)
+
+        self.api_version = "v2"
+        self.create_resp = 202
diff --git a/tempest/services/volume/xml/snapshots_client.py b/tempest/services/volume/xml/snapshots_client.py
index 7636707..ce98eea 100644
--- a/tempest/services/volume/xml/snapshots_client.py
+++ b/tempest/services/volume/xml/snapshots_client.py
@@ -26,16 +26,17 @@
 LOG = logging.getLogger(__name__)
 
 
-class SnapshotsClientXML(rest_client.RestClient):
-    """Client class to send CRUD Volume API requests."""
+class BaseSnapshotsClientXML(rest_client.RestClient):
+    """Base Client class to send CRUD Volume API requests."""
     TYPE = "xml"
 
     def __init__(self, auth_provider):
-        super(SnapshotsClientXML, self).__init__(auth_provider)
+        super(BaseSnapshotsClientXML, self).__init__(auth_provider)
 
         self.service = CONF.volume.catalog_type
         self.build_interval = CONF.volume.build_interval
         self.build_timeout = CONF.volume.build_timeout
+        self.create_resp = 200
 
     def list_snapshots(self, params=None):
         """List all snapshot."""
@@ -90,7 +91,7 @@
         resp, body = self.post('snapshots',
                                str(common.Document(snapshot)))
         body = common.xml_to_json(etree.fromstring(body))
-        self.expected_success(200, resp.status)
+        self.expected_success(self.create_resp, resp.status)
         return resp, body
 
     def update_snapshot(self, snapshot_id, **kwargs):
@@ -243,3 +244,7 @@
             body = common.xml_to_json(etree.fromstring(body))
         self.expected_success(202, resp.status)
         return resp, body
+
+
+class SnapshotsClientXML(BaseSnapshotsClientXML):
+    """Client class to send CRUD Volume V1 API requests."""