locking: Change locking order, move NAL after instances
[ganeti-local] / test / ganeti.utils.text_unittest.py
index 5a9af02..181e2f6 100755 (executable)
@@ -261,14 +261,14 @@ class TestShellQuoting(unittest.TestCase):
   """Test case for shell quoting functions"""
 
   def testShellQuote(self):
-    self.assertEqual(utils.ShellQuote('abc'), "abc")
+    self.assertEqual(utils.ShellQuote("abc"), "abc")
     self.assertEqual(utils.ShellQuote('ab"c'), "'ab\"c'")
     self.assertEqual(utils.ShellQuote("a'bc"), "'a'\\''bc'")
     self.assertEqual(utils.ShellQuote("a b c"), "'a b c'")
     self.assertEqual(utils.ShellQuote("a b\\ c"), "'a b\\ c'")
 
   def testShellQuoteArgs(self):
-    self.assertEqual(utils.ShellQuoteArgs(['a', 'b', 'c']), "a b c")
+    self.assertEqual(utils.ShellQuoteArgs(["a", "b", "c"]), "a b c")
     self.assertEqual(utils.ShellQuoteArgs(['a', 'b"', 'c']), "a 'b\"' c")
     self.assertEqual(utils.ShellQuoteArgs(['a', 'b\'', 'c']), "a 'b'\\\''' c")
 
@@ -315,27 +315,44 @@ class TestShellWriter(unittest.TestCase):
     sw = None
     self.assertEqual(buf.getvalue(), "")
 
+  def testEmptyNoIndent(self):
+    buf = StringIO()
+    sw = utils.ShellWriter(buf, indent=False)
+    sw = None
+    self.assertEqual(buf.getvalue(), "")
+
+  @classmethod
+  def _AddLevel(cls, sw, level):
+    if level == 6:
+      return
+
+    sw.IncIndent()
+    try:
+      # Add empty line, it should not be indented
+      sw.Write("")
+      sw.Write(str(level))
+      cls._AddLevel(sw, level + 1)
+    finally:
+      sw.DecIndent()
+
   def testEmptyLines(self):
     buf = StringIO()
     sw = utils.ShellWriter(buf)
 
-    def _AddLevel(level):
-      if level == 6:
-        return
-      sw.IncIndent()
-      try:
-        # Add empty line, it should not be indented
-        sw.Write("")
-        sw.Write(str(level))
-        _AddLevel(level + 1)
-      finally:
-        sw.DecIndent()
-
-    _AddLevel(1)
+    self._AddLevel(sw, 1)
 
     self.assertEqual(buf.getvalue(),
                      "".join("\n%s%s\n" % (i * "  ", i) for i in range(1, 6)))
 
+  def testEmptyLinesNoIndent(self):
+    buf = StringIO()
+    sw = utils.ShellWriter(buf, indent=False)
+
+    self._AddLevel(sw, 1)
+
+    self.assertEqual(buf.getvalue(),
+                     "".join("\n%s\n" % i for i in range(1, 6)))
+
 
 class TestNormalizeAndValidateMac(unittest.TestCase):
   def testInvalid(self):
@@ -429,21 +446,25 @@ class TestFormatTime(unittest.TestCase):
   """Testing case for FormatTime"""
 
   @staticmethod
-  def _TestInProcess(tz, timestamp, expected):
+  def _TestInProcess(tz, timestamp, usecs, expected):
     os.environ["TZ"] = tz
     time.tzset()
-    return utils.FormatTime(timestamp) == expected
+    return utils.FormatTime(timestamp, usecs=usecs) == expected
 
   def _Test(self, *args):
     # Need to use separate process as we want to change TZ
     self.assert_(utils.RunInSeparateProcess(self._TestInProcess, *args))
 
   def test(self):
-    self._Test("UTC", 0, "1970-01-01 00:00:00")
-    self._Test("America/Sao_Paulo", 1292606926, "2010-12-17 15:28:46")
-    self._Test("Europe/London", 1292606926, "2010-12-17 17:28:46")
-    self._Test("Europe/Zurich", 1292606926, "2010-12-17 18:28:46")
-    self._Test("Australia/Sydney", 1292606926, "2010-12-18 04:28:46")
+    self._Test("UTC", 0, None, "1970-01-01 00:00:00")
+    self._Test("America/Sao_Paulo", 1292606926, None, "2010-12-17 15:28:46")
+    self._Test("Europe/London", 1292606926, None, "2010-12-17 17:28:46")
+    self._Test("Europe/Zurich", 1292606926, None, "2010-12-17 18:28:46")
+    self._Test("Europe/Zurich", 1332944288, 8787, "2012-03-28 16:18:08.008787")
+    self._Test("Australia/Sydney", 1292606926, None, "2010-12-18 04:28:46")
+    self._Test("Australia/Sydney", 1292606926, None, "2010-12-18 04:28:46")
+    self._Test("Australia/Sydney", 1292606926, 999999,
+               "2010-12-18 04:28:46.999999")
 
   def testNone(self):
     self.failUnlessEqual(utils.FormatTime(None), "N/A")
@@ -572,5 +593,37 @@ class TestTruncate(unittest.TestCase):
       self.assertRaises(AssertionError, utils.Truncate, "", i)
 
 
+class TestFilterEmptyLinesAndComments(unittest.TestCase):
+  def testEmpty(self):
+    self.assertEqual(utils.FilterEmptyLinesAndComments(""), [])
+    self.assertEqual(utils.FilterEmptyLinesAndComments("\n"), [])
+    self.assertEqual(utils.FilterEmptyLinesAndComments("\n" * 100), [])
+    self.assertEqual(utils.FilterEmptyLinesAndComments("\n  \n\t \n"), [])
+
+  def test(self):
+    text = """
+      This
+        is
+      # with comments
+          a
+            test
+            # in
+            #
+            saying
+      ...#...
+        # multiple places
+        Hello World!
+      """
+    self.assertEqual(utils.FilterEmptyLinesAndComments(text), [
+      "This",
+      "is",
+      "a",
+      "test",
+      "saying",
+      "...#...",
+      "Hello World!",
+      ])
+
+
 if __name__ == "__main__":
   testutils.GanetiTestProgram()