rapi.testutils: Return headers from mock utility
authorMichael Hanselmann <hansmi@google.com>
Wed, 7 Nov 2012 16:22:35 +0000 (17:22 +0100)
committerMichael Hanselmann <hansmi@google.com>
Thu, 8 Nov 2012 13:20:02 +0000 (14:20 +0100)
A newly added test for RAPI will also verify the returned headers. A
test in ganeti.rapi.client_unittest.py is split into smaller stand-alone
tests.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

lib/rapi/testutils.py
test/ganeti.rapi.client_unittest.py

index b71f21e..fc9273d 100644 (file)
@@ -187,7 +187,7 @@ class FakeCurl:
         "%s %s" % (http.auth.HTTP_BASIC_AUTH, base64.b64encode(userpwd))
 
     path = _GetPathFromUri(url)
-    (code, resp_body) = \
+    (code, _, resp_body) = \
       self._handler.FetchResponse(path, method, headers, request_body)
 
     self._info[pycurl.RESPONSE_CODE] = code
@@ -223,7 +223,7 @@ class _RapiMock:
     @param request_body: Request body
     @type headers: mimetools.Message
     @param headers: Request headers
-    @return: Tuple containing status code and response body
+    @return: Tuple containing status code, response headers and response body
 
     """
     req_msg = http.HttpMessage()
@@ -235,7 +235,7 @@ class _RapiMock:
     (_, _, _, resp_msg) = \
       http.server.HttpResponder(self.handler)(lambda: (req_msg, None))
 
-    return (resp_msg.start_line.code, resp_msg.body)
+    return (resp_msg.start_line.code, resp_msg.headers, resp_msg.body)
 
 
 class _TestLuxiTransport:
index a09d4a2..7cad53b 100755 (executable)
@@ -96,7 +96,7 @@ class RapiMock(object):
 
       (code, response) = self._responses.pop()
 
-    return code, response
+    return (code, NotImplemented, response)
 
 
 class TestConstants(unittest.TestCase):
@@ -136,14 +136,20 @@ class TestConstants(unittest.TestCase):
 
 
 class RapiMockTest(unittest.TestCase):
-  def test(self):
+  def test404(self):
+    (code, _, body) = RapiMock().FetchResponse("/foo", "GET", None, None)
+    self.assertEqual(code, 404)
+    self.assertTrue(body is None)
+
+  def test501(self):
+    (code, _, body) = RapiMock().FetchResponse("/version", "POST", None, None)
+    self.assertEqual(code, 501)
+    self.assertEqual(body, "Method not implemented")
+
+  def test200(self):
     rapi = RapiMock()
-    path = "/version"
-    self.assertEqual((404, None), rapi.FetchResponse("/foo", "GET", None, None))
-    self.assertEqual((501, "Method not implemented"),
-                     rapi.FetchResponse("/version", "POST", None, None))
     rapi.AddResponse("2")
-    code, response = rapi.FetchResponse("/version", "GET", None, None)
+    (code, _, response) = rapi.FetchResponse("/version", "GET", None, None)
     self.assertEqual(200, code)
     self.assertEqual("2", response)
     self.failUnless(isinstance(rapi.GetLastHandler(), rlib2.R_version))