1010# License for the specific language governing permissions and limitations
1111# under the License.
1212
13- import re
13+ import json
1414import uuid
1515
1616from openstackclient .tests .functional import base
@@ -20,21 +20,11 @@ class PortTests(base.TestCase):
2020 """Functional tests for port. """
2121 NAME = uuid .uuid4 ().hex
2222 NETWORK_NAME = uuid .uuid4 ().hex
23- HEADERS = ['Name' ]
24- FIELDS = ['name' ]
2523
2624 @classmethod
2725 def setUpClass (cls ):
28- # Set up some regex for matching below
29- cls .re_id = re .compile ("\s+id\s+\|\s+(\S+)" )
30- cls .re_name = re .compile ("\s+name\s+\|\s+([^|]+?)\s+\|" )
31- cls .re_description = re .compile ("\s+description\s+\|\s+([^|]+?)\s+\|" )
32- cls .re_mac_address = re .compile ("\s+mac_address\s+\|\s+([^|]+?)\s+\|" )
33- cls .re_state = re .compile ("\s+admin_state_up\s+\|\s+([^|]+?)\s+\|" )
34-
3526 # Create a network for the port
36- raw_output = cls .openstack ('network create ' + cls .NETWORK_NAME )
37- cls .network_id = re .search (cls .re_id , raw_output ).group (1 )
27+ cls .openstack ('network create ' + cls .NETWORK_NAME )
3828
3929 @classmethod
4030 def tearDownClass (cls ):
@@ -43,134 +33,104 @@ def tearDownClass(cls):
4333
4434 def test_port_delete (self ):
4535 """Test create, delete multiple"""
46- raw_output = self .openstack (
47- 'port create --network ' + self .NETWORK_NAME + ' ' + self .NAME
48- )
49- re_id1 = re .search (self .re_id , raw_output )
50- self .assertIsNotNone (re_id1 )
51- id1 = re_id1 .group (1 )
52- self .assertIsNotNone (
53- re .search (self .re_mac_address , raw_output ).group (1 ),
54- )
55- self .assertEqual (
56- self .NAME ,
57- re .search (self .re_name , raw_output ).group (1 ),
58- )
59-
60- raw_output = self .openstack (
61- 'port create ' +
62- '--network ' + self .NETWORK_NAME + ' ' +
36+ json_output = json .loads (self .openstack (
37+ 'port create -f json --network ' +
38+ self .NETWORK_NAME + ' ' + self .NAME
39+ ))
40+ id1 = json_output .get ('id' )
41+ self .assertIsNotNone (id1 )
42+ self .assertIsNotNone (json_output .get ('mac_address' ))
43+ self .assertEqual (self .NAME , json_output .get ('name' ))
44+
45+ json_output = json .loads (self .openstack (
46+ 'port create -f json --network ' + self .NETWORK_NAME + ' ' +
6347 self .NAME + 'x'
64- )
65- id2 = re .search (self .re_id , raw_output ).group (1 )
66- self .assertIsNotNone (
67- re .search (self .re_mac_address , raw_output ).group (1 ),
68- )
69- self .assertEqual (
70- self .NAME + 'x' ,
71- re .search (self .re_name , raw_output ).group (1 ),
72- )
48+ ))
49+ id2 = json_output .get ('id' )
50+ self .assertIsNotNone (id2 )
51+ self .assertIsNotNone (json_output .get ('mac_address' ))
52+ self .assertEqual (self .NAME + 'x' , json_output .get ('name' ))
7353
7454 # Clean up after ourselves
7555 raw_output = self .openstack ('port delete ' + id1 + ' ' + id2 )
7656 self .assertOutput ('' , raw_output )
7757
7858 def test_port_list (self ):
7959 """Test create defaults, list, delete"""
80- raw_output = self .openstack (
81- 'port create --network ' + self .NETWORK_NAME + ' ' + self .NAME
82- )
83- re_id1 = re .search (self .re_id , raw_output )
84- self .assertIsNotNone (re_id1 )
85- id1 = re_id1 .group (1 )
86- mac1 = re .search (self .re_mac_address , raw_output ).group (1 )
60+ json_output = json .loads (self .openstack (
61+ 'port create -f json --network ' + self .NETWORK_NAME + ' ' +
62+ self .NAME
63+ ))
64+ id1 = json_output .get ('id' )
65+ self .assertIsNotNone (id1 )
66+ mac1 = json_output .get ('mac_address' )
67+ self .assertIsNotNone (mac1 )
8768 self .addCleanup (self .openstack , 'port delete ' + id1 )
88- self .assertEqual (
89- self .NAME ,
90- re .search (self .re_name , raw_output ).group (1 ),
91- )
69+ self .assertEqual (self .NAME , json_output .get ('name' ))
9270
93- raw_output = self .openstack (
94- 'port create ' +
95- '--network ' + self .NETWORK_NAME + ' ' +
71+ json_output = json .loads (self .openstack (
72+ 'port create -f json --network ' + self .NETWORK_NAME + ' ' +
9673 self .NAME + 'x'
97- )
98- id2 = re .search (self .re_id , raw_output ).group (1 )
99- mac2 = re .search (self .re_mac_address , raw_output ).group (1 )
74+ ))
75+ id2 = json_output .get ('id' )
76+ self .assertIsNotNone (id2 )
77+ mac2 = json_output .get ('mac_address' )
78+ self .assertIsNotNone (mac2 )
10079 self .addCleanup (self .openstack , 'port delete ' + id2 )
101- self .assertEqual (
102- self .NAME + 'x' ,
103- re .search (self .re_name , raw_output ).group (1 ),
104- )
80+ self .assertEqual (self .NAME + 'x' , json_output .get ('name' ))
10581
10682 # Test list
107- raw_output = self .openstack ('port list' )
108- self .assertIsNotNone (re .search ("\|\s+" + id1 + "\s+\|" , raw_output ))
109- self .assertIsNotNone (re .search ("\|\s+" + id2 + "\s+\|" , raw_output ))
110- self .assertIsNotNone (re .search ("\|\s+" + mac1 + "\s+\|" , raw_output ))
111- self .assertIsNotNone (re .search ("\|\s+" + mac2 + "\s+\|" , raw_output ))
83+ json_output = json .loads (self .openstack (
84+ 'port list -f json'
85+ ))
86+ item_map = {item .get ('ID' ): item .get ('MAC Address' ) for item in
87+ json_output }
88+ self .assertIn (id1 , item_map .keys ())
89+ self .assertIn (id2 , item_map .keys ())
90+ self .assertIn (mac1 , item_map .values ())
91+ self .assertIn (mac2 , item_map .values ())
11292
11393 # Test list --long
114- raw_output = self .openstack ('port list --long' )
115- self .assertIsNotNone (re .search ("\|\s+" + id1 + "\s+\|" , raw_output ))
116- self .assertIsNotNone (re .search ("\|\s+" + id2 + "\s+\|" , raw_output ))
94+ json_output = json .loads (self .openstack (
95+ 'port list --long -f json'
96+ ))
97+ id_list = [item .get ('ID' ) for item in json_output ]
98+ self .assertIn (id1 , id_list )
99+ self .assertIn (id2 , id_list )
117100
118101 # Test list --mac-address
119- raw_output = self .openstack ('port list --mac-address ' + mac2 )
120- self .assertIsNone (re .search ("\|\s+" + id1 + "\s+\|" , raw_output ))
121- self .assertIsNotNone (re .search ("\|\s+" + id2 + "\s+\|" , raw_output ))
122- self .assertIsNone (re .search ("\|\s+" + mac1 + "\s+\|" , raw_output ))
123- self .assertIsNotNone (re .search ("\|\s+" + mac2 + "\s+\|" , raw_output ))
102+ json_output = json .loads (self .openstack (
103+ 'port list -f json --mac-address ' + mac2
104+ ))
105+ item_map = {item .get ('ID' ): item .get ('MAC Address' ) for item in
106+ json_output }
107+ self .assertNotIn (id1 , item_map .keys ())
108+ self .assertIn (id2 , item_map .keys ())
109+ self .assertNotIn (mac1 , item_map .values ())
110+ self .assertIn (mac2 , item_map .values ())
124111
125112 def test_port_set (self ):
126113 """Test create, set, show, delete"""
127- raw_output = self .openstack (
128- 'port create ' +
114+ json_output = json . loads ( self .openstack (
115+ 'port create -f json ' +
129116 '--network ' + self .NETWORK_NAME + ' ' +
130117 '--description xyzpdq '
131118 '--disable ' +
132119 self .NAME
133- )
134- re_id = re .search (self .re_id , raw_output )
135- self .assertIsNotNone (re_id )
136- id = re_id .group (1 )
137- self .addCleanup (self .openstack , 'port delete ' + id )
138- self .assertEqual (
139- self .NAME ,
140- re .search (self .re_name , raw_output ).group (1 ),
141- )
142- self .assertEqual (
143- 'xyzpdq' ,
144- re .search (self .re_description , raw_output ).group (1 ),
145- )
146- self .assertEqual (
147- 'DOWN' ,
148- re .search (self .re_state , raw_output ).group (1 ),
149- )
150-
151- raw_output = self .openstack (
152- 'port set ' +
153- '--enable ' +
154- self .NAME
155- )
120+ ))
121+ id1 = json_output .get ('id' )
122+ self .addCleanup (self .openstack , 'port delete ' + id1 )
123+ self .assertEqual (self .NAME , json_output .get ('name' ))
124+ self .assertEqual ('xyzpdq' , json_output .get ('description' ))
125+ self .assertEqual ('DOWN' , json_output .get ('admin_state_up' ))
126+
127+ raw_output = self .openstack ('port set ' + '--enable ' + self .NAME )
156128 self .assertOutput ('' , raw_output )
157129
158- raw_output = self .openstack (
159- 'port show ' +
160- self .NAME
161- )
162- self .assertEqual (
163- self .NAME ,
164- re .search (self .re_name , raw_output ).group (1 ),
165- )
166- self .assertEqual (
167- 'xyzpdq' ,
168- re .search (self .re_description , raw_output ).group (1 ),
169- )
170- self .assertEqual (
171- 'UP' ,
172- re .search (self .re_state , raw_output ).group (1 ),
173- )
174- self .assertIsNotNone (
175- re .search (self .re_mac_address , raw_output ).group (1 ),
176- )
130+ json_output = json .loads (self .openstack (
131+ 'port show -f json ' + self .NAME
132+ ))
133+ self .assertEqual (self .NAME , json_output .get ('name' ))
134+ self .assertEqual ('xyzpdq' , json_output .get ('description' ))
135+ self .assertEqual ('UP' , json_output .get ('admin_state_up' ))
136+ self .assertIsNotNone (json_output .get ('mac_address' ))
0 commit comments