X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/b39d17b13021e61581ad6da4f54cbbd2ce2c4fcb..48aaca91efa214b37dba94f28582be73f3c90dbd:/test/ganeti.utils.text_unittest.py?ds=sidebyside diff --git a/test/ganeti.utils.text_unittest.py b/test/ganeti.utils.text_unittest.py index c2fc549..181e2f6 100755 --- a/test/ganeti.utils.text_unittest.py +++ b/test/ganeti.utils.text_unittest.py @@ -106,6 +106,58 @@ class TestMatchNameComponent(unittest.TestCase): None) +class TestDnsNameGlobPattern(unittest.TestCase): + def setUp(self): + self.names = [ + "node1.example.com", + "node2-0.example.com", + "node2-1.example.com", + "node1.example.net", + "web1.example.com", + "web2.example.com", + "sub.site.example.com", + ] + + def _Test(self, pattern): + re_pat = utils.DnsNameGlobPattern(pattern) + + return filter(re.compile(re_pat).match, self.names) + + def test(self): + for pattern in ["xyz", "node", " ", "example.net", "x*.example.*", + "x*.example.com"]: + self.assertEqual(self._Test(pattern), []) + + for pattern in ["*", "???*"]: + self.assertEqual(self._Test(pattern), self.names) + + self.assertEqual(self._Test("node1.*.net"), ["node1.example.net"]) + self.assertEqual(self._Test("*.example.net"), ["node1.example.net"]) + self.assertEqual(self._Test("web1.example.com"), ["web1.example.com"]) + + for pattern in ["*.*.*.*", "???", "*.site"]: + self.assertEqual(self._Test(pattern), ["sub.site.example.com"]) + + self.assertEqual(self._Test("node1"), [ + "node1.example.com", + "node1.example.net", + ]) + self.assertEqual(self._Test("node?*.example.*"), [ + "node1.example.com", + "node2-0.example.com", + "node2-1.example.com", + "node1.example.net", + ]) + self.assertEqual(self._Test("*-?"), [ + "node2-0.example.com", + "node2-1.example.com", + ]) + self.assertEqual(self._Test("node2-?.example.com"), [ + "node2-0.example.com", + "node2-1.example.com", + ]) + + class TestFormatUnit(unittest.TestCase): """Test case for the FormatUnit function""" @@ -209,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") @@ -263,6 +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) + + 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): @@ -356,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") @@ -456,5 +550,80 @@ class TestBuildShellCmd(unittest.TestCase): self.assertEqual(utils.BuildShellCmd("ls %s", "ab"), "ls ab") +class TestOrdinal(unittest.TestCase): + def test(self): + checks = { + 0: "0th", 1: "1st", 2: "2nd", 3: "3rd", 4: "4th", 5: "5th", 6: "6th", + 7: "7th", 8: "8th", 9: "9th", 10: "10th", 11: "11th", 12: "12th", + 13: "13th", 14: "14th", 15: "15th", 16: "16th", 17: "17th", + 18: "18th", 19: "19th", 20: "20th", 21: "21st", 25: "25th", 30: "30th", + 32: "32nd", 40: "40th", 50: "50th", 55: "55th", 60: "60th", 62: "62nd", + 70: "70th", 80: "80th", 83: "83rd", 90: "90th", 91: "91st", + 582: "582nd", 999: "999th", + } + + for value, ordinal in checks.items(): + self.assertEqual(utils.FormatOrdinal(value), ordinal) + + +class TestTruncate(unittest.TestCase): + def _Test(self, text, length): + result = utils.Truncate(text, length) + self.assertTrue(len(result) <= length) + return result + + def test(self): + self.assertEqual(self._Test("", 80), "") + self.assertEqual(self._Test("abc", 4), "abc") + self.assertEqual(self._Test("Hello World", 80), "Hello World") + self.assertEqual(self._Test("Hello World", 4), "H...") + self.assertEqual(self._Test("Hello World", 5), "He...") + + for i in [4, 10, 100]: + data = i * "FooBarBaz" + self.assertEqual(self._Test(data, len(data)), data) + + for (length, exp) in [(8, u"T\u00e4st\u2026xyz"), (7, u"T\u00e4st...")]: + self.assertEqual(self._Test(u"T\u00e4st\u2026xyz", length), exp) + + self.assertEqual(self._Test(range(100), 20), "[0, 1, 2, 3, 4, 5...") + + def testError(self): + for i in range(4): + 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()