Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.cli_unittest.py @ 4f31882e

History | View | Annotate | Download (3.1 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

    
83
class TestToStream(unittest.TestCase):
84
  """Thes the ToStream functions"""
85

    
86
  def testBasic(self):
87
    for data in ["foo",
88
                 "foo %s",
89
                 "foo %(test)s",
90
                 "foo %s %s",
91
                 "",
92
                 ]:
93
      buf = StringIO()
94
      cli._ToStream(buf, data)
95
      self.failUnlessEqual(buf.getvalue(), data+'\n')
96

    
97
  def testParams(self):
98
      buf = StringIO()
99
      cli._ToStream(buf, "foo %s", 1)
100
      self.failUnlessEqual(buf.getvalue(), "foo 1\n")
101
      buf = StringIO()
102
      cli._ToStream(buf, "foo %s", (15,16))
103
      self.failUnlessEqual(buf.getvalue(), "foo (15, 16)\n")
104
      buf = StringIO()
105
      cli._ToStream(buf, "foo %s %s", "a", "b")
106
      self.failUnlessEqual(buf.getvalue(), "foo a b\n")
107

    
108
if __name__ == '__main__':
109
  unittest.main()