Ignore log messages in unittests
[ganeti-local] / test / ganeti.cli_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2008 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 """Script for unittesting the cli module"""
23
24 import unittest
25 from cStringIO import StringIO
26
27 import ganeti
28 import testutils
29
30 from ganeti import constants
31 from ganeti import cli
32 from ganeti.errors import OpPrereqError, ParameterError
33
34
35 class TestParseTimespec(unittest.TestCase):
36   """Testing case for ParseTimespec"""
37
38   def testValidTimes(self):
39     """Test valid timespecs"""
40     test_data = [
41       ('1s', 1),
42       ('1', 1),
43       ('1m', 60),
44       ('1h', 60 * 60),
45       ('1d', 60 * 60 * 24),
46       ('1w', 60 * 60 * 24 * 7),
47       ('4h', 4 * 60 * 60),
48       ('61m', 61 * 60),
49       ]
50     for value, expected_result in test_data:
51       self.failUnlessEqual(cli.ParseTimespec(value), expected_result)
52
53   def testInvalidTime(self):
54     """Test invalid timespecs"""
55     test_data = [
56       '1y',
57       '',
58       'aaa',
59       's',
60       ]
61     for value in test_data:
62       self.failUnlessRaises(OpPrereqError, cli.ParseTimespec, value)
63
64
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}
69
70   def testSplitKeyVal(self):
71     """Test splitting"""
72     self.failUnlessEqual(cli._SplitKeyVal("option", self.DATA), self.RESULT)
73
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,
78                             "option", data)
79
80   def testEmptyData(self):
81     """Test how we handle splitting an empty string"""
82     self.failUnlessEqual(cli._SplitKeyVal("option", ""), {})
83
84 class TestIdentKeyVal(unittest.TestCase):
85   """Testing case for cli.check_ident_key_val"""
86
87   def testIdentKeyVal(self):
88     """Test identkeyval"""
89     def cikv(value):
90       return cli.check_ident_key_val("option", "opt", value)
91
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")
100
101
102 class TestToStream(unittest.TestCase):
103   """Thes the ToStream functions"""
104
105   def testBasic(self):
106     for data in ["foo",
107                  "foo %s",
108                  "foo %(test)s",
109                  "foo %s %s",
110                  "",
111                  ]:
112       buf = StringIO()
113       cli._ToStream(buf, data)
114       self.failUnlessEqual(buf.getvalue(), data+'\n')
115
116   def testParams(self):
117       buf = StringIO()
118       cli._ToStream(buf, "foo %s", 1)
119       self.failUnlessEqual(buf.getvalue(), "foo 1\n")
120       buf = StringIO()
121       cli._ToStream(buf, "foo %s", (15,16))
122       self.failUnlessEqual(buf.getvalue(), "foo (15, 16)\n")
123       buf = StringIO()
124       cli._ToStream(buf, "foo %s %s", "a", "b")
125       self.failUnlessEqual(buf.getvalue(), "foo a b\n")
126
127 if __name__ == '__main__':
128   testutils.GanetiTestProgram()