A small start on unit tests: mostly a proof of concept that contains a test

for api/ports.py
This commit is contained in:
Brad Hall 2011-06-27 16:08:58 -07:00
parent 41835dc9ed
commit c0260982ac
2 changed files with 106 additions and 0 deletions

64
tests/unit/test_api.py Normal file
View File

@ -0,0 +1,64 @@
import quantum.api.ports as ports
import quantum.api.networks as networks
import tests.unit.testlib as testlib
import unittest
class APIPortsTest(unittest.TestCase):
def setUp(self):
self.port = ports.Controller()
self.network = networks.Controller()
# Fault names copied here for reference
#
# _fault_names = {
# 400: "malformedRequest",
# 401: "unauthorized",
# 420: "networkNotFound",
# 421: "networkInUse",
# 430: "portNotFound",
# 431: "requestedStateInvalid",
# 432: "portInUse",
# 440: "alreadyAttached",
# 470: "serviceUnavailable",
# 471: "pluginFault"}
def test_deletePort(self):
tenant = "tenant1"
network = "test1"
req = testlib.create_network_request(tenant, network)
network_obj = self.network.create(req, tenant)
network_id = network_obj["networks"]["network"]["id"]
req = testlib.create_empty_request()
rv = self.port.create(req, tenant, network_id)
port_id = rv["ports"]["port"]["id"]
self.assertTrue(port_id > 0)
rv = self.port.delete("", tenant, network_id, port_id)
self.assertEqual(rv.status_int, 202)
def test_deletePortNegative(self):
tenant = "tenant1"
network = "test1"
# Check for network not found
rv = self.port.delete("", tenant, network, 2)
self.assertEqual(rv.wrapped_exc.status_int, 420)
# Create a network to put the port on
req = testlib.create_network_request(tenant, network)
network_obj = self.network.create(req, tenant)
network_id = network_obj["networks"]["network"]["id"]
# Test for portnotfound
rv = self.port.delete("", tenant, network_id, 2)
self.assertEqual(rv.wrapped_exc.status_int, 430)
# Test for portinuse
rv = self.port.create(req, tenant, network_id)
port_id = rv["ports"]["port"]["id"]
req = testlib.create_attachment_request(tenant, network_id,
port_id, "fudd")
rv = self.port.attach_resource(req, tenant, network_id, port_id)
self.assertEqual(rv.status_int, 202)
rv = self.port.delete("", tenant, network_id, port_id)
self.assertEqual(rv.wrapped_exc.status_int, 432)

42
tests/unit/testlib.py Normal file
View File

@ -0,0 +1,42 @@
import webob
from quantum.common.wsgi import Serializer
class Request(webob.Request):
def best_match_content_type(self):
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['Accept'] = "application/json"
req.body = body
return req
def create_empty_request():
return create_request("/v0.1/tenant.json", "")
def create_network_request(tenant_id, network_name):
path = "/v0.1/tenants/%s/networks.json" % tenant_id
data = {'network': {'network-name': '%s' % network_name}}
content_type = "application/json"
body = Serializer().serialize(data, content_type)
return create_request(path, body)
def create_attachment_request(tid, nid, pid, attachment_id):
path = "/v0.1/tenants/%s/networks/%s/ports/%s/attachment.json" % (tid,
nid, pid)
data = {'port': {'attachment-id': attachment_id}}
content_type = "application/json"
body = Serializer().serialize(data, content_type)
return create_request(path, body)