Skip to content

Commit bdf129b

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Remove races in floating ip functional tests"
2 parents a88e958 + 31c47ad commit bdf129b

1 file changed

Lines changed: 67 additions & 53 deletions

File tree

openstackclient/tests/functional/network/v2/test_floating_ip.py

Lines changed: 67 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@ class FloatingIpTests(common.NetworkTests):
2424
def setUpClass(cls):
2525
common.NetworkTests.setUpClass()
2626
if cls.haz_network:
27+
# Create common networks that all tests share
2728
cls.EXTERNAL_NETWORK_NAME = uuid.uuid4().hex
28-
cls.EXTERNAL_SUBNET_NAME = uuid.uuid4().hex
2929
cls.PRIVATE_NETWORK_NAME = uuid.uuid4().hex
30-
cls.PRIVATE_SUBNET_NAME = uuid.uuid4().hex
31-
cls.ROUTER = uuid.uuid4().hex
32-
cls.PORT_NAME = uuid.uuid4().hex
3330

3431
# Create a network for the floating ip
3532
json_output = json.loads(cls.openstack(
@@ -46,56 +43,10 @@ def setUpClass(cls):
4643
))
4744
cls.private_network_id = json_output["id"]
4845

49-
# Try random subnet range for subnet creating
50-
# Because we can not determine ahead of time what subnets are
51-
# already in use, possibly by another test running in parallel,
52-
# try 4 times
53-
for i in range(4):
54-
# Make a random subnet
55-
cls.external_subnet = ".".join(map(
56-
str,
57-
(random.randint(0, 223) for _ in range(3))
58-
)) + ".0/26"
59-
cls.private_subnet = ".".join(map(
60-
str,
61-
(random.randint(0, 223) for _ in range(3))
62-
)) + ".0/26"
63-
try:
64-
# Create a subnet for the network
65-
json_output = json.loads(cls.openstack(
66-
'subnet create -f json ' +
67-
'--network ' + cls.EXTERNAL_NETWORK_NAME + ' ' +
68-
'--subnet-range ' + cls.external_subnet + ' ' +
69-
cls.EXTERNAL_SUBNET_NAME
70-
))
71-
cls.external_subnet_id = json_output["id"]
72-
# Create a subnet for the private network
73-
json_output = json.loads(cls.openstack(
74-
'subnet create -f json ' +
75-
'--network ' + cls.PRIVATE_NETWORK_NAME + ' ' +
76-
'--subnet-range ' + cls.private_subnet + ' ' +
77-
cls.PRIVATE_SUBNET_NAME
78-
))
79-
cls.private_subnet_id = json_output["id"]
80-
except Exception:
81-
if (i == 3):
82-
# raise the exception at the last time
83-
raise
84-
pass
85-
else:
86-
# break and no longer retry if create successfully
87-
break
88-
8946
@classmethod
9047
def tearDownClass(cls):
9148
try:
9249
if cls.haz_network:
93-
del_output = cls.openstack(
94-
'subnet delete ' +
95-
cls.EXTERNAL_SUBNET_NAME + ' ' +
96-
cls.PRIVATE_SUBNET_NAME
97-
)
98-
cls.assertOutput('', del_output)
9950
del_output = cls.openstack(
10051
'network delete ' +
10152
cls.EXTERNAL_NETWORK_NAME + ' ' +
@@ -114,11 +65,50 @@ def setUp(self):
11465
# Verify setup
11566
self.assertIsNotNone(self.external_network_id)
11667
self.assertIsNotNone(self.private_network_id)
117-
self.assertIsNotNone(self.external_subnet_id)
118-
self.assertIsNotNone(self.private_subnet_id)
68+
69+
def _create_subnet(self, network_name, subnet_name):
70+
subnet_id = None
71+
72+
# Try random subnet range for subnet creating
73+
# Because we can not determine ahead of time what subnets are
74+
# already in use, possibly by another test running in parallel,
75+
# try 4 times
76+
for i in range(4):
77+
# Make a random subnet
78+
subnet = ".".join(map(
79+
str,
80+
(random.randint(0, 223) for _ in range(3))
81+
)) + ".0/26"
82+
try:
83+
# Create a subnet for the network
84+
json_output = json.loads(self.openstack(
85+
'subnet create -f json ' +
86+
'--network ' + network_name + ' ' +
87+
'--subnet-range ' + subnet + ' ' +
88+
subnet_name
89+
))
90+
self.assertIsNotNone(json_output["id"])
91+
subnet_id = json_output["id"]
92+
except Exception:
93+
if (i == 3):
94+
# raise the exception at the last time
95+
raise
96+
pass
97+
else:
98+
# break and no longer retry if create successfully
99+
break
100+
return subnet_id
119101

120102
def test_floating_ip_delete(self):
121103
"""Test create, delete multiple"""
104+
105+
# Subnets must exist even if not directly referenced here
106+
ext_subnet_id = self._create_subnet(
107+
self.EXTERNAL_NETWORK_NAME,
108+
"ext-test-delete"
109+
)
110+
self.addCleanup(self.openstack, 'subnet delete ' + ext_subnet_id)
111+
122112
json_output = json.loads(self.openstack(
123113
'floating ip create -f json ' +
124114
'--description aaaa ' +
@@ -151,6 +141,14 @@ def test_floating_ip_delete(self):
151141

152142
def test_floating_ip_list(self):
153143
"""Test create defaults, list filters, delete"""
144+
145+
# Subnets must exist even if not directly referenced here
146+
ext_subnet_id = self._create_subnet(
147+
self.EXTERNAL_NETWORK_NAME,
148+
"ext-test-delete"
149+
)
150+
self.addCleanup(self.openstack, 'subnet delete ' + ext_subnet_id)
151+
154152
json_output = json.loads(self.openstack(
155153
'floating ip create -f json ' +
156154
'--description aaaa ' +
@@ -237,6 +235,22 @@ def test_floating_ip_list(self):
237235

238236
def test_floating_ip_set_and_unset_port(self):
239237
"""Test Floating IP Set and Unset port"""
238+
239+
# Subnets must exist even if not directly referenced here
240+
ext_subnet_id = self._create_subnet(
241+
self.EXTERNAL_NETWORK_NAME,
242+
"ext-test-delete"
243+
)
244+
self.addCleanup(self.openstack, 'subnet delete ' + ext_subnet_id)
245+
priv_subnet_id = self._create_subnet(
246+
self.PRIVATE_NETWORK_NAME,
247+
"priv-test-delete"
248+
)
249+
self.addCleanup(self.openstack, 'subnet delete ' + priv_subnet_id)
250+
251+
self.ROUTER = uuid.uuid4().hex
252+
self.PORT_NAME = uuid.uuid4().hex
253+
240254
json_output = json.loads(self.openstack(
241255
'floating ip create -f json ' +
242256
'--description aaaa ' +
@@ -253,7 +267,7 @@ def test_floating_ip_set_and_unset_port(self):
253267
json_output = json.loads(self.openstack(
254268
'port create -f json ' +
255269
'--network ' + self.PRIVATE_NETWORK_NAME + ' ' +
256-
'--fixed-ip subnet=' + self.PRIVATE_SUBNET_NAME + ' ' +
270+
'--fixed-ip subnet=' + priv_subnet_id + ' ' +
257271
self.PORT_NAME
258272
))
259273
self.assertIsNotNone(json_output["id"])

0 commit comments

Comments
 (0)