From 50c7d82ee6626b853bdd6e23533024f4e60c87c5 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 8 Sep 2011 13:35:21 +0200
Subject: [PATCH] rlib2: Convert /2/modify to OpcodeResource
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Also add unittests.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: RenΓ© Nussbaumer <rn@google.com>
---
 lib/rapi/rlib2.py                  | 13 +------
 test/ganeti.rapi.rlib2_unittest.py | 60 +++++++++++++++++++++++++++++-
 2 files changed, 61 insertions(+), 12 deletions(-)

diff --git a/lib/rapi/rlib2.py b/lib/rapi/rlib2.py
index e969c3802..e45092317 100644
--- a/lib/rapi/rlib2.py
+++ b/lib/rapi/rlib2.py
@@ -240,20 +240,11 @@ class R_2_redist_config(baserlib.ResourceBase):
     return self.SubmitJob([opcodes.OpClusterRedistConf()])
 
 
-class R_2_cluster_modify(baserlib.ResourceBase):
+class R_2_cluster_modify(baserlib.OpcodeResource):
   """/2/modify resource.
 
   """
-  def PUT(self):
-    """Modifies cluster parameters.
-
-    @return: a job id
-
-    """
-    op = baserlib.FillOpcode(opcodes.OpClusterSetParams, self.request_body,
-                             None)
-
-    return self.SubmitJob([op])
+  PUT_OPCODE = opcodes.OpClusterSetParams
 
 
 class R_2_jobs(baserlib.ResourceBase):
diff --git a/test/ganeti.rapi.rlib2_unittest.py b/test/ganeti.rapi.rlib2_unittest.py
index 5b0f22d7a..15917fcc0 100755
--- a/test/ganeti.rapi.rlib2_unittest.py
+++ b/test/ganeti.rapi.rlib2_unittest.py
@@ -25,7 +25,8 @@
 
 
 import unittest
-import tempfile
+import itertools
+import random
 
 from ganeti import constants
 from ganeti import opcodes
@@ -55,6 +56,33 @@ def _CreateHandler(cls, items, queryargs, body_data, client_cls):
              _client_cls=client_cls)
 
 
+class _FakeClient:
+  def __init__(self):
+    self._jobs = []
+
+  def GetNextSubmittedJob(self):
+    return self._jobs.pop(0)
+
+  def SubmitJob(self, ops):
+    job_id = str(1 + int(random.random() * 1000000))
+    self._jobs.append((job_id, ops))
+    return job_id
+
+
+class _FakeClientFactory:
+  def __init__(self, cls):
+    self._client_cls = cls
+    self._clients = []
+
+  def GetNextClient(self):
+    return self._clients.pop(0)
+
+  def __call__(self):
+    cl = self._client_cls()
+    self._clients.append(cl)
+    return cl
+
+
 class TestConstants(unittest.TestCase):
   def testConsole(self):
     # Exporting the console field without authentication might expose
@@ -101,6 +129,36 @@ class TestJobSubmitError(unittest.TestCase):
     self.assertRaises(http.HttpServiceUnavailable, handler.PUT)
 
 
+class TestClusterModify(unittest.TestCase):
+  def test(self):
+    clfactory = _FakeClientFactory(_FakeClient)
+    handler = _CreateHandler(rlib2.R_2_cluster_modify, [], [], {
+      "vg_name": "testvg",
+      "candidate_pool_size": 100,
+      }, clfactory)
+    job_id = handler.PUT()
+
+    cl = clfactory.GetNextClient()
+    self.assertRaises(IndexError, clfactory.GetNextClient)
+
+    (exp_job_id, (op, )) = cl.GetNextSubmittedJob()
+    self.assertEqual(job_id, exp_job_id)
+    self.assertTrue(isinstance(op, opcodes.OpClusterSetParams))
+    self.assertEqual(op.vg_name, "testvg")
+    self.assertEqual(op.candidate_pool_size, 100)
+
+    self.assertRaises(IndexError, cl.GetNextSubmittedJob)
+
+  def testInvalidValue(self):
+    for attr in ["vg_name", "candidate_pool_size", "beparams", "_-Unknown#"]:
+      clfactory = _FakeClientFactory(_FakeClient)
+      handler = _CreateHandler(rlib2.R_2_cluster_modify, [], [], {
+        attr: True,
+        }, clfactory)
+      self.assertRaises(http.HttpBadRequest, handler.PUT)
+      self.assertRaises(IndexError, clfactory.GetNextClient)
+
+
 class TestParseInstanceCreateRequestVersion1(testutils.GanetiTestCase):
   def setUp(self):
     testutils.GanetiTestCase.setUp(self)
-- 
GitLab