Verify service attributes through Nova "get services" API

This patch adds checks whether a response of Nova "get services" API
includes the attributes to block the backward incompatibility change
in the future.

This patch implements the base part of a response validation.
The design is the following:
 * Each API schema is defined under tempest/api/compute/api_schema/
 * If API schemas of v2 and v3 are the same, define common API schema
   under tempest/api/compute/api_schema/
 * Otherwise, API schemas of v2 should be defined under v2/ and the
   one of v3 should be under v3/
 * Each API schema defines the succeeded status code('status_code')
   and response body('response_body')

Partially implements blueprint nova-api-attribute-test

Change-Id: Id0b4c31d47f7c6abafcb3c2ded9309fac61cb3dc
diff --git a/tempest/api/compute/admin/test_services.py b/tempest/api/compute/admin/test_services.py
index 3e45d65..ded688a 100644
--- a/tempest/api/compute/admin/test_services.py
+++ b/tempest/api/compute/admin/test_services.py
@@ -14,6 +14,7 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from tempest.api.compute.api_schema import services as schema
 from tempest.api.compute import base
 from tempest.test import attr
 
@@ -32,7 +33,7 @@
     @attr(type='gate')
     def test_list_services(self):
         resp, services = self.client.list_services()
-        self.assertEqual(200, resp.status)
+        self.validate_response(schema.list_services, resp, services)
         self.assertNotEqual(0, len(services))
 
     @attr(type='gate')
@@ -40,7 +41,7 @@
         binary_name = 'nova-compute'
         params = {'binary': binary_name}
         resp, services = self.client.list_services(params)
-        self.assertEqual(200, resp.status)
+        self.validate_response(schema.list_services, resp, services)
         self.assertNotEqual(0, len(services))
         for service in services:
             self.assertEqual(binary_name, service['binary'])
@@ -48,11 +49,14 @@
     @attr(type='gate')
     def test_get_service_by_host_name(self):
         resp, services = self.client.list_services()
+        self.validate_response(schema.list_services, resp, services)
         host_name = services[0]['host']
         services_on_host = [service for service in services if
                             service['host'] == host_name]
         params = {'host': host_name}
+
         resp, services = self.client.list_services(params)
+        self.validate_response(schema.list_services, resp, services)
 
         # we could have a periodic job checkin between the 2 service
         # lookups, so only compare binary lists.
@@ -66,11 +70,13 @@
     @attr(type='gate')
     def test_get_service_by_service_and_host_name(self):
         resp, services = self.client.list_services()
+        self.validate_response(schema.list_services, resp, services)
         host_name = services[0]['host']
         binary_name = services[0]['binary']
         params = {'host': host_name, 'binary': binary_name}
+
         resp, services = self.client.list_services(params)
-        self.assertEqual(200, resp.status)
+        self.validate_response(schema.list_services, resp, services)
         self.assertEqual(1, len(services))
         self.assertEqual(host_name, services[0]['host'])
         self.assertEqual(binary_name, services[0]['binary'])
diff --git a/tempest/api/compute/api_schema/__init__.py b/tempest/api/compute/api_schema/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tempest/api/compute/api_schema/__init__.py
diff --git a/tempest/api/compute/api_schema/services.py b/tempest/api/compute/api_schema/services.py
new file mode 100644
index 0000000..ef5868c
--- /dev/null
+++ b/tempest/api/compute/api_schema/services.py
@@ -0,0 +1,38 @@
+# Copyright 2014 NEC 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.
+
+list_services = {
+    'status_code': [200],
+    'response_body': {
+        'type': 'array',
+        'items': {
+            'type': 'object',
+            'properties': {
+                # NOTE: Now the type of 'id' is integer, but here allows
+                # 'string' also because we will be able to change it to
+                # 'uuid' in the future.
+                'id': {'type': ['integer', 'string']},
+                'zone': {'type': 'string'},
+                'host': {'type': 'string'},
+                'state': {'type': 'string'},
+                'binary': {'type': 'string'},
+                'status': {'type': 'string'},
+                'updated_at': {'type': 'string'},
+                'disabled_reason': {'type': ['string', 'null']},
+            },
+            'required': ['id', 'zone', 'host', 'state', 'binary', 'status',
+                         'updated_at', 'disabled_reason'],
+        },
+    }
+}
diff --git a/tempest/api/compute/base.py b/tempest/api/compute/base.py
index 398297d..e9b9efa 100644
--- a/tempest/api/compute/base.py
+++ b/tempest/api/compute/base.py
@@ -15,6 +15,8 @@
 
 import time
 
+import jsonschema
+
 from tempest import clients
 from tempest.common.utils import data_utils
 from tempest import config
@@ -176,6 +178,25 @@
 
         return resp, body
 
+    @classmethod
+    def validate_response(cls, schema, resp, body):
+        response_code = schema['status_code']
+        if resp.status not in response_code:
+            msg = ("The status code(%s) is different than the expected "
+                   "one(%s)") % (resp.status, response_code)
+            raise exceptions.InvalidHttpSuccessCode(msg)
+        response_schema = schema.get('response_body')
+        if response_schema:
+            try:
+                jsonschema.validate(body, response_schema)
+            except jsonschema.ValidationError as ex:
+                msg = ("HTTP response body is invalid (%s)") % ex
+                raise exceptions.InvalidHTTPResponseBody(msg)
+        else:
+            if body:
+                msg = ("HTTP response body should not exist (%s)") % body
+                raise exceptions.InvalidHTTPResponseBody(msg)
+
     def wait_for(self, condition):
         """Repeatedly calls condition() until a timeout."""
         start_time = int(time.time())
diff --git a/tempest/api/compute/v3/admin/test_services.py b/tempest/api/compute/v3/admin/test_services.py
index 914a2a4..0a7c7f1 100644
--- a/tempest/api/compute/v3/admin/test_services.py
+++ b/tempest/api/compute/v3/admin/test_services.py
@@ -14,6 +14,7 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from tempest.api.compute.api_schema import services as schema
 from tempest.api.compute import base
 from tempest.test import attr
 
@@ -32,7 +33,7 @@
     @attr(type='gate')
     def test_list_services(self):
         resp, services = self.client.list_services()
-        self.assertEqual(200, resp.status)
+        self.validate_response(schema.list_services, resp, services)
         self.assertNotEqual(0, len(services))
 
     @attr(type='gate')
@@ -40,7 +41,7 @@
         binary_name = 'nova-compute'
         params = {'binary': binary_name}
         resp, services = self.client.list_services(params)
-        self.assertEqual(200, resp.status)
+        self.validate_response(schema.list_services, resp, services)
         self.assertNotEqual(0, len(services))
         for service in services:
             self.assertEqual(binary_name, service['binary'])
@@ -48,11 +49,14 @@
     @attr(type='gate')
     def test_get_service_by_host_name(self):
         resp, services = self.client.list_services()
+        self.validate_response(schema.list_services, resp, services)
         host_name = services[0]['host']
         services_on_host = [service for service in services if
                             service['host'] == host_name]
         params = {'host': host_name}
+
         resp, services = self.client.list_services(params)
+        self.validate_response(schema.list_services, resp, services)
 
         # we could have a periodic job checkin between the 2 service
         # lookups, so only compare binary lists.
@@ -66,11 +70,13 @@
     @attr(type='gate')
     def test_get_service_by_service_and_host_name(self):
         resp, services = self.client.list_services()
+        self.validate_response(schema.list_services, resp, services)
         host_name = services[0]['host']
         binary_name = services[0]['binary']
         params = {'host': host_name, 'binary': binary_name}
+
         resp, services = self.client.list_services(params)
-        self.assertEqual(200, resp.status)
+        self.validate_response(schema.list_services, resp, services)
         self.assertEqual(1, len(services))
         self.assertEqual(host_name, services[0]['host'])
         self.assertEqual(binary_name, services[0]['binary'])