root / test / ganeti.cli_unittest.py @ 6263189c
History | View | Annotate | Download (3.8 kB)
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 |
from ganeti import constants |
30 |
from ganeti import cli |
31 |
from ganeti.errors import OpPrereqError, ParameterError |
32 |
|
33 |
class TestParseTimespec(unittest.TestCase): |
34 |
"""Testing case for ParseTimespec"""
|
35 |
|
36 |
def testValidTimes(self): |
37 |
"""Test valid timespecs"""
|
38 |
test_data = [ |
39 |
('1s', 1), |
40 |
('1', 1), |
41 |
('1m', 60), |
42 |
('1h', 60 * 60), |
43 |
('1d', 60 * 60 * 24), |
44 |
('1w', 60 * 60 * 24 * 7), |
45 |
('4h', 4 * 60 * 60), |
46 |
('61m', 61 * 60), |
47 |
] |
48 |
for value, expected_result in test_data: |
49 |
self.failUnlessEqual(cli.ParseTimespec(value), expected_result)
|
50 |
|
51 |
def testInvalidTime(self): |
52 |
"""Test invalid timespecs"""
|
53 |
test_data = [ |
54 |
'1y',
|
55 |
'',
|
56 |
'aaa',
|
57 |
's',
|
58 |
] |
59 |
for value in test_data: |
60 |
self.failUnlessRaises(OpPrereqError, cli.ParseTimespec, value)
|
61 |
|
62 |
|
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} |
67 |
|
68 |
def testSplitKeyVal(self): |
69 |
"""Test splitting"""
|
70 |
self.failUnlessEqual(cli._SplitKeyVal("option", self.DATA), self.RESULT) |
71 |
|
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,
|
76 |
"option", data)
|
77 |
|
78 |
def testEmptyData(self): |
79 |
"""Test how we handle splitting an empty string"""
|
80 |
self.failUnlessEqual(cli._SplitKeyVal("option", ""), {}) |
81 |
|
82 |
class TestIdentKeyVal(unittest.TestCase): |
83 |
"""Testing case for cli.check_ident_key_val"""
|
84 |
|
85 |
def testIdentKeyVal(self): |
86 |
"""Test identkeyval"""
|
87 |
def cikv(value): |
88 |
return cli.check_ident_key_val("option", "opt", value) |
89 |
|
90 |
self.assertEqual(cikv("foo:bar"), ("foo", {"bar": True})) |
91 |
self.assertEqual(cikv("foo:bar=baz"), ("foo", {"bar": "baz"})) |
92 |
self.assertEqual(cikv("bar:b=c,c=a"), ("bar", {"b": "c", "c": "a"})) |
93 |
self.assertEqual(cikv("no_bar"), ("bar", False)) |
94 |
self.assertRaises(ParameterError, cikv, "no_bar:foo") |
95 |
self.assertRaises(ParameterError, cikv, "no_bar:foo=baz") |
96 |
self.assertEqual(cikv("-foo"), ("foo", None)) |
97 |
self.assertRaises(ParameterError, cikv, "-foo:a=c") |
98 |
|
99 |
|
100 |
class TestToStream(unittest.TestCase): |
101 |
"""Thes the ToStream functions"""
|
102 |
|
103 |
def testBasic(self): |
104 |
for data in ["foo", |
105 |
"foo %s",
|
106 |
"foo %(test)s",
|
107 |
"foo %s %s",
|
108 |
"",
|
109 |
]: |
110 |
buf = StringIO() |
111 |
cli._ToStream(buf, data) |
112 |
self.failUnlessEqual(buf.getvalue(), data+'\n') |
113 |
|
114 |
def testParams(self): |
115 |
buf = StringIO() |
116 |
cli._ToStream(buf, "foo %s", 1) |
117 |
self.failUnlessEqual(buf.getvalue(), "foo 1\n") |
118 |
buf = StringIO() |
119 |
cli._ToStream(buf, "foo %s", (15,16)) |
120 |
self.failUnlessEqual(buf.getvalue(), "foo (15, 16)\n") |
121 |
buf = StringIO() |
122 |
cli._ToStream(buf, "foo %s %s", "a", "b") |
123 |
self.failUnlessEqual(buf.getvalue(), "foo a b\n") |
124 |
|
125 |
if __name__ == '__main__': |
126 |
unittest.main() |