Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.cli_unittest.py @ 01e2ce3a

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