Change fixed/floating ip clients to return one value and update tests

Add ResponseBodyData for cases like delete_floating_ips_bulk where a
string is returned as the body. The client has to dereference the data
but I think this is a bit cleaner than having some methods return two values.

Partially implements: blueprint clients-return-one-value

Change-Id: I87b092b2dd2a231049fc39312038f2a2d5101028
This commit is contained in:
David Kranz 2015-02-10 10:50:42 -05:00
parent e290f3eec4
commit e4e3b4100f
16 changed files with 70 additions and 77 deletions

View File

@ -42,19 +42,17 @@ class FixedIPsTestJson(base.BaseV2ComputeAdminTest):
@test.attr(type='gate')
@test.services('network')
def test_list_fixed_ip_details(self):
resp, fixed_ip = self.client.get_fixed_ip_details(self.ip)
fixed_ip = self.client.get_fixed_ip_details(self.ip)
self.assertEqual(fixed_ip['address'], self.ip)
@test.attr(type='gate')
@test.services('network')
def test_set_reserve(self):
body = {"reserve": "None"}
resp, body = self.client.reserve_fixed_ip(self.ip, body)
self.assertEqual(resp.status, 202)
self.client.reserve_fixed_ip(self.ip, body)
@test.attr(type='gate')
@test.services('network')
def test_set_unreserve(self):
body = {"unreserve": "None"}
resp, body = self.client.reserve_fixed_ip(self.ip, body)
self.assertEqual(resp.status, 202)
self.client.reserve_fixed_ip(self.ip, body)

View File

@ -40,7 +40,7 @@ class FloatingIPsBulkAdminTestJSON(base.BaseV2ComputeAdminTest):
@classmethod
def verify_unallocated_floating_ip_range(cls, ip_range):
# Verify whether configure floating IP range is not already allocated.
_, body = cls.client.list_floating_ips_bulk()
body = cls.client.list_floating_ips_bulk()
allocated_ips_list = map(lambda x: x['address'], body)
for ip_addr in netaddr.IPNetwork(ip_range).iter_hosts():
if str(ip_addr) in allocated_ips_list:
@ -65,18 +65,14 @@ class FloatingIPsBulkAdminTestJSON(base.BaseV2ComputeAdminTest):
# anywhere. Using the below mentioned interface which is not ever
# expected to be used. Clean Up has been done for created IP range
interface = 'eth0'
resp, body = self.client.create_floating_ips_bulk(self.ip_range,
pool,
interface)
self.assertEqual(200, resp.status)
body = self.client.create_floating_ips_bulk(self.ip_range,
pool,
interface)
self.addCleanup(self._delete_floating_ips_bulk, self.ip_range)
self.assertEqual(self.ip_range, body['ip_range'])
resp, ips_list = self.client.list_floating_ips_bulk()
self.assertEqual(200, resp.status)
ips_list = self.client.list_floating_ips_bulk()
self.assertNotEqual(0, len(ips_list))
for ip in netaddr.IPNetwork(self.ip_range).iter_hosts():
self.assertIn(str(ip), map(lambda x: x['address'], ips_list))
resp, body = self.client.delete_floating_ips_bulk(self.ip_range)
self.assertEqual(200, resp.status)
self.assertEqual(self.ip_range, body)
body = self.client.delete_floating_ips_bulk(self.ip_range)
self.assertEqual(self.ip_range, body.data)

View File

@ -35,7 +35,7 @@ class FloatingIPsTestJSON(base.BaseFloatingIPsTest):
resp, server = cls.create_test_server(wait_until='ACTIVE')
cls.server_id = server['id']
# Floating IP creation
resp, body = cls.client.create_floating_ip()
body = cls.client.create_floating_ip()
cls.floating_ip_id = body['id']
cls.floating_ip = body['ip']
@ -43,7 +43,7 @@ class FloatingIPsTestJSON(base.BaseFloatingIPsTest):
def resource_cleanup(cls):
# Deleting the floating IP which is created in this method
if cls.floating_ip_id:
resp, body = cls.client.delete_floating_ip(cls.floating_ip_id)
cls.client.delete_floating_ip(cls.floating_ip_id)
super(FloatingIPsTestJSON, cls).resource_cleanup()
def _try_delete_floating_ip(self, floating_ip_id):
@ -59,15 +59,14 @@ class FloatingIPsTestJSON(base.BaseFloatingIPsTest):
def test_allocate_floating_ip(self):
# Positive test:Allocation of a new floating IP to a project
# should be successful
resp, body = self.client.create_floating_ip()
body = self.client.create_floating_ip()
floating_ip_id_allocated = body['id']
self.addCleanup(self.client.delete_floating_ip,
floating_ip_id_allocated)
self.assertEqual(200, resp.status)
resp, floating_ip_details = \
floating_ip_details = \
self.client.get_floating_ip_details(floating_ip_id_allocated)
# Checking if the details of allocated IP is in list of floating IP
resp, body = self.client.list_floating_ips()
body = self.client.list_floating_ips()
self.assertIn(floating_ip_details, body)
@test.attr(type='gate')
@ -76,14 +75,10 @@ class FloatingIPsTestJSON(base.BaseFloatingIPsTest):
# Positive test:Deletion of valid floating IP from project
# should be successful
# Creating the floating IP that is to be deleted in this method
resp, floating_ip_body = self.client.create_floating_ip()
floating_ip_body = self.client.create_floating_ip()
self.addCleanup(self._try_delete_floating_ip, floating_ip_body['id'])
# Storing the details of floating IP before deleting it
cli_resp = self.client.get_floating_ip_details(floating_ip_body['id'])
resp, floating_ip_details = cli_resp
# Deleting the floating IP from the project
resp, body = self.client.delete_floating_ip(floating_ip_body['id'])
self.assertEqual(202, resp.status)
self.client.delete_floating_ip(floating_ip_body['id'])
# Check it was really deleted.
self.client.wait_for_resource_deletion(floating_ip_body['id'])
@ -94,20 +89,18 @@ class FloatingIPsTestJSON(base.BaseFloatingIPsTest):
# to a specific server should be successful
# Association of floating IP to fixed IP address
resp, body = self.client.associate_floating_ip_to_server(
self.client.associate_floating_ip_to_server(
self.floating_ip,
self.server_id)
self.assertEqual(202, resp.status)
# Check instance_id in the floating_ip body
resp, body = self.client.get_floating_ip_details(self.floating_ip_id)
body = self.client.get_floating_ip_details(self.floating_ip_id)
self.assertEqual(self.server_id, body['instance_id'])
# Disassociation of floating IP that was associated in this method
resp, body = self.client.disassociate_floating_ip_from_server(
self.client.disassociate_floating_ip_from_server(
self.floating_ip,
self.server_id)
self.assertEqual(202, resp.status)
@test.attr(type='gate')
@test.services('network')
@ -119,21 +112,20 @@ class FloatingIPsTestJSON(base.BaseFloatingIPsTest):
resp, body = self.create_test_server(name=new_name)
self.servers_client.wait_for_server_status(body['id'], 'ACTIVE')
self.new_server_id = body['id']
self.addCleanup(self.servers_client.delete_server, self.new_server_id)
# Associating floating IP for the first time
resp, _ = self.client.associate_floating_ip_to_server(
self.client.associate_floating_ip_to_server(
self.floating_ip,
self.server_id)
# Associating floating IP for the second time
resp, body = self.client.associate_floating_ip_to_server(
self.client.associate_floating_ip_to_server(
self.floating_ip,
self.new_server_id)
self.addCleanup(self.servers_client.delete_server, self.new_server_id)
if (resp['status'] is not None):
self.addCleanup(self.client.disassociate_floating_ip_from_server,
self.floating_ip,
self.new_server_id)
self.addCleanup(self.client.disassociate_floating_ip_from_server,
self.floating_ip,
self.new_server_id)
# Make sure no longer associated with old server
self.assertRaises((exceptions.NotFound,

View File

@ -37,7 +37,7 @@ class FloatingIPsNegativeTestJSON(base.BaseFloatingIPsTest):
cls.server_id = server['id']
# Generating a nonexistent floatingIP id
cls.floating_ip_ids = []
resp, body = cls.client.list_floating_ips()
body = cls.client.list_floating_ips()
for i in range(len(body)):
cls.floating_ip_ids.append(body[i]['id'])
while True:

View File

@ -26,7 +26,7 @@ class FloatingIPDetailsTestJSON(base.BaseV2ComputeTest):
cls.floating_ip = []
cls.floating_ip_id = []
for i in range(3):
resp, body = cls.client.create_floating_ip()
body = cls.client.create_floating_ip()
cls.floating_ip.append(body)
cls.floating_ip_id.append(body['id'])
@ -40,8 +40,7 @@ class FloatingIPDetailsTestJSON(base.BaseV2ComputeTest):
@test.services('network')
def test_list_floating_ips(self):
# Positive test:Should return the list of floating IPs
resp, body = self.client.list_floating_ips()
self.assertEqual(200, resp.status)
body = self.client.list_floating_ips()
floating_ips = body
self.assertNotEqual(0, len(floating_ips),
"Expected floating IPs. Got zero.")
@ -53,16 +52,14 @@ class FloatingIPDetailsTestJSON(base.BaseV2ComputeTest):
def test_get_floating_ip_details(self):
# Positive test:Should be able to GET the details of floatingIP
# Creating a floating IP for which details are to be checked
resp, body = self.client.create_floating_ip()
body = self.client.create_floating_ip()
floating_ip_id = body['id']
self.addCleanup(self.client.delete_floating_ip,
floating_ip_id)
floating_ip_instance_id = body['instance_id']
floating_ip_ip = body['ip']
floating_ip_fixed_ip = body['fixed_ip']
resp, body = \
self.client.get_floating_ip_details(floating_ip_id)
self.assertEqual(200, resp.status)
body = self.client.get_floating_ip_details(floating_ip_id)
# Comparing the details of floating IP
self.assertEqual(floating_ip_instance_id,
body['instance_id'])
@ -75,7 +72,6 @@ class FloatingIPDetailsTestJSON(base.BaseV2ComputeTest):
@test.services('network')
def test_list_floating_ip_pools(self):
# Positive test:Should return the list of floating IP Pools
resp, floating_ip_pools = self.client.list_floating_ip_pools()
self.assertEqual(200, resp.status)
floating_ip_pools = self.client.list_floating_ip_pools()
self.assertNotEqual(0, len(floating_ip_pools),
"Expected floating IP Pools. Got zero.")

View File

@ -33,7 +33,7 @@ class ServerRescueTestJSON(base.BaseV2ComputeTest):
super(ServerRescueTestJSON, cls).resource_setup()
# Floating IP creation
resp, body = cls.floating_ips_client.create_floating_ip()
body = cls.floating_ips_client.create_floating_ip()
cls.floating_ip_id = str(body['id']).strip()
cls.floating_ip = str(body['ip']).strip()
@ -90,15 +90,12 @@ class ServerRescueTestJSON(base.BaseV2ComputeTest):
# Association of floating IP to a rescued vm
client = self.floating_ips_client
resp, body = client.associate_floating_ip_to_server(self.floating_ip,
self.server_id)
self.assertEqual(202, resp.status)
client.associate_floating_ip_to_server(self.floating_ip,
self.server_id)
# Disassociation of floating IP that was associated in this method
resp, body = \
client.disassociate_floating_ip_from_server(self.floating_ip,
self.server_id)
self.assertEqual(202, resp.status)
client.disassociate_floating_ip_from_server(self.floating_ip,
self.server_id)
@test.attr(type='gate')
def test_rescued_vm_add_remove_security_group(self):

View File

@ -297,7 +297,7 @@ class FloatingIpService(BaseService):
def list(self):
client = self.client
_, floating_ips = client.list_floating_ips()
floating_ips = client.list_floating_ips()
LOG.debug("List count, %s Floating IPs" % len(floating_ips))
return floating_ips

View File

@ -94,6 +94,18 @@ class ResponseBody(dict):
return "response: %s\nBody: %s" % (self.response, body)
class ResponseBodyData(object):
"""Class that wraps an http response and string data into a single value.
"""
def __init__(self, response, data):
self.response = response
self.data = data
def __str__(self):
return "response: %s\nBody: %s" % (self.response, self.data)
class ResponseBodyList(list):
"""Class that wraps an http response and list body into a single value.

View File

@ -512,7 +512,7 @@ class ScenarioTest(tempest.test.BaseTestCase):
Nova clients
"""
_, floating_ip = self.floating_ips_client.create_floating_ip(pool_name)
floating_ip = self.floating_ips_client.create_floating_ip(pool_name)
self.addCleanup(self.delete_wrapper,
self.floating_ips_client.delete_floating_ip,
floating_ip['id'])

View File

@ -104,7 +104,7 @@ class BaremetalBasicOps(manager.BaremetalScenarioTest):
return int(ephemeral)
def add_floating_ip(self):
_, floating_ip = self.floating_ips_client.create_floating_ip()
floating_ip = self.floating_ips_client.create_floating_ip()
self.floating_ips_client.associate_floating_ip_to_server(
floating_ip['ip'], self.instance['id'])
return floating_ip['ip']

View File

@ -80,7 +80,7 @@ class TestServerBasicOps(manager.ScenarioTest):
def verify_ssh(self):
if self.run_ssh:
# Obtain a floating IP
_, floating_ip = self.floating_ips_client.create_floating_ip()
floating_ip = self.floating_ips_client.create_floating_ip()
self.addCleanup(self.delete_wrapper,
self.floating_ips_client.delete_floating_ip,
floating_ip['id'])

View File

@ -77,7 +77,7 @@ class TestShelveInstance(manager.ScenarioTest):
create_kwargs=create_kwargs)
if CONF.compute.use_floatingip_for_ssh:
_, floating_ip = self.floating_ips_client.create_floating_ip()
floating_ip = self.floating_ips_client.create_floating_ip()
self.addCleanup(self.delete_wrapper,
self.floating_ips_client.delete_floating_ip,
floating_ip['id'])

View File

@ -99,7 +99,7 @@ class TestVolumeBootPattern(manager.ScenarioTest):
def _ssh_to_server(self, server, keypair):
if CONF.compute.use_floatingip_for_ssh:
_, floating_ip = self.floating_ips_client.create_floating_ip()
floating_ip = self.floating_ips_client.create_floating_ip()
self.addCleanup(self.delete_wrapper,
self.floating_ips_client.delete_floating_ip,
floating_ip['id'])

View File

@ -26,11 +26,11 @@ class FixedIPsClientJSON(service_client.ServiceClient):
resp, body = self.get(url)
body = json.loads(body)
self.validate_response(schema.fixed_ips, resp, body)
return resp, body['fixed_ip']
return service_client.ResponseBody(resp, body['fixed_ip'])
def reserve_fixed_ip(self, ip, body):
"""This reserves and unreserves fixed ips."""
url = "os-fixed-ips/%s/action" % (ip)
resp, body = self.post(url, json.dumps(body))
self.validate_response(schema.fixed_ip_action, resp, body)
return resp, body
return service_client.ResponseBody(resp)

View File

@ -32,7 +32,7 @@ class FloatingIPsClientJSON(service_client.ServiceClient):
resp, body = self.get(url)
body = json.loads(body)
self.validate_response(schema.list_floating_ips, resp, body)
return resp, body['floating_ips']
return service_client.ResponseBodyList(resp, body['floating_ips'])
def get_floating_ip_details(self, floating_ip_id):
"""Get the details of a floating IP."""
@ -42,7 +42,7 @@ class FloatingIPsClientJSON(service_client.ServiceClient):
if resp.status == 404:
raise exceptions.NotFound(body)
self.validate_response(schema.floating_ip, resp, body)
return resp, body['floating_ip']
return service_client.ResponseBody(resp, body['floating_ip'])
def create_floating_ip(self, pool_name=None):
"""Allocate a floating IP to the project."""
@ -52,14 +52,14 @@ class FloatingIPsClientJSON(service_client.ServiceClient):
resp, body = self.post(url, post_body)
body = json.loads(body)
self.validate_response(schema.floating_ip, resp, body)
return resp, body['floating_ip']
return service_client.ResponseBody(resp, body['floating_ip'])
def delete_floating_ip(self, floating_ip_id):
"""Deletes the provided floating IP from the project."""
url = "os-floating-ips/%s" % str(floating_ip_id)
resp, body = self.delete(url)
self.validate_response(schema.add_remove_floating_ip, resp, body)
return resp, body
return service_client.ResponseBody(resp, body)
def associate_floating_ip_to_server(self, floating_ip, server_id):
"""Associate the provided floating IP to a specific server."""
@ -73,7 +73,7 @@ class FloatingIPsClientJSON(service_client.ServiceClient):
post_body = json.dumps(post_body)
resp, body = self.post(url, post_body)
self.validate_response(schema.add_remove_floating_ip, resp, body)
return resp, body
return service_client.ResponseBody(resp, body)
def disassociate_floating_ip_from_server(self, floating_ip, server_id):
"""Disassociate the provided floating IP from a specific server."""
@ -87,7 +87,7 @@ class FloatingIPsClientJSON(service_client.ServiceClient):
post_body = json.dumps(post_body)
resp, body = self.post(url, post_body)
self.validate_response(schema.add_remove_floating_ip, resp, body)
return resp, body
return service_client.ResponseBody(resp, body)
def is_resource_deleted(self, id):
try:
@ -110,7 +110,7 @@ class FloatingIPsClientJSON(service_client.ServiceClient):
resp, body = self.get(url)
body = json.loads(body)
self.validate_response(schema.floating_ip_pools, resp, body)
return resp, body['floating_ip_pools']
return service_client.ResponseBodyList(resp, body['floating_ip_pools'])
def create_floating_ips_bulk(self, ip_range, pool, interface):
"""Allocate floating IPs in bulk."""
@ -123,14 +123,15 @@ class FloatingIPsClientJSON(service_client.ServiceClient):
resp, body = self.post('os-floating-ips-bulk', post_body)
body = json.loads(body)
self.validate_response(schema.create_floating_ips_bulk, resp, body)
return resp, body['floating_ips_bulk_create']
return service_client.ResponseBody(resp,
body['floating_ips_bulk_create'])
def list_floating_ips_bulk(self):
"""Returns a list of all floating IPs bulk."""
resp, body = self.get('os-floating-ips-bulk')
body = json.loads(body)
self.validate_response(schema.list_floating_ips_bulk, resp, body)
return resp, body['floating_ip_info']
return service_client.ResponseBodyList(resp, body['floating_ip_info'])
def delete_floating_ips_bulk(self, ip_range):
"""Deletes the provided floating IPs bulk."""
@ -138,4 +139,5 @@ class FloatingIPsClientJSON(service_client.ServiceClient):
resp, body = self.put('os-floating-ips-bulk/delete', post_body)
body = json.loads(body)
self.validate_response(schema.delete_floating_ips_bulk, resp, body)
return resp, body['floating_ips_bulk_delete']
data = body['floating_ips_bulk_delete']
return service_client.ResponseBodyData(resp, data)

View File

@ -55,7 +55,7 @@ def cleanup():
except Exception:
pass
_, floating_ips = admin_manager.floating_ips_client.list_floating_ips()
floating_ips = admin_manager.floating_ips_client.list_floating_ips()
LOG.info("Cleanup::remove %s floating ips" % len(floating_ips))
for f in floating_ips:
try: