]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
A small start on unit tests: mostly a proof of concept that contains a test
authorBrad Hall <bhall@nicira.com>
Mon, 27 Jun 2011 23:08:58 +0000 (16:08 -0700)
committerBrad Hall <bhall@nicira.com>
Mon, 27 Jun 2011 23:08:58 +0000 (16:08 -0700)
for api/ports.py

tests/unit/test_api.py [new file with mode: 0644]
tests/unit/testlib.py [new file with mode: 0644]

diff --git a/tests/unit/test_api.py b/tests/unit/test_api.py
new file mode 100644 (file)
index 0000000..ee189e5
--- /dev/null
@@ -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)
diff --git a/tests/unit/testlib.py b/tests/unit/testlib.py
new file mode 100644 (file)
index 0000000..a9ba11a
--- /dev/null
@@ -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)