"""Script for unittesting the cli module"""
import unittest
+from cStringIO import StringIO
import ganeti
import testutils
from ganeti import constants
from ganeti import cli
-from ganeti.errors import OpPrereqError
+from ganeti.errors import OpPrereqError, ParameterError
class TestParseTimespec(unittest.TestCase):
"""Testing case for ParseTimespec"""
self.failUnlessRaises(OpPrereqError, cli.ParseTimespec, value)
+class TestSplitKeyVal(unittest.TestCase):
+ """Testing case for cli._SplitKeyVal"""
+ DATA = "a=b,c,no_d,-e"
+ RESULT = {"a": "b", "c": True, "d": False, "e": None}
+
+ def testSplitKeyVal(self):
+ """Test splitting"""
+ self.failUnlessEqual(cli._SplitKeyVal("option", self.DATA), self.RESULT)
+
+ def testDuplicateParam(self):
+ """Test duplicate parameters"""
+ for data in ("a=1,a=2", "a,no_a"):
+ self.failUnlessRaises(ParameterError, cli._SplitKeyVal,
+ "option", data)
+
+
+class TestToStream(unittest.TestCase):
+ """Thes the ToStream functions"""
+
+ def testBasic(self):
+ for data in ["foo",
+ "foo %s",
+ "foo %(test)s",
+ "foo %s %s",
+ "",
+ ]:
+ buf = StringIO()
+ cli._ToStream(buf, data)
+ self.failUnlessEqual(buf.getvalue(), data+'\n')
+
+ def testParams(self):
+ buf = StringIO()
+ cli._ToStream(buf, "foo %s", 1)
+ self.failUnlessEqual(buf.getvalue(), "foo 1\n")
+ buf = StringIO()
+ cli._ToStream(buf, "foo %s", (15,16))
+ self.failUnlessEqual(buf.getvalue(), "foo (15, 16)\n")
+ buf = StringIO()
+ cli._ToStream(buf, "foo %s %s", "a", "b")
+ self.failUnlessEqual(buf.getvalue(), "foo a b\n")
+
if __name__ == '__main__':
unittest.main()