Merge "Add unit tests for rest_client"
diff --git a/tempest/api/compute/v3/admin/test_instance_usage_audit_log.py b/tempest/api/compute/v3/admin/test_instance_usage_audit_log.py
new file mode 100644
index 0000000..cea6e92
--- /dev/null
+++ b/tempest/api/compute/v3/admin/test_instance_usage_audit_log.py
@@ -0,0 +1,64 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 IBM Corporation
+# 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 datetime
+
+from tempest.api.compute import base
+from tempest.test import attr
+import urllib
+
+
+class InstanceUsageAuditLogTestJSON(base.BaseV2ComputeAdminTest):
+
+    _interface = 'json'
+
+    @classmethod
+    def setUpClass(cls):
+        super(InstanceUsageAuditLogTestJSON, cls).setUpClass()
+        cls.adm_client = cls.os_adm.instance_usages_audit_log_client
+
+    @attr(type='gate')
+    def test_list_instance_usage_audit_logs(self):
+        # list instance usage audit logs
+        resp, body = self.adm_client.list_instance_usage_audit_logs()
+        self.assertEqual(200, resp.status)
+        expected_items = ['total_errors', 'total_instances', 'log',
+                          'num_hosts_running', 'num_hosts_done',
+                          'num_hosts', 'hosts_not_run', 'overall_status',
+                          'period_ending', 'period_beginning',
+                          'num_hosts_not_run']
+        for item in expected_items:
+            self.assertIn(item, body)
+
+    @attr(type='gate')
+    def test_get_instance_usage_audit_log(self):
+        # Get instance usage audit log before specified time
+        now = datetime.datetime.now()
+        resp, body = self.adm_client.get_instance_usage_audit_log(
+            urllib.quote(now.strftime("%Y-%m-%d %H:%M:%S")))
+
+        self.assertEqual(200, resp.status)
+        expected_items = ['total_errors', 'total_instances', 'log',
+                          'num_hosts_running', 'num_hosts_done', 'num_hosts',
+                          'hosts_not_run', 'overall_status', 'period_ending',
+                          'period_beginning', 'num_hosts_not_run']
+        for item in expected_items:
+            self.assertIn(item, body)
+
+
+class InstanceUsageAuditLogTestXML(InstanceUsageAuditLogTestJSON):
+    _interface = 'xml'
diff --git a/tempest/api/compute/v3/admin/test_instance_usage_audit_log_negative.py b/tempest/api/compute/v3/admin/test_instance_usage_audit_log_negative.py
new file mode 100644
index 0000000..dcf41c5
--- /dev/null
+++ b/tempest/api/compute/v3/admin/test_instance_usage_audit_log_negative.py
@@ -0,0 +1,56 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 IBM Corporation
+# 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 datetime
+
+from tempest.api.compute import base
+from tempest import exceptions
+from tempest.test import attr
+import urllib
+
+
+class InstanceUsageAuditLogNegativeTestJSON(base.BaseV2ComputeAdminTest):
+
+    _interface = 'json'
+
+    @classmethod
+    def setUpClass(cls):
+        super(InstanceUsageAuditLogNegativeTestJSON, cls).setUpClass()
+        cls.adm_client = cls.os_adm.instance_usages_audit_log_client
+
+    @attr(type=['negative', 'gate'])
+    def test_instance_usage_audit_logs_with_nonadmin_user(self):
+        # the instance_usage_audit_logs API just can be accessed by admin user
+        self.assertRaises(exceptions.Unauthorized,
+                          self.instance_usages_audit_log_client.
+                          list_instance_usage_audit_logs)
+        now = datetime.datetime.now()
+        self.assertRaises(exceptions.Unauthorized,
+                          self.instance_usages_audit_log_client.
+                          get_instance_usage_audit_log,
+                          urllib.quote(now.strftime("%Y-%m-%d %H:%M:%S")))
+
+    @attr(type=['negative', 'gate'])
+    def test_get_instance_usage_audit_logs_with_invalid_time(self):
+        self.assertRaises(exceptions.BadRequest,
+                          self.adm_client.get_instance_usage_audit_log,
+                          "invalid_time")
+
+
+class InstanceUsageAuditLogNegativeTestXML(
+    InstanceUsageAuditLogNegativeTestJSON):
+    _interface = 'xml'
diff --git a/tempest/api/volume/base.py b/tempest/api/volume/base.py
index 465f570..ba99309 100644
--- a/tempest/api/volume/base.py
+++ b/tempest/api/volume/base.py
@@ -42,6 +42,7 @@
         cls.volumes_client = os.volumes_client
         cls.snapshots_client = os.snapshots_client
         cls.servers_client = os.servers_client
+        cls.volumes_extension_client = os.volumes_extension_client
         cls.image_ref = cls.config.compute.image_ref
         cls.flavor_ref = cls.config.compute.flavor_ref
         cls.build_interval = cls.config.volume.build_interval
diff --git a/tempest/api/volume/test_extensions.py b/tempest/api/volume/test_extensions.py
new file mode 100644
index 0000000..90988a2
--- /dev/null
+++ b/tempest/api/volume/test_extensions.py
@@ -0,0 +1,43 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 IBM Corp.
+# 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.test import attr
+
+
+class ExtensionsTestJSON(base.BaseVolumeTest):
+    _interface = 'json'
+
+    @attr(type='gate')
+    def test_list_extensions(self):
+        # List of all extensions
+        resp, extensions = self.volumes_extension_client.list_extensions()
+        self.assertEqual(200, resp.status)
+        if len(self.config.volume_feature_enabled.api_extensions) == 0:
+            raise self.skipException('There are not any extensions configured')
+        ext = self.config.volume_feature_enabled.api_extensions[0]
+        if ext == 'all':
+            self.assertIn('Hosts', map(lambda x: x['name'], extensions))
+        elif ext:
+            self.assertIn(ext, map(lambda x: x['name'], extensions))
+        else:
+            raise self.skipException('There are not any extensions configured')
+
+
+class ExtensionsTestXML(ExtensionsTestJSON):
+    _interface = 'xml'
diff --git a/tempest/clients.py b/tempest/clients.py
index 22ed825..e22b1ec 100644
--- a/tempest/clients.py
+++ b/tempest/clients.py
@@ -148,12 +148,16 @@
     VolumeHostsClientJSON
 from tempest.services.volume.json.admin.volume_types_client import \
     VolumeTypesClientJSON
+from tempest.services.volume.json.extensions_client import \
+    ExtensionsClientJSON as VolumeExtensionClientJSON
 from tempest.services.volume.json.snapshots_client import SnapshotsClientJSON
 from tempest.services.volume.json.volumes_client import VolumesClientJSON
 from tempest.services.volume.xml.admin.volume_hosts_client import \
     VolumeHostsClientXML
 from tempest.services.volume.xml.admin.volume_types_client import \
     VolumeTypesClientXML
+from tempest.services.volume.xml.extensions_client import \
+    ExtensionsClientXML as VolumeExtensionClientXML
 from tempest.services.volume.xml.snapshots_client import SnapshotsClientXML
 from tempest.services.volume.xml.volumes_client import VolumesClientXML
 
@@ -255,6 +259,8 @@
             self.instance_usages_audit_log_client = \
                 InstanceUsagesAuditLogClientXML(*client_args)
             self.volume_hosts_client = VolumeHostsClientXML(*client_args)
+            self.volumes_extension_client = VolumeExtensionClientXML(
+                *client_args)
 
             if client_args_v3_auth:
                 self.servers_client_v3_auth = ServersClientXML(
@@ -308,6 +314,8 @@
             self.instance_usages_audit_log_client = \
                 InstanceUsagesAuditLogClientJSON(*client_args)
             self.volume_hosts_client = VolumeHostsClientJSON(*client_args)
+            self.volumes_extension_client = VolumeExtensionClientJSON(
+                *client_args)
 
             if client_args_v3_auth:
                 self.servers_client_v3_auth = ServersClientJSON(
diff --git a/tempest/scenario/manager.py b/tempest/scenario/manager.py
index 9f97964..a3b51eb 100644
--- a/tempest/scenario/manager.py
+++ b/tempest/scenario/manager.py
@@ -207,7 +207,7 @@
         cls.isolated_creds = isolated_creds.IsolatedCreds(
             __name__, tempest_client=False)
 
-        username, tenant_name, password = cls.credentials()
+        username, password, tenant_name = cls.credentials()
 
         cls.manager = OfficialClientManager(username, password, tenant_name)
         cls.compute_client = cls.manager.compute_client
@@ -220,14 +220,27 @@
         cls.os_resources = []
 
     @classmethod
-    def credentials(cls):
+    def _get_credentials(cls, get_creds, prefix):
         if cls.config.compute.allow_tenant_isolation:
-            return cls.isolated_creds.get_primary_creds()
+            username, tenant_name, password = get_creds()
+        else:
+            username = getattr(cls.config.identity, prefix + 'username')
+            password = getattr(cls.config.identity, prefix + 'password')
+            tenant_name = getattr(cls.config.identity, prefix + 'tenant_name')
+        return username, password, tenant_name
 
-        username = cls.config.identity.username
-        password = cls.config.identity.password
-        tenant_name = cls.config.identity.tenant_name
-        return username, tenant_name, password
+    @classmethod
+    def credentials(cls):
+        return cls._get_credentials(cls.isolated_creds.get_primary_creds, '')
+
+    @classmethod
+    def alt_credentials(cls):
+        return cls._get_credentials(cls.isolated_creds.get_alt_creds, 'alt_')
+
+    @classmethod
+    def admin_credentials(cls):
+        return cls._get_credentials(cls.isolated_creds.get_admin_creds,
+                                    'admin_')
 
     @classmethod
     def tearDownClass(cls):
@@ -348,8 +361,8 @@
             # so case sensitive comparisons can really mess things
             # up.
             if new_status.lower() == error_status.lower():
-                message = "%s failed to get to expected status. \
-                          In %s state." % (thing, new_status)
+                message = ("%s failed to get to expected status. "
+                           "In %s state.") % (thing, new_status)
                 raise exceptions.BuildErrorException(message)
             elif new_status == expected_status and expected_status is not None:
                 return True  # All good.
@@ -360,8 +373,8 @@
             check_status,
             self.config.compute.build_timeout,
             self.config.compute.build_interval):
-            message = "Timed out waiting for thing %s \
-                      to become %s" % (thing_id, log_status)
+            message = ("Timed out waiting for thing %s "
+                       "to become %s") % (thing_id, log_status)
             raise exceptions.TimeoutException(message)
 
     def _create_loginable_secgroup_rule_nova(self, client=None,
diff --git a/tempest/scenario/test_cross_tenant_connectivity.py b/tempest/scenario/test_cross_tenant_connectivity.py
index ad2c271..faba987 100644
--- a/tempest/scenario/test_cross_tenant_connectivity.py
+++ b/tempest/scenario/test_cross_tenant_connectivity.py
@@ -143,10 +143,9 @@
     @classmethod
     def setUpClass(cls):
         super(TestNetworkCrossTenant, cls).setUpClass()
+        alt_creds = cls.alt_credentials()
         cls.alt_tenant_id = cls.manager._get_identity_client(
-            cls.config.identity.alt_username,
-            cls.config.identity.alt_password,
-            cls.config.identity.alt_tenant_name
+            *alt_creds
         ).tenant_id
         cls.check_preconditions()
         # TODO(mnewby) Consider looking up entities as needed instead
@@ -161,15 +160,11 @@
         cls.tenants = {}
         cls.demo_tenant = cls.TenantProperties(
             cls.tenant_id,
-            cls.config.identity.username,
-            cls.config.identity.password,
-            cls.config.identity.tenant_name
+            *cls.credentials()
         )
         cls.alt_tenant = cls.TenantProperties(
             cls.alt_tenant_id,
-            cls.config.identity.alt_username,
-            cls.config.identity.alt_password,
-            cls.config.identity.alt_tenant_name
+            *alt_creds
         )
         for tenant in [cls.demo_tenant, cls.alt_tenant]:
             cls.tenants[tenant.tenant_id] = tenant
diff --git a/tempest/services/compute/v3/json/instance_usage_audit_log_client.py b/tempest/services/compute/v3/json/instance_usage_audit_log_client.py
new file mode 100644
index 0000000..07ce1bb
--- /dev/null
+++ b/tempest/services/compute/v3/json/instance_usage_audit_log_client.py
@@ -0,0 +1,40 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 IBM Corporation
+# 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 json
+
+from tempest.common.rest_client import RestClient
+
+
+class InstanceUsagesAuditLogClientJSON(RestClient):
+
+    def __init__(self, config, username, password, auth_url, tenant_name=None):
+        super(InstanceUsagesAuditLogClientJSON, self).__init__(
+            config, username, password, auth_url, tenant_name)
+        self.service = self.config.compute.catalog_type
+
+    def list_instance_usage_audit_logs(self):
+        url = 'os-instance_usage_audit_log'
+        resp, body = self.get(url)
+        body = json.loads(body)
+        return resp, body["instance_usage_audit_logs"]
+
+    def get_instance_usage_audit_log(self, time_before):
+        url = 'os-instance_usage_audit_log/%s' % time_before
+        resp, body = self.get(url)
+        body = json.loads(body)
+        return resp, body["instance_usage_audit_log"]
diff --git a/tempest/services/compute/v3/xml/instance_usage_audit_log_client.py b/tempest/services/compute/v3/xml/instance_usage_audit_log_client.py
new file mode 100644
index 0000000..175997b
--- /dev/null
+++ b/tempest/services/compute/v3/xml/instance_usage_audit_log_client.py
@@ -0,0 +1,41 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 IBM Corporation
+# 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 lxml import etree
+
+from tempest.common.rest_client import RestClientXML
+from tempest.services.compute.xml.common import xml_to_json
+
+
+class InstanceUsagesAuditLogClientXML(RestClientXML):
+
+    def __init__(self, config, username, password, auth_url, tenant_name=None):
+        super(InstanceUsagesAuditLogClientXML, self).__init__(
+            config, username, password, auth_url, tenant_name)
+        self.service = self.config.compute.catalog_type
+
+    def list_instance_usage_audit_logs(self):
+        url = 'os-instance_usage_audit_log'
+        resp, body = self.get(url, self.headers)
+        instance_usage_audit_logs = xml_to_json(etree.fromstring(body))
+        return resp, instance_usage_audit_logs
+
+    def get_instance_usage_audit_log(self, time_before):
+        url = 'os-instance_usage_audit_log/%s' % time_before
+        resp, body = self.get(url, self.headers)
+        instance_usage_audit_log = xml_to_json(etree.fromstring(body))
+        return resp, instance_usage_audit_log
diff --git a/tempest/services/image/v1/json/image_client.py b/tempest/services/image/v1/json/image_client.py
index 9f5a405..61dd050 100644
--- a/tempest/services/image/v1/json/image_client.py
+++ b/tempest/services/image/v1/json/image_client.py
@@ -36,7 +36,8 @@
         super(ImageClientJSON, self).__init__(config, username, password,
                                               auth_url, tenant_name)
         self.service = self.config.images.catalog_type
-        self.http = self._get_http()
+        if config.service_available.glance:
+            self.http = self._get_http()
 
     def _image_meta_from_headers(self, headers):
         meta = {'properties': {}}
diff --git a/tempest/services/image/v2/json/image_client.py b/tempest/services/image/v2/json/image_client.py
index 3d37267..c654a49 100644
--- a/tempest/services/image/v2/json/image_client.py
+++ b/tempest/services/image/v2/json/image_client.py
@@ -31,7 +31,8 @@
         super(ImageClientV2JSON, self).__init__(config, username, password,
                                                 auth_url, tenant_name)
         self.service = self.config.images.catalog_type
-        self.http = self._get_http()
+        if config.service_available.glance:
+            self.http = self._get_http()
 
     def _get_http(self):
         token, endpoint = self.keystone_auth(self.user, self.password,
diff --git a/tempest/services/volume/json/extensions_client.py b/tempest/services/volume/json/extensions_client.py
new file mode 100644
index 0000000..01dd3e9
--- /dev/null
+++ b/tempest/services/volume/json/extensions_client.py
@@ -0,0 +1,34 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2012 OpenStack Foundation
+# 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 json
+
+from tempest.common.rest_client import RestClient
+
+
+class ExtensionsClientJSON(RestClient):
+
+    def __init__(self, config, username, password, auth_url, tenant_name=None):
+        super(ExtensionsClientJSON, self).__init__(config, username, password,
+                                                   auth_url, tenant_name)
+        self.service = self.config.volume.catalog_type
+
+    def list_extensions(self):
+        url = 'extensions'
+        resp, body = self.get(url)
+        body = json.loads(body)
+        return resp, body['extensions']
diff --git a/tempest/services/volume/xml/extensions_client.py b/tempest/services/volume/xml/extensions_client.py
new file mode 100644
index 0000000..b4e6536
--- /dev/null
+++ b/tempest/services/volume/xml/extensions_client.py
@@ -0,0 +1,40 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2012 OpenStack Foundation
+# 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 lxml import etree
+from tempest.common.rest_client import RestClientXML
+from tempest.services.compute.xml.common import xml_to_json
+
+
+class ExtensionsClientXML(RestClientXML):
+
+    def __init__(self, config, username, password, auth_url, tenant_name=None):
+        super(ExtensionsClientXML, self).__init__(config, username, password,
+                                                  auth_url, tenant_name)
+        self.service = self.config.volume.catalog_type
+
+    def _parse_array(self, node):
+        array = []
+        for child in node:
+            array.append(xml_to_json(child))
+        return array
+
+    def list_extensions(self):
+        url = 'extensions'
+        resp, body = self.get(url, self.headers)
+        body = self._parse_array(etree.fromstring(body))
+        return resp, body