1010# License for the specific language governing permissions and limitations
1111# under the License.
1212
13+ import re
1314import uuid
1415
1516from openstackclient .tests .functional import base
@@ -24,35 +25,152 @@ class PortTests(base.TestCase):
2425
2526 @classmethod
2627 def setUpClass (cls ):
27- # Create a network for the subnet.
28- cls .openstack ('network create ' + cls .NETWORK_NAME )
29- opts = cls .get_opts (cls .FIELDS )
30- raw_output = cls .openstack (
31- 'port create --network ' + cls .NETWORK_NAME + ' ' +
32- cls .NAME + opts
33- )
34- expected = cls .NAME + '\n '
35- cls .assertOutput (expected , raw_output )
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+
35+ # 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 )
3638
3739 @classmethod
3840 def tearDownClass (cls ):
39- raw_output = cls .openstack ('port delete ' + cls .NAME )
40- cls .assertOutput ('' , raw_output )
4141 raw_output = cls .openstack ('network delete ' + cls .NETWORK_NAME )
4242 cls .assertOutput ('' , raw_output )
4343
44+ def test_port_delete (self ):
45+ """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 + ' ' +
63+ 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+ )
73+
74+ # Clean up after ourselves
75+ raw_output = self .openstack ('port delete ' + id1 + ' ' + id2 )
76+ self .assertOutput ('' , raw_output )
77+
4478 def test_port_list (self ):
45- opts = self .get_opts (self .HEADERS )
46- raw_output = self .openstack ('port list' + opts )
47- self .assertIn (self .NAME , raw_output )
79+ """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 )
87+ self .addCleanup (self .openstack , 'port delete ' + id1 )
88+ self .assertEqual (
89+ self .NAME ,
90+ re .search (self .re_name , raw_output ).group (1 ),
91+ )
92+
93+ raw_output = self .openstack (
94+ 'port create ' +
95+ '--network ' + self .NETWORK_NAME + ' ' +
96+ 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 )
100+ 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+ )
105+
106+ # 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 ))
112+
113+ # 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 ))
117+
118+ # 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 ))
48124
49125 def test_port_set (self ):
50- self .openstack ('port set --disable ' + self .NAME )
51- opts = self .get_opts (['name' , 'admin_state_up' ])
52- raw_output = self .openstack ('port show ' + self .NAME + opts )
53- self .assertEqual ("DOWN\n " + self .NAME + "\n " , raw_output )
54-
55- def test_port_show (self ):
56- opts = self .get_opts (self .FIELDS )
57- raw_output = self .openstack ('port show ' + self .NAME + opts )
58- self .assertEqual (self .NAME + "\n " , raw_output )
126+ """Test create, set, show, delete"""
127+ raw_output = self .openstack (
128+ 'port create ' +
129+ '--network ' + self .NETWORK_NAME + ' ' +
130+ '--description xyzpdq '
131+ '--disable ' +
132+ 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+ )
156+ self .assertOutput ('' , raw_output )
157+
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+ )
0 commit comments