From e02836d663e75162d0e4a623bd2f7cca5cbb065b Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 8 Sep 2011 12:05:28 +0200
Subject: [PATCH] rlib2: Add unittests for two error conditions
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Adds tests for errors on connecting to the server and for a full queue.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: RenΓ© Nussbaumer <rn@google.com>
---
 test/ganeti.rapi.rlib2_unittest.py | 45 ++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/test/ganeti.rapi.rlib2_unittest.py b/test/ganeti.rapi.rlib2_unittest.py
index 6584c38e8..5b0f22d7a 100755
--- a/test/ganeti.rapi.rlib2_unittest.py
+++ b/test/ganeti.rapi.rlib2_unittest.py
@@ -32,12 +32,29 @@ from ganeti import opcodes
 from ganeti import compat
 from ganeti import http
 from ganeti import query
+from ganeti import luxi
+from ganeti import errors
 
 from ganeti.rapi import rlib2
 
 import testutils
 
 
+class _FakeRequestPrivateData:
+  def __init__(self, body_data):
+    self.body_data = body_data
+
+
+class _FakeRequest:
+  def __init__(self, body_data):
+    self.private = _FakeRequestPrivateData(body_data)
+
+
+def _CreateHandler(cls, items, queryargs, body_data, client_cls):
+  return cls(items, queryargs, _FakeRequest(body_data),
+             _client_cls=client_cls)
+
+
 class TestConstants(unittest.TestCase):
   def testConsole(self):
     # Exporting the console field without authentication might expose
@@ -56,6 +73,34 @@ class TestConstants(unittest.TestCase):
       self.assertFalse(set(fields) - set(query.ALL_FIELDS[qr].keys()))
 
 
+class TestClientConnectError(unittest.TestCase):
+  @staticmethod
+  def _FailingClient():
+    raise luxi.NoMasterError("test")
+
+  def test(self):
+    resources = [
+      rlib2.R_2_groups,
+      rlib2.R_2_instances,
+      rlib2.R_2_nodes,
+      ]
+    for cls in resources:
+      handler = _CreateHandler(cls, ["name"], [], None, self._FailingClient)
+      self.assertRaises(http.HttpBadGateway, handler.GET)
+
+
+class TestJobSubmitError(unittest.TestCase):
+  class _SubmitErrorClient:
+    @staticmethod
+    def SubmitJob(ops):
+      raise errors.JobQueueFull("test")
+
+  def test(self):
+    handler = _CreateHandler(rlib2.R_2_redist_config, [], [], None,
+                             self._SubmitErrorClient)
+    self.assertRaises(http.HttpServiceUnavailable, handler.PUT)
+
+
 class TestParseInstanceCreateRequestVersion1(testutils.GanetiTestCase):
   def setUp(self):
     testutils.GanetiTestCase.setUp(self)
-- 
GitLab