Merge "Add exception handling doc"
diff --git a/tempest/api/compute/admin/test_hosts.py b/tempest/api/compute/admin/test_hosts.py
new file mode 100644
index 0000000..a47e6c9
--- /dev/null
+++ b/tempest/api/compute/admin/test_hosts.py
@@ -0,0 +1,76 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 IBM Corp.
+#
+#    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 import exceptions
+from tempest.test import attr
+
+
+class HostsAdminTestJSON(base.BaseComputeAdminTest):
+
+    """
+    Tests hosts API using admin privileges.
+    """
+
+    _interface = 'json'
+
+    @classmethod
+    def setUpClass(cls):
+        super(HostsAdminTestJSON, cls).setUpClass()
+        cls.client = cls.os_adm.hosts_client
+        cls.non_admin_client = cls.os.hosts_client
+
+    @attr(type=['positive', 'gate'])
+    def test_list_hosts(self):
+        resp, hosts = self.client.list_hosts()
+        self.assertEqual(200, resp.status)
+        self.assertTrue(len(hosts) >= 2)
+
+    @attr(type='positive')
+    def test_list_hosts_with_zone(self):
+        resp, hosts = self.client.list_hosts()
+        host = hosts[0]
+        zone_name = host['zone']
+        params = {'zone': zone_name}
+        resp, hosts = self.client.list_hosts(params)
+        self.assertEqual(200, resp.status)
+        self.assertTrue(len(hosts) >= 1)
+        self.assertTrue(host in 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')
+    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
+        params = {'zone': ''}
+        resp, hosts = self.client.list_hosts(params)
+        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)
+
+
+class HostsAdminTestXML(HostsAdminTestJSON):
+    _interface = 'xml'
diff --git a/tempest/services/compute/json/hosts_client.py b/tempest/services/compute/json/hosts_client.py
index dc3c524..8093d19 100644
--- a/tempest/services/compute/json/hosts_client.py
+++ b/tempest/services/compute/json/hosts_client.py
@@ -1,4 +1,21 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+#
+# Copyright 2013 IBM Corp.
+#
+#    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 json
+import urllib
 
 from tempest.common.rest_client import RestClient
 
@@ -10,10 +27,13 @@
                                               auth_url, tenant_name)
         self.service = self.config.compute.catalog_type
 
-    def list_hosts(self):
+    def list_hosts(self, params=None):
         """Lists all hosts."""
 
         url = 'os-hosts'
+        if params:
+            url += '?%s' % urllib.urlencode(params)
+
         resp, body = self.get(url)
         body = json.loads(body)
         return resp, body['hosts']
diff --git a/tempest/services/compute/xml/hosts_client.py b/tempest/services/compute/xml/hosts_client.py
new file mode 100644
index 0000000..70aeb48
--- /dev/null
+++ b/tempest/services/compute/xml/hosts_client.py
@@ -0,0 +1,41 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+#
+# Copyright 2013 IBM Corp.
+#
+#    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 urllib
+
+from lxml import etree
+from tempest.common.rest_client import RestClientXML
+from tempest.services.compute.xml.common import xml_to_json
+
+
+class HostsClientXML(RestClientXML):
+
+    def __init__(self, config, username, password, auth_url, tenant_name=None):
+        super(HostsClientXML, self).__init__(config, username, password,
+                                             auth_url, tenant_name)
+        self.service = self.config.compute.catalog_type
+
+    def list_hosts(self, params=None):
+        """Lists all hosts."""
+
+        url = 'os-hosts'
+        if params:
+            url += '?%s' % urllib.urlencode(params)
+
+        resp, body = self.get(url, self.headers)
+        node = etree.fromstring(body)
+        body = [xml_to_json(x) for x in node.getchildren()]
+        return resp, body
diff --git a/test-requirements.txt b/test-requirements.txt
index 3912695..2185997 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -3,7 +3,6 @@
 pyflakes==0.7.2
 flake8==2.0
 hacking>=0.5.3,<0.6
-psycopg2
 # needed for doc build
 sphinx>=1.1.2