Skip to content

Commit 6fe0ae4

Browse files
committed
Refactor network fakes to sdk properties PART 2
Included resources: ip_availibility network Change-Id: I141bcb43272594da75e776a84a74158fc866ac94
1 parent 23ad682 commit 6fe0ae4

13 files changed

Lines changed: 221 additions & 220 deletions

openstackclient/network/v2/ip_availability.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
def _get_columns(item):
2929
column_map = {}
30-
hidden_columns = ['location']
30+
hidden_columns = ['id', 'name', 'location', 'tenant_id']
3131
return utils.get_osc_show_columns_for_sdk_resource(
3232
item,
3333
column_map,

openstackclient/tests/unit/network/v2/fakes.py

Lines changed: 132 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from openstack.network.v2 import availability_zone as _availability_zone
2525
from openstack.network.v2 import local_ip as _local_ip
2626
from openstack.network.v2 import local_ip_association as _local_ip_association
27+
from openstack.network.v2 import network as _network
28+
from openstack.network.v2 import network_ip_availability as _ip_availability
2729

2830
from openstackclient.tests.unit import fakes
2931
from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes_v3
@@ -90,56 +92,6 @@ def setUp(self):
9092
)
9193

9294

93-
class FakeIPAvailability(object):
94-
"""Fake one or more network ip availabilities."""
95-
96-
@staticmethod
97-
def create_one_ip_availability(attrs=None):
98-
"""Create a fake list with ip availability stats of a network.
99-
100-
:param Dictionary attrs:
101-
A dictionary with all attributes
102-
:return:
103-
A FakeResource object with network_name, network_id, etc.
104-
"""
105-
attrs = attrs or {}
106-
107-
# Set default attributes.
108-
network_ip_attrs = {
109-
'network_id': 'network-id-' + uuid.uuid4().hex,
110-
'network_name': 'network-name-' + uuid.uuid4().hex,
111-
'project_id': '',
112-
'subnet_ip_availability': [],
113-
'total_ips': 254,
114-
'used_ips': 6,
115-
'location': 'MUNCHMUNCHMUNCH',
116-
}
117-
network_ip_attrs.update(attrs)
118-
119-
network_ip_availability = fakes.FakeResource(
120-
info=copy.deepcopy(network_ip_attrs),
121-
loaded=True)
122-
123-
return network_ip_availability
124-
125-
@staticmethod
126-
def create_ip_availability(count=2):
127-
"""Create fake list of ip availability stats of multiple networks.
128-
129-
:param int count:
130-
The number of networks to fake
131-
:return:
132-
A list of FakeResource objects faking network ip availability stats
133-
"""
134-
network_ip_availabilities = []
135-
for i in range(0, count):
136-
network_ip_availability = \
137-
FakeIPAvailability.create_one_ip_availability()
138-
network_ip_availabilities.append(network_ip_availability)
139-
140-
return network_ip_availabilities
141-
142-
14395
class FakeExtension(object):
14496
"""Fake one or more extension."""
14597

@@ -173,111 +125,6 @@ def create_one_extension(attrs=None):
173125
return extension
174126

175127

176-
class FakeNetwork(object):
177-
"""Fake one or more networks."""
178-
179-
@staticmethod
180-
def create_one_network(attrs=None):
181-
"""Create a fake network.
182-
183-
:param Dictionary attrs:
184-
A dictionary with all attributes
185-
:return:
186-
A FakeResource object, with id, name, etc.
187-
"""
188-
attrs = attrs or {}
189-
190-
# Set default attributes.
191-
network_attrs = {
192-
'id': 'network-id-' + uuid.uuid4().hex,
193-
'name': 'network-name-' + uuid.uuid4().hex,
194-
'status': 'ACTIVE',
195-
'description': 'network-description-' + uuid.uuid4().hex,
196-
'dns_domain': 'example.org.',
197-
'mtu': '1350',
198-
'project_id': 'project-id-' + uuid.uuid4().hex,
199-
'admin_state_up': True,
200-
'shared': False,
201-
'subnets': ['a', 'b'],
202-
'provider:network_type': 'vlan',
203-
'provider:physical_network': 'physnet1',
204-
'provider:segmentation_id': "400",
205-
'router:external': True,
206-
'availability_zones': [],
207-
'availability_zone_hints': [],
208-
'is_default': False,
209-
'port_security_enabled': True,
210-
'qos_policy_id': 'qos-policy-id-' + uuid.uuid4().hex,
211-
'ipv4_address_scope': 'ipv4' + uuid.uuid4().hex,
212-
'ipv6_address_scope': 'ipv6' + uuid.uuid4().hex,
213-
'tags': [],
214-
'location': 'MUNCHMUNCHMUNCH',
215-
}
216-
217-
# Overwrite default attributes.
218-
network_attrs.update(attrs)
219-
220-
network = fakes.FakeResource(info=copy.deepcopy(network_attrs),
221-
loaded=True)
222-
223-
# Set attributes with special mapping in OpenStack SDK.
224-
network.is_router_external = network_attrs['router:external']
225-
network.is_admin_state_up = network_attrs['admin_state_up']
226-
network.is_port_security_enabled = \
227-
network_attrs['port_security_enabled']
228-
network.subnet_ids = network_attrs['subnets']
229-
network.is_shared = network_attrs['shared']
230-
network.is_tags = network_attrs['tags']
231-
network.provider_network_type = \
232-
network_attrs['provider:network_type']
233-
network.provider_physical_network = \
234-
network_attrs['provider:physical_network']
235-
network.provider_segmentation_id = \
236-
network_attrs['provider:segmentation_id']
237-
network.ipv4_address_scope_id = \
238-
network_attrs['ipv4_address_scope']
239-
network.ipv6_address_scope_id = \
240-
network_attrs['ipv6_address_scope']
241-
242-
return network
243-
244-
@staticmethod
245-
def create_networks(attrs=None, count=2):
246-
"""Create multiple fake networks.
247-
248-
:param Dictionary attrs:
249-
A dictionary with all attributes
250-
:param int count:
251-
The number of networks to fake
252-
:return:
253-
A list of FakeResource objects faking the networks
254-
"""
255-
networks = []
256-
for i in range(0, count):
257-
networks.append(FakeNetwork.create_one_network(attrs))
258-
259-
return networks
260-
261-
@staticmethod
262-
def get_networks(networks=None, count=2):
263-
"""Get an iterable Mock object with a list of faked networks.
264-
265-
If networks list is provided, then initialize the Mock object with the
266-
list. Otherwise create one.
267-
268-
:param List networks:
269-
A list of FakeResource objects faking networks
270-
:param int count:
271-
The number of networks to fake
272-
:return:
273-
An iterable Mock object with side_effect set to a list of faked
274-
networks
275-
"""
276-
if networks is None:
277-
networks = FakeNetwork.create_networks(count)
278-
return mock.Mock(side_effect=networks)
279-
280-
281128
class FakeNetworkFlavor(object):
282129
"""Fake Network Flavor."""
283130

@@ -2019,6 +1866,136 @@ def create_availability_zones(attrs=None, count=2):
20191866
return availability_zones
20201867

20211868

1869+
def create_one_ip_availability(attrs=None):
1870+
"""Create a fake list with ip availability stats of a network.
1871+
1872+
:param Dictionary attrs:
1873+
A dictionary with all attributes
1874+
:return:
1875+
A NetworkIPAvailability object with network_name, network_id, etc.
1876+
"""
1877+
attrs = attrs or {}
1878+
1879+
# Set default attributes.
1880+
network_ip_attrs = {
1881+
'network_id': 'network-id-' + uuid.uuid4().hex,
1882+
'network_name': 'network-name-' + uuid.uuid4().hex,
1883+
'project_id': '',
1884+
'subnet_ip_availability': [],
1885+
'total_ips': 254,
1886+
'used_ips': 6,
1887+
'location': 'MUNCHMUNCHMUNCH',
1888+
}
1889+
network_ip_attrs.update(attrs)
1890+
1891+
network_ip_availability = _ip_availability.NetworkIPAvailability(
1892+
**network_ip_attrs)
1893+
1894+
return network_ip_availability
1895+
1896+
1897+
def create_ip_availability(count=2):
1898+
"""Create fake list of ip availability stats of multiple networks.
1899+
1900+
:param int count:
1901+
The number of networks to fake
1902+
:return:
1903+
A list of NetworkIPAvailability objects faking
1904+
network ip availability stats
1905+
"""
1906+
network_ip_availabilities = []
1907+
for i in range(0, count):
1908+
network_ip_availability = create_one_ip_availability()
1909+
network_ip_availabilities.append(network_ip_availability)
1910+
1911+
return network_ip_availabilities
1912+
1913+
1914+
def create_one_network(attrs=None):
1915+
"""Create a fake network.
1916+
1917+
:param Dictionary attrs:
1918+
A dictionary with all attributes
1919+
:return:
1920+
An Network object, with id, name, etc.
1921+
"""
1922+
attrs = attrs or {}
1923+
1924+
# Set default attributes.
1925+
network_attrs = {
1926+
'created_at': '2021-11-29T10:10:23.000000',
1927+
'id': 'network-id-' + uuid.uuid4().hex,
1928+
'name': 'network-name-' + uuid.uuid4().hex,
1929+
'status': 'ACTIVE',
1930+
'description': 'network-description-' + uuid.uuid4().hex,
1931+
'dns_domain': 'example.org.',
1932+
'mtu': '1350',
1933+
'project_id': 'project-id-' + uuid.uuid4().hex,
1934+
'admin_state_up': True,
1935+
'shared': False,
1936+
'subnets': ['a', 'b'],
1937+
'segments': 'network-segment-' + uuid.uuid4().hex,
1938+
'provider:network_type': 'vlan',
1939+
'provider:physical_network': 'physnet1',
1940+
'provider:segmentation_id': "400",
1941+
'router:external': True,
1942+
'availability_zones': [],
1943+
'availability_zone_hints': [],
1944+
'is_default': False,
1945+
'is_vlan_transparent': True,
1946+
'port_security_enabled': True,
1947+
'qos_policy_id': 'qos-policy-id-' + uuid.uuid4().hex,
1948+
'ipv4_address_scope': 'ipv4' + uuid.uuid4().hex,
1949+
'ipv6_address_scope': 'ipv6' + uuid.uuid4().hex,
1950+
'tags': [],
1951+
'location': 'MUNCHMUNCHMUNCH',
1952+
'updated_at': '2021-11-29T10:10:25.000000',
1953+
}
1954+
1955+
# Overwrite default attributes.
1956+
network_attrs.update(attrs)
1957+
1958+
network = _network.Network(**network_attrs)
1959+
1960+
return network
1961+
1962+
1963+
def create_networks(attrs=None, count=2):
1964+
"""Create multiple fake networks.
1965+
1966+
:param Dictionary attrs:
1967+
A dictionary with all attributes
1968+
:param int count:
1969+
The number of networks to fake
1970+
:return:
1971+
A list of Network objects faking the networks
1972+
"""
1973+
networks = []
1974+
for i in range(0, count):
1975+
networks.append(create_one_network(attrs))
1976+
1977+
return networks
1978+
1979+
1980+
def get_networks(networks=None, count=2):
1981+
"""Get an iterable Mock object with a list of faked networks.
1982+
1983+
If networks list is provided, then initialize the Mock object with the
1984+
list. Otherwise create one.
1985+
1986+
:param List networks:
1987+
A list of Network objects faking networks
1988+
:param int count:
1989+
The number of networks to fake
1990+
:return:
1991+
An iterable Mock object with side_effect set to a list of faked
1992+
networks
1993+
"""
1994+
if networks is None:
1995+
networks = create_networks(count)
1996+
return mock.Mock(side_effect=networks)
1997+
1998+
20221999
def create_one_local_ip(attrs=None):
20232000
"""Create a fake local ip.
20242001

openstackclient/tests/unit/network/v2/test_floating_ip_network.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def setUp(self):
4040
class TestCreateFloatingIPNetwork(TestFloatingIPNetwork):
4141

4242
# Fake data for option tests.
43-
floating_network = network_fakes.FakeNetwork.create_one_network()
43+
floating_network = network_fakes.create_one_network()
4444
subnet = network_fakes.FakeSubnet.create_one_subnet()
4545
port = network_fakes.FakePort.create_one_port()
4646

@@ -378,7 +378,7 @@ class TestListFloatingIPNetwork(TestFloatingIPNetwork):
378378

379379
# The floating ips to list up
380380
floating_ips = network_fakes.FakeFloatingIP.create_floating_ips(count=3)
381-
fake_network = network_fakes.FakeNetwork.create_one_network({
381+
fake_network = network_fakes.create_one_network({
382382
'id': 'fake_network_id',
383383
})
384384
fake_port = network_fakes.FakePort.create_one_port({
@@ -700,7 +700,7 @@ def test_floating_ip_show(self):
700700
class TestSetFloatingIP(TestFloatingIPNetwork):
701701

702702
# Fake data for option tests.
703-
floating_network = network_fakes.FakeNetwork.create_one_network()
703+
floating_network = network_fakes.create_one_network()
704704
subnet = network_fakes.FakeSubnet.create_one_subnet()
705705
port = network_fakes.FakePort.create_one_port()
706706

@@ -932,7 +932,7 @@ def test_set_with_no_tag(self):
932932

933933
class TestUnsetFloatingIP(TestFloatingIPNetwork):
934934

935-
floating_network = network_fakes.FakeNetwork.create_one_network()
935+
floating_network = network_fakes.create_one_network()
936936
subnet = network_fakes.FakeSubnet.create_one_subnet()
937937
port = network_fakes.FakePort.create_one_port()
938938

openstackclient/tests/unit/network/v2/test_ip_availability.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ def setUp(self):
3838

3939
class TestListIPAvailability(TestIPAvailability):
4040

41-
_ip_availability = \
42-
network_fakes.FakeIPAvailability.create_ip_availability(count=3)
41+
_ip_availability = network_fakes.create_ip_availability(count=3)
4342
columns = (
4443
'Network ID',
4544
'Network Name',
@@ -117,10 +116,9 @@ def test_list_project(self):
117116

118117
class TestShowIPAvailability(TestIPAvailability):
119118

120-
_network = network_fakes.FakeNetwork.create_one_network()
121-
_ip_availability = \
122-
network_fakes.FakeIPAvailability.create_one_ip_availability(
123-
attrs={'network_id': _network.id})
119+
_network = network_fakes.create_one_network()
120+
_ip_availability = network_fakes.create_one_ip_availability(
121+
attrs={'network_id': _network.id})
124122

125123
columns = (
126124
'network_id',

0 commit comments

Comments
 (0)