@@ -21,6 +21,10 @@ class FloatingIpTests(base.TestCase):
2121 """Functional tests for floating ip"""
2222 SUBNET_NAME = uuid .uuid4 ().hex
2323 NETWORK_NAME = uuid .uuid4 ().hex
24+ PRIVATE_NETWORK_NAME = uuid .uuid4 ().hex
25+ PRIVATE_SUBNET_NAME = uuid .uuid4 ().hex
26+ ROUTER = uuid .uuid4 ().hex
27+ PORT_NAME = uuid .uuid4 ().hex
2428
2529 @classmethod
2630 def setUpClass (cls ):
@@ -30,13 +34,21 @@ def setUpClass(cls):
3034 cls .re_fixed_ip = re .compile ("fixed_ip_address\s+\|\s+(\S+)" )
3135 cls .re_description = re .compile ("description\s+\|\s+([^|]+?)\s+\|" )
3236 cls .re_network_id = re .compile ("floating_network_id\s+\|\s+(\S+)" )
37+ cls .re_port_id = re .compile ("\s+id\s+\|\s+(\S+)" )
38+ cls .re_fp_port_id = re .compile ("\s+port_id\s+\|\s+(\S+)" )
3339
3440 # Create a network for the floating ip
3541 raw_output = cls .openstack (
3642 'network create --external ' + cls .NETWORK_NAME
3743 )
3844 cls .network_id = re .search (cls .re_id , raw_output ).group (1 )
3945
46+ # Create a private network for the port
47+ raw_output = cls .openstack (
48+ 'network create ' + cls .PRIVATE_NETWORK_NAME
49+ )
50+ cls .private_network_id = re .search (cls .re_id , raw_output ).group (1 )
51+
4052 # Try random subnet range for subnet creating
4153 # Because we can not determine ahead of time what subnets are already
4254 # in use, possibly by another test running in parallel, try 4 times
@@ -46,6 +58,10 @@ def setUpClass(cls):
4658 str ,
4759 (random .randint (0 , 223 ) for _ in range (3 ))
4860 )) + ".0/26"
61+ cls .private_subnet = "." .join (map (
62+ str ,
63+ (random .randint (0 , 223 ) for _ in range (3 ))
64+ )) + ".0/26"
4965 try :
5066 # Create a subnet for the network
5167 raw_output = cls .openstack (
@@ -54,6 +70,13 @@ def setUpClass(cls):
5470 '--subnet-range ' + cls .subnet + ' ' +
5571 cls .SUBNET_NAME
5672 )
73+ # Create a subnet for the private network
74+ priv_raw_output = cls .openstack (
75+ 'subnet create ' +
76+ '--network ' + cls .PRIVATE_NETWORK_NAME + ' ' +
77+ '--subnet-range ' + cls .private_subnet + ' ' +
78+ cls .PRIVATE_SUBNET_NAME
79+ )
5780 except Exception :
5881 if (i == 3 ):
5982 # raise the exception at the last time
@@ -64,13 +87,19 @@ def setUpClass(cls):
6487 break
6588
6689 cls .subnet_id = re .search (cls .re_id , raw_output ).group (1 )
90+ cls .private_subnet_id = re .search (cls .re_id , priv_raw_output ).group (1 )
6791
6892 @classmethod
6993 def tearDownClass (cls ):
7094 raw_output = cls .openstack ('subnet delete ' + cls .SUBNET_NAME )
7195 cls .assertOutput ('' , raw_output )
96+ raw_output = cls .openstack ('subnet delete ' + cls .PRIVATE_SUBNET_NAME )
97+ cls .assertOutput ('' , raw_output )
7298 raw_output = cls .openstack ('network delete ' + cls .NETWORK_NAME )
7399 cls .assertOutput ('' , raw_output )
100+ raw_output = cls .openstack (
101+ 'network delete ' + cls .PRIVATE_NETWORK_NAME )
102+ cls .assertOutput ('' , raw_output )
74103
75104 def test_floating_ip_delete (self ):
76105 """Test create, delete multiple"""
@@ -168,3 +197,50 @@ def test_floating_ip_show(self):
168197 # re.search(self.re_floating_ip, raw_output).group(1),
169198 # )
170199 self .assertIsNotNone (re .search (self .re_network_id , raw_output ))
200+
201+ def test_floating_ip_set_and_unset_port (self ):
202+ """Test Floating IP Set and Unset port"""
203+ raw_output = self .openstack (
204+ 'floating ip create ' +
205+ '--description shosho ' +
206+ self .NETWORK_NAME
207+ )
208+ re_ip = re .search (self .re_floating_ip , raw_output )
209+ fp_ip = re_ip .group (1 )
210+ self .addCleanup (self .openstack , 'floating ip delete ' + fp_ip )
211+ self .assertIsNotNone (fp_ip )
212+
213+ raw_output1 = self .openstack (
214+ 'port create --network ' + self .PRIVATE_NETWORK_NAME
215+ + ' --fixed-ip subnet=' + self .PRIVATE_SUBNET_NAME +
216+ ' ' + self .PORT_NAME
217+ )
218+ re_port_id = re .search (self .re_port_id , raw_output1 )
219+ self .assertIsNotNone (re_port_id )
220+ port_id = re_port_id .group (1 )
221+
222+ router = self .openstack ('router create ' + self .ROUTER )
223+ self .assertIsNotNone (router )
224+ self .addCleanup (self .openstack , 'router delete ' + self .ROUTER )
225+
226+ self .openstack ('router add port ' + self .ROUTER +
227+ ' ' + port_id )
228+ self .openstack ('router set --external-gateway ' + self .NETWORK_NAME +
229+ ' ' + self .ROUTER )
230+
231+ self .addCleanup (self .openstack , 'router unset --external-gateway '
232+ + self .ROUTER )
233+ self .addCleanup (self .openstack , 'router remove port ' + self .ROUTER
234+ + ' ' + port_id )
235+
236+ raw_output = self .openstack (
237+ 'floating ip set ' +
238+ fp_ip + ' --port ' + port_id )
239+ self .addCleanup (self .openstack , 'floating ip unset --port ' + fp_ip )
240+
241+ show_output = self .openstack (
242+ 'floating ip show ' + fp_ip )
243+
244+ self .assertEqual (
245+ port_id ,
246+ re .search (self .re_fp_port_id , show_output ).group (1 ))
0 commit comments