Merge "Add tests for snapshot_metadata"
diff --git a/tempest/api/volume/test_snapshot_metadata.py b/tempest/api/volume/test_snapshot_metadata.py
new file mode 100644
index 0000000..0326f3c
--- /dev/null
+++ b/tempest/api/volume/test_snapshot_metadata.py
@@ -0,0 +1,119 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 Huawei Technologies Co.,LTD
+# All Rights Reserved.
+#
+#    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.api.volume import base
+from tempest import test
+
+
+class SnapshotMetadataTest(base.BaseVolumeV1Test):
+    _interface = "json"
+
+    @classmethod
+    def setUpClass(cls):
+        super(SnapshotMetadataTest, cls).setUpClass()
+        cls.client = cls.snapshots_client
+        # Create a volume
+        cls.volume = cls.create_volume()
+        # Create a snapshot
+        cls.snapshot = cls.create_snapshot(volume_id=cls.volume['id'])
+        cls.snapshot_id = cls.snapshot['id']
+
+    def tearDown(self):
+        # Update the metadata to {}
+        self.client.update_snapshot_metadata(self.snapshot_id, {})
+        super(SnapshotMetadataTest, self).tearDown()
+
+    @test.attr(type='gate')
+    def test_create_get_delete_snapshot_metadata(self):
+        # Create metadata for the snapshot
+        metadata = {"key1": "value1",
+                    "key2": "value2",
+                    "key3": "value3"}
+        expected = {"key2": "value2",
+                    "key3": "value3"}
+        resp, body = self.client.create_snapshot_metadata(self.snapshot_id,
+                                                          metadata)
+        self.assertEqual(200, resp.status)
+        # Get the metadata of the snapshot
+        resp, body = self.client.get_snapshot_metadata(self.snapshot_id)
+        self.assertEqual(200, resp.status)
+        self.assertEqual(metadata, body)
+        # Delete one item metadata of the snapshot
+        resp, body = self.client.delete_snapshot_metadata_item(
+            self.snapshot_id,
+            "key1")
+        self.assertEqual(200, resp.status)
+        resp, body = self.client.get_snapshot_metadata(self.snapshot_id)
+        self.assertEqual(expected, body)
+
+    @test.attr(type='gate')
+    def test_update_snapshot_metadata(self):
+        # Update metadata for the snapshot
+        metadata = {"key1": "value1",
+                    "key2": "value2",
+                    "key3": "value3"}
+        update = {"key3": "value3_update",
+                  "key4": "value4"}
+        # Create metadata for the snapshot
+        resp, body = self.client.create_snapshot_metadata(self.snapshot_id,
+                                                          metadata)
+        self.assertEqual(200, resp.status)
+        # Get the metadata of the snapshot
+        resp, body = self.client.get_snapshot_metadata(self.snapshot_id)
+        self.assertEqual(200, resp.status)
+        self.assertEqual(metadata, body)
+        # Update metadata item
+        resp, body = self.client.update_snapshot_metadata(
+            self.snapshot_id,
+            update)
+        self.assertEqual(200, resp.status)
+        # Get the metadata of the snapshot
+        resp, body = self.client.get_snapshot_metadata(self.snapshot_id)
+        self.assertEqual(200, resp.status)
+        self.assertEqual(update, body)
+
+    @test.attr(type='gate')
+    def test_update_snapshot_metadata_item(self):
+        # Update metadata item for the snapshot
+        metadata = {"key1": "value1",
+                    "key2": "value2",
+                    "key3": "value3"}
+        update_item = {"key3": "value3_update"}
+        expect = {"key1": "value1",
+                  "key2": "value2",
+                  "key3": "value3_update"}
+        # Create metadata for the snapshot
+        resp, body = self.client.create_snapshot_metadata(self.snapshot_id,
+                                                          metadata)
+        self.assertEqual(200, resp.status)
+        # Get the metadata of the snapshot
+        resp, body = self.client.get_snapshot_metadata(self.snapshot_id)
+        self.assertEqual(metadata, body)
+        # Update metadata item
+        resp, body = self.client.update_snapshot_metadata_item(
+            self.snapshot_id,
+            "key3",
+            update_item)
+        self.assertEqual(200, resp.status)
+        # Get the metadata of the snapshot
+        resp, body = self.client.get_snapshot_metadata(self.snapshot_id)
+        self.assertEqual(200, resp.status)
+        self.assertEqual(expect, body)
+
+
+class SnapshotMetadataTestXML(SnapshotMetadataTest):
+    _interface = "xml"
diff --git a/tempest/services/volume/json/snapshots_client.py b/tempest/services/volume/json/snapshots_client.py
index 9435122..c270de8 100644
--- a/tempest/services/volume/json/snapshots_client.py
+++ b/tempest/services/volume/json/snapshots_client.py
@@ -149,3 +149,40 @@
         url = 'snapshots/%s/action' % str(snapshot_id)
         resp, body = self.post(url, post_body, self.headers)
         return resp, body
+
+    def create_snapshot_metadata(self, snapshot_id, metadata):
+        """Create metadata for the snapshot."""
+        put_body = json.dumps({'metadata': metadata})
+        url = "snapshots/%s/metadata" % str(snapshot_id)
+        resp, body = self.post(url, put_body, self.headers)
+        body = json.loads(body)
+        return resp, body['metadata']
+
+    def get_snapshot_metadata(self, snapshot_id):
+        """Get metadata of the snapshot."""
+        url = "snapshots/%s/metadata" % str(snapshot_id)
+        resp, body = self.get(url, self.headers)
+        body = json.loads(body)
+        return resp, body['metadata']
+
+    def update_snapshot_metadata(self, snapshot_id, metadata):
+        """Update metadata for the snapshot."""
+        put_body = json.dumps({'metadata': metadata})
+        url = "snapshots/%s/metadata" % str(snapshot_id)
+        resp, body = self.put(url, put_body, self.headers)
+        body = json.loads(body)
+        return resp, body['metadata']
+
+    def update_snapshot_metadata_item(self, snapshot_id, id, meta_item):
+        """Update metadata item for the snapshot."""
+        put_body = json.dumps({'meta': meta_item})
+        url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
+        resp, body = self.put(url, put_body, self.headers)
+        body = json.loads(body)
+        return resp, body['meta']
+
+    def delete_snapshot_metadata_item(self, snapshot_id, id):
+        """Delete metadata item for the snapshot."""
+        url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
+        resp, body = self.delete(url, self.headers)
+        return resp, body
diff --git a/tempest/services/volume/xml/snapshots_client.py b/tempest/services/volume/xml/snapshots_client.py
index 5d59b07..3a70eab 100644
--- a/tempest/services/volume/xml/snapshots_client.py
+++ b/tempest/services/volume/xml/snapshots_client.py
@@ -22,6 +22,7 @@
 from tempest.openstack.common import log as logging
 from tempest.services.compute.xml.common import Document
 from tempest.services.compute.xml.common import Element
+from tempest.services.compute.xml.common import Text
 from tempest.services.compute.xml.common import xml_to_json
 from tempest.services.compute.xml.common import XMLNS_11
 
@@ -170,3 +171,57 @@
         if body:
             body = xml_to_json(etree.fromstring(body))
         return resp, body
+
+    def _metadata_body(self, meta):
+        post_body = Element('metadata')
+        for k, v in meta.items():
+            data = Element('meta', key=k)
+            data.append(Text(v))
+            post_body.append(data)
+        return post_body
+
+    def _parse_key_value(self, node):
+        """Parse <foo key='key'>value</foo> data into {'key': 'value'}."""
+        data = {}
+        for node in node.getchildren():
+            data[node.get('key')] = node.text
+        return data
+
+    def create_snapshot_metadata(self, snapshot_id, metadata):
+        """Create metadata for the snapshot."""
+        post_body = self._metadata_body(metadata)
+        resp, body = self.post('snapshots/%s/metadata' % snapshot_id,
+                               str(Document(post_body)),
+                               self.headers)
+        body = self._parse_key_value(etree.fromstring(body))
+        return resp, body
+
+    def get_snapshot_metadata(self, snapshot_id):
+        """Get metadata of the snapshot."""
+        url = "snapshots/%s/metadata" % str(snapshot_id)
+        resp, body = self.get(url, self.headers)
+        body = self._parse_key_value(etree.fromstring(body))
+        return resp, body
+
+    def update_snapshot_metadata(self, snapshot_id, metadata):
+        """Update metadata for the snapshot."""
+        put_body = self._metadata_body(metadata)
+        url = "snapshots/%s/metadata" % str(snapshot_id)
+        resp, body = self.put(url, str(Document(put_body)), self.headers)
+        body = self._parse_key_value(etree.fromstring(body))
+        return resp, body
+
+    def update_snapshot_metadata_item(self, snapshot_id, id, meta_item):
+        """Update metadata item for the snapshot."""
+        for k, v in meta_item.items():
+            put_body = Element('meta', key=k)
+            put_body.append(Text(v))
+        url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
+        resp, body = self.put(url, str(Document(put_body)), self.headers)
+        body = xml_to_json(etree.fromstring(body))
+        return resp, body
+
+    def delete_snapshot_metadata_item(self, snapshot_id, id):
+        """Delete metadata item for the snapshot."""
+        url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
+        return self.delete(url)