root / test / ganeti.cli_unittest.py @ 6263189c
History | View | Annotate | Download (3.8 kB)
1 | 2241e2b9 | Iustin Pop | #!/usr/bin/python
|
---|---|---|---|
2 | 2241e2b9 | Iustin Pop | #
|
3 | 2241e2b9 | Iustin Pop | |
4 | 2241e2b9 | Iustin Pop | # Copyright (C) 2008 Google Inc.
|
5 | 2241e2b9 | Iustin Pop | #
|
6 | 2241e2b9 | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | 2241e2b9 | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | 2241e2b9 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | 2241e2b9 | Iustin Pop | # (at your option) any later version.
|
10 | 2241e2b9 | Iustin Pop | #
|
11 | 2241e2b9 | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | 2241e2b9 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 2241e2b9 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 2241e2b9 | Iustin Pop | # General Public License for more details.
|
15 | 2241e2b9 | Iustin Pop | #
|
16 | 2241e2b9 | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | 2241e2b9 | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | 2241e2b9 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 2241e2b9 | Iustin Pop | # 02110-1301, USA.
|
20 | 2241e2b9 | Iustin Pop | |
21 | 2241e2b9 | Iustin Pop | |
22 | 2241e2b9 | Iustin Pop | """Script for unittesting the cli module"""
|
23 | 2241e2b9 | Iustin Pop | |
24 | 2241e2b9 | Iustin Pop | import unittest |
25 | 46fbdd04 | Iustin Pop | from cStringIO import StringIO |
26 | 2241e2b9 | Iustin Pop | |
27 | 2241e2b9 | Iustin Pop | import ganeti |
28 | 2241e2b9 | Iustin Pop | import testutils |
29 | 2241e2b9 | Iustin Pop | from ganeti import constants |
30 | 2241e2b9 | Iustin Pop | from ganeti import cli |
31 | a8469393 | Iustin Pop | from ganeti.errors import OpPrereqError, ParameterError |
32 | 2241e2b9 | Iustin Pop | |
33 | 2241e2b9 | Iustin Pop | class TestParseTimespec(unittest.TestCase): |
34 | 2241e2b9 | Iustin Pop | """Testing case for ParseTimespec"""
|
35 | 2241e2b9 | Iustin Pop | |
36 | 2241e2b9 | Iustin Pop | def testValidTimes(self): |
37 | 2241e2b9 | Iustin Pop | """Test valid timespecs"""
|
38 | 2241e2b9 | Iustin Pop | test_data = [ |
39 | 2241e2b9 | Iustin Pop | ('1s', 1), |
40 | 2241e2b9 | Iustin Pop | ('1', 1), |
41 | 2241e2b9 | Iustin Pop | ('1m', 60), |
42 | 2241e2b9 | Iustin Pop | ('1h', 60 * 60), |
43 | 2241e2b9 | Iustin Pop | ('1d', 60 * 60 * 24), |
44 | 2241e2b9 | Iustin Pop | ('1w', 60 * 60 * 24 * 7), |
45 | 2241e2b9 | Iustin Pop | ('4h', 4 * 60 * 60), |
46 | 2241e2b9 | Iustin Pop | ('61m', 61 * 60), |
47 | 2241e2b9 | Iustin Pop | ] |
48 | 2241e2b9 | Iustin Pop | for value, expected_result in test_data: |
49 | 2241e2b9 | Iustin Pop | self.failUnlessEqual(cli.ParseTimespec(value), expected_result)
|
50 | 2241e2b9 | Iustin Pop | |
51 | 2241e2b9 | Iustin Pop | def testInvalidTime(self): |
52 | 2241e2b9 | Iustin Pop | """Test invalid timespecs"""
|
53 | 2241e2b9 | Iustin Pop | test_data = [ |
54 | 2241e2b9 | Iustin Pop | '1y',
|
55 | 2241e2b9 | Iustin Pop | '',
|
56 | 2241e2b9 | Iustin Pop | 'aaa',
|
57 | 2241e2b9 | Iustin Pop | 's',
|
58 | 2241e2b9 | Iustin Pop | ] |
59 | 2241e2b9 | Iustin Pop | for value in test_data: |
60 | 2241e2b9 | Iustin Pop | self.failUnlessRaises(OpPrereqError, cli.ParseTimespec, value)
|
61 | 2241e2b9 | Iustin Pop | |
62 | 2241e2b9 | Iustin Pop | |
63 | a8469393 | Iustin Pop | class TestSplitKeyVal(unittest.TestCase): |
64 | a8469393 | Iustin Pop | """Testing case for cli._SplitKeyVal"""
|
65 | fcd62d84 | Iustin Pop | DATA = "a=b,c,no_d,-e"
|
66 | fcd62d84 | Iustin Pop | RESULT = {"a": "b", "c": True, "d": False, "e": None} |
67 | a8469393 | Iustin Pop | |
68 | a8469393 | Iustin Pop | def testSplitKeyVal(self): |
69 | a8469393 | Iustin Pop | """Test splitting"""
|
70 | a8469393 | Iustin Pop | self.failUnlessEqual(cli._SplitKeyVal("option", self.DATA), self.RESULT) |
71 | a8469393 | Iustin Pop | |
72 | a8469393 | Iustin Pop | def testDuplicateParam(self): |
73 | a8469393 | Iustin Pop | """Test duplicate parameters"""
|
74 | a8469393 | Iustin Pop | for data in ("a=1,a=2", "a,no_a"): |
75 | a8469393 | Iustin Pop | self.failUnlessRaises(ParameterError, cli._SplitKeyVal,
|
76 | a8469393 | Iustin Pop | "option", data)
|
77 | a8469393 | Iustin Pop | |
78 | 4f31882e | Guido Trotter | def testEmptyData(self): |
79 | 4f31882e | Guido Trotter | """Test how we handle splitting an empty string"""
|
80 | 4f31882e | Guido Trotter | self.failUnlessEqual(cli._SplitKeyVal("option", ""), {}) |
81 | 4f31882e | Guido Trotter | |
82 | 8b46606c | Guido Trotter | class TestIdentKeyVal(unittest.TestCase): |
83 | 8b46606c | Guido Trotter | """Testing case for cli.check_ident_key_val"""
|
84 | 8b46606c | Guido Trotter | |
85 | 8b46606c | Guido Trotter | def testIdentKeyVal(self): |
86 | 8b46606c | Guido Trotter | """Test identkeyval"""
|
87 | 8b46606c | Guido Trotter | def cikv(value): |
88 | 8b46606c | Guido Trotter | return cli.check_ident_key_val("option", "opt", value) |
89 | 8b46606c | Guido Trotter | |
90 | 8b46606c | Guido Trotter | self.assertEqual(cikv("foo:bar"), ("foo", {"bar": True})) |
91 | 8b46606c | Guido Trotter | self.assertEqual(cikv("foo:bar=baz"), ("foo", {"bar": "baz"})) |
92 | 8b46606c | Guido Trotter | self.assertEqual(cikv("bar:b=c,c=a"), ("bar", {"b": "c", "c": "a"})) |
93 | 8b46606c | Guido Trotter | self.assertEqual(cikv("no_bar"), ("bar", False)) |
94 | 8b46606c | Guido Trotter | self.assertRaises(ParameterError, cikv, "no_bar:foo") |
95 | 8b46606c | Guido Trotter | self.assertRaises(ParameterError, cikv, "no_bar:foo=baz") |
96 | 8b46606c | Guido Trotter | self.assertEqual(cikv("-foo"), ("foo", None)) |
97 | 8b46606c | Guido Trotter | self.assertRaises(ParameterError, cikv, "-foo:a=c") |
98 | 8b46606c | Guido Trotter | |
99 | a8469393 | Iustin Pop | |
100 | 46fbdd04 | Iustin Pop | class TestToStream(unittest.TestCase): |
101 | 46fbdd04 | Iustin Pop | """Thes the ToStream functions"""
|
102 | 46fbdd04 | Iustin Pop | |
103 | 46fbdd04 | Iustin Pop | def testBasic(self): |
104 | 46fbdd04 | Iustin Pop | for data in ["foo", |
105 | 46fbdd04 | Iustin Pop | "foo %s",
|
106 | 46fbdd04 | Iustin Pop | "foo %(test)s",
|
107 | 46fbdd04 | Iustin Pop | "foo %s %s",
|
108 | 46fbdd04 | Iustin Pop | "",
|
109 | 46fbdd04 | Iustin Pop | ]: |
110 | 46fbdd04 | Iustin Pop | buf = StringIO() |
111 | 46fbdd04 | Iustin Pop | cli._ToStream(buf, data) |
112 | 46fbdd04 | Iustin Pop | self.failUnlessEqual(buf.getvalue(), data+'\n') |
113 | 46fbdd04 | Iustin Pop | |
114 | 46fbdd04 | Iustin Pop | def testParams(self): |
115 | 46fbdd04 | Iustin Pop | buf = StringIO() |
116 | 46fbdd04 | Iustin Pop | cli._ToStream(buf, "foo %s", 1) |
117 | 46fbdd04 | Iustin Pop | self.failUnlessEqual(buf.getvalue(), "foo 1\n") |
118 | 46fbdd04 | Iustin Pop | buf = StringIO() |
119 | 46fbdd04 | Iustin Pop | cli._ToStream(buf, "foo %s", (15,16)) |
120 | 46fbdd04 | Iustin Pop | self.failUnlessEqual(buf.getvalue(), "foo (15, 16)\n") |
121 | 46fbdd04 | Iustin Pop | buf = StringIO() |
122 | 46fbdd04 | Iustin Pop | cli._ToStream(buf, "foo %s %s", "a", "b") |
123 | 46fbdd04 | Iustin Pop | self.failUnlessEqual(buf.getvalue(), "foo a b\n") |
124 | 46fbdd04 | Iustin Pop | |
125 | 2241e2b9 | Iustin Pop | if __name__ == '__main__': |
126 | 2241e2b9 | Iustin Pop | unittest.main() |