Merge "add some tests for host and seperate negative ones"
diff --git a/tempest/api/compute/admin/test_hosts.py b/tempest/api/compute/admin/test_hosts.py
index aa769ba..8e451a0 100644
--- a/tempest/api/compute/admin/test_hosts.py
+++ b/tempest/api/compute/admin/test_hosts.py
@@ -16,8 +16,6 @@
 
 from tempest.api.compute import base
 from tempest.common import tempest_fixtures as fixtures
-from tempest.common.utils.data_utils import rand_name
-from tempest import exceptions
 from tempest.test import attr
 
 
@@ -33,7 +31,13 @@
     def setUpClass(cls):
         super(HostsAdminTestJSON, cls).setUpClass()
         cls.client = cls.os_adm.hosts_client
-        cls.non_admin_client = cls.os.hosts_client
+
+    def _get_host_name(self):
+        resp, hosts = self.client.list_hosts()
+        self.assertEqual(200, resp.status)
+        self.assertTrue(len(hosts) >= 1)
+        hostname = hosts[0]['host_name']
+        return hostname
 
     @attr(type='gate')
     def test_list_hosts(self):
@@ -53,14 +57,7 @@
         self.assertTrue(len(hosts) >= 1)
         self.assertIn(host, hosts)
 
-    @attr(type='negative')
-    def test_list_hosts_with_non_existent_zone(self):
-        params = {'zone': 'xxx'}
-        resp, hosts = self.client.list_hosts(params)
-        self.assertEqual(0, len(hosts))
-        self.assertEqual(200, resp.status)
-
-    @attr(type='negative')
+    @attr(type='gate')
     def test_list_hosts_with_a_blank_zone(self):
         # If send the request with a blank zone, the request will be successful
         # and it will return all the hosts list
@@ -69,17 +66,18 @@
         self.assertNotEqual(0, len(hosts))
         self.assertEqual(200, resp.status)
 
-    @attr(type=['negative', 'gate'])
-    def test_list_hosts_with_non_admin_user(self):
-        self.assertRaises(exceptions.Unauthorized,
-                          self.non_admin_client.list_hosts)
+    @attr(type='gate')
+    def test_list_hosts_with_nonexistent_zone(self):
+        # If send the request with a nonexistent zone, the request will be
+        # successful and no hosts will be retured
+        params = {'zone': 'xxx'}
+        resp, hosts = self.client.list_hosts(params)
+        self.assertEqual(0, len(hosts))
+        self.assertEqual(200, resp.status)
 
     @attr(type='gate')
     def test_show_host_detail(self):
-        resp, hosts = self.client.list_hosts()
-        self.assertEqual(200, resp.status)
-        self.assertTrue(len(hosts) >= 1)
-        hostname = hosts[0]['host_name']
+        hostname = self._get_host_name()
 
         resp, resources = self.client.show_host_detail(hostname)
         self.assertEqual(200, resp.status)
@@ -92,12 +90,6 @@
         self.assertIsNotNone(host_resource['project'])
         self.assertEqual(hostname, host_resource['host'])
 
-    @attr(type='negative')
-    def test_show_host_detail_with_nonexist_hostname(self):
-        hostname = rand_name('rand_hostname')
-        self.assertRaises(exceptions.NotFound,
-                          self.client.show_host_detail, hostname)
-
 
 class HostsAdminTestXML(HostsAdminTestJSON):
     _interface = 'xml'
diff --git a/tempest/api/compute/admin/test_hosts_negative.py b/tempest/api/compute/admin/test_hosts_negative.py
new file mode 100644
index 0000000..9d4c62b
--- /dev/null
+++ b/tempest/api/compute/admin/test_hosts_negative.py
@@ -0,0 +1,174 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 Huawei Technologies Co.,LTD.
+#
+#    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.compute import base
+from tempest.common.utils import data_utils
+from tempest import exceptions
+from tempest.test import attr
+
+
+class HostsAdminNegativeTestJSON(base.BaseComputeAdminTest):
+
+    """
+    Tests hosts API using admin privileges.
+    """
+
+    _interface = 'json'
+
+    @classmethod
+    def setUpClass(cls):
+        super(HostsAdminNegativeTestJSON, cls).setUpClass()
+        cls.client = cls.os_adm.hosts_client
+        cls.non_admin_client = cls.os.hosts_client
+
+    def _get_host_name(self):
+        resp, hosts = self.client.list_hosts()
+        self.assertEqual(200, resp.status)
+        self.assertTrue(len(hosts) >= 1)
+        hostname = hosts[0]['host_name']
+        return hostname
+
+    @attr(type=['negative', 'gate'])
+    def test_list_hosts_with_non_admin_user(self):
+        self.assertRaises(exceptions.Unauthorized,
+                          self.non_admin_client.list_hosts)
+
+    @attr(type=['negative', 'gate'])
+    def test_show_host_detail_with_nonexistent_hostname(self):
+        nonexitent_hostname = data_utils.rand_name('rand_hostname')
+        self.assertRaises(exceptions.NotFound,
+                          self.client.show_host_detail, nonexitent_hostname)
+
+    @attr(type=['negative', 'gate'])
+    def test_show_host_detail_with_non_admin_user(self):
+        hostname = self._get_host_name()
+
+        self.assertRaises(exceptions.Unauthorized,
+                          self.non_admin_client.show_host_detail,
+                          hostname)
+
+    @attr(type=['negative', 'gate'])
+    def test_update_host_with_non_admin_user(self):
+        hostname = self._get_host_name()
+
+        self.assertRaises(exceptions.Unauthorized,
+                          self.non_admin_client.update_host,
+                          hostname)
+
+    @attr(type=['negative', 'gate'])
+    def test_update_host_with_extra_param(self):
+        # only 'status' and 'maintenance_mode' are the valid params.
+        hostname = self._get_host_name()
+
+        self.assertRaises(exceptions.BadRequest,
+                          self.client.update_host,
+                          hostname,
+                          status='enable',
+                          maintenance_mode='enable',
+                          param='XXX')
+
+    @attr(type=['negative', 'gate'])
+    def test_update_host_with_invalid_status(self):
+        # 'status' can only be 'enable' or 'disable'
+        hostname = self._get_host_name()
+
+        self.assertRaises(exceptions.BadRequest,
+                          self.client.update_host,
+                          hostname,
+                          status='invalid',
+                          maintenance_mode='enable')
+
+    @attr(type=['negative', 'gate'])
+    def test_update_host_with_invalid_maintenance_mode(self):
+        # 'maintenance_mode' can only be 'enable' or 'disable'
+        hostname = self._get_host_name()
+
+        self.assertRaises(exceptions.BadRequest,
+                          self.client.update_host,
+                          hostname,
+                          status='enable',
+                          maintenance_mode='invalid')
+
+    @attr(type=['negative', 'gate'])
+    def test_update_host_without_param(self):
+        # 'status' or 'maintenance_mode' needed for host update
+        hostname = self._get_host_name()
+
+        self.assertRaises(exceptions.BadRequest,
+                          self.client.update_host,
+                          hostname)
+
+    @attr(type=['negative', 'gate'])
+    def test_update_nonexistent_host(self):
+        nonexitent_hostname = data_utils.rand_name('rand_hostname')
+
+        self.assertRaises(exceptions.NotFound,
+                          self.client.update_host,
+                          nonexitent_hostname,
+                          status='enable',
+                          maintenance_mode='enable')
+
+    @attr(type=['negative', 'gate'])
+    def test_startup_nonexistent_host(self):
+        nonexitent_hostname = data_utils.rand_name('rand_hostname')
+
+        self.assertRaises(exceptions.NotFound,
+                          self.client.startup_host,
+                          nonexitent_hostname)
+
+    @attr(type=['negative', 'gate'])
+    def test_startup_host_with_non_admin_user(self):
+        hostname = self._get_host_name()
+
+        self.assertRaises(exceptions.Unauthorized,
+                          self.non_admin_client.startup_host,
+                          hostname)
+
+    @attr(type=['negative', 'gate'])
+    def test_shutdown_nonexistent_host(self):
+        nonexitent_hostname = data_utils.rand_name('rand_hostname')
+
+        self.assertRaises(exceptions.NotFound,
+                          self.client.shutdown_host,
+                          nonexitent_hostname)
+
+    @attr(type=['negative', 'gate'])
+    def test_shutdown_host_with_non_admin_user(self):
+        hostname = self._get_host_name()
+
+        self.assertRaises(exceptions.Unauthorized,
+                          self.non_admin_client.shutdown_host,
+                          hostname)
+
+    @attr(type=['negative', 'gate'])
+    def test_reboot_nonexistent_host(self):
+        nonexitent_hostname = data_utils.rand_name('rand_hostname')
+
+        self.assertRaises(exceptions.NotFound,
+                          self.client.reboot_host,
+                          nonexitent_hostname)
+
+    @attr(type=['negative', 'gate'])
+    def test_reboot_host_with_non_admin_user(self):
+        hostname = self._get_host_name()
+
+        self.assertRaises(exceptions.Unauthorized,
+                          self.non_admin_client.reboot_host,
+                          hostname)
+
+
+class HostsAdminNegativeTestXML(HostsAdminNegativeTestJSON):
+    _interface = 'xml'
diff --git a/tempest/services/compute/json/hosts_client.py b/tempest/services/compute/json/hosts_client.py
index 30a3f7b..f51879d 100644
--- a/tempest/services/compute/json/hosts_client.py
+++ b/tempest/services/compute/json/hosts_client.py
@@ -44,3 +44,39 @@
         resp, body = self.get("os-hosts/%s" % str(hostname))
         body = json.loads(body)
         return resp, body['host']
+
+    def update_host(self, hostname, **kwargs):
+        """Update a host."""
+
+        request_body = {
+            'status': None,
+            'maintenance_mode': None,
+        }
+        request_body.update(**kwargs)
+        request_body = json.dumps(request_body)
+
+        resp, body = self.put("os-hosts/%s" % str(hostname), request_body,
+                              self.headers)
+        body = json.loads(body)
+        return resp, body
+
+    def startup_host(self, hostname):
+        """Startup a host."""
+
+        resp, body = self.get("os-hosts/%s/startup" % str(hostname))
+        body = json.loads(body)
+        return resp, body['host']
+
+    def shutdown_host(self, hostname):
+        """Shutdown a host."""
+
+        resp, body = self.get("os-hosts/%s/shutdown" % str(hostname))
+        body = json.loads(body)
+        return resp, body['host']
+
+    def reboot_host(self, hostname):
+        """reboot a host."""
+
+        resp, body = self.get("os-hosts/%s/reboot" % str(hostname))
+        body = json.loads(body)
+        return resp, body['host']
diff --git a/tempest/services/compute/xml/hosts_client.py b/tempest/services/compute/xml/hosts_client.py
index 9743143..f7d7b0a 100644
--- a/tempest/services/compute/xml/hosts_client.py
+++ b/tempest/services/compute/xml/hosts_client.py
@@ -18,6 +18,8 @@
 
 from lxml import etree
 from tempest.common.rest_client import RestClientXML
+from tempest.services.compute.xml.common import Document
+from tempest.services.compute.xml.common import Element
 from tempest.services.compute.xml.common import xml_to_json
 
 
@@ -47,3 +49,46 @@
         node = etree.fromstring(body)
         body = [xml_to_json(x) for x in node.getchildren()]
         return resp, body
+
+    def update_host(self, hostname, status=None, maintenance_mode=None,
+                    **kwargs):
+        """Update a host."""
+
+        request_body = Element(status=status,
+                               maintenance_mode=maintenance_mode)
+        if kwargs:
+            for k, v in kwargs.iteritem():
+                request_body.add_attr(k, v)
+        resp, body = self.put("os-hosts/%s" % str(hostname),
+                              str(Document(request_body)),
+                              self.headers)
+        node = etree.fromstring(body)
+        body = [xml_to_json(x) for x in node.getchildren()]
+        return resp, body
+
+    def startup_host(self, hostname):
+        """Startup a host."""
+
+        resp, body = self.get("os-hosts/%s/startup" % str(hostname),
+                              self.headers)
+        node = etree.fromstring(body)
+        body = [xml_to_json(x) for x in node.getchildren()]
+        return resp, body
+
+    def shutdown_host(self, hostname):
+        """Shutdown a host."""
+
+        resp, body = self.get("os-hosts/%s/shutdown" % str(hostname),
+                              self.headers)
+        node = etree.fromstring(body)
+        body = [xml_to_json(x) for x in node.getchildren()]
+        return resp, body
+
+    def reboot_host(self, hostname):
+        """Reboot a host."""
+
+        resp, body = self.get("os-hosts/%s/reboot" % str(hostname),
+                              self.headers)
+        node = etree.fromstring(body)
+        body = [xml_to_json(x) for x in node.getchildren()]
+        return resp, body