Merge "Add a test to list the security group rules"
diff --git a/tempest/services/compute/json/security_groups_client.py b/tempest/services/compute/json/security_groups_client.py
index 95f2831..7f430d8 100644
--- a/tempest/services/compute/json/security_groups_client.py
+++ b/tempest/services/compute/json/security_groups_client.py
@@ -95,3 +95,12 @@
     def delete_security_group_rule(self, group_rule_id):
         """Deletes the provided Security Group rule."""
         return self.delete('os-security-group-rules/%s' % str(group_rule_id))
+
+    def list_security_group_rules(self, security_group_id):
+        """List all rules for a security group."""
+        resp, body = self.get('os-security-groups')
+        body = json.loads(body)
+        for sg in body['security_groups']:
+            if sg['id'] == security_group_id:
+                return resp, sg['rules']
+        raise exceptions.NotFound('No such Security Group')
diff --git a/tempest/services/compute/xml/security_groups_client.py b/tempest/services/compute/xml/security_groups_client.py
index ac70f1b..7db60a1 100644
--- a/tempest/services/compute/xml/security_groups_client.py
+++ b/tempest/services/compute/xml/security_groups_client.py
@@ -23,6 +23,7 @@
 from tempest.services.compute.xml.common import Element
 from tempest.services.compute.xml.common import Text
 from tempest.services.compute.xml.common import xml_to_json
+from tempest.services.compute.xml.common import XMLNS_11
 
 
 class SecurityGroupsClientXML(RestClientXML):
@@ -128,3 +129,16 @@
         """Deletes the provided Security Group rule."""
         return self.delete('os-security-group-rules/%s' %
                            str(group_rule_id), self.headers)
+
+    def list_security_group_rules(self, security_group_id):
+        """List all rules for a security group."""
+        url = "os-security-groups"
+        resp, body = self.get(url, self.headers)
+        body = etree.fromstring(body)
+        secgroups = body.getchildren()
+        for secgroup in secgroups:
+            if secgroup.get('id') == security_group_id:
+                node = secgroup.find('{%s}rules' % XMLNS_11)
+                rules = [xml_to_json(x) for x in node.getchildren()]
+                return resp, rules
+        raise exceptions.NotFound('No such Security Group')
diff --git a/tempest/tests/compute/security_groups/test_security_group_rules.py b/tempest/tests/compute/security_groups/test_security_group_rules.py
index dc85f4b..99d9a5d 100644
--- a/tempest/tests/compute/security_groups/test_security_group_rules.py
+++ b/tempest/tests/compute/security_groups/test_security_group_rules.py
@@ -232,6 +232,49 @@
                           self.client.delete_security_group_rule,
                           rand_name('999'))
 
+    @attr(type='positive')
+    def test_security_group_rules_list(self):
+        # Positive test: Created Security Group rules should be
+        # in the list of all rules
+        # Creating a Security Group to add rules to it
+        s_name = rand_name('securitygroup-')
+        s_description = rand_name('description-')
+        resp, securitygroup = \
+            self.client.create_security_group(s_name, s_description)
+        securitygroup_id = securitygroup['id']
+        # Delete the Security Group at the end of this method
+        self.addCleanup(self.client.delete_security_group, securitygroup_id)
+
+        # Add a first rule to the created Security Group
+        ip_protocol1 = 'tcp'
+        from_port1 = 22
+        to_port1 = 22
+        resp, rule = \
+            self.client.create_security_group_rule(securitygroup_id,
+                                                   ip_protocol1,
+                                                   from_port1, to_port1)
+        rule1_id = rule['id']
+        # Delete the Security Group rule1 at the end of this method
+        self.addCleanup(self.client.delete_security_group_rule, rule1_id)
+
+        # Add a second rule to the created Security Group
+        ip_protocol2 = 'icmp'
+        from_port2 = -1
+        to_port2 = -1
+        resp, rule = \
+            self.client.create_security_group_rule(securitygroup_id,
+                                                   ip_protocol2,
+                                                   from_port2, to_port2)
+        rule2_id = rule['id']
+        # Delete the Security Group rule2 at the end of this method
+        self.addCleanup(self.client.delete_security_group_rule, rule2_id)
+
+        # Get rules of the created Security Group
+        resp, rules = \
+            self.client.list_security_group_rules(securitygroup_id)
+        self.assertTrue(any([i for i in rules if i['id'] == rule1_id]))
+        self.assertTrue(any([i for i in rules if i['id'] == rule2_id]))
+
 
 class SecurityGroupRulesTestXML(SecurityGroupRulesTestJSON):
     _interface = 'xml'