Adds test for Sec group default rule Nova V2 API

This patch adds the test for below 'os-security-group-default-rules'
Nova V2 APIs-
- Create Security Group default rule
- Delete Security Group default rule
- List Security Group default rule
- GET Security Group default rule

This patch also adds the new json client.

Related-Bug: 1311500

Change-Id: Icf52a64b5719587cd02b906f3a0f8d17886241aa
diff --git a/tempest/api/compute/admin/test_security_group_default_rules.py b/tempest/api/compute/admin/test_security_group_default_rules.py
new file mode 100644
index 0000000..07408a8
--- /dev/null
+++ b/tempest/api/compute/admin/test_security_group_default_rules.py
@@ -0,0 +1,127 @@
+# 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 testtools
+
+from tempest.api.compute import base
+from tempest import config
+from tempest import exceptions
+from tempest import test
+
+CONF = config.CONF
+
+
+class SecurityGroupDefaultRulesTest(base.BaseV2ComputeAdminTest):
+
+    @classmethod
+    # TODO(GMann): Once Bug# 1311500 is fixed, these test can run
+    # for Neutron also.
+    @testtools.skipIf(CONF.service_available.neutron,
+                      "Skip as this functionality is not yet "
+                      "implemented in Neutron. Related Bug#1311500")
+    @test.safe_setup
+    def setUpClass(cls):
+        # A network and a subnet will be created for these tests
+        cls.set_network_resources(network=True, subnet=True)
+        super(SecurityGroupDefaultRulesTest, cls).setUpClass()
+        cls.adm_client = cls.os_adm.security_group_default_rules_client
+
+    def _create_security_group_default_rules(self, ip_protocol='tcp',
+                                             from_port=22, to_port=22,
+                                             cidr='10.10.0.0/24'):
+        # Create Security Group default rule
+        _, rule = self.adm_client.create_security_default_group_rule(
+            ip_protocol,
+            from_port,
+            to_port,
+            cidr=cidr)
+        self.assertEqual(ip_protocol, rule['ip_protocol'])
+        self.assertEqual(from_port, rule['from_port'])
+        self.assertEqual(to_port, rule['to_port'])
+        self.assertEqual(cidr, rule['ip_range']['cidr'])
+        return rule
+
+    @test.attr(type='smoke')
+    def test_create_delete_security_group_default_rules(self):
+        # Create and delete Security Group default rule
+        ip_protocols = {'tcp', 'udp', 'icmp'}
+        for ip_protocol in ip_protocols:
+            rule = self._create_security_group_default_rules(ip_protocol)
+            # Delete Security Group default rule
+            self.adm_client.delete_security_group_default_rule(rule['id'])
+            self.assertRaises(exceptions.NotFound,
+                              self.adm_client.get_security_group_default_rule,
+                              rule['id'])
+
+    @test.attr(type='smoke')
+    def test_create_security_group_default_rule_without_cidr(self):
+        ip_protocol = 'udp'
+        from_port = 80
+        to_port = 80
+        _, rule = self.adm_client.create_security_default_group_rule(
+            ip_protocol,
+            from_port,
+            to_port)
+        self.addCleanup(self.adm_client.delete_security_group_default_rule,
+                        rule['id'])
+        self.assertNotEqual(0, rule['id'])
+        self.assertEqual('0.0.0.0/0', rule['ip_range']['cidr'])
+
+    @test.attr(type='smoke')
+    def test_create_security_group_default_rule_with_blank_cidr(self):
+        ip_protocol = 'icmp'
+        from_port = 10
+        to_port = 10
+        cidr = ''
+        _, rule = self.adm_client.create_security_default_group_rule(
+            ip_protocol,
+            from_port,
+            to_port,
+            cidr=cidr)
+        self.addCleanup(self.adm_client.delete_security_group_default_rule,
+                        rule['id'])
+        self.assertNotEqual(0, rule['id'])
+        self.assertEqual('0.0.0.0/0', rule['ip_range']['cidr'])
+
+    @test.attr(type='smoke')
+    def test_security_group_default_rules_list(self):
+        ip_protocol = 'tcp'
+        from_port = 22
+        to_port = 22
+        cidr = '10.10.0.0/24'
+        rule = self._create_security_group_default_rules(ip_protocol,
+                                                         from_port,
+                                                         to_port,
+                                                         cidr)
+        self.addCleanup(self.adm_client.delete_security_group_default_rule,
+                        rule['id'])
+        _, rules = self.adm_client.list_security_group_default_rules()
+        self.assertNotEqual(0, len(rules))
+        self.assertIn(rule, rules)
+
+    @test.attr(type='smoke')
+    def test_default_security_group_default_rule_show(self):
+        ip_protocol = 'tcp'
+        from_port = 22
+        to_port = 22
+        cidr = '10.10.0.0/24'
+        rule = self._create_security_group_default_rules(ip_protocol,
+                                                         from_port,
+                                                         to_port,
+                                                         cidr)
+        self.addCleanup(self.adm_client.delete_security_group_default_rule,
+                        rule['id'])
+        _, fetched_rule = self.adm_client.get_security_group_default_rule(
+            rule['id'])
+        self.assertEqual(rule, fetched_rule)
diff --git a/tempest/api/compute/base.py b/tempest/api/compute/base.py
index caf4174..c164e39 100644
--- a/tempest/api/compute/base.py
+++ b/tempest/api/compute/base.py
@@ -83,6 +83,8 @@
             cls.hypervisor_client = cls.os.hypervisor_client
             cls.certificates_client = cls.os.certificates_client
             cls.migrations_client = cls.os.migrations_client
+            cls.security_group_default_rules_client = (
+                cls.os.security_group_default_rules_client)
 
         elif cls._api_version == 3:
             if not CONF.compute_feature_enabled.api_v3:
diff --git a/tempest/clients.py b/tempest/clients.py
index 4e2205e..519e686 100644
--- a/tempest/clients.py
+++ b/tempest/clients.py
@@ -52,6 +52,8 @@
     MigrationsClientJSON
 from tempest.services.compute.json.quotas_client import QuotaClassesClientJSON
 from tempest.services.compute.json.quotas_client import QuotasClientJSON
+from tempest.services.compute.json.security_group_default_rules_client import \
+    SecurityGroupDefaultRulesClientJSON
 from tempest.services.compute.json.security_groups_client import \
     SecurityGroupsClientJSON
 from tempest.services.compute.json.servers_client import ServersClientJSON
@@ -406,6 +408,8 @@
         self.data_processing_client = DataProcessingClient(
             self.auth_provider)
         self.migrations_client = MigrationsClientJSON(self.auth_provider)
+        self.security_group_default_rules_client = (
+            SecurityGroupDefaultRulesClientJSON(self.auth_provider))
 
 
 class AltManager(Manager):
diff --git a/tempest/services/compute/json/security_group_default_rules_client.py b/tempest/services/compute/json/security_group_default_rules_client.py
new file mode 100644
index 0000000..6d29837
--- /dev/null
+++ b/tempest/services/compute/json/security_group_default_rules_client.py
@@ -0,0 +1,74 @@
+# 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 json
+
+from tempest.common import rest_client
+from tempest import config
+
+CONF = config.CONF
+
+
+class SecurityGroupDefaultRulesClientJSON(rest_client.RestClient):
+
+    def __init__(self, auth_provider):
+        super(SecurityGroupDefaultRulesClientJSON,
+              self).__init__(auth_provider)
+        self.service = CONF.compute.catalog_type
+
+    def create_security_default_group_rule(self, ip_protocol, from_port,
+                                           to_port, **kwargs):
+        """
+        Creating security group default rules.
+        ip_protocol : ip_protocol (icmp, tcp, udp).
+        from_port: Port at start of range.
+        to_port  : Port at end of range.
+        cidr     : CIDR for address range.
+        """
+        post_body = {
+            'ip_protocol': ip_protocol,
+            'from_port': from_port,
+            'to_port': to_port,
+            'cidr': kwargs.get('cidr'),
+        }
+        post_body = json.dumps({'security_group_default_rule': post_body})
+        url = 'os-security-group-default-rules'
+        resp, body = self.post(url, post_body)
+        self.expected_success(200, resp.status)
+        body = json.loads(body)
+        return resp, body['security_group_default_rule']
+
+    def delete_security_group_default_rule(self,
+                                           security_group_default_rule_id):
+        """Deletes the provided Security Group default rule."""
+        resp, body = self.delete('os-security-group-default-rules/%s' % str(
+            security_group_default_rule_id))
+        self.expected_success(204, resp.status)
+        return resp, body
+
+    def list_security_group_default_rules(self):
+        """List all Security Group default rules."""
+        resp, body = self.get('os-security-group-default-rules')
+        self.expected_success(200, resp.status)
+        body = json.loads(body)
+        return resp, body['security_group_default_rules']
+
+    def get_security_group_default_rule(self, security_group_default_rule_id):
+        """Return the details of provided Security Group default rule."""
+        resp, body = self.get('os-security-group-default-rules/%s' % str(
+            security_group_default_rule_id))
+        self.expected_success(200, resp.status)
+        body = json.loads(body)
+        return resp, body['security_group_default_rule']