From 95e279176b05e0096988ae596f1e02adda248935 Mon Sep 17 00:00:00 2001
From: Akihiro Motoki <amotoki@gmail.com>
Date: Wed, 23 Aug 2017 21:14:47 +0000
Subject: [PATCH] Convert network security group functional tests to JSON

Change-Id: Icb63aa0dfbce9016fb824f97915a660cf130d120
---
 .../network/v2/test_security_group.py         | 70 +++++----------
 .../network/v2/test_security_group_rule.py    | 88 +++++++------------
 2 files changed, 52 insertions(+), 106 deletions(-)

diff --git a/openstackclient/tests/functional/network/v2/test_security_group.py b/openstackclient/tests/functional/network/v2/test_security_group.py
index b601c913fd..8ae24b7247 100644
--- a/openstackclient/tests/functional/network/v2/test_security_group.py
+++ b/openstackclient/tests/functional/network/v2/test_security_group.py
@@ -10,6 +10,7 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+import json
 import uuid
 
 from openstackclient.tests.functional.network.v2 import common
@@ -17,44 +18,6 @@ from openstackclient.tests.functional.network.v2 import common
 
 class SecurityGroupTests(common.NetworkTests):
     """Functional tests for security group"""
-    HEADERS = ['Name']
-    FIELDS = ['name']
-
-    @classmethod
-    def setUpClass(cls):
-        common.NetworkTests.setUpClass()
-        if cls.haz_network:
-            cls.NAME = uuid.uuid4().hex
-            cls.OTHER_NAME = uuid.uuid4().hex
-
-            opts = cls.get_opts(cls.FIELDS)
-            raw_output = cls.openstack(
-                'security group create ' +
-                cls.NAME +
-                opts
-            )
-            expected = cls.NAME + '\n'
-            cls.assertOutput(expected, raw_output)
-
-    @classmethod
-    def tearDownClass(cls):
-        try:
-            if cls.haz_network:
-                # Rename test
-                raw_output = cls.openstack(
-                    'security group set --name ' +
-                    cls.OTHER_NAME + ' ' +
-                    cls.NAME
-                )
-                cls.assertOutput('', raw_output)
-                # Delete test
-                raw_output = cls.openstack(
-                    'security group delete ' +
-                    cls.OTHER_NAME
-                )
-                cls.assertOutput('', raw_output)
-        finally:
-            super(SecurityGroupTests, cls).tearDownClass()
 
     def setUp(self):
         super(SecurityGroupTests, self).setUp()
@@ -62,22 +25,33 @@ class SecurityGroupTests(common.NetworkTests):
         if not self.haz_network:
             self.skipTest("No Network service present")
 
+        self.NAME = uuid.uuid4().hex
+        self.OTHER_NAME = uuid.uuid4().hex
+        cmd_output = json.loads(self.openstack(
+            'security group create -f json ' +
+            self.NAME
+        ))
+        self.addCleanup(self.openstack,
+                        'security group delete ' + cmd_output['id'])
+        self.assertEqual(self.NAME, cmd_output['name'])
+
     def test_security_group_list(self):
-        opts = self.get_opts(self.HEADERS)
-        raw_output = self.openstack('security group list' + opts)
-        self.assertIn(self.NAME, raw_output)
+        cmd_output = json.loads(self.openstack('security group list -f json'))
+        self.assertIn(self.NAME, [sg['Name'] for sg in cmd_output])
 
     def test_security_group_set(self):
+        other_name = uuid.uuid4().hex
         raw_output = self.openstack(
-            'security group set --description NSA ' + self.NAME
+            'security group set --description NSA --name ' +
+            other_name + ' ' + self.NAME
         )
         self.assertEqual('', raw_output)
 
-        opts = self.get_opts(['description'])
-        raw_output = self.openstack('security group show ' + self.NAME + opts)
-        self.assertEqual("NSA\n", raw_output)
+        cmd_output = json.loads(self.openstack(
+            'security group show -f json ' + other_name))
+        self.assertEqual('NSA', cmd_output['description'])
 
     def test_security_group_show(self):
-        opts = self.get_opts(self.FIELDS)
-        raw_output = self.openstack('security group show ' + self.NAME + opts)
-        self.assertEqual(self.NAME + "\n", raw_output)
+        cmd_output = json.loads(self.openstack(
+            'security group show -f json ' + self.NAME))
+        self.assertEqual(self.NAME, cmd_output['name'])
diff --git a/openstackclient/tests/functional/network/v2/test_security_group_rule.py b/openstackclient/tests/functional/network/v2/test_security_group_rule.py
index 40951a011d..fe78bf477a 100644
--- a/openstackclient/tests/functional/network/v2/test_security_group_rule.py
+++ b/openstackclient/tests/functional/network/v2/test_security_group_rule.py
@@ -10,6 +10,7 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+import json
 import uuid
 
 from openstackclient.tests.functional.network.v2 import common
@@ -17,54 +18,6 @@ from openstackclient.tests.functional.network.v2 import common
 
 class SecurityGroupRuleTests(common.NetworkTests):
     """Functional tests for security group rule"""
-    SECURITY_GROUP_RULE_ID = None
-    NAME_FIELD = ['name']
-    ID_FIELD = ['id']
-    ID_HEADER = ['ID']
-
-    @classmethod
-    def setUpClass(cls):
-        common.NetworkTests.setUpClass()
-        if cls.haz_network:
-            cls.SECURITY_GROUP_NAME = uuid.uuid4().hex
-
-            # Create the security group to hold the rule
-            opts = cls.get_opts(cls.NAME_FIELD)
-            raw_output = cls.openstack(
-                'security group create ' +
-                cls.SECURITY_GROUP_NAME +
-                opts
-            )
-            expected = cls.SECURITY_GROUP_NAME + '\n'
-            cls.assertOutput(expected, raw_output)
-
-            # Create the security group rule.
-            opts = cls.get_opts(cls.ID_FIELD)
-            raw_output = cls.openstack(
-                'security group rule create ' +
-                cls.SECURITY_GROUP_NAME + ' ' +
-                '--protocol tcp --dst-port 80:80 ' +
-                '--ingress --ethertype IPv4 ' +
-                opts
-            )
-            cls.SECURITY_GROUP_RULE_ID = raw_output.strip('\n')
-
-    @classmethod
-    def tearDownClass(cls):
-        try:
-            if cls.haz_network:
-                raw_output = cls.openstack(
-                    'security group rule delete ' +
-                    cls.SECURITY_GROUP_RULE_ID
-                )
-                cls.assertOutput('', raw_output)
-                raw_output = cls.openstack(
-                    'security group delete ' +
-                    cls.SECURITY_GROUP_NAME
-                )
-                cls.assertOutput('', raw_output)
-        finally:
-            super(SecurityGroupRuleTests, cls).tearDownClass()
 
     def setUp(self):
         super(SecurityGroupRuleTests, self).setUp()
@@ -72,16 +25,35 @@ class SecurityGroupRuleTests(common.NetworkTests):
         if not self.haz_network:
             self.skipTest("No Network service present")
 
+        self.SECURITY_GROUP_NAME = uuid.uuid4().hex
+
+        # Create the security group to hold the rule
+        cmd_output = json.loads(self.openstack(
+            'security group create -f json ' +
+            self.SECURITY_GROUP_NAME
+        ))
+        self.addCleanup(self.openstack,
+                        'security group delete ' + self.SECURITY_GROUP_NAME)
+        self.assertEqual(self.SECURITY_GROUP_NAME, cmd_output['name'])
+
+        # Create the security group rule.
+        cmd_output = json.loads(self.openstack(
+            'security group rule create -f json ' +
+            self.SECURITY_GROUP_NAME + ' ' +
+            '--protocol tcp --dst-port 80:80 ' +
+            '--ingress --ethertype IPv4 '
+        ))
+        self.addCleanup(self.openstack,
+                        'security group rule delete ' + cmd_output['id'])
+        self.SECURITY_GROUP_RULE_ID = cmd_output['id']
+
     def test_security_group_rule_list(self):
-        opts = self.get_opts(self.ID_HEADER)
-        raw_output = self.openstack('security group rule list ' +
-                                    self.SECURITY_GROUP_NAME +
-                                    opts)
-        self.assertIn(self.SECURITY_GROUP_RULE_ID, raw_output)
+        cmd_output = json.loads(self.openstack(
+            'security group rule list -f json ' + self.SECURITY_GROUP_NAME))
+        self.assertIn(self.SECURITY_GROUP_RULE_ID,
+                      [rule['ID'] for rule in cmd_output])
 
     def test_security_group_rule_show(self):
-        opts = self.get_opts(self.ID_FIELD)
-        raw_output = self.openstack('security group rule show ' +
-                                    self.SECURITY_GROUP_RULE_ID +
-                                    opts)
-        self.assertEqual(self.SECURITY_GROUP_RULE_ID + "\n", raw_output)
+        cmd_output = json.loads(self.openstack(
+            'security group rule show -f json ' + self.SECURITY_GROUP_RULE_ID))
+        self.assertEqual(self.SECURITY_GROUP_RULE_ID, cmd_output['id'])