Refactor network fakes to sdk properties PART 1

Included resources:
address_group
address_scope
auto_allocated_topology
availability_zone

Change-Id: I943f988588efbe68dd3ab17a18441b25ac8c8d4d
This commit is contained in:
Nurmatov Mamatisa 2022-02-18 15:01:12 +03:00
parent c9b84106c3
commit 23ad68264b
11 changed files with 230 additions and 260 deletions

View File

@ -29,7 +29,7 @@ LOG = logging.getLogger(__name__)
def _get_columns(item):
column_map = {}
hidden_columns = ['location']
hidden_columns = ['location', 'tenant_id']
return utils.get_osc_show_columns_for_sdk_resource(
item,
column_map,

View File

@ -30,7 +30,7 @@ def _get_columns(item):
column_map = {
'is_shared': 'shared',
}
hidden_columns = ['location']
hidden_columns = ['location', 'tenant_id']
return utils.get_osc_show_columns_for_sdk_resource(
item,
column_map,

View File

@ -26,7 +26,7 @@ LOG = logging.getLogger(__name__)
def _get_columns(item):
column_map = {}
hidden_columns = ['location']
hidden_columns = ['name', 'location', 'tenant_id']
return utils.get_osc_show_columns_for_sdk_resource(
item,
column_map,

View File

@ -115,8 +115,7 @@ class TestAvailabilityZoneList(TestAvailabilityZone):
compute_fakes.FakeAvailabilityZone.create_availability_zones()
volume_azs = \
volume_fakes.FakeAvailabilityZone.create_availability_zones(count=1)
network_azs = \
network_fakes.FakeAvailabilityZone.create_availability_zones()
network_azs = network_fakes.create_availability_zones()
short_columnslist = ('Zone Name', 'Zone Status')
long_columnslist = (

View File

@ -18,6 +18,10 @@ from random import randint
from unittest import mock
import uuid
from openstack.network.v2 import address_group as _address_group
from openstack.network.v2 import address_scope as _address_scope
from openstack.network.v2 import auto_allocated_topology as allocated_topology
from openstack.network.v2 import availability_zone as _availability_zone
from openstack.network.v2 import local_ip as _local_ip
from openstack.network.v2 import local_ip_association as _local_ip_association
@ -86,221 +90,6 @@ class TestNetworkV2(utils.TestCommand):
)
class FakeAddressGroup(object):
"""Fake one or more address groups."""
@staticmethod
def create_one_address_group(attrs=None):
"""Create a fake address group.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object with name, id, etc.
"""
attrs = attrs or {}
# Set default attributes.
address_group_attrs = {
'name': 'address-group-name-' + uuid.uuid4().hex,
'description': 'address-group-description-' + uuid.uuid4().hex,
'id': 'address-group-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'addresses': ['10.0.0.1/32'],
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
address_group_attrs.update(attrs)
address_group = fakes.FakeResource(
info=copy.deepcopy(address_group_attrs),
loaded=True)
return address_group
@staticmethod
def create_address_groups(attrs=None, count=2):
"""Create multiple fake address groups.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of address groups to fake
:return:
A list of FakeResource objects faking the address groups
"""
address_groups = []
for i in range(0, count):
address_groups.append(
FakeAddressGroup.create_one_address_group(attrs))
return address_groups
@staticmethod
def get_address_groups(address_groups=None, count=2):
"""Get an iterable Mock object with a list of faked address groups.
If address groups list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List address_groups:
A list of FakeResource objects faking address groups
:param int count:
The number of address groups to fake
:return:
An iterable Mock object with side_effect set to a list of faked
address groups
"""
if address_groups is None:
address_groups = FakeAddressGroup.create_address_groups(count)
return mock.Mock(side_effect=address_groups)
class FakeAddressScope(object):
"""Fake one or more address scopes."""
@staticmethod
def create_one_address_scope(attrs=None):
"""Create a fake address scope.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object with name, id, etc.
"""
attrs = attrs or {}
# Set default attributes.
address_scope_attrs = {
'name': 'address-scope-name-' + uuid.uuid4().hex,
'id': 'address-scope-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'shared': False,
'ip_version': 4,
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
address_scope_attrs.update(attrs)
address_scope = fakes.FakeResource(
info=copy.deepcopy(address_scope_attrs),
loaded=True)
# Set attributes with special mapping in OpenStack SDK.
address_scope.is_shared = address_scope_attrs['shared']
return address_scope
@staticmethod
def create_address_scopes(attrs=None, count=2):
"""Create multiple fake address scopes.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of address scopes to fake
:return:
A list of FakeResource objects faking the address scopes
"""
address_scopes = []
for i in range(0, count):
address_scopes.append(
FakeAddressScope.create_one_address_scope(attrs))
return address_scopes
@staticmethod
def get_address_scopes(address_scopes=None, count=2):
"""Get an iterable Mock object with a list of faked address scopes.
If address scopes list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List address_scopes:
A list of FakeResource objects faking address scopes
:param int count:
The number of address scopes to fake
:return:
An iterable Mock object with side_effect set to a list of faked
address scopes
"""
if address_scopes is None:
address_scopes = FakeAddressScope.create_address_scopes(count)
return mock.Mock(side_effect=address_scopes)
class FakeAutoAllocatedTopology(object):
"""Fake Auto Allocated Topology"""
@staticmethod
def create_one_topology(attrs=None):
attrs = attrs or {}
auto_allocated_topology_attrs = {
'id': 'network-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
}
auto_allocated_topology_attrs.update(attrs)
auto_allocated_topology = fakes.FakeResource(
info=copy.deepcopy(auto_allocated_topology_attrs),
loaded=True)
return auto_allocated_topology
class FakeAvailabilityZone(object):
"""Fake one or more network availability zones (AZs)."""
@staticmethod
def create_one_availability_zone(attrs=None):
"""Create a fake AZ.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object with name, state, etc.
"""
attrs = attrs or {}
# Set default attributes.
availability_zone = {
'name': uuid.uuid4().hex,
'state': 'available',
'resource': 'network',
}
# Overwrite default attributes.
availability_zone.update(attrs)
availability_zone = fakes.FakeResource(
info=copy.deepcopy(availability_zone),
loaded=True)
return availability_zone
@staticmethod
def create_availability_zones(attrs=None, count=2):
"""Create multiple fake AZs.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of AZs to fake
:return:
A list of FakeResource objects faking the AZs
"""
availability_zones = []
for i in range(0, count):
availability_zone = \
FakeAvailabilityZone.create_one_availability_zone(attrs)
availability_zones.append(availability_zone)
return availability_zones
class FakeIPAvailability(object):
"""Fake one or more network ip availabilities."""
@ -2038,6 +1827,198 @@ class FakeL3ConntrackHelper(object):
return mock.Mock(side_effect=ct_helpers)
def create_one_address_group(attrs=None):
"""Create a fake address group.
:param Dictionary attrs:
A dictionary with all attributes
:return:
An AddressGroup object with name, id, etc.
"""
attrs = attrs or {}
# Set default attributes.
address_group_attrs = {
'name': 'address-group-name-' + uuid.uuid4().hex,
'description': 'address-group-description-' + uuid.uuid4().hex,
'id': 'address-group-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'addresses': ['10.0.0.1/32'],
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
address_group_attrs.update(attrs)
address_group = _address_group.AddressGroup(**address_group_attrs)
return address_group
def create_address_groups(attrs=None, count=2):
"""Create multiple fake address groups.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of address groups to fake
:return:
A list of AddressGroup objects faking the address groups
"""
address_groups = []
for i in range(0, count):
address_groups.append(create_one_address_group(attrs))
return address_groups
def get_address_groups(address_groups=None, count=2):
"""Get an iterable Mock object with a list of faked address groups.
If address groups list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List address_groups:
A list of FakeResource objects faking address groups
:param int count:
The number of address groups to fake
:return:
An iterable Mock object with side_effect set to a list of faked
AddressGroup objects
"""
if address_groups is None:
address_groups = create_address_groups(count)
return mock.Mock(side_effect=address_groups)
def create_one_address_scope(attrs=None):
"""Create a fake address scope.
:param Dictionary attrs:
A dictionary with all attributes
:return:
An AddressScope object with name, id, etc.
"""
attrs = attrs or {}
# Set default attributes.
address_scope_attrs = {
'name': 'address-scope-name-' + uuid.uuid4().hex,
'id': 'address-scope-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'shared': False,
'ip_version': 4,
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
address_scope_attrs.update(attrs)
address_scope = _address_scope.AddressScope(**address_scope_attrs)
# Set attributes with special mapping in OpenStack SDK.
address_scope.is_shared = address_scope_attrs['shared']
return address_scope
def create_address_scopes(attrs=None, count=2):
"""Create multiple fake address scopes.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of address scopes to fake
:return:
A list of AddressScope objects faking the address scopes
"""
address_scopes = []
for i in range(0, count):
address_scopes.append(create_one_address_scope(attrs))
return address_scopes
def get_address_scopes(address_scopes=None, count=2):
"""Get an iterable Mock object with a list of faked address scopes.
If address scopes list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List address_scopes:
A list of FakeResource objects faking address scopes
:param int count:
The number of address scopes to fake
:return:
An iterable Mock object with side_effect set to a list of faked
AddressScope objects
"""
if address_scopes is None:
address_scopes = create_address_scopes(count)
return mock.Mock(side_effect=address_scopes)
def create_one_topology(attrs=None):
attrs = attrs or {}
auto_allocated_topology_attrs = {
'id': 'network-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
}
auto_allocated_topology_attrs.update(attrs)
auto_allocated_topology = allocated_topology.AutoAllocatedTopology(
**auto_allocated_topology_attrs
)
return auto_allocated_topology
def create_one_availability_zone(attrs=None):
"""Create a fake AZ.
:param Dictionary attrs:
A dictionary with all attributes
:return:
An AvailabilityZone object with name, state, etc.
"""
attrs = attrs or {}
# Set default attributes.
availability_zone = {
'name': uuid.uuid4().hex,
'state': 'available',
'resource': 'network',
}
# Overwrite default attributes.
availability_zone.update(attrs)
availability_zone = _availability_zone.AvailabilityZone(
**availability_zone
)
return availability_zone
def create_availability_zones(attrs=None, count=2):
"""Create multiple fake AZs.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of AZs to fake
:return:
A list of AvailabilityZone objects faking the AZs
"""
availability_zones = []
for i in range(0, count):
availability_zone = create_one_availability_zone(attrs)
availability_zones.append(availability_zone)
return availability_zones
def create_one_local_ip(attrs=None):
"""Create a fake local ip.

View File

@ -41,7 +41,7 @@ class TestCreateAddressGroup(TestAddressGroup):
domain = identity_fakes_v3.FakeDomain.create_one_domain()
# The new address group created.
new_address_group = (
network_fakes.FakeAddressGroup.create_one_address_group(
network_fakes.create_one_address_group(
attrs={
'project_id': project.id,
}
@ -133,16 +133,13 @@ class TestCreateAddressGroup(TestAddressGroup):
class TestDeleteAddressGroup(TestAddressGroup):
# The address group to delete.
_address_groups = (
network_fakes.FakeAddressGroup.create_address_groups(count=2))
_address_groups = network_fakes.create_address_groups(count=2)
def setUp(self):
super(TestDeleteAddressGroup, self).setUp()
self.network.delete_address_group = mock.Mock(return_value=None)
self.network.find_address_group = (
network_fakes.FakeAddressGroup.get_address_groups(
address_groups=self._address_groups)
)
self.network.find_address_group = network_fakes.get_address_groups(
address_groups=self._address_groups)
# Get the command object to test
self.cmd = address_group.DeleteAddressGroup(self.app, self.namespace)
@ -216,8 +213,7 @@ class TestDeleteAddressGroup(TestAddressGroup):
class TestListAddressGroup(TestAddressGroup):
# The address groups to list up.
address_groups = (
network_fakes.FakeAddressGroup.create_address_groups(count=3))
address_groups = network_fakes.create_address_groups(count=3)
columns = (
'ID',
'Name',
@ -308,7 +304,7 @@ class TestListAddressGroup(TestAddressGroup):
class TestSetAddressGroup(TestAddressGroup):
# The address group to set.
_address_group = network_fakes.FakeAddressGroup.create_one_address_group()
_address_group = network_fakes.create_one_address_group()
def setUp(self):
super(TestSetAddressGroup, self).setUp()
@ -392,7 +388,7 @@ class TestSetAddressGroup(TestAddressGroup):
class TestShowAddressGroup(TestAddressGroup):
# The address group to show.
_address_group = network_fakes.FakeAddressGroup.create_one_address_group()
_address_group = network_fakes.create_one_address_group()
columns = (
'addresses',
'description',
@ -444,7 +440,7 @@ class TestShowAddressGroup(TestAddressGroup):
class TestUnsetAddressGroup(TestAddressGroup):
# The address group to unset.
_address_group = network_fakes.FakeAddressGroup.create_one_address_group()
_address_group = network_fakes.create_one_address_group()
def setUp(self):
super(TestUnsetAddressGroup, self).setUp()

View File

@ -40,12 +40,10 @@ class TestCreateAddressScope(TestAddressScope):
project = identity_fakes_v3.FakeProject.create_one_project()
domain = identity_fakes_v3.FakeDomain.create_one_domain()
# The new address scope created.
new_address_scope = (
network_fakes.FakeAddressScope.create_one_address_scope(
attrs={
'project_id': project.id,
}
))
new_address_scope = network_fakes.create_one_address_scope(
attrs={
'project_id': project.id,
})
columns = (
'id',
'ip_version',
@ -58,7 +56,7 @@ class TestCreateAddressScope(TestAddressScope):
new_address_scope.ip_version,
new_address_scope.name,
new_address_scope.project_id,
new_address_scope.shared,
new_address_scope.is_shared,
)
def setUp(self):
@ -153,16 +151,13 @@ class TestCreateAddressScope(TestAddressScope):
class TestDeleteAddressScope(TestAddressScope):
# The address scope to delete.
_address_scopes = (
network_fakes.FakeAddressScope.create_address_scopes(count=2))
_address_scopes = network_fakes.create_address_scopes(count=2)
def setUp(self):
super(TestDeleteAddressScope, self).setUp()
self.network.delete_address_scope = mock.Mock(return_value=None)
self.network.find_address_scope = (
network_fakes.FakeAddressScope.get_address_scopes(
address_scopes=self._address_scopes)
)
self.network.find_address_scope = network_fakes.get_address_scopes(
address_scopes=self._address_scopes)
# Get the command object to test
self.cmd = address_scope.DeleteAddressScope(self.app, self.namespace)
@ -237,8 +232,7 @@ class TestDeleteAddressScope(TestAddressScope):
class TestListAddressScope(TestAddressScope):
# The address scopes to list up.
address_scopes = (
network_fakes.FakeAddressScope.create_address_scopes(count=3))
address_scopes = network_fakes.create_address_scopes(count=3)
columns = (
'ID',
'Name',
@ -252,7 +246,7 @@ class TestListAddressScope(TestAddressScope):
scope.id,
scope.name,
scope.ip_version,
scope.shared,
scope.is_shared,
scope.project_id,
))
@ -377,7 +371,7 @@ class TestListAddressScope(TestAddressScope):
class TestSetAddressScope(TestAddressScope):
# The address scope to set.
_address_scope = network_fakes.FakeAddressScope.create_one_address_scope()
_address_scope = network_fakes.create_one_address_scope()
def setUp(self):
super(TestSetAddressScope, self).setUp()
@ -448,7 +442,7 @@ class TestShowAddressScope(TestAddressScope):
# The address scope to show.
_address_scope = (
network_fakes.FakeAddressScope.create_one_address_scope())
network_fakes.create_one_address_scope())
columns = (
'id',
'ip_version',
@ -461,7 +455,7 @@ class TestShowAddressScope(TestAddressScope):
_address_scope.ip_version,
_address_scope.name,
_address_scope.project_id,
_address_scope.shared,
_address_scope.is_shared,
)
def setUp(self):

View File

@ -31,7 +31,7 @@ class TestCreateAutoAllocatedTopology(TestAutoAllocatedTopology):
project = identity_fakes.FakeProject.create_one_project()
network_object = network_fakes.FakeNetwork.create_one_network()
topology = network_fakes.FakeAutoAllocatedTopology.create_one_topology(
topology = network_fakes.create_one_topology(
attrs={'id': network_object.id,
'project_id': project.id}
)
@ -129,7 +129,7 @@ class TestValidateAutoAllocatedTopology(TestAutoAllocatedTopology):
project = identity_fakes.FakeProject.create_one_project()
network_object = network_fakes.FakeNetwork.create_one_network()
topology = network_fakes.FakeAutoAllocatedTopology.create_one_topology(
topology = network_fakes.create_one_topology(
attrs={'id': network_object.id,
'project_id': project.id}
)
@ -206,7 +206,7 @@ class TestDeleteAutoAllocatedTopology(TestAutoAllocatedTopology):
project = identity_fakes.FakeProject.create_one_project()
network_object = network_fakes.FakeNetwork.create_one_network()
topology = network_fakes.FakeAutoAllocatedTopology.create_one_topology(
topology = network_fakes.create_one_topology(
attrs={'id': network_object.id,
'project_id': project.id}
)

View File

@ -40,9 +40,9 @@ class TestCreateNetworkRBAC(TestNetworkRBAC):
network_object = network_fakes.FakeNetwork.create_one_network()
qos_object = network_fakes.FakeNetworkQosPolicy.create_one_qos_policy()
sg_object = network_fakes.FakeNetworkSecGroup.create_one_security_group()
as_object = network_fakes.FakeAddressScope.create_one_address_scope()
as_object = network_fakes.create_one_address_scope()
snp_object = network_fakes.FakeSubnetPool.create_one_subnet_pool()
ag_object = network_fakes.FakeAddressGroup.create_one_address_group()
ag_object = network_fakes.create_one_address_group()
project = identity_fakes_v3.FakeProject.create_one_project()
rbac_policy = network_fakes.FakeNetworkRBAC.create_one_network_rbac(
attrs={'project_id': project.id,

View File

@ -47,7 +47,7 @@ class TestCreateSecurityGroupRuleNetwork(TestSecurityGroupRuleNetwork):
network_fakes.FakeSecurityGroup.create_one_security_group()
# The address group to be used in security group rules
_address_group = network_fakes.FakeAddressGroup.create_one_address_group()
_address_group = network_fakes.create_one_address_group()
expected_columns = (
'description',

View File

@ -44,7 +44,7 @@ class TestCreateSubnetPool(TestSubnetPool):
# The new subnet pool to create.
_subnet_pool = network_fakes.FakeSubnetPool.create_one_subnet_pool()
_address_scope = network_fakes.FakeAddressScope.create_one_address_scope()
_address_scope = network_fakes.create_one_address_scope()
columns = (
'address_scope_id',
@ -614,7 +614,7 @@ class TestListSubnetPool(TestSubnetPool):
self.assertCountEqual(self.data, list(data))
def test_subnet_pool_list_address_scope(self):
addr_scope = network_fakes.FakeAddressScope.create_one_address_scope()
addr_scope = network_fakes.create_one_address_scope()
self.network.find_address_scope = mock.Mock(return_value=addr_scope)
arglist = [
'--address-scope', addr_scope.id,
@ -665,7 +665,7 @@ class TestSetSubnetPool(TestSubnetPool):
'tags': ['green', 'red']}
)
_address_scope = network_fakes.FakeAddressScope.create_one_address_scope()
_address_scope = network_fakes.create_one_address_scope()
def setUp(self):
super(TestSetSubnetPool, self).setUp()