Merge "Validate hypervisors_servers Nova V2/V3 API"
diff --git a/tempest/api/network/admin/test_dhcp_agent_scheduler.py b/tempest/api/network/admin/test_dhcp_agent_scheduler.py
index 0e601d1..13ae1c0 100644
--- a/tempest/api/network/admin/test_dhcp_agent_scheduler.py
+++ b/tempest/api/network/admin/test_dhcp_agent_scheduler.py
@@ -59,17 +59,31 @@
         return network_id in network_ids
 
     @test.attr(type='smoke')
-    def test_remove_network_from_dhcp_agent(self):
+    def test_add_remove_network_from_dhcp_agent(self):
         # The agent is now bound to the network, we can free the port
         self.client.delete_port(self.port['id'])
         self.ports.remove(self.port)
-        resp, body = self.admin_client.list_dhcp_agent_hosting_network(
-            self.network['id'])
+        agent = dict()
+        agent['agent_type'] = None
+        resp, body = self.admin_client.list_agents()
         agents = body['agents']
-        self.assertIsNotNone(agents)
-        # Get an agent.
-        agent = agents[0]
-        network_id = self.network['id']
+        for a in agents:
+            if a['agent_type'] == 'DHCP agent':
+                agent = a
+                break
+        self.assertEqual(agent['agent_type'], 'DHCP agent', 'Could not find '
+                         'DHCP agent in agent list though dhcp_agent_scheduler'
+                         ' is enabled.')
+        network = self.create_network()
+        network_id = network['id']
+        if self._check_network_in_dhcp_agent(network_id, agent):
+            self._remove_network_from_dhcp_agent(network_id, agent)
+            self._add_dhcp_agent_to_network(network_id, agent)
+        else:
+            self._add_dhcp_agent_to_network(network_id, agent)
+            self._remove_network_from_dhcp_agent(network_id, agent)
+
+    def _remove_network_from_dhcp_agent(self, network_id, agent):
         resp, body = self.admin_client.remove_network_from_dhcp_agent(
             agent_id=agent['id'],
             network_id=network_id)
@@ -77,6 +91,13 @@
         self.assertFalse(self._check_network_in_dhcp_agent(
             network_id, agent))
 
+    def _add_dhcp_agent_to_network(self, network_id, agent):
+        resp, body = self.admin_client.add_dhcp_agent_to_network(
+            agent['id'], network_id)
+        self.assertEqual(resp['status'], '201')
+        self.assertTrue(self._check_network_in_dhcp_agent(
+            network_id, agent))
+
 
 class DHCPAgentSchedulersTestXML(DHCPAgentSchedulersTestJSON):
     _interface = 'xml'
diff --git a/tempest/api_schema/compute/keypairs.py b/tempest/api_schema/compute/keypairs.py
index 8973c02..b8f905f 100644
--- a/tempest/api_schema/compute/keypairs.py
+++ b/tempest/api_schema/compute/keypairs.py
@@ -39,3 +39,27 @@
         'required': ['keypairs']
     }
 }
+
+create_keypair = {
+    'type': 'object',
+    'properties': {
+        'keypair': {
+            'type': 'object',
+            'properties': {
+                'fingerprint': {'type': 'string'},
+                'name': {'type': 'string'},
+                'public_key': {'type': 'string'},
+                # NOTE: Now the type of 'user_id' is integer, but here
+                # allows 'string' also because we will be able to change
+                # it to 'uuid' in the future.
+                'user_id': {'type': ['integer', 'string']},
+                'private_key': {'type': 'string'}
+            },
+            # When create keypair API is being called with 'Public key'
+            # (Importing keypair) then, response body does not contain
+            # 'private_key' So it is not defined as 'required'
+            'required': ['fingerprint', 'name', 'public_key', 'user_id']
+        }
+    },
+    'required': ['keypair']
+}
diff --git a/tempest/api_schema/compute/v2/keypairs.py b/tempest/api_schema/compute/v2/keypairs.py
index 3225b0d..9a025c3 100644
--- a/tempest/api_schema/compute/v2/keypairs.py
+++ b/tempest/api_schema/compute/v2/keypairs.py
@@ -12,6 +12,8 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from tempest.api_schema.compute import keypairs
+
 get_keypair = {
     'status_code': [200],
     'response_body': {
@@ -45,3 +47,12 @@
         'required': ['keypair']
     }
 }
+
+create_keypair = {
+    'status_code': [200],
+    'response_body': keypairs.create_keypair
+}
+
+delete_keypair = {
+    'status_code': [202],
+}
diff --git a/tempest/api_schema/compute/v3/keypairs.py b/tempest/api_schema/compute/v3/keypairs.py
index 0197c84..de5f4ba 100644
--- a/tempest/api_schema/compute/v3/keypairs.py
+++ b/tempest/api_schema/compute/v3/keypairs.py
@@ -12,6 +12,8 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from tempest.api_schema.compute import keypairs
+
 get_keypair = {
     'status_code': [200],
     'response_body': {
@@ -30,3 +32,12 @@
         'required': ['keypair']
     }
 }
+
+create_keypair = {
+    'status_code': [201],
+    'response_body': keypairs.create_keypair
+}
+
+delete_keypair = {
+    'status_code': [204],
+}
diff --git a/tempest/services/compute/json/keypairs_client.py b/tempest/services/compute/json/keypairs_client.py
index 71f235d..be93789 100644
--- a/tempest/services/compute/json/keypairs_client.py
+++ b/tempest/services/compute/json/keypairs_client.py
@@ -53,7 +53,10 @@
         post_body = json.dumps(post_body)
         resp, body = self.post("os-keypairs", body=post_body)
         body = json.loads(body)
+        self.validate_response(schema.create_keypair, resp, body)
         return resp, body['keypair']
 
     def delete_keypair(self, key_name):
-        return self.delete("os-keypairs/%s" % str(key_name))
+        resp, body = self.delete("os-keypairs/%s" % str(key_name))
+        self.validate_response(schema.delete_keypair, resp, body)
+        return resp, body
diff --git a/tempest/services/compute/v3/json/keypairs_client.py b/tempest/services/compute/v3/json/keypairs_client.py
index d315bc4..f090d7d 100644
--- a/tempest/services/compute/v3/json/keypairs_client.py
+++ b/tempest/services/compute/v3/json/keypairs_client.py
@@ -53,7 +53,10 @@
         post_body = json.dumps(post_body)
         resp, body = self.post("keypairs", body=post_body)
         body = json.loads(body)
+        self.validate_response(schema.create_keypair, resp, body)
         return resp, body['keypair']
 
     def delete_keypair(self, key_name):
-        return self.delete("keypairs/%s" % str(key_name))
+        resp, body = self.delete("keypairs/%s" % str(key_name))
+        self.validate_response(schema.delete_keypair, resp, body)
+        return resp, body
diff --git a/tempest/services/network/json/network_client.py b/tempest/services/network/json/network_client.py
index e5309ff..f9dd8ef 100644
--- a/tempest/services/network/json/network_client.py
+++ b/tempest/services/network/json/network_client.py
@@ -300,3 +300,11 @@
         resp, body = self.get(uri)
         body = json.loads(body)
         return resp, body
+
+    def add_dhcp_agent_to_network(self, agent_id, network_id):
+        post_body = {'network_id': network_id}
+        body = json.dumps(post_body)
+        uri = '%s/agents/%s/dhcp-networks' % (self.uri_prefix, agent_id)
+        resp, body = self.post(uri, body)
+        body = json.loads(body)
+        return resp, body
diff --git a/tempest/services/network/xml/network_client.py b/tempest/services/network/xml/network_client.py
index 26dc672..0945b09 100644
--- a/tempest/services/network/xml/network_client.py
+++ b/tempest/services/network/xml/network_client.py
@@ -250,6 +250,13 @@
         body = _root_tag_fetcher_and_xml_to_json_parse(body)
         return resp, body
 
+    def add_dhcp_agent_to_network(self, agent_id, network_id):
+        uri = '%s/agents/%s/dhcp-networks' % (self.uri_prefix, agent_id)
+        network = common.Element("network_id", network_id)
+        resp, body = self.post(uri, str(common.Document(network)))
+        body = _root_tag_fetcher_and_xml_to_json_parse(body)
+        return resp, body
+
 
 def _root_tag_fetcher_and_xml_to_json_parse(xml_returned_body):
     body = ET.fromstring(xml_returned_body)
diff --git a/tempest/tests/common/test_debug.py b/tempest/tests/common/test_debug.py
new file mode 100644
index 0000000..cd9936c
--- /dev/null
+++ b/tempest/tests/common/test_debug.py
@@ -0,0 +1,122 @@
+# 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.
+
+import mock
+
+from tempest.common import debug
+from tempest import config
+from tempest.openstack.common.fixture import mockpatch
+from tempest import test
+from tempest.tests import base
+from tempest.tests import fake_config
+
+
+class TestDebug(base.TestCase):
+
+    def setUp(self):
+        super(TestDebug, self).setUp()
+        self.useFixture(fake_config.ConfigFixture())
+        self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
+
+        common_pre = 'tempest.common.commands'
+        self.ip_addr_raw_mock = self.patch(common_pre + '.ip_addr_raw')
+        self.ip_route_raw_mock = self.patch(common_pre + '.ip_route_raw')
+        self.iptables_raw_mock = self.patch(common_pre + '.iptables_raw')
+        self.ip_ns_list_mock = self.patch(common_pre + '.ip_ns_list')
+        self.ip_ns_addr_mock = self.patch(common_pre + '.ip_ns_addr')
+        self.ip_ns_route_mock = self.patch(common_pre + '.ip_ns_route')
+        self.iptables_ns_mock = self.patch(common_pre + '.iptables_ns')
+        self.ovs_db_dump_mock = self.patch(common_pre + '.ovs_db_dump')
+
+        self.log_mock = self.patch('tempest.common.debug.LOG')
+
+    def test_log_ip_ns_debug_disabled(self):
+        self.useFixture(mockpatch.PatchObject(test.CONF.debug,
+                                              'enable', False))
+        debug.log_ip_ns()
+        self.assertFalse(self.ip_addr_raw_mock.called)
+        self.assertFalse(self.log_mock.info.called)
+
+    def test_log_ip_ns_debug_enabled(self):
+        self.useFixture(mockpatch.PatchObject(test.CONF.debug,
+                                              'enable', True))
+
+        tables = ['filter', 'nat', 'mangle']
+        self.ip_ns_list_mock.return_value = [1, 2]
+
+        debug.log_ip_ns()
+        self.ip_addr_raw_mock.assert_called_with()
+        self.assertTrue(self.log_mock.info.called)
+        self.ip_route_raw_mock.assert_called_with()
+        self.assertEqual(len(tables), self.iptables_raw_mock.call_count)
+        for table in tables:
+            self.assertIn(mock.call(table),
+                          self.iptables_raw_mock.call_args_list)
+
+        self.ip_ns_list_mock.assert_called_with()
+        self.assertEqual(len(self.ip_ns_list_mock.return_value),
+                         self.ip_ns_addr_mock.call_count)
+        self.assertEqual(len(self.ip_ns_list_mock.return_value),
+                         self.ip_ns_route_mock.call_count)
+        for ns in self.ip_ns_list_mock.return_value:
+            self.assertIn(mock.call(ns),
+                          self.ip_ns_addr_mock.call_args_list)
+            self.assertIn(mock.call(ns),
+                          self.ip_ns_route_mock.call_args_list)
+
+        self.assertEqual(len(tables) * len(self.ip_ns_list_mock.return_value),
+                         self.iptables_ns_mock.call_count)
+        for ns in self.ip_ns_list_mock.return_value:
+            for table in tables:
+                self.assertIn(mock.call(ns, table),
+                              self.iptables_ns_mock.call_args_list)
+
+    def test_log_ovs_db_debug_disabled(self):
+        self.useFixture(mockpatch.PatchObject(test.CONF.debug,
+                                              'enable', False))
+        self.useFixture(mockpatch.PatchObject(test.CONF.service_available,
+                                              'neutron', False))
+        debug.log_ovs_db()
+        self.assertFalse(self.ovs_db_dump_mock.called)
+
+        self.useFixture(mockpatch.PatchObject(test.CONF.debug,
+                                              'enable', True))
+        self.useFixture(mockpatch.PatchObject(test.CONF.service_available,
+                                              'neutron', False))
+        debug.log_ovs_db()
+        self.assertFalse(self.ovs_db_dump_mock.called)
+
+        self.useFixture(mockpatch.PatchObject(test.CONF.debug,
+                                              'enable', False))
+        self.useFixture(mockpatch.PatchObject(test.CONF.service_available,
+                                              'neutron', True))
+        debug.log_ovs_db()
+        self.assertFalse(self.ovs_db_dump_mock.called)
+
+    def test_log_ovs_db_debug_enabled(self):
+        self.useFixture(mockpatch.PatchObject(test.CONF.debug,
+                                              'enable', True))
+        self.useFixture(mockpatch.PatchObject(test.CONF.service_available,
+                                              'neutron', True))
+        debug.log_ovs_db()
+        self.ovs_db_dump_mock.assert_called_with()
+
+    def test_log_net_debug(self):
+        self.log_ip_ns_mock = self.patch('tempest.common.debug.log_ip_ns')
+        self.log_ovs_db_mock = self.patch('tempest.common.debug.log_ovs_db')
+
+        debug.log_net_debug()
+        self.log_ip_ns_mock.assert_called_with()
+        self.log_ovs_db_mock.assert_called_with()