[Kimchi-devel] [PATCH 1/2] bug fix: Update test cases to use HTTPS

Aline Manera alinefm at linux.vnet.ibm.com
Fri May 9 17:48:51 UTC 2014


From: Aline Manera <alinefm at br.ibm.com>

As all HTTP requests are redirected to HTTPS, the tests must be done
using HTTPS, otherwise all the responses from HTTP requests will be:

<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx/1.2.1</center>
</body>
</html>

instead of the real data.
Fix it.

Signed-off-by: Aline Manera <alinefm at br.ibm.com>
---
 tests/test_authorization.py |    5 ++--
 tests/test_exception.py     |   24 ++++++++-------
 tests/test_mockmodel.py     |   25 +++++++++-------
 tests/test_plugin.py        |    5 ++--
 tests/test_rest.py          |   70 ++++++++++++++++++++++---------------------
 tests/test_server.py        |    7 +++--
 tests/utils.py              |    5 ----
 7 files changed, 73 insertions(+), 68 deletions(-)

diff --git a/tests/test_authorization.py b/tests/test_authorization.py
index b211e06..196625e 100644
--- a/tests/test_authorization.py
+++ b/tests/test_authorization.py
@@ -44,7 +44,8 @@ def setUpModule():
     model = kimchi.mockmodel.MockModel('/tmp/obj-store-test')
     host = '127.0.0.1'
     port = get_free_port('http')
-    test_server = run_server(host, port, None, test_mode=True, model=model)
+    ssl_port = get_free_port('https')
+    test_server = run_server(host, port, ssl_port, test_mode=True, model=model)
 
 
 def tearDownModule():
@@ -54,7 +55,7 @@ def tearDownModule():
 
 class AuthorizationTests(unittest.TestCase):
     def setUp(self):
-        self.request = partial(request, host, port)
+        self.request = partial(request, host, ssl_port)
         model.reset()
 
     def test_nonroot_access(self):
diff --git a/tests/test_exception.py b/tests/test_exception.py
index 42041bc..61f4f30 100644
--- a/tests/test_exception.py
+++ b/tests/test_exception.py
@@ -31,16 +31,18 @@ test_server = None
 model = None
 host = None
 port = None
+ssl_port = None
 
 
 def setup_server(environment='development'):
-    global test_server, model, host, port
+    global test_server, model, host, port, ssl_port
 
     patch_auth()
     model = kimchi.mockmodel.MockModel('/tmp/obj-store-test')
     host = '127.0.0.1'
-    port = get_free_port()
-    test_server = run_server(host, port, None, test_mode=True, model=model,
+    port = get_free_port('http')
+    ssl_port = get_free_port('https')
+    test_server = run_server(host, port, ssl_port, test_mode=True, model=model,
                              environment=environment)
 
 
@@ -55,17 +57,17 @@ class ExceptionTests(unittest.TestCase):
         """
         setup_server('production')
         # test 404
-        resp = json.loads(request(host, port, '/vms/blah').read())
+        resp = json.loads(request(host, ssl_port, '/vms/blah').read())
         self.assertEquals('404 Not Found', resp.get('code'))
 
         # test 405 wrong method
-        resp = json.loads(request(host, port, '/', None, 'DELETE').read())
+        resp = json.loads(request(host, ssl_port, '/', None, 'DELETE').read())
         msg = u'KCHAPI0002E: Delete is not allowed for kimchiroot'
         self.assertEquals('405 Method Not Allowed', resp.get('code'))
         self.assertEquals(msg, resp.get('reason'))
 
         # test 400 parse error
-        resp = json.loads(request(host, port, '/vms', '{', 'POST').read())
+        resp = json.loads(request(host, ssl_port, '/vms', '{', 'POST').read())
         msg = u'KCHAPI0006E: Unable to parse JSON request'
         self.assertEquals('400 Bad Request', resp.get('code'))
         self.assertEquals(msg, resp.get('reason'))
@@ -73,7 +75,7 @@ class ExceptionTests(unittest.TestCase):
 
         # test 400 missing required parameter
         req = json.dumps({})
-        resp = json.loads(request(host, port, '/vms', req, 'POST').read())
+        resp = json.loads(request(host, ssl_port, '/vms', req, 'POST').read())
         self.assertEquals('400 Bad Request', resp.get('code'))
         msg = u"KCHVM0016E: Specify a template to create a virtual machine from"
         self.assertEquals(msg, resp.get('reason'))
@@ -85,17 +87,17 @@ class ExceptionTests(unittest.TestCase):
         """
         setup_server()
         # test 404
-        resp = json.loads(request(host, port, '/vms/blah').read())
+        resp = json.loads(request(host, ssl_port, '/vms/blah').read())
         self.assertEquals('404 Not Found', resp.get('code'))
 
         # test 405 wrong method
-        resp = json.loads(request(host, port, '/', None, 'DELETE').read())
+        resp = json.loads(request(host, ssl_port, '/', None, 'DELETE').read())
         msg = u'KCHAPI0002E: Delete is not allowed for kimchiroot'
         self.assertEquals('405 Method Not Allowed', resp.get('code'))
         self.assertEquals(msg, resp.get('reason'))
 
         # test 400 parse error
-        resp = json.loads(request(host, port, '/vms', '{', 'POST').read())
+        resp = json.loads(request(host, ssl_port, '/vms', '{', 'POST').read())
         msg = u'KCHAPI0006E: Unable to parse JSON request'
         self.assertEquals('400 Bad Request', resp.get('code'))
         self.assertEquals(msg, resp.get('reason'))
@@ -103,7 +105,7 @@ class ExceptionTests(unittest.TestCase):
 
         # test 400 missing required parameter
         req = json.dumps({})
-        resp = json.loads(request(host, port, '/vms', req, 'POST').read())
+        resp = json.loads(request(host, ssl_port, '/vms', req, 'POST').read())
         msg = u"KCHVM0016E: Specify a template to create a virtual machine from"
         self.assertEquals('400 Bad Request', resp.get('code'))
         self.assertEquals(msg, resp.get('reason'))
diff --git a/tests/test_mockmodel.py b/tests/test_mockmodel.py
index 8dd6a03..d43d37c 100644
--- a/tests/test_mockmodel.py
+++ b/tests/test_mockmodel.py
@@ -33,17 +33,20 @@ test_server = None
 model = None
 host = None
 port = None
+ssl_port = None
 
 
 class MockModelTests(unittest.TestCase):
     def setUp(self):
-        global port, host, model, test_server
+        global host, port, ssl_port, model, test_server
         cherrypy.request.headers = {'Accept': 'application/json'}
         model = kimchi.mockmodel.MockModel('/tmp/obj-store-test')
         patch_auth()
-        port = get_free_port()
+        port = get_free_port('http')
+        ssl_port = get_free_port('https')
         host = '127.0.0.1'
-        test_server = run_server(host, port, None, test_mode=True, model=model)
+        test_server = run_server(host, port, ssl_port, test_mode=True,
+                                 model=model)
 
     def tearDown(self):
         test_server.stop()
@@ -86,21 +89,21 @@ class MockModelTests(unittest.TestCase):
     def test_screenshot_refresh(self):
         # Create a VM
         req = json.dumps({'name': 'test', 'cdrom': '/nonexistent.iso'})
-        request(host, port, '/templates', req, 'POST')
+        request(host, ssl_port, '/templates', req, 'POST')
         req = json.dumps({'name': 'test-vm', 'template': '/templates/test'})
-        request(host, port, '/vms', req, 'POST')
+        request(host, ssl_port, '/vms', req, 'POST')
 
         # Test screenshot refresh for running vm
-        request(host, port, '/vms/test-vm/start', '{}', 'POST')
-        resp = request(host, port, '/vms/test-vm/screenshot')
+        request(host, ssl_port, '/vms/test-vm/start', '{}', 'POST')
+        resp = request(host, ssl_port, '/vms/test-vm/screenshot')
         self.assertEquals(200, resp.status)
         self.assertEquals('image/png', resp.getheader('content-type'))
-        resp1 = request(host, port, '/vms/test-vm')
+        resp1 = request(host, ssl_port, '/vms/test-vm')
         rspBody = resp1.read()
         testvm_Data = json.loads(rspBody)
         screenshotURL = testvm_Data['screenshot']
         time.sleep(5)
-        resp2 = request(host, port, screenshotURL)
+        resp2 = request(host, ssl_port, screenshotURL)
         self.assertEquals(200, resp2.status)
         self.assertEquals(resp2.getheader('content-type'),
                           resp.getheader('content-type'))
@@ -111,13 +114,13 @@ class MockModelTests(unittest.TestCase):
 
     def test_vm_list_sorted(self):
         req = json.dumps({'name': 'test', 'cdrom': '/nonexistent.iso'})
-        request(host, port, '/templates', req, 'POST')
+        request(host, ssl_port, '/templates', req, 'POST')
 
         def add_vm(name):
 
             # Create a VM
             req = json.dumps({'name': name, 'template': '/templates/test'})
-            request(host, port, '/vms', req, 'POST')
+            request(host, ssl_port, '/vms', req, 'POST')
 
         add_vm('bca')
         add_vm('xba')
diff --git a/tests/test_plugin.py b/tests/test_plugin.py
index 62c2583..7773a0e 100644
--- a/tests/test_plugin.py
+++ b/tests/test_plugin.py
@@ -34,6 +34,7 @@ test_server = None
 model = None
 host = None
 port = None
+ssl_port = None
 
 
 def setUpModule():
@@ -42,7 +43,7 @@ def setUpModule():
     model = kimchi.mockmodel.MockModel('/tmp/obj-store-test')
     host = '127.0.0.1'
     port = utils.get_free_port('http')
-    ssl_port = None
+    ssl_port = utils.get_free_port('https')
     test_server = utils.run_server(host, port, ssl_port, test_mode=True,
                                    model=model)
 
@@ -55,7 +56,7 @@ def tearDownModule():
 class PluginTests(unittest.TestCase):
 
     def setUp(self):
-        self.request = partial(utils.request, host, port)
+        self.request = partial(utils.request, host, ssl_port)
 
     def _create_rectangle(self, name, length, width):
         req = json.dumps({'name': name, 'length': length, 'width': width})
diff --git a/tests/test_rest.py b/tests/test_rest.py
index da3432f..bb52e3f 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -35,7 +35,7 @@ import iso_gen
 import kimchi.mockmodel
 import kimchi.server
 from kimchi.rollbackcontext import RollbackContext
-from utils import fake_user, get_free_port, https_request, patch_auth, request
+from utils import fake_user, get_free_port, patch_auth, request
 from utils import run_server
 
 
@@ -83,7 +83,7 @@ class RestTests(unittest.TestCase):
         cb('in progress')
 
     def setUp(self):
-        self.request = partial(request, host, port)
+        self.request = partial(request, host, ssl_port)
         model.reset()
 
     def assertHTTPStatus(self, code, *args):
@@ -822,7 +822,7 @@ class RestTests(unittest.TestCase):
                           'allocated': 512,
                           'path': '/var/lib/libvirt/images/%i' % i,
                           'type': 'dir'})
-        resp = request(host, port, '/storagepools', req, 'POST')
+        resp = request(host, ssl_port, '/storagepools', req, 'POST')
         self.assertEquals(400, resp.status)
 
         storagepools = json.loads(self.request('/storagepools').read())
@@ -1160,13 +1160,13 @@ class RestTests(unittest.TestCase):
         req = json.dumps({'name': 'test-network',
                           'connection': 'nat',
                           'net': '127.0.1.0/24'})
-        resp = request(host, port, '/networks', req, 'POST')
+        resp = request(host, ssl_port, '/networks', req, 'POST')
         self.assertEquals(201, resp.status)
 
         req = json.dumps({'name': 'test-storagepool',
                           'path': '/tmp/kimchi-images',
                           'type': 'dir'})
-        resp = request(host, port, '/storagepools', req, 'POST')
+        resp = request(host, ssl_port, '/storagepools', req, 'POST')
         self.assertEquals(201, resp.status)
 
         t = {'name': 'test', 'memory': 1024, 'cpus': 1,
@@ -1180,19 +1180,20 @@ class RestTests(unittest.TestCase):
         shutil.rmtree(path)
         # Try to delete network
         # It should fail as it is associated to a template
-        resp = json.loads(request(host, port, '/networks/test-network',
+        resp = json.loads(request(host, ssl_port, '/networks/test-network',
                                   '{}', 'DELETE').read())
         self.assertIn("KCHNET0017E", resp["reason"])
 
         # Update template to release network and then delete it
         params = {'networks': []}
         req = json.dumps(params)
-        resp = request(host, port, '/templates/test', req, 'PUT')
-        resp = request(host, port, '/networks/test-network', '{}', 'DELETE')
+        resp = request(host, ssl_port, '/templates/test', req, 'PUT')
+        resp = request(host, ssl_port, '/networks/test-network', '{}',
+                       'DELETE')
         self.assertEquals(204, resp.status)
 
         # Delete the storagepool
-        resp = request(host, port, '/storagepools/test-storagepool',
+        resp = request(host, ssl_port, '/storagepools/test-storagepool',
                        '{}', 'DELETE')
         self.assertEquals(204, resp.status)
 
@@ -1202,7 +1203,7 @@ class RestTests(unittest.TestCase):
         self.assertEquals(res['invalid']['storagepools'], ['test-storagepool'])
 
         # Delete the template
-        resp = request(host, port, '/templates/test', '{}', 'DELETE')
+        resp = request(host, ssl_port, '/templates/test', '{}', 'DELETE')
         self.assertEquals(204, resp.status)
 
     def test_iso_scan_shallow(self):
@@ -1325,7 +1326,7 @@ class RestTests(unittest.TestCase):
         self.assertEquals(interface['status'], "active")
 
     def test_get_networks(self):
-        networks = json.loads(request(host, port, '/networks').read())
+        networks = json.loads(request(host, ssl_port, '/networks').read())
         self.assertEquals(1, len(networks))
         self.assertEquals('default', networks[0]['name'])
         self.assertEquals([], networks[0]['vms'])
@@ -1337,21 +1338,21 @@ class RestTests(unittest.TestCase):
                               'connection': 'nat',
                               'subnet': '127.0.10%i.0/24' % i})
 
-            resp = request(host, port, '/networks', req, 'POST')
+            resp = request(host, ssl_port, '/networks', req, 'POST')
             self.assertEquals(201, resp.status)
             network = json.loads(resp.read())
             self.assertEquals([], network["vms"])
 
-        networks = json.loads(request(host, port, '/networks').read())
+        networks = json.loads(request(host, ssl_port, '/networks').read())
         self.assertEquals(6, len(networks))
 
-        network = json.loads(request(host, port,
+        network = json.loads(request(host, ssl_port,
                                      '/networks/network-1').read())
         self.assertEquals('network-1', network['name'])
         self.assertEquals('inactive', network['state'])
         # Delete the network
         for i in xrange(5):
-            resp = request(host, port, '/networks/network-%i' % i,
+            resp = request(host, ssl_port, '/networks/network-%i' % i,
                            '{}', 'DELETE')
             self.assertEquals(204, resp.status)
 
@@ -1360,31 +1361,32 @@ class RestTests(unittest.TestCase):
         req = json.dumps({'name': 'test-network',
                           'connection': 'nat',
                           'net': '127.0.1.0/24'})
-        resp = request(host, port, '/networks', req, 'POST')
+        resp = request(host, ssl_port, '/networks', req, 'POST')
         self.assertEquals(201, resp.status)
 
         # Verify the network
-        network = json.loads(request(host, port,
+        network = json.loads(request(host, ssl_port,
                                      '/networks/test-network').read())
         self.assertEquals('inactive', network['state'])
         self.assertTrue(network['persistent'])
 
         # activate the network
-        resp = request(host, port,
+        resp = request(host, ssl_port,
                        '/networks/test-network/activate', '{}', 'POST')
-        network = json.loads(request(host, port,
+        network = json.loads(request(host, ssl_port,
                                      '/networks/test-network').read())
         self.assertEquals('active', network['state'])
 
         # Deactivate the network
-        resp = request(host, port,
+        resp = request(host, ssl_port,
                        '/networks/test-network/deactivate', '{}', 'POST')
-        network = json.loads(request(host, port,
+        network = json.loads(request(host, ssl_port,
                                      '/networks/test-network').read())
         self.assertEquals('inactive', network['state'])
 
         # Delete the network
-        resp = request(host, port, '/networks/test-network', '{}', 'DELETE')
+        resp = request(host, ssl_port, '/networks/test-network', '{}',
+                       'DELETE')
         self.assertEquals(204, resp.status)
 
     def _wait_task(self, taskid, timeout=5):
@@ -1540,23 +1542,23 @@ class RestTests(unittest.TestCase):
             self.assertIn('KCHDISTRO0001E', distro.get('reason'))
 
     def test_debugreports(self):
-        resp = request(host, port, '/debugreports')
+        resp = request(host, ssl_port, '/debugreports')
         self.assertEquals(200, resp.status)
 
     def _report_delete(self, name):
-        request(host, port, '/debugreports/%s' % name, '{}', 'DELETE')
+        request(host, ssl_port, '/debugreports/%s' % name, '{}', 'DELETE')
 
     def test_create_debugreport(self):
         req = json.dumps({'name': 'report1'})
         with RollbackContext() as rollback:
-            resp = request(host, port, '/debugreports', req, 'POST')
+            resp = request(host, ssl_port, '/debugreports', req, 'POST')
             self.assertEquals(202, resp.status)
             task = json.loads(resp.read())
             # make sure the debugreport doesn't exist until the
             # the task is finished
             self._wait_task(task['id'])
             rollback.prependDefer(self._report_delete, 'report1')
-            resp = request(host, port, '/debugreports/report1')
+            resp = request(host, ssl_port, '/debugreports/report1')
             debugreport = json.loads(resp.read())
             self.assertEquals("report1", debugreport['name'])
             self.assertEquals(200, resp.status)
@@ -1564,22 +1566,22 @@ class RestTests(unittest.TestCase):
     def test_debugreport_download(self):
         req = json.dumps({'name': 'report1'})
         with RollbackContext() as rollback:
-            resp = request(host, port, '/debugreports', req, 'POST')
+            resp = request(host, ssl_port, '/debugreports', req, 'POST')
             self.assertEquals(202, resp.status)
             task = json.loads(resp.read())
             # make sure the debugreport doesn't exist until the
             # the task is finished
             self._wait_task(task['id'], 20)
             rollback.prependDefer(self._report_delete, 'report1')
-            resp = request(host, port, '/debugreports/report1')
+            resp = request(host, ssl_port, '/debugreports/report1')
             debugreport = json.loads(resp.read())
             self.assertEquals("report1", debugreport['name'])
             self.assertEquals(200, resp.status)
-            resp = request(host, port, '/debugreports/report1/content')
+            resp = request(host, ssl_port, '/debugreports/report1/content')
             self.assertEquals(200, resp.status)
-            resp = request(host, port, '/debugreports/report1')
+            resp = request(host, ssl_port, '/debugreports/report1')
             debugre = json.loads(resp.read())
-            resp = request(host, port, debugre['uri'])
+            resp = request(host, ssl_port, debugre['uri'])
             self.assertEquals(200, resp.status)
 
     def test_host(self):
@@ -1655,12 +1657,12 @@ class RestTests(unittest.TestCase):
         resp = self.request('/vms', req, 'POST')
         self.assertEquals(201, resp.status)
 
-        resp = request(host, port, '/vms')
+        resp = request(host, ssl_port, '/vms')
         self.assertEquals(200, resp.status)
         res = json.loads(resp.read())
         self.assertEquals(2, len(res))
 
-        resp = request(host, port, '/vms?name=test-vm1')
+        resp = request(host, ssl_port, '/vms?name=test-vm1')
         self.assertEquals(200, resp.status)
         res = json.loads(resp.read())
         self.assertEquals(1, len(res))
@@ -1709,5 +1711,5 @@ class HttpsRestTests(RestTests):
     Run all of the same tests as above, but use https instead
     """
     def setUp(self):
-        self.request = partial(https_request, host, ssl_port)
+        self.request = partial(request, host, ssl_port)
         model.reset()
diff --git a/tests/test_server.py b/tests/test_server.py
index d06e17a..0f38e72 100644
--- a/tests/test_server.py
+++ b/tests/test_server.py
@@ -32,11 +32,12 @@ class ServerTests(unittest.TestCase):
         Test that we can start a server and receive HTTP:200.
         """
         host = '127.0.0.1'
-        port = utils.get_free_port()
+        port = utils.get_free_port('http')
+        ssl_port = utils.get_free_port('https')
         model = kimchi.mockmodel.MockModel('/tmp/obj-store-test')
-        s = utils.run_server(host, port, None, test_mode=True, model=model)
+        s = utils.run_server(host, port, ssl_port, test_mode=True, model=model)
         try:
-            resp = utils.request(host, port, '/')
+            resp = utils.request(host, ssl_port, '/')
             self.assertEquals(200, resp.status)
         except:
             raise
diff --git a/tests/utils.py b/tests/utils.py
index 77e5d1d..11c02fb 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -141,11 +141,6 @@ def _request(conn, path, data, method, headers):
 
 
 def request(host, port, path, data=None, method='GET', headers=None):
-    conn = httplib.HTTPConnection(host, port)
-    return _request(conn, path, data, method, headers)
-
-
-def https_request(host, port, path, data=None, method='GET', headers=None):
     conn = httplib.HTTPSConnection(host, port)
     return _request(conn, path, data, method, headers)
 
-- 
1.7.10.4




More information about the Kimchi-devel mailing list