root / test / ganeti.cli_unittest.py @ f34901f8
History | View | Annotate | Download (2.9 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 |
|
79 |
class TestToStream(unittest.TestCase): |
80 |
"""Thes the ToStream functions"""
|
81 |
|
82 |
def testBasic(self): |
83 |
for data in ["foo", |
84 |
"foo %s",
|
85 |
"foo %(test)s",
|
86 |
"foo %s %s",
|
87 |
"",
|
88 |
]: |
89 |
buf = StringIO() |
90 |
cli._ToStream(buf, data) |
91 |
self.failUnlessEqual(buf.getvalue(), data+'\n') |
92 |
|
93 |
def testParams(self): |
94 |
buf = StringIO() |
95 |
cli._ToStream(buf, "foo %s", 1) |
96 |
self.failUnlessEqual(buf.getvalue(), "foo 1\n") |
97 |
buf = StringIO() |
98 |
cli._ToStream(buf, "foo %s", (15,16)) |
99 |
self.failUnlessEqual(buf.getvalue(), "foo (15, 16)\n") |
100 |
buf = StringIO() |
101 |
cli._ToStream(buf, "foo %s %s", "a", "b") |
102 |
self.failUnlessEqual(buf.getvalue(), "foo a b\n") |
103 |
|
104 |
if __name__ == '__main__': |
105 |
unittest.main() |