On 22 January 2013 17:36, Michael Hanselmann <[email protected]> wrote:
> 2013/1/22 Bernardo Dal Seno <[email protected]>:
>> +class TestVerifyErrors(unittest.TestCase):
>> +  # Use two arbitary error codes to pass validation of ignore_errors
>> +  _ERR1ID = constants.CV_ECLUSTERCFG[1]
>> +  _ERR2ID = constants.CV_ECLUSTERCERT[1]
>
> Please use unpacking (ignoring the unwanted values) instead of indexing:
>
> (_, _ERR2ID, _) = constants.CV_ECLUSTERCERT
>
> This way structure changes have a slightly bigger change of being noticed.
>
>> +  _NODESTR = "node"
>> +  _NODENAME = "mynode"
>> +  _INSTSTR = "instance"
>> +  _INSTNAME = "myinstance"
>> +  _ERR1CODE = (_NODESTR, _ERR1ID, "Error one")
>> +  _ERR2CODE = (_INSTSTR, _ERR2ID, "Error two")
>> +  _ERR1ARGS = (_ERR1CODE, _NODENAME, "Error1 is %s", "an error")
>> +  _ERR1MSG = _ERR1ARGS[2] % _ERR1ARGS[3]
>> +  _ERR2ARGS = (_ERR2CODE, _INSTNAME, "Error2 has no argument")
>> +  _ERR2MSG = _ERR2ARGS[2]
>
> I lost track. :-)

I've used unpacking and rearranged the lines a little to make more
clear what's going on.


>> +    self.assertNotEqual(string.find(item), -1)
>
> Don't use “str.find”, but rather “item in str”. Also, please don't
> “string” as that's a built-in module name (it's easy to avoid here).
> Elsewhere too.

str is reserved, string too. It's very annoying :-).  Anyway, fixed, thanks.

diff --git a/test/py/ganeti.cmdlib_unittest.py
b/test/py/ganeti.cmdlib_unittest.py
index 37ad495..6766648 100755
--- a/test/py/ganeti.cmdlib_unittest.py
+++ b/test/py/ganeti.cmdlib_unittest.py
@@ -1561,18 +1561,21 @@ class _LuTestVerifyErrors(cmdlib._VerifyErrors):


 class TestVerifyErrors(unittest.TestCase):
-  # Use two arbitary error codes to pass validation of ignore_errors
-  _ERR1ID = constants.CV_ECLUSTERCFG[1]
-  _ERR2ID = constants.CV_ECLUSTERCERT[1]
+  # Fake cluster-verify error code structures; we use two arbitary real error
+  # codes to pass validation of ignore_errors
+  (_, _ERR1ID, _) = constants.CV_ECLUSTERCFG
   _NODESTR = "node"
   _NODENAME = "mynode"
+  _ERR1CODE = (_NODESTR, _ERR1ID, "Error one")
+  (_, _ERR2ID, _) = constants.CV_ECLUSTERCERT
   _INSTSTR = "instance"
   _INSTNAME = "myinstance"
-  _ERR1CODE = (_NODESTR, _ERR1ID, "Error one")
   _ERR2CODE = (_INSTSTR, _ERR2ID, "Error two")
+  # Arguments used to call _Error() or _ErrorIf()
   _ERR1ARGS = (_ERR1CODE, _NODENAME, "Error1 is %s", "an error")
-  _ERR1MSG = _ERR1ARGS[2] % _ERR1ARGS[3]
   _ERR2ARGS = (_ERR2CODE, _INSTNAME, "Error2 has no argument")
+  # Expected error messages
+  _ERR1MSG = _ERR1ARGS[2] % _ERR1ARGS[3]
   _ERR2MSG = _ERR2ARGS[2]

   def testNoError(self):
@@ -1594,22 +1597,22 @@ class TestVerifyErrors(unittest.TestCase):
     # Test-specific checks are made on one LU
     return self.lu1

-  def _checkMsgCommon(self, string, errmsg, itype, item, warning):
-    self.assertNotEqual(string.find(errmsg), -1)
+  def _checkMsgCommon(self, logstr, errmsg, itype, item, warning):
+    self.assertTrue(errmsg in logstr)
     if warning:
-      self.assertNotEqual(string.find("WARNING"), -1)
+      self.assertTrue("WARNING" in logstr)
     else:
-      self.assertNotEqual(string.find("ERROR"), -1)
-    self.assertNotEqual(string.find(itype), -1)
-    self.assertNotEqual(string.find(item), -1)
+      self.assertTrue("ERROR" in logstr)
+    self.assertTrue(itype in logstr)
+    self.assertTrue(item in logstr)

-  def _checkMsg1(self, string, warning=False):
-    self._checkMsgCommon(string, self._ERR1MSG, self._NODESTR, self._NODENAME,
-                    warning)
+  def _checkMsg1(self, logstr, warning=False):
+    self._checkMsgCommon(logstr, self._ERR1MSG, self._NODESTR,
+                         self._NODENAME, warning)

-  def _checkMsg2(self, string, warning=False):
-    self._checkMsgCommon(string, self._ERR2MSG, self._INSTSTR, self._INSTNAME,
-                         warning)
+  def _checkMsg2(self, logstr, warning=False):
+    self._checkMsgCommon(logstr, self._ERR2MSG, self._INSTSTR,
+                         self._INSTNAME, warning)

   def testPlain(self):
     self._InitTest()
@@ -1665,7 +1668,7 @@ class TestVerifyErrors(unittest.TestCase):
     self.assertTrue(lu.bad)
     self.assertEqual(len(lu.msglist), 1)
     self._checkMsg1(lu.msglist[0])
-    self.assertNotEqual(lu.msglist[0].find(self._ERR1ID), -1)
+    self.assertTrue(self._ERR1ID in lu.msglist[0])


 if __name__ == "__main__":

Reply via email to