Skip to content

Commit 8bff115

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Functional test for subnet"
2 parents a8ec2c9 + b201a11 commit 8bff115

1 file changed

Lines changed: 231 additions & 37 deletions

File tree

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

Lines changed: 231 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,251 @@
1010
# License for the specific language governing permissions and limitations
1111
# under the License.
1212

13+
import json
14+
import random
1315
import uuid
1416

1517
from openstackclient.tests.functional import base
1618

1719

1820
class SubnetTests(base.TestCase):
1921
"""Functional tests for subnet. """
20-
NAME = uuid.uuid4().hex
21-
NETWORK_NAME = uuid.uuid4().hex
22-
HEADERS = ['Name']
23-
FIELDS = ['name']
2422

2523
@classmethod
2624
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-
'subnet create --network ' + cls.NETWORK_NAME +
32-
' --subnet-range 10.10.10.0/24 ' +
33-
cls.NAME + opts
34-
)
35-
expected = cls.NAME + '\n'
36-
cls.assertOutput(expected, raw_output)
25+
# Create a network for the all subnet tests.
26+
cls.NETWORK_NAME = uuid.uuid4().hex
27+
cmd_output = json.loads(cls.openstack(
28+
'network create -f json ' +
29+
cls.NETWORK_NAME
30+
))
31+
# Get network_id for assertEqual
32+
cls.NETWORK_ID = cmd_output["id"]
3733

3834
@classmethod
3935
def tearDownClass(cls):
40-
raw_output = cls.openstack('subnet delete ' + cls.NAME)
41-
cls.assertOutput('', raw_output)
4236
raw_output = cls.openstack('network delete ' + cls.NETWORK_NAME)
4337
cls.assertOutput('', raw_output)
4438

39+
def test_subnet_create_and_delete(self):
40+
"""Test create, delete"""
41+
name1 = uuid.uuid4().hex
42+
cmd = ('subnet create -f json --network ' +
43+
self.NETWORK_NAME +
44+
' --subnet-range')
45+
cmd_output = self._subnet_create(cmd, name1)
46+
self.assertEqual(
47+
name1,
48+
cmd_output["name"],
49+
)
50+
self.assertEqual(
51+
self.NETWORK_ID,
52+
cmd_output["network_id"],
53+
)
54+
55+
del_output = self.openstack(
56+
'subnet delete ' + name1)
57+
self.assertOutput('', del_output)
58+
4559
def test_subnet_list(self):
46-
opts = self.get_opts(self.HEADERS)
47-
raw_output = self.openstack('subnet list' + opts)
48-
self.assertIn(self.NAME, raw_output)
49-
50-
def test_subnet_set(self):
51-
self.openstack('subnet set --no-dhcp ' + self.NAME)
52-
opts = self.get_opts(['name', 'enable_dhcp'])
53-
raw_output = self.openstack('subnet show ' + self.NAME + opts)
54-
self.assertEqual("False\n" + self.NAME + "\n", raw_output)
55-
56-
def test_subnet_set_service_type(self):
57-
TYPE = 'network:floatingip_agent_gateway'
58-
self.openstack('subnet set --service-type ' + TYPE + ' ' + self.NAME)
59-
opts = self.get_opts(['name', 'service_types'])
60-
raw_output = self.openstack('subnet show ' + self.NAME + opts)
61-
self.assertEqual(self.NAME + "\n" + TYPE + "\n", raw_output)
62-
63-
def test_subnet_show(self):
64-
opts = self.get_opts(self.FIELDS)
65-
raw_output = self.openstack('subnet show ' + self.NAME + opts)
66-
self.assertEqual(self.NAME + "\n", raw_output)
60+
"""Test create, list filter"""
61+
name1 = uuid.uuid4().hex
62+
name2 = uuid.uuid4().hex
63+
cmd = ('subnet create -f json ' +
64+
'--network ' + self.NETWORK_NAME +
65+
' --dhcp --subnet-range')
66+
cmd_output = self._subnet_create(cmd, name1)
67+
self.assertEqual(
68+
name1,
69+
cmd_output["name"],
70+
)
71+
self.assertEqual(
72+
True,
73+
cmd_output["enable_dhcp"],
74+
)
75+
self.assertEqual(
76+
self.NETWORK_ID,
77+
cmd_output["network_id"],
78+
)
79+
self.assertEqual(
80+
4,
81+
cmd_output["ip_version"],
82+
)
83+
84+
cmd = ('subnet create -f json ' +
85+
'--network ' + self.NETWORK_NAME +
86+
' --ip-version 6 --no-dhcp ' +
87+
'--subnet-range')
88+
cmd_output = self._subnet_create(cmd, name2, is_type_ipv4=False)
89+
self.assertEqual(
90+
name2,
91+
cmd_output["name"],
92+
)
93+
self.assertEqual(
94+
False,
95+
cmd_output["enable_dhcp"],
96+
)
97+
self.assertEqual(
98+
self.NETWORK_ID,
99+
cmd_output["network_id"],
100+
)
101+
self.assertEqual(
102+
6,
103+
cmd_output["ip_version"],
104+
)
105+
106+
# Test list --long
107+
cmd_output = json.loads(self.openstack(
108+
'subnet list -f json ' +
109+
'--long '
110+
))
111+
names = [x["Name"] for x in cmd_output]
112+
self.assertIn(name1, names)
113+
self.assertIn(name2, names)
114+
115+
# Test list --name
116+
cmd_output = json.loads(self.openstack(
117+
'subnet list -f json ' +
118+
'--name ' + name1
119+
))
120+
names = [x["Name"] for x in cmd_output]
121+
self.assertIn(name1, names)
122+
self.assertNotIn(name2, names)
123+
124+
# Test list --ip-version
125+
cmd_output = json.loads(self.openstack(
126+
'subnet list -f json ' +
127+
'--ip-version 6'
128+
))
129+
names = [x["Name"] for x in cmd_output]
130+
self.assertNotIn(name1, names)
131+
self.assertIn(name2, names)
132+
133+
# Test list --network
134+
cmd_output = json.loads(self.openstack(
135+
'subnet list -f json ' +
136+
'--network ' + self.NETWORK_ID
137+
))
138+
names = [x["Name"] for x in cmd_output]
139+
self.assertIn(name1, names)
140+
self.assertIn(name2, names)
141+
142+
# Test list --no-dhcp
143+
cmd_output = json.loads(self.openstack(
144+
'subnet list -f json ' +
145+
'--no-dhcp '
146+
))
147+
names = [x["Name"] for x in cmd_output]
148+
self.assertNotIn(name1, names)
149+
self.assertIn(name2, names)
150+
151+
del_output = self.openstack(
152+
'subnet delete ' + name1 + ' ' + name2)
153+
self.assertOutput('', del_output)
154+
155+
def test_subnet_set_show_unset(self):
156+
"""Test create subnet, set, unset, show, delete"""
157+
158+
name = uuid.uuid4().hex
159+
new_name = name + "_"
160+
cmd = ('subnet create -f json ' +
161+
'--network ' + self.NETWORK_NAME +
162+
' --description aaaa --subnet-range')
163+
cmd_output = self._subnet_create(cmd, name)
164+
self.assertEqual(
165+
name,
166+
cmd_output["name"],
167+
)
168+
self.assertEqual(
169+
'aaaa',
170+
cmd_output["description"],
171+
)
172+
173+
# Test set --no-dhcp --name --gateway --description
174+
cmd_output = self.openstack(
175+
'subnet set ' +
176+
'--name ' + new_name +
177+
' --description bbbb ' +
178+
'--no-dhcp ' +
179+
'--gateway 10.10.11.1 ' +
180+
'--service-type network:floatingip_agent_gateway ' +
181+
name
182+
)
183+
self.assertOutput('', cmd_output)
184+
185+
cmd_output = json.loads(self.openstack(
186+
'subnet show -f json ' +
187+
new_name
188+
))
189+
self.assertEqual(
190+
new_name,
191+
cmd_output["name"],
192+
)
193+
self.assertEqual(
194+
'bbbb',
195+
cmd_output["description"],
196+
)
197+
self.assertEqual(
198+
False,
199+
cmd_output["enable_dhcp"],
200+
)
201+
self.assertEqual(
202+
'10.10.11.1',
203+
cmd_output["gateway_ip"],
204+
)
205+
self.assertEqual(
206+
'network:floatingip_agent_gateway',
207+
cmd_output["service_types"],
208+
)
209+
210+
# Test unset
211+
cmd_output = self.openstack(
212+
'subnet unset ' +
213+
'--service-type network:floatingip_agent_gateway ' +
214+
new_name
215+
)
216+
self.assertOutput('', cmd_output)
217+
218+
cmd_output = json.loads(self.openstack(
219+
'subnet show -f json ' +
220+
new_name
221+
))
222+
self.assertEqual(
223+
'',
224+
cmd_output["service_types"],
225+
)
226+
227+
del_output = self.openstack(
228+
'subnet delete ' + new_name)
229+
self.assertOutput('', del_output)
230+
231+
def _subnet_create(self, cmd, name, is_type_ipv4=True):
232+
# Try random subnet range for subnet creating
233+
# Because we can not determine ahead of time what subnets are already
234+
# in use, possibly by another test running in parallel, try 4 times
235+
for i in range(4):
236+
# Make a random subnet
237+
if is_type_ipv4:
238+
subnet = ".".join(map(
239+
str,
240+
(random.randint(0, 223) for _ in range(3))
241+
)) + ".0/26"
242+
else:
243+
subnet = ":".join(map(
244+
str,
245+
(hex(random.randint(0, 65535))[2:] for _ in range(7))
246+
)) + ":0/112"
247+
try:
248+
cmd_output = json.loads(self.openstack(
249+
cmd + ' ' + subnet + ' ' +
250+
name
251+
))
252+
except Exception:
253+
if (i == 3):
254+
# raise the exception at the last time
255+
raise
256+
pass
257+
else:
258+
# break and no longer retry if create sucessfully
259+
break
260+
return cmd_output

0 commit comments

Comments
 (0)