4 # Copyright (C) 2008 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Script for unittesting the cli module"""
25 from cStringIO import StringIO
29 from ganeti import constants
30 from ganeti import cli
31 from ganeti.errors import OpPrereqError, ParameterError
33 class TestParseTimespec(unittest.TestCase):
34 """Testing case for ParseTimespec"""
36 def testValidTimes(self):
37 """Test valid timespecs"""
44 ('1w', 60 * 60 * 24 * 7),
48 for value, expected_result in test_data:
49 self.failUnlessEqual(cli.ParseTimespec(value), expected_result)
51 def testInvalidTime(self):
52 """Test invalid timespecs"""
59 for value in test_data:
60 self.failUnlessRaises(OpPrereqError, cli.ParseTimespec, value)
63 class TestSplitKeyVal(unittest.TestCase):
64 """Testing case for cli._SplitKeyVal"""
65 DATA = "a=b,c,no_d,-e"
66 RESULT = {"a": "b", "c": True, "d": False, "e": None}
68 def testSplitKeyVal(self):
70 self.failUnlessEqual(cli._SplitKeyVal("option", self.DATA), self.RESULT)
72 def testDuplicateParam(self):
73 """Test duplicate parameters"""
74 for data in ("a=1,a=2", "a,no_a"):
75 self.failUnlessRaises(ParameterError, cli._SplitKeyVal,
78 def testEmptyData(self):
79 """Test how we handle splitting an empty string"""
80 self.failUnlessEqual(cli._SplitKeyVal("option", ""), {})
83 class TestToStream(unittest.TestCase):
84 """Thes the ToStream functions"""
94 cli._ToStream(buf, data)
95 self.failUnlessEqual(buf.getvalue(), data+'\n')
99 cli._ToStream(buf, "foo %s", 1)
100 self.failUnlessEqual(buf.getvalue(), "foo 1\n")
102 cli._ToStream(buf, "foo %s", (15,16))
103 self.failUnlessEqual(buf.getvalue(), "foo (15, 16)\n")
105 cli._ToStream(buf, "foo %s %s", "a", "b")
106 self.failUnlessEqual(buf.getvalue(), "foo a b\n")
108 if __name__ == '__main__':