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
30 from ganeti import constants
31 from ganeti import cli
32 from ganeti.errors import OpPrereqError, ParameterError
35 class TestParseTimespec(unittest.TestCase):
36 """Testing case for ParseTimespec"""
38 def testValidTimes(self):
39 """Test valid timespecs"""
46 ('1w', 60 * 60 * 24 * 7),
50 for value, expected_result in test_data:
51 self.failUnlessEqual(cli.ParseTimespec(value), expected_result)
53 def testInvalidTime(self):
54 """Test invalid timespecs"""
61 for value in test_data:
62 self.failUnlessRaises(OpPrereqError, cli.ParseTimespec, value)
65 class TestSplitKeyVal(unittest.TestCase):
66 """Testing case for cli._SplitKeyVal"""
67 DATA = "a=b,c,no_d,-e"
68 RESULT = {"a": "b", "c": True, "d": False, "e": None}
70 def testSplitKeyVal(self):
72 self.failUnlessEqual(cli._SplitKeyVal("option", self.DATA), self.RESULT)
74 def testDuplicateParam(self):
75 """Test duplicate parameters"""
76 for data in ("a=1,a=2", "a,no_a"):
77 self.failUnlessRaises(ParameterError, cli._SplitKeyVal,
80 def testEmptyData(self):
81 """Test how we handle splitting an empty string"""
82 self.failUnlessEqual(cli._SplitKeyVal("option", ""), {})
84 class TestIdentKeyVal(unittest.TestCase):
85 """Testing case for cli.check_ident_key_val"""
87 def testIdentKeyVal(self):
88 """Test identkeyval"""
90 return cli.check_ident_key_val("option", "opt", value)
92 self.assertEqual(cikv("foo:bar"), ("foo", {"bar": True}))
93 self.assertEqual(cikv("foo:bar=baz"), ("foo", {"bar": "baz"}))
94 self.assertEqual(cikv("bar:b=c,c=a"), ("bar", {"b": "c", "c": "a"}))
95 self.assertEqual(cikv("no_bar"), ("bar", False))
96 self.assertRaises(ParameterError, cikv, "no_bar:foo")
97 self.assertRaises(ParameterError, cikv, "no_bar:foo=baz")
98 self.assertEqual(cikv("-foo"), ("foo", None))
99 self.assertRaises(ParameterError, cikv, "-foo:a=c")
102 class TestToStream(unittest.TestCase):
103 """Thes the ToStream functions"""
113 cli._ToStream(buf, data)
114 self.failUnlessEqual(buf.getvalue(), data+'\n')
116 def testParams(self):
118 cli._ToStream(buf, "foo %s", 1)
119 self.failUnlessEqual(buf.getvalue(), "foo 1\n")
121 cli._ToStream(buf, "foo %s", (15,16))
122 self.failUnlessEqual(buf.getvalue(), "foo (15, 16)\n")
124 cli._ToStream(buf, "foo %s %s", "a", "b")
125 self.failUnlessEqual(buf.getvalue(), "foo a b\n")
127 if __name__ == '__main__':
128 testutils.GanetiTestProgram()