Starting implementation of unit tests

Fixing minor bugs with FakePlugin
This commit is contained in:
Salvatore Orlando 2011-06-30 18:13:01 +01:00
parent 2a8dc1cc88
commit fcc0f44230
7 changed files with 136 additions and 75 deletions

View File

@ -80,7 +80,7 @@ class Controller(common.QuantumController):
return faults.Fault(e) return faults.Fault(e)
network = self.network_manager.\ network = self.network_manager.\
create_network(tenant_id, create_network(tenant_id,
request_params['network-name']) request_params['net-name'])
builder = networks_view.get_view_builder(request) builder = networks_view.get_view_builder(request)
result = builder.build(network) result = builder.build(network)
return dict(networks=result) return dict(networks=result)

View File

@ -132,8 +132,8 @@ class Controller(common.QuantumController):
def get_resource(self, request, tenant_id, network_id, id): def get_resource(self, request, tenant_id, network_id, id):
try: try:
result = self.network_manager.get_interface_details( result = self.network_manager.get_port_details(
tenant_id, network_id, id) tenant_id, network_id, id).get('attachment-id', None)
return dict(attachment=result) return dict(attachment=result)
except exception.NetworkNotFound as e: except exception.NetworkNotFound as e:
return faults.Fault(faults.NetworkNotFound(e)) return faults.Fault(faults.NetworkNotFound(e))

View File

@ -44,5 +44,4 @@ class ViewBuilder(object):
def _build_detail(self, port_data): def _build_detail(self, port_data):
"""Return a simple model of a server.""" """Return a simple model of a server."""
return dict(port=dict(id=port_data['port-id'], return dict(port=dict(id=port_data['port-id'],
attachment=port_data['attachment'],
state=port_data['port-state'])) state=port_data['port-state']))

View File

@ -299,7 +299,6 @@ class Router(object):
Route the incoming request to a controller based on self.map. Route the incoming request to a controller based on self.map.
If no match, return a 404. If no match, return a 404.
""" """
LOG.debug("HERE - wsgi.Router.__call__")
return self._router return self._router
@staticmethod @staticmethod
@ -337,10 +336,6 @@ class Controller(object):
arg_dict = req.environ['wsgiorg.routing_args'][1] arg_dict = req.environ['wsgiorg.routing_args'][1]
action = arg_dict['action'] action = arg_dict['action']
method = getattr(self, action) method = getattr(self, action)
LOG.debug("ARG_DICT:%s", arg_dict)
LOG.debug("Action:%s", action)
LOG.debug("Method:%s", method)
LOG.debug("%s %s" % (req.method, req.url))
del arg_dict['controller'] del arg_dict['controller']
del arg_dict['action'] del arg_dict['action']
if 'format' in arg_dict: if 'format' in arg_dict:

View File

@ -285,7 +285,9 @@ class FakePlugin(object):
are attached to the network are attached to the network
""" """
LOG.debug("FakePlugin.get_network_details() called") LOG.debug("FakePlugin.get_network_details() called")
return self._get_network(tenant_id, net_id) net = self._get_network(tenant_id, net_id)
return {'net-id':str(net.uuid),
'net-name':net.name}
def create_network(self, tenant_id, net_name): def create_network(self, tenant_id, net_name):
""" """
@ -306,10 +308,9 @@ class FakePlugin(object):
net = self._get_network(tenant_id, net_id) net = self._get_network(tenant_id, net_id)
# Verify that no attachments are plugged into the network # Verify that no attachments are plugged into the network
if net: if net:
if net['net-ports']: for port in db.port_list(net_id):
for port in db.port_list(net_id): if port['interface-id']:
if port['interface-id']: raise exc.NetworkInUse(net_id=net_id)
raise exc.NetworkInUse(net_id=net_id)
db.network_destroy(net_id) db.network_destroy(net_id)
return net return net
# Network not found # Network not found
@ -344,7 +345,11 @@ class FakePlugin(object):
that is attached to this particular port. that is attached to this particular port.
""" """
LOG.debug("FakePlugin.get_port_details() called") LOG.debug("FakePlugin.get_port_details() called")
return self._get_port(tenant_id, net_id, port_id) port = self._get_port(tenant_id, net_id, port_id)
return {'port-id':str(port.uuid),
'attachment-id':port.interface_id,
'port-state':port.state}
def create_port(self, tenant_id, net_id, port_state=None): def create_port(self, tenant_id, net_id, port_state=None):
""" """
@ -359,10 +364,9 @@ class FakePlugin(object):
""" """
Updates the state of a port on the specified Virtual Network. Updates the state of a port on the specified Virtual Network.
""" """
port=self._get_port(tenant_id, net_id, port_id)
LOG.debug("FakePlugin.update_port() called") LOG.debug("FakePlugin.update_port() called")
self._validate_port_state(port_state) self._validate_port_state(new_state)
db.port_set_state(new_state) db.port_set_state(port_id,new_state)
return return
def delete_port(self, tenant_id, net_id, port_id): def delete_port(self, tenant_id, net_id, port_id):
@ -375,9 +379,9 @@ class FakePlugin(object):
LOG.debug("FakePlugin.delete_port() called") LOG.debug("FakePlugin.delete_port() called")
net = self._get_network(tenant_id, net_id) net = self._get_network(tenant_id, net_id)
port = self._get_port(tenant_id, net_id, port_id) port = self._get_port(tenant_id, net_id, port_id)
if port['attachment']: if port['interface_id']:
raise exc.PortInUse(net_id=net_id, port_id=port_id, raise exc.PortInUse(net_id=net_id, port_id=port_id,
att_id=port['attachment']) att_id=port['interface_id'])
try: try:
port = db.port_destroy(port_id) port = db.port_destroy(port_id)
except Exception, e: except Exception, e:
@ -396,9 +400,9 @@ class FakePlugin(object):
self._validate_attachment(tenant_id, net_id, port_id, self._validate_attachment(tenant_id, net_id, port_id,
remote_interface_id) remote_interface_id)
port = self._get_port(tenant_id, net_id, port_id) port = self._get_port(tenant_id, net_id, port_id)
if port['attachment']: if port['interface_id']:
raise exc.PortInUse(net_id=net_id, port_id=port_id, raise exc.PortInUse(net_id=net_id, port_id=port_id,
att_id=port['attachment']) att_id=port['interface_id'])
db.port_set_attachment(port_id, remote_interface_id) db.port_set_attachment(port_id, remote_interface_id)
def unplug_interface(self, tenant_id, net_id, port_id): def unplug_interface(self, tenant_id, net_id, port_id):

View File

@ -17,15 +17,36 @@
# @author: Brad Hall, Nicira Networks # @author: Brad Hall, Nicira Networks
# @author: Salvatore Orlando, Citrix Systems # @author: Salvatore Orlando, Citrix Systems
import tests.unit.testlib as testlib import logging
import unittest import unittest
import tests.unit.testlib as testlib
from quantum import api as server from quantum import api as server
from quantum.common.wsgi import Serializer
LOG = logging.getLogger('quantum.tests.test_api')
class APIPortsTest(unittest.TestCase): class APIPortsTest(unittest.TestCase):
def setUp(self): def setUp(self):
self.api = server.APIRouterv01() self.api = server.APIRouterV01()
self.tenant_id = "test_tenant"
self.network_name = "test_network"
def tearDown(self):
"""Clear the test environment"""
# Remove all the networks.
network_req = testlib.create_list_networks_request(self.tenant_id)
network_res = network_req.get_response(self.api)
network_data = Serializer().deserialize(network_res.body,"application/xml")
for network in network_data["networks"].values():
network_delete_req = testlib. \
create_network_delete_request(self.tenant_id,network['id'])
network_delete_req.get_response(self.api)
# Fault names copied here for reference # Fault names copied here for reference
# #
# _fault_names = { # _fault_names = {
@ -40,42 +61,65 @@ class APIPortsTest(unittest.TestCase):
# 470: "serviceUnavailable", # 470: "serviceUnavailable",
# 471: "pluginFault"} # 471: "pluginFault"}
def test_deletePort(self):
tenant = "tenant1" def _test_delete_port(self, format):
network = "test1" content_type = "application/" + format
req = testlib.create_network_request(tenant, network) port_state = "ACTIVE"
network_obj = self.network.create(req, tenant) network_req = testlib.create_new_network_request(self.tenant_id,
network_id = network_obj["networks"]["network"]["id"] self.network_name,
req = testlib.create_empty_request() format)
rv = self.port.create(req, tenant, network_id) network_res = network_req.get_response(self.api)
port_id = rv["ports"]["port"]["id"] self.assertEqual(network_res.status_int, 200)
self.assertTrue(port_id > 0) network_data = Serializer().deserialize(network_res.body,
rv = self.port.delete("", tenant, network_id, port_id) content_type)
self.assertEqual(rv.status_int, 202) network_id = network_data["networks"]["network"]["id"]
port_req = testlib.create_new_port_request(self.tenant_id,
network_id, port_state,
format)
port_res = port_req.get_response(self.api)
self.assertEqual(port_res.status_int, 200)
port_data = Serializer().deserialize(port_res.body, content_type)
port_id = port_data["ports"]["port"]["id"]
LOG.debug("Deleting port %(port_id)s for network %(network_id)s"\
" of tenant %(tenant_id)s", locals())
delete_port_req = testlib.create_port_delete_request(self.tenant_id,
network_id,
port_id,
format)
delete_port_res = delete_port_req.get_response(self.api)
self.assertEqual(delete_port_res.status_int, 202)
def test_deletePortNegative(self): def test_deletePort_xml(self):
tenant = "tenant1" self._test_delete_port('xml')
network = "test1"
def test_deletePort_json(self):
self._test_delete_port('json')
#def test_deletePortNegative(self):
# tenant = "tenant1"
# network = "test1"
# Check for network not found # Check for network not found
rv = self.port.delete("", tenant, network, 2) #rv = self.port.delete("", tenant, network, 2)
self.assertEqual(rv.wrapped_exc.status_int, 420) #self.assertEqual(rv.wrapped_exc.status_int, 420)
# Create a network to put the port on # Create a network to put the port on
req = testlib.create_network_request(tenant, network) #req = testlib.create_network_request(tenant, network)
network_obj = self.network.create(req, tenant) #network_obj = self.network.create(req, tenant)
network_id = network_obj["networks"]["network"]["id"] #network_id = network_obj["networks"]["network"]["id"]
# Test for portnotfound # Test for portnotfound
rv = self.port.delete("", tenant, network_id, 2) #rv = self.port.delete("", tenant, network_id, 2)
self.assertEqual(rv.wrapped_exc.status_int, 430) #self.assertEqual(rv.wrapped_exc.status_int, 430)
# Test for portinuse # Test for portinuse
rv = self.port.create(req, tenant, network_id) #rv = self.port.create(req, tenant, network_id)
port_id = rv["ports"]["port"]["id"] #port_id = rv["ports"]["port"]["id"]
req = testlib.create_attachment_request(tenant, network_id, #req = testlib.create_attachment_request(tenant, network_id,
port_id, "fudd") # port_id, "fudd")
rv = self.port.attach_resource(req, tenant, network_id, port_id) #rv = self.port.attach_resource(req, tenant, network_id, port_id)
self.assertEqual(rv.status_int, 202) #self.assertEqual(rv.status_int, 202)
rv = self.port.delete("", tenant, network_id, port_id) #rv = self.port.delete("", tenant, network_id, port_id)
self.assertEqual(rv.wrapped_exc.status_int, 432) #self.assertEqual(rv.wrapped_exc.status_int, 432)
#

View File

@ -3,34 +3,53 @@ import webob
from quantum.common.wsgi import Serializer from quantum.common.wsgi import Serializer
class Request(webob.Request): def create_request(path, body, content_type, method= 'GET'):
req = webob.Request.blank(path)
def best_match_content_type(self): req.method = method
return "application/json"
def get_content_type(self):
return "application/json"
def create_request(path, body):
req = Request.blank(path)
req.method = "POST"
req.headers = {} req.headers = {}
req.headers['Accept'] = "application/json" req.headers['Accept'] = content_type
req.body = body req.body = body
return req return req
def create_list_networks_request(tenant_id, format='xml'):
def create_empty_request(): method = 'GET'
return create_request("/v0.1/tenant.json", "") path = "/tenants/%(tenant_id)s/networks.%(format)s" % locals()
content_type = "application/" + format
return create_request(path, None, content_type, method)
def create_network_request(tenant_id, network_name): def create_new_network_request(tenant_id, network_name, format='xml'):
path = "/v0.1/tenants/%s/networks.json" % tenant_id method = 'POST'
data = {'network': {'network-name': '%s' % network_name}} path = "/tenants/%(tenant_id)s/networks.%(format)s" % locals()
content_type = "application/json" data = {'network': {'net-name': '%s' % network_name}}
content_type = "application/" + format
body = Serializer().serialize(data, content_type) body = Serializer().serialize(data, content_type)
return create_request(path, body) return create_request(path, body, content_type, method)
def create_network_delete_request(tenant_id, network_id, format='xml'):
method = 'DELETE'
path = "/tenants/%(tenant_id)s/networks/" \
"%(network_id)s.%(format)s" % locals()
content_type = "application/" + format
return create_request(path, None, content_type, method)
def create_new_port_request(tenant_id, network_id, port_state, format='xml'):
method = 'POST'
path = "/tenants/%(tenant_id)s/networks/" \
"%(network_id)s/ports.%(format)s" % locals()
data = {'port': {'port-state': '%s' % port_state}}
content_type = "application/" + format
body = Serializer().serialize(data, content_type)
return create_request(path, body, content_type, method)
def create_port_delete_request(tenant_id, network_id, port_id, format='xml'):
method = 'DELETE'
path = "/tenants/%(tenant_id)s/networks/" \
"%(network_id)s/ports/%(port_id)s.%(format)s" % locals()
content_type = "application/" + format
return create_request(path, None, content_type, method)
def create_attachment_request(tid, nid, pid, attachment_id): def create_attachment_request(tid, nid, pid, attachment_id):