Define FakeFloatingIP class in tests/compute for nova network commands

"ip floating list" command is not available for Neutron now because
the implementation is incorrect.

The FloatingIP objects returned from Nova and Neutron network are
quite different. So they need different FakeFloatingIP class to
do the tests.

This patch copies class FakeFloatingIP in tests/network to tests/compute
for Nova network tests.

Will fix the problem in "ip floating list" command and change FakeFloatingIP
in tests/network to fit Neutron network tests.

Change-Id: Ia29d257868e0f1dc6cd7cfe3819875e5913f76ec
Partial-Bug: 1519502
Partially implements: blueprint neutron-client
This commit is contained in:
Tang Chen 2016-02-16 15:11:19 +08:00
parent c8753808a2
commit da3d65299b
2 changed files with 81 additions and 2 deletions

View File

@ -446,3 +446,82 @@ class FakeAvailabilityZone(object):
availability_zones.append(availability_zone)
return availability_zones
class FakeFloatingIP(object):
"""Fake one or more floating ip."""
@staticmethod
def create_one_floating_ip(attrs={}, methods={}):
"""Create a fake floating ip.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:return:
A FakeResource object, with id, ip, and so on
"""
# Set default attributes.
floating_ip_attrs = {
'id': 'floating-ip-id-' + uuid.uuid4().hex,
'ip': '1.0.9.0',
'fixed_ip': '2.0.9.0',
'instance_id': 'server-id-' + uuid.uuid4().hex,
'pool': 'public',
}
# Overwrite default attributes.
floating_ip_attrs.update(attrs)
# Set default methods.
floating_ip_methods = {}
# Overwrite default methods.
floating_ip_methods.update(methods)
floating_ip = fakes.FakeResource(
info=copy.deepcopy(floating_ip_attrs),
methods=copy.deepcopy(floating_ip_methods),
loaded=True)
return floating_ip
@staticmethod
def create_floating_ips(attrs={}, methods={}, count=2):
"""Create multiple fake floating ips.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:param int count:
The number of floating ips to fake
:return:
A list of FakeResource objects faking the floating ips
"""
floating_ips = []
for i in range(0, count):
floating_ips.append(FakeFloatingIP.create_one_floating_ip(
attrs,
methods
))
return floating_ips
@staticmethod
def get_floating_ips(floating_ips=None, count=2):
"""Get an iterable MagicMock object with a list of faked floating ips.
If floating_ips list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List floating ips:
A list of FakeResource objects faking floating ips
:param int count:
The number of floating ips to fake
:return:
An iterable Mock object with side_effect set to a list of faked
floating ips
"""
if floating_ips is None:
floating_ips = FakeFloatingIP.create_floating_ips(count)
return mock.MagicMock(side_effect=floating_ips)

View File

@ -110,7 +110,7 @@ class TestFloatingIPCompute(compute_fakes.TestComputev2):
class TestDeleteFloatingIPCompute(TestFloatingIPCompute):
# The floating ip to be deleted.
floating_ip = network_fakes.FakeFloatingIP.create_one_floating_ip()
floating_ip = compute_fakes.FakeFloatingIP.create_one_floating_ip()
def setUp(self):
super(TestDeleteFloatingIPCompute, self).setUp()
@ -145,7 +145,7 @@ class TestDeleteFloatingIPCompute(TestFloatingIPCompute):
class TestListFloatingIPCompute(TestFloatingIPCompute):
# The floating ips to be list up
floating_ips = network_fakes.FakeFloatingIP.create_floating_ips(count=3)
floating_ips = compute_fakes.FakeFloatingIP.create_floating_ips(count=3)
columns = ('ID', 'Floating IP', 'Fixed IP', 'Server ID', 'Pool')