Rework port functional tests on json output format

Some functional tests try to parse the CLI table output
format, that cause much work on parse string by using
regular expression. Using json format in functional tests
is better and easier way, the patch rework for port related
tests.

Change-Id: I0ec997bf63da4925742848c593db09d89655ca34
This commit is contained in:
Rui Chen 2017-01-23 15:41:01 +08:00
parent bc4a3f467f
commit c7e0948a2d
2 changed files with 75 additions and 115 deletions

View File

@ -84,7 +84,7 @@ class ImageTests(base.TestCase):
self.openstack( self.openstack(
'image set --accept ' + self.NAME) 'image set --accept ' + self.NAME)
shared_img_list = self.parse_listing( shared_img_list = self.parse_listing(
self.openstack('image list --shared', self.get_opts(['name'])) self.openstack('image list --shared')
) )
self.assertIn(self.NAME, [img['Name'] for img in shared_img_list]) self.assertIn(self.NAME, [img['Name'] for img in shared_img_list])

View File

@ -10,7 +10,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import re import json
import uuid import uuid
from openstackclient.tests.functional import base from openstackclient.tests.functional import base
@ -20,21 +20,11 @@ class PortTests(base.TestCase):
"""Functional tests for port. """ """Functional tests for port. """
NAME = uuid.uuid4().hex NAME = uuid.uuid4().hex
NETWORK_NAME = uuid.uuid4().hex NETWORK_NAME = uuid.uuid4().hex
HEADERS = ['Name']
FIELDS = ['name']
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
# Set up some regex for matching below
cls.re_id = re.compile("\s+id\s+\|\s+(\S+)")
cls.re_name = re.compile("\s+name\s+\|\s+([^|]+?)\s+\|")
cls.re_description = re.compile("\s+description\s+\|\s+([^|]+?)\s+\|")
cls.re_mac_address = re.compile("\s+mac_address\s+\|\s+([^|]+?)\s+\|")
cls.re_state = re.compile("\s+admin_state_up\s+\|\s+([^|]+?)\s+\|")
# Create a network for the port # Create a network for the port
raw_output = cls.openstack('network create ' + cls.NETWORK_NAME) cls.openstack('network create ' + cls.NETWORK_NAME)
cls.network_id = re.search(cls.re_id, raw_output).group(1)
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
@ -43,33 +33,23 @@ class PortTests(base.TestCase):
def test_port_delete(self): def test_port_delete(self):
"""Test create, delete multiple""" """Test create, delete multiple"""
raw_output = self.openstack( json_output = json.loads(self.openstack(
'port create --network ' + self.NETWORK_NAME + ' ' + self.NAME 'port create -f json --network ' +
) self.NETWORK_NAME + ' ' + self.NAME
re_id1 = re.search(self.re_id, raw_output) ))
self.assertIsNotNone(re_id1) id1 = json_output.get('id')
id1 = re_id1.group(1) self.assertIsNotNone(id1)
self.assertIsNotNone( self.assertIsNotNone(json_output.get('mac_address'))
re.search(self.re_mac_address, raw_output).group(1), self.assertEqual(self.NAME, json_output.get('name'))
)
self.assertEqual(
self.NAME,
re.search(self.re_name, raw_output).group(1),
)
raw_output = self.openstack( json_output = json.loads(self.openstack(
'port create ' + 'port create -f json --network ' + self.NETWORK_NAME + ' ' +
'--network ' + self.NETWORK_NAME + ' ' +
self.NAME + 'x' self.NAME + 'x'
) ))
id2 = re.search(self.re_id, raw_output).group(1) id2 = json_output.get('id')
self.assertIsNotNone( self.assertIsNotNone(id2)
re.search(self.re_mac_address, raw_output).group(1), self.assertIsNotNone(json_output.get('mac_address'))
) self.assertEqual(self.NAME + 'x', json_output.get('name'))
self.assertEqual(
self.NAME + 'x',
re.search(self.re_name, raw_output).group(1),
)
# Clean up after ourselves # Clean up after ourselves
raw_output = self.openstack('port delete ' + id1 + ' ' + id2) raw_output = self.openstack('port delete ' + id1 + ' ' + id2)
@ -77,100 +57,80 @@ class PortTests(base.TestCase):
def test_port_list(self): def test_port_list(self):
"""Test create defaults, list, delete""" """Test create defaults, list, delete"""
raw_output = self.openstack( json_output = json.loads(self.openstack(
'port create --network ' + self.NETWORK_NAME + ' ' + self.NAME 'port create -f json --network ' + self.NETWORK_NAME + ' ' +
) self.NAME
re_id1 = re.search(self.re_id, raw_output) ))
self.assertIsNotNone(re_id1) id1 = json_output.get('id')
id1 = re_id1.group(1) self.assertIsNotNone(id1)
mac1 = re.search(self.re_mac_address, raw_output).group(1) mac1 = json_output.get('mac_address')
self.assertIsNotNone(mac1)
self.addCleanup(self.openstack, 'port delete ' + id1) self.addCleanup(self.openstack, 'port delete ' + id1)
self.assertEqual( self.assertEqual(self.NAME, json_output.get('name'))
self.NAME,
re.search(self.re_name, raw_output).group(1),
)
raw_output = self.openstack( json_output = json.loads(self.openstack(
'port create ' + 'port create -f json --network ' + self.NETWORK_NAME + ' ' +
'--network ' + self.NETWORK_NAME + ' ' +
self.NAME + 'x' self.NAME + 'x'
) ))
id2 = re.search(self.re_id, raw_output).group(1) id2 = json_output.get('id')
mac2 = re.search(self.re_mac_address, raw_output).group(1) self.assertIsNotNone(id2)
mac2 = json_output.get('mac_address')
self.assertIsNotNone(mac2)
self.addCleanup(self.openstack, 'port delete ' + id2) self.addCleanup(self.openstack, 'port delete ' + id2)
self.assertEqual( self.assertEqual(self.NAME + 'x', json_output.get('name'))
self.NAME + 'x',
re.search(self.re_name, raw_output).group(1),
)
# Test list # Test list
raw_output = self.openstack('port list') json_output = json.loads(self.openstack(
self.assertIsNotNone(re.search("\|\s+" + id1 + "\s+\|", raw_output)) 'port list -f json'
self.assertIsNotNone(re.search("\|\s+" + id2 + "\s+\|", raw_output)) ))
self.assertIsNotNone(re.search("\|\s+" + mac1 + "\s+\|", raw_output)) item_map = {item.get('ID'): item.get('MAC Address') for item in
self.assertIsNotNone(re.search("\|\s+" + mac2 + "\s+\|", raw_output)) json_output}
self.assertIn(id1, item_map.keys())
self.assertIn(id2, item_map.keys())
self.assertIn(mac1, item_map.values())
self.assertIn(mac2, item_map.values())
# Test list --long # Test list --long
raw_output = self.openstack('port list --long') json_output = json.loads(self.openstack(
self.assertIsNotNone(re.search("\|\s+" + id1 + "\s+\|", raw_output)) 'port list --long -f json'
self.assertIsNotNone(re.search("\|\s+" + id2 + "\s+\|", raw_output)) ))
id_list = [item.get('ID') for item in json_output]
self.assertIn(id1, id_list)
self.assertIn(id2, id_list)
# Test list --mac-address # Test list --mac-address
raw_output = self.openstack('port list --mac-address ' + mac2) json_output = json.loads(self.openstack(
self.assertIsNone(re.search("\|\s+" + id1 + "\s+\|", raw_output)) 'port list -f json --mac-address ' + mac2
self.assertIsNotNone(re.search("\|\s+" + id2 + "\s+\|", raw_output)) ))
self.assertIsNone(re.search("\|\s+" + mac1 + "\s+\|", raw_output)) item_map = {item.get('ID'): item.get('MAC Address') for item in
self.assertIsNotNone(re.search("\|\s+" + mac2 + "\s+\|", raw_output)) json_output}
self.assertNotIn(id1, item_map.keys())
self.assertIn(id2, item_map.keys())
self.assertNotIn(mac1, item_map.values())
self.assertIn(mac2, item_map.values())
def test_port_set(self): def test_port_set(self):
"""Test create, set, show, delete""" """Test create, set, show, delete"""
raw_output = self.openstack( json_output = json.loads(self.openstack(
'port create ' + 'port create -f json ' +
'--network ' + self.NETWORK_NAME + ' ' + '--network ' + self.NETWORK_NAME + ' ' +
'--description xyzpdq ' '--description xyzpdq '
'--disable ' + '--disable ' +
self.NAME self.NAME
) ))
re_id = re.search(self.re_id, raw_output) id1 = json_output.get('id')
self.assertIsNotNone(re_id) self.addCleanup(self.openstack, 'port delete ' + id1)
id = re_id.group(1) self.assertEqual(self.NAME, json_output.get('name'))
self.addCleanup(self.openstack, 'port delete ' + id) self.assertEqual('xyzpdq', json_output.get('description'))
self.assertEqual( self.assertEqual('DOWN', json_output.get('admin_state_up'))
self.NAME,
re.search(self.re_name, raw_output).group(1),
)
self.assertEqual(
'xyzpdq',
re.search(self.re_description, raw_output).group(1),
)
self.assertEqual(
'DOWN',
re.search(self.re_state, raw_output).group(1),
)
raw_output = self.openstack( raw_output = self.openstack('port set ' + '--enable ' + self.NAME)
'port set ' +
'--enable ' +
self.NAME
)
self.assertOutput('', raw_output) self.assertOutput('', raw_output)
raw_output = self.openstack( json_output = json.loads(self.openstack(
'port show ' + 'port show -f json ' + self.NAME
self.NAME ))
) self.assertEqual(self.NAME, json_output.get('name'))
self.assertEqual( self.assertEqual('xyzpdq', json_output.get('description'))
self.NAME, self.assertEqual('UP', json_output.get('admin_state_up'))
re.search(self.re_name, raw_output).group(1), self.assertIsNotNone(json_output.get('mac_address'))
)
self.assertEqual(
'xyzpdq',
re.search(self.re_description, raw_output).group(1),
)
self.assertEqual(
'UP',
re.search(self.re_state, raw_output).group(1),
)
self.assertIsNotNone(
re.search(self.re_mac_address, raw_output).group(1),
)