add some tests for hypervisor operation

For hypervisor operation, there are some APIs: list, detail, show, uptime,
search, servers, statistics. This patch will mainly add some negative
tests in dedicated 'negative' test file.

Change-Id: I637e0d0a8395ce52e4ed76029071238259957750
diff --git a/tempest/api/compute/admin/test_hypervisor.py b/tempest/api/compute/admin/test_hypervisor.py
index 5ca16f4..b7c3574 100644
--- a/tempest/api/compute/admin/test_hypervisor.py
+++ b/tempest/api/compute/admin/test_hypervisor.py
@@ -16,7 +16,6 @@
 #    under the License.
 
 from tempest.api.compute import base
-from tempest import exceptions
 from tempest.test import attr
 
 
@@ -32,7 +31,6 @@
     def setUpClass(cls):
         super(HypervisorAdminTestJSON, cls).setUpClass()
         cls.client = cls.os_adm.hypervisor_client
-        cls.non_adm_client = cls.hypervisor_client
 
     def _list_hypervisors(self):
         # List of hypervisors
@@ -93,19 +91,14 @@
         self.assertEqual(200, resp.status)
         self.assertTrue(len(uptime) > 0)
 
-    @attr(type=['negative', 'gate'])
-    def test_get_hypervisor_list_with_non_admin_user(self):
-        # List of hypervisor and available services with non admin user
-        self.assertRaises(
-            exceptions.Unauthorized,
-            self.non_adm_client.get_hypervisor_list)
-
-    @attr(type=['negative', 'gate'])
-    def test_get_hypervisor_list_details_with_non_admin_user(self):
-        # List of hypervisor details and available services with non admin user
-        self.assertRaises(
-            exceptions.Unauthorized,
-            self.non_adm_client.get_hypervisor_list_details)
+    @attr(type='gate')
+    def test_search_hypervisor(self):
+        hypers = self._list_hypervisors()
+        self.assertTrue(len(hypers) > 0)
+        resp, hypers = self.client.search_hypervisor(
+            hypers[0]['hypervisor_hostname'])
+        self.assertEqual(200, resp.status)
+        self.assertTrue(len(hypers) > 0)
 
 
 class HypervisorAdminTestXML(HypervisorAdminTestJSON):
diff --git a/tempest/api/compute/admin/test_hypervisor_negative.py b/tempest/api/compute/admin/test_hypervisor_negative.py
new file mode 100644
index 0000000..69b8d9c
--- /dev/null
+++ b/tempest/api/compute/admin/test_hypervisor_negative.py
@@ -0,0 +1,144 @@
+# 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.
+
+import uuid
+
+from tempest.api.compute import base
+from tempest.common.utils import data_utils
+from tempest import exceptions
+from tempest.test import attr
+
+
+class HypervisorAdminNegativeTestJSON(base.BaseComputeAdminTest):
+
+    """
+    Tests Hypervisors API that require admin privileges
+    """
+
+    _interface = 'json'
+
+    @classmethod
+    def setUpClass(cls):
+        super(HypervisorAdminNegativeTestJSON, cls).setUpClass()
+        cls.client = cls.os_adm.hypervisor_client
+        cls.non_adm_client = cls.hypervisor_client
+
+    def _list_hypervisors(self):
+        # List of hypervisors
+        resp, hypers = self.client.get_hypervisor_list()
+        self.assertEqual(200, resp.status)
+        return hypers
+
+    @attr(type=['negative', 'gate'])
+    def test_show_nonexistent_hypervisor(self):
+        nonexistent_hypervisor_id = str(uuid.uuid4())
+
+        self.assertRaises(
+            exceptions.NotFound,
+            self.client.get_hypervisor_show_details,
+            nonexistent_hypervisor_id)
+
+    @attr(type=['negative', 'gate'])
+    def test_show_hypervisor_with_non_admin_user(self):
+        hypers = self._list_hypervisors()
+        self.assertTrue(len(hypers) > 0)
+
+        self.assertRaises(
+            exceptions.Unauthorized,
+            self.non_adm_client.get_hypervisor_show_details,
+            hypers[0]['id'])
+
+    @attr(type=['negative', 'gate'])
+    def test_show_servers_with_non_admin_user(self):
+        hypers = self._list_hypervisors()
+        self.assertTrue(len(hypers) > 0)
+
+        self.assertRaises(
+            exceptions.Unauthorized,
+            self.non_adm_client.get_hypervisor_servers,
+            hypers[0]['id'])
+
+    @attr(type=['negative', 'gate'])
+    def test_show_servers_with_nonexistent_hypervisor(self):
+        nonexistent_hypervisor_id = str(uuid.uuid4())
+
+        self.assertRaises(
+            exceptions.NotFound,
+            self.client.get_hypervisor_servers,
+            nonexistent_hypervisor_id)
+
+    @attr(type=['negative', 'gate'])
+    def test_get_hypervisor_stats_with_non_admin_user(self):
+        self.assertRaises(
+            exceptions.Unauthorized,
+            self.non_adm_client.get_hypervisor_stats)
+
+    @attr(type=['negative', 'gate'])
+    def test_get_nonexistent_hypervisor_uptime(self):
+        nonexistent_hypervisor_id = str(uuid.uuid4())
+
+        self.assertRaises(
+            exceptions.NotFound,
+            self.client.get_hypervisor_uptime,
+            nonexistent_hypervisor_id)
+
+    @attr(type=['negative', 'gate'])
+    def test_get_hypervisor_uptime_with_non_admin_user(self):
+        hypers = self._list_hypervisors()
+        self.assertTrue(len(hypers) > 0)
+
+        self.assertRaises(
+            exceptions.Unauthorized,
+            self.non_adm_client.get_hypervisor_uptime,
+            hypers[0]['id'])
+
+    @attr(type=['negative', 'gate'])
+    def test_get_hypervisor_list_with_non_admin_user(self):
+        # List of hypervisor and available services with non admin user
+        self.assertRaises(
+            exceptions.Unauthorized,
+            self.non_adm_client.get_hypervisor_list)
+
+    @attr(type=['negative', 'gate'])
+    def test_get_hypervisor_list_details_with_non_admin_user(self):
+        # List of hypervisor details and available services with non admin user
+        self.assertRaises(
+            exceptions.Unauthorized,
+            self.non_adm_client.get_hypervisor_list_details)
+
+    @attr(type=['negative', 'gate'])
+    def test_search_nonexistent_hypervisor(self):
+        nonexistent_hypervisor_name = data_utils.rand_name('test_hypervisor')
+
+        self.assertRaises(
+            exceptions.NotFound,
+            self.client.search_hypervisor,
+            nonexistent_hypervisor_name)
+
+    @attr(type=['negative', 'gate'])
+    def test_search_hypervisor_with_non_admin_user(self):
+        hypers = self._list_hypervisors()
+        self.assertTrue(len(hypers) > 0)
+
+        self.assertRaises(
+            exceptions.Unauthorized,
+            self.non_adm_client.search_hypervisor,
+            hypers[0]['hypervisor_hostname'])
+
+
+class HypervisorAdminNegativeTestXML(HypervisorAdminNegativeTestJSON):
+    _interface = 'xml'
diff --git a/tempest/services/compute/json/hypervisor_client.py b/tempest/services/compute/json/hypervisor_client.py
index e2e5c7b..fba5acb 100644
--- a/tempest/services/compute/json/hypervisor_client.py
+++ b/tempest/services/compute/json/hypervisor_client.py
@@ -63,3 +63,9 @@
         resp, body = self.get('os-hypervisors/%s/uptime' % hyper_id)
         body = json.loads(body)
         return resp, body['hypervisor']
+
+    def search_hypervisor(self, hyper_name):
+        """Search specified hypervisor."""
+        resp, body = self.get('os-hypervisors/%s/search' % hyper_name)
+        body = json.loads(body)
+        return resp, body['hypervisors']
diff --git a/tempest/services/compute/xml/hypervisor_client.py b/tempest/services/compute/xml/hypervisor_client.py
index 3c4f2b8..c10fed9 100644
--- a/tempest/services/compute/xml/hypervisor_client.py
+++ b/tempest/services/compute/xml/hypervisor_client.py
@@ -70,3 +70,10 @@
                               self.headers)
         uptime = xml_to_json(etree.fromstring(body))
         return resp, uptime
+
+    def search_hypervisor(self, hyper_name):
+        """Search specified hypervisor."""
+        resp, body = self.get('os-hypervisors/%s/search' % hyper_name,
+                              self.headers)
+        hypervisors = self._parse_array(etree.fromstring(body))
+        return resp, hypervisors