Merge "Add snapshot instances admin APIs"
diff --git a/manila_tempest_tests/config.py b/manila_tempest_tests/config.py
index 4b394c6..55f2e15 100644
--- a/manila_tempest_tests/config.py
+++ b/manila_tempest_tests/config.py
@@ -34,7 +34,7 @@
                help="The minimum api microversion is configured to be the "
                     "value of the minimum microversion supported by Manila."),
     cfg.StrOpt("max_api_microversion",
-               default="2.18",
+               default="2.19",
                help="The maximum api microversion is configured to be the "
                     "value of the latest microversion supported by Manila."),
     cfg.StrOpt("region",
diff --git a/manila_tempest_tests/services/share/v2/json/shares_client.py b/manila_tempest_tests/services/share/v2/json/shares_client.py
index 3995161..46c0ce7 100755
--- a/manila_tempest_tests/services/share/v2/json/shares_client.py
+++ b/manila_tempest_tests/services/share/v2/json/shares_client.py
@@ -552,6 +552,68 @@
 
 ###############
 
+    def get_snapshot_instance(self, instance_id, version=LATEST_MICROVERSION):
+        resp, body = self.get("snapshot-instances/%s" % instance_id,
+                              version=version)
+        self.expected_success(200, resp.status)
+        return self._parse_resp(body)
+
+    def list_snapshot_instances(self, detail=False, snapshot_id=None,
+                                version=LATEST_MICROVERSION):
+        """Get list of share snapshot instances."""
+        uri = "snapshot-instances%s" % ('/detail' if detail else '')
+        if snapshot_id is not None:
+            uri += '?snapshot_id=%s' % snapshot_id
+        resp, body = self.get(uri, version=version)
+        self.expected_success(200, resp.status)
+        return self._parse_resp(body)
+
+    def reset_snapshot_instance_status(self, instance_id,
+                                       status=constants.STATUS_AVAILABLE,
+                                       version=LATEST_MICROVERSION):
+        """Reset the status."""
+        uri = 'snapshot-instances/%s/action' % instance_id
+        post_body = {
+            'reset_status': {
+                'status': status
+            }
+        }
+        body = json.dumps(post_body)
+        resp, body = self.post(uri, body, extra_headers=True, version=version)
+        self.expected_success(202, resp.status)
+        return self._parse_resp(body)
+
+    def wait_for_snapshot_instance_status(self, instance_id, expected_status):
+        """Waits for a snapshot instance status to reach a given status."""
+        body = self.get_snapshot_instance(instance_id)
+        instance_status = body['status']
+        start = int(time.time())
+
+        while instance_status != expected_status:
+            time.sleep(self.build_interval)
+            body = self.get_snapshot_instance(instance_id)
+            instance_status = body['status']
+            if instance_status == expected_status:
+                return
+            if 'error' in instance_status:
+                raise share_exceptions.SnapshotInstanceBuildErrorException(
+                    id=instance_id)
+
+            if int(time.time()) - start >= self.build_timeout:
+                message = ('The status of snapshot instance %(id)s failed to '
+                           'reach %(expected_status)s status within the '
+                           'required time (%(time)ss). Current '
+                           'status: %(current_status)s.' %
+                           {
+                               'expected_status': expected_status,
+                               'time': self.build_timeout,
+                               'id': instance_id,
+                               'current_status': instance_status,
+                           })
+                raise exceptions.TimeoutException(message)
+
+###############
+
     def _get_access_action_name(self, version, action):
         if utils.is_microversion_gt(version, "2.6"):
             return action.split('os-')[-1]
diff --git a/manila_tempest_tests/share_exceptions.py b/manila_tempest_tests/share_exceptions.py
index 3a11531..9b84d02 100644
--- a/manila_tempest_tests/share_exceptions.py
+++ b/manila_tempest_tests/share_exceptions.py
@@ -37,6 +37,11 @@
     message = "Snapshot %(snapshot_id)s failed to build and is in ERROR status"
 
 
+class SnapshotInstanceBuildErrorException(exceptions.TempestException):
+    message = ("Snapshot instance %(id)s failed to build and is in "
+               "ERROR status.")
+
+
 class CGSnapshotBuildErrorException(exceptions.TempestException):
     message = ("CGSnapshot %(cgsnapshot_id)s failed to build and is in ERROR "
                "status")
diff --git a/manila_tempest_tests/tests/api/admin/test_share_snapshot_instances.py b/manila_tempest_tests/tests/api/admin/test_share_snapshot_instances.py
new file mode 100644
index 0000000..68f5661
--- /dev/null
+++ b/manila_tempest_tests/tests/api/admin/test_share_snapshot_instances.py
@@ -0,0 +1,121 @@
+# Copyright 2016 Huawei
+# 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.
+
+import ddt
+from tempest import config
+from tempest import test
+import testtools
+
+from manila_tempest_tests.tests.api import base
+
+CONF = config.CONF
+
+
+@testtools.skipUnless(CONF.share.run_snapshot_tests,
+                      'Snapshot tests are disabled.')
+@base.skip_if_microversion_lt("2.19")
+@ddt.ddt
+class ShareSnapshotInstancesTest(base.BaseSharesAdminTest):
+
+    @classmethod
+    def resource_setup(cls):
+        super(ShareSnapshotInstancesTest, cls).resource_setup()
+        cls.share = cls.create_share()
+        snap = cls.create_snapshot_wait_for_active(cls.share["id"])
+        cls.snapshot = cls.shares_v2_client.get_snapshot(snap['id'])
+
+    @ddt.data(True, False)
+    @test.attr(type=[base.TAG_POSITIVE, base.TAG_API_WITH_BACKEND])
+    def test_list_snapshot_instances_by_snapshot(self, detail):
+        """Test that we get only the 1 snapshot instance from snapshot."""
+        snapshot_instances = self.shares_v2_client.list_snapshot_instances(
+            detail=detail, snapshot_id=self.snapshot['id'])
+
+        expected_keys = ['id', 'snapshot_id', 'status']
+
+        if detail:
+            extra_detail_keys = ['provider_location', 'share_id',
+                                 'share_instance_id', 'created_at',
+                                 'updated_at', 'progress']
+            expected_keys.extend(extra_detail_keys)
+
+        si_num = len(snapshot_instances)
+        self.assertEqual(1, si_num,
+                         'Incorrect amount of snapshot instances found; '
+                         'expected 1, found %s.' % si_num)
+
+        si = snapshot_instances[0]
+        self.assertEqual(self.snapshot['id'], si['snapshot_id'],
+                         'Snapshot instance %s has incorrect snapshot id;'
+                         ' expected %s, got %s.' % (si['id'],
+                                                    self.snapshot['id'],
+                                                    si['snapshot_id']))
+        if detail:
+            self.assertEqual(self.snapshot['share_id'], si['share_id'])
+
+        for key in si:
+            self.assertIn(key, expected_keys)
+        self.assertEqual(len(expected_keys), len(si))
+
+    @test.attr(type=[base.TAG_POSITIVE, base.TAG_API_WITH_BACKEND])
+    def test_list_snapshot_instances(self):
+        """Test that we get at least the snapshot instance."""
+        snapshot_instances = self.shares_v2_client.list_snapshot_instances()
+
+        snapshot_ids = [si['snapshot_id'] for si in snapshot_instances]
+
+        msg = ('Snapshot instance for snapshot %s was not found.' %
+               self.snapshot['id'])
+        self.assertIn(self.snapshot['id'], snapshot_ids, msg)
+
+    @test.attr(type=[base.TAG_POSITIVE, base.TAG_API_WITH_BACKEND])
+    def test_get_snapshot_instance(self):
+        instances = self.shares_v2_client.list_snapshot_instances(
+            snapshot_id=self.snapshot['id'])
+        instance_detail = self.shares_v2_client.get_snapshot_instance(
+            instance_id=instances[0]['id'])
+
+        expected_keys = (
+            'id', 'created_at', 'updated_at', 'progress', 'provider_location',
+            'share_id', 'share_instance_id', 'snapshot_id', 'status',
+        )
+
+        for key in instance_detail:
+            self.assertIn(key, expected_keys)
+        self.assertEqual(len(expected_keys), len(instance_detail))
+        self.assertEqual(self.snapshot['id'], instance_detail['snapshot_id'])
+        self.assertEqual(self.snapshot['share_id'],
+                         instance_detail['share_id'])
+        self.assertEqual(self.snapshot['provider_location'],
+                         instance_detail['provider_location'])
+
+    @test.attr(type=[base.TAG_POSITIVE, base.TAG_API_WITH_BACKEND])
+    def test_reset_snapshot_instance_status_and_delete(self):
+        """Test resetting a snapshot instance's status attribute."""
+        snapshot = self.create_snapshot_wait_for_active(self.share["id"])
+
+        snapshot_instances = self.shares_v2_client.list_snapshot_instances(
+            snapshot_id=snapshot['id'])
+
+        sii = snapshot_instances[0]['id']
+
+        for status in ("error", "available"):
+            self.shares_v2_client.reset_snapshot_instance_status(
+                sii, status=status)
+            self.shares_v2_client.wait_for_snapshot_instance_status(
+                sii, expected_status=status)
+        self.shares_v2_client.delete_snapshot(snapshot['id'])
+        self.shares_v2_client.wait_for_resource_deletion(
+            snapshot_id=snapshot['id'])
diff --git a/manila_tempest_tests/tests/api/admin/test_share_snapshot_instances_negative.py b/manila_tempest_tests/tests/api/admin/test_share_snapshot_instances_negative.py
new file mode 100644
index 0000000..b76481c
--- /dev/null
+++ b/manila_tempest_tests/tests/api/admin/test_share_snapshot_instances_negative.py
@@ -0,0 +1,88 @@
+# Copyright 2016 Huawei
+# 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 import config
+from tempest.lib import exceptions as lib_exc
+from tempest import test
+import testtools
+
+from manila_tempest_tests.tests.api import base
+
+CONF = config.CONF
+
+
+@testtools.skipUnless(CONF.share.run_snapshot_tests,
+                      'Snapshot tests are disabled.')
+@base.skip_if_microversion_lt("2.19")
+class SnapshotInstancesNegativeTest(base.BaseSharesMixedTest):
+
+    @classmethod
+    def resource_setup(cls):
+        super(SnapshotInstancesNegativeTest, cls).resource_setup()
+        cls.admin_client = cls.admin_shares_v2_client
+        cls.member_client = cls.shares_v2_client
+        cls.share = cls.create_share(client=cls.admin_client)
+        cls.snapshot = cls.create_snapshot_wait_for_active(
+            cls.share["id"], client=cls.admin_client)
+
+    @test.attr(type=[base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND])
+    def test_list_snapshot_instances_with_snapshot_by_non_admin(self):
+        self.assertRaises(
+            lib_exc.Forbidden,
+            self.member_client.list_snapshot_instances,
+            snapshot_id=self.snapshot['id'])
+
+    @test.attr(type=[base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND])
+    def test_get_snapshot_instance_by_non_admin(self):
+        instances = self.admin_client.list_snapshot_instances(
+            snapshot_id=self.snapshot['id'])
+        self.assertRaises(
+            lib_exc.Forbidden,
+            self.member_client.get_snapshot_instance,
+            instance_id=instances[0]['id'])
+
+    @test.attr(type=[base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND])
+    def test_reset_snapshot_instance_status_by_non_admin(self):
+        instances = self.admin_client.list_snapshot_instances(
+            snapshot_id=self.snapshot['id'])
+        self.assertRaises(
+            lib_exc.Forbidden,
+            self.member_client.reset_snapshot_instance_status,
+            instances[0]['id'],
+            'error')
+
+
+@testtools.skipUnless(CONF.share.run_snapshot_tests,
+                      'Snapshot tests are disabled.')
+@base.skip_if_microversion_lt("2.19")
+class SnapshotInstancesNegativeNoResourceTest(base.BaseSharesMixedTest):
+
+    @classmethod
+    def resource_setup(cls):
+        super(SnapshotInstancesNegativeNoResourceTest, cls).resource_setup()
+        cls.admin_client = cls.admin_shares_v2_client
+        cls.member_client = cls.shares_v2_client
+
+    @test.attr(type=[base.TAG_NEGATIVE, base.TAG_API])
+    def test_get_snapshot_instance_with_non_existent_instance(self):
+        self.assertRaises(lib_exc.NotFound,
+                          self.admin_client.get_snapshot_instance,
+                          instance_id="nonexistent_instance")
+
+    @test.attr(type=[base.TAG_NEGATIVE, base.TAG_API])
+    def test_list_snapshot_instances_by_non_admin(self):
+        self.assertRaises(
+            lib_exc.Forbidden,
+            self.member_client.list_snapshot_instances)