Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.cli_unittest.py @ 93be53da

History | View | Annotate | Download (6.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 25231ec5 Michael Hanselmann
30 2241e2b9 Iustin Pop
from ganeti import constants
31 2241e2b9 Iustin Pop
from ganeti import cli
32 a8469393 Iustin Pop
from ganeti.errors import OpPrereqError, ParameterError
33 2241e2b9 Iustin Pop
34 25231ec5 Michael Hanselmann
35 2241e2b9 Iustin Pop
class TestParseTimespec(unittest.TestCase):
36 2241e2b9 Iustin Pop
  """Testing case for ParseTimespec"""
37 2241e2b9 Iustin Pop
38 2241e2b9 Iustin Pop
  def testValidTimes(self):
39 2241e2b9 Iustin Pop
    """Test valid timespecs"""
40 2241e2b9 Iustin Pop
    test_data = [
41 2241e2b9 Iustin Pop
      ('1s', 1),
42 2241e2b9 Iustin Pop
      ('1', 1),
43 2241e2b9 Iustin Pop
      ('1m', 60),
44 2241e2b9 Iustin Pop
      ('1h', 60 * 60),
45 2241e2b9 Iustin Pop
      ('1d', 60 * 60 * 24),
46 2241e2b9 Iustin Pop
      ('1w', 60 * 60 * 24 * 7),
47 2241e2b9 Iustin Pop
      ('4h', 4 * 60 * 60),
48 2241e2b9 Iustin Pop
      ('61m', 61 * 60),
49 2241e2b9 Iustin Pop
      ]
50 2241e2b9 Iustin Pop
    for value, expected_result in test_data:
51 2241e2b9 Iustin Pop
      self.failUnlessEqual(cli.ParseTimespec(value), expected_result)
52 2241e2b9 Iustin Pop
53 2241e2b9 Iustin Pop
  def testInvalidTime(self):
54 2241e2b9 Iustin Pop
    """Test invalid timespecs"""
55 2241e2b9 Iustin Pop
    test_data = [
56 2241e2b9 Iustin Pop
      '1y',
57 2241e2b9 Iustin Pop
      '',
58 2241e2b9 Iustin Pop
      'aaa',
59 2241e2b9 Iustin Pop
      's',
60 2241e2b9 Iustin Pop
      ]
61 2241e2b9 Iustin Pop
    for value in test_data:
62 2241e2b9 Iustin Pop
      self.failUnlessRaises(OpPrereqError, cli.ParseTimespec, value)
63 2241e2b9 Iustin Pop
64 2241e2b9 Iustin Pop
65 a8469393 Iustin Pop
class TestSplitKeyVal(unittest.TestCase):
66 a8469393 Iustin Pop
  """Testing case for cli._SplitKeyVal"""
67 fcd62d84 Iustin Pop
  DATA = "a=b,c,no_d,-e"
68 fcd62d84 Iustin Pop
  RESULT = {"a": "b", "c": True, "d": False, "e": None}
69 a8469393 Iustin Pop
70 a8469393 Iustin Pop
  def testSplitKeyVal(self):
71 a8469393 Iustin Pop
    """Test splitting"""
72 a8469393 Iustin Pop
    self.failUnlessEqual(cli._SplitKeyVal("option", self.DATA), self.RESULT)
73 a8469393 Iustin Pop
74 a8469393 Iustin Pop
  def testDuplicateParam(self):
75 a8469393 Iustin Pop
    """Test duplicate parameters"""
76 a8469393 Iustin Pop
    for data in ("a=1,a=2", "a,no_a"):
77 a8469393 Iustin Pop
      self.failUnlessRaises(ParameterError, cli._SplitKeyVal,
78 a8469393 Iustin Pop
                            "option", data)
79 a8469393 Iustin Pop
80 4f31882e Guido Trotter
  def testEmptyData(self):
81 4f31882e Guido Trotter
    """Test how we handle splitting an empty string"""
82 4f31882e Guido Trotter
    self.failUnlessEqual(cli._SplitKeyVal("option", ""), {})
83 4f31882e Guido Trotter
84 8b46606c Guido Trotter
class TestIdentKeyVal(unittest.TestCase):
85 8b46606c Guido Trotter
  """Testing case for cli.check_ident_key_val"""
86 8b46606c Guido Trotter
87 8b46606c Guido Trotter
  def testIdentKeyVal(self):
88 8b46606c Guido Trotter
    """Test identkeyval"""
89 8b46606c Guido Trotter
    def cikv(value):
90 8b46606c Guido Trotter
      return cli.check_ident_key_val("option", "opt", value)
91 8b46606c Guido Trotter
92 8b46606c Guido Trotter
    self.assertEqual(cikv("foo:bar"), ("foo", {"bar": True}))
93 8b46606c Guido Trotter
    self.assertEqual(cikv("foo:bar=baz"), ("foo", {"bar": "baz"}))
94 8b46606c Guido Trotter
    self.assertEqual(cikv("bar:b=c,c=a"), ("bar", {"b": "c", "c": "a"}))
95 8b46606c Guido Trotter
    self.assertEqual(cikv("no_bar"), ("bar", False))
96 8b46606c Guido Trotter
    self.assertRaises(ParameterError, cikv, "no_bar:foo")
97 8b46606c Guido Trotter
    self.assertRaises(ParameterError, cikv, "no_bar:foo=baz")
98 8b46606c Guido Trotter
    self.assertEqual(cikv("-foo"), ("foo", None))
99 8b46606c Guido Trotter
    self.assertRaises(ParameterError, cikv, "-foo:a=c")
100 8b46606c Guido Trotter
101 a8469393 Iustin Pop
102 46fbdd04 Iustin Pop
class TestToStream(unittest.TestCase):
103 46fbdd04 Iustin Pop
  """Thes the ToStream functions"""
104 46fbdd04 Iustin Pop
105 46fbdd04 Iustin Pop
  def testBasic(self):
106 46fbdd04 Iustin Pop
    for data in ["foo",
107 46fbdd04 Iustin Pop
                 "foo %s",
108 46fbdd04 Iustin Pop
                 "foo %(test)s",
109 46fbdd04 Iustin Pop
                 "foo %s %s",
110 46fbdd04 Iustin Pop
                 "",
111 46fbdd04 Iustin Pop
                 ]:
112 46fbdd04 Iustin Pop
      buf = StringIO()
113 46fbdd04 Iustin Pop
      cli._ToStream(buf, data)
114 46fbdd04 Iustin Pop
      self.failUnlessEqual(buf.getvalue(), data+'\n')
115 46fbdd04 Iustin Pop
116 46fbdd04 Iustin Pop
  def testParams(self):
117 46fbdd04 Iustin Pop
      buf = StringIO()
118 46fbdd04 Iustin Pop
      cli._ToStream(buf, "foo %s", 1)
119 46fbdd04 Iustin Pop
      self.failUnlessEqual(buf.getvalue(), "foo 1\n")
120 46fbdd04 Iustin Pop
      buf = StringIO()
121 46fbdd04 Iustin Pop
      cli._ToStream(buf, "foo %s", (15,16))
122 46fbdd04 Iustin Pop
      self.failUnlessEqual(buf.getvalue(), "foo (15, 16)\n")
123 46fbdd04 Iustin Pop
      buf = StringIO()
124 46fbdd04 Iustin Pop
      cli._ToStream(buf, "foo %s %s", "a", "b")
125 46fbdd04 Iustin Pop
      self.failUnlessEqual(buf.getvalue(), "foo a b\n")
126 46fbdd04 Iustin Pop
127 2ebf1568 Michael Hanselmann
128 2ebf1568 Michael Hanselmann
class TestGenerateTable(unittest.TestCase):
129 2ebf1568 Michael Hanselmann
  HEADERS = dict([("f%s" % i, "Field%s" % i) for i in range(5)])
130 2ebf1568 Michael Hanselmann
131 2ebf1568 Michael Hanselmann
  FIELDS1 = ["f1", "f2"]
132 2ebf1568 Michael Hanselmann
  DATA1 = [
133 2ebf1568 Michael Hanselmann
    ["abc", 1234],
134 2ebf1568 Michael Hanselmann
    ["foobar", 56],
135 2ebf1568 Michael Hanselmann
    ["b", -14],
136 2ebf1568 Michael Hanselmann
    ]
137 2ebf1568 Michael Hanselmann
138 2ebf1568 Michael Hanselmann
  def _test(self, headers, fields, separator, data,
139 2ebf1568 Michael Hanselmann
            numfields, unitfields, units, expected):
140 2ebf1568 Michael Hanselmann
    table = cli.GenerateTable(headers, fields, separator, data,
141 2ebf1568 Michael Hanselmann
                              numfields=numfields, unitfields=unitfields,
142 2ebf1568 Michael Hanselmann
                              units=units)
143 2ebf1568 Michael Hanselmann
    self.assertEqual(table, expected)
144 2ebf1568 Michael Hanselmann
145 2ebf1568 Michael Hanselmann
  def testPlain(self):
146 2ebf1568 Michael Hanselmann
    exp = [
147 2ebf1568 Michael Hanselmann
      "Field1 Field2",
148 2ebf1568 Michael Hanselmann
      "abc    1234",
149 2ebf1568 Michael Hanselmann
      "foobar 56",
150 2ebf1568 Michael Hanselmann
      "b      -14",
151 2ebf1568 Michael Hanselmann
      ]
152 2ebf1568 Michael Hanselmann
    self._test(self.HEADERS, self.FIELDS1, None, self.DATA1,
153 2ebf1568 Michael Hanselmann
               None, None, "m", exp)
154 2ebf1568 Michael Hanselmann
155 2ebf1568 Michael Hanselmann
  def testNoFields(self):
156 2ebf1568 Michael Hanselmann
    self._test(self.HEADERS, [], None, [[], []],
157 2ebf1568 Michael Hanselmann
               None, None, "m", ["", "", ""])
158 2ebf1568 Michael Hanselmann
    self._test(None, [], None, [[], []],
159 2ebf1568 Michael Hanselmann
               None, None, "m", ["", ""])
160 2ebf1568 Michael Hanselmann
161 2ebf1568 Michael Hanselmann
  def testSeparator(self):
162 2ebf1568 Michael Hanselmann
    for sep in ["#", ":", ",", "^", "!", "%", "|", "###", "%%", "!!!", "||"]:
163 2ebf1568 Michael Hanselmann
      exp = [
164 2ebf1568 Michael Hanselmann
        "Field1%sField2" % sep,
165 2ebf1568 Michael Hanselmann
        "abc%s1234" % sep,
166 2ebf1568 Michael Hanselmann
        "foobar%s56" % sep,
167 2ebf1568 Michael Hanselmann
        "b%s-14" % sep,
168 2ebf1568 Michael Hanselmann
        ]
169 2ebf1568 Michael Hanselmann
      self._test(self.HEADERS, self.FIELDS1, sep, self.DATA1,
170 2ebf1568 Michael Hanselmann
                 None, None, "m", exp)
171 2ebf1568 Michael Hanselmann
172 2ebf1568 Michael Hanselmann
  def testNoHeader(self):
173 2ebf1568 Michael Hanselmann
    exp = [
174 2ebf1568 Michael Hanselmann
      "abc    1234",
175 2ebf1568 Michael Hanselmann
      "foobar 56",
176 2ebf1568 Michael Hanselmann
      "b      -14",
177 2ebf1568 Michael Hanselmann
      ]
178 2ebf1568 Michael Hanselmann
    self._test(None, self.FIELDS1, None, self.DATA1,
179 2ebf1568 Michael Hanselmann
               None, None, "m", exp)
180 2ebf1568 Michael Hanselmann
181 2ebf1568 Michael Hanselmann
  def testUnknownField(self):
182 2ebf1568 Michael Hanselmann
    headers = {
183 2ebf1568 Michael Hanselmann
      "f1": "Field1",
184 2ebf1568 Michael Hanselmann
      }
185 2ebf1568 Michael Hanselmann
    exp = [
186 2ebf1568 Michael Hanselmann
      "Field1 UNKNOWN",
187 2ebf1568 Michael Hanselmann
      "abc    1234",
188 2ebf1568 Michael Hanselmann
      "foobar 56",
189 2ebf1568 Michael Hanselmann
      "b      -14",
190 2ebf1568 Michael Hanselmann
      ]
191 2ebf1568 Michael Hanselmann
    self._test(headers, ["f1", "UNKNOWN"], None, self.DATA1,
192 2ebf1568 Michael Hanselmann
               None, None, "m", exp)
193 2ebf1568 Michael Hanselmann
194 2ebf1568 Michael Hanselmann
  def testNumfields(self):
195 2ebf1568 Michael Hanselmann
    fields = ["f1", "f2", "f3"]
196 2ebf1568 Michael Hanselmann
    data = [
197 2ebf1568 Michael Hanselmann
      ["abc", 1234, 0],
198 2ebf1568 Michael Hanselmann
      ["foobar", 56, 3],
199 2ebf1568 Michael Hanselmann
      ["b", -14, "-"],
200 2ebf1568 Michael Hanselmann
      ]
201 2ebf1568 Michael Hanselmann
    exp = [
202 2ebf1568 Michael Hanselmann
      "Field1 Field2 Field3",
203 2ebf1568 Michael Hanselmann
      "abc      1234      0",
204 2ebf1568 Michael Hanselmann
      "foobar     56      3",
205 2ebf1568 Michael Hanselmann
      "b         -14      -",
206 2ebf1568 Michael Hanselmann
      ]
207 2ebf1568 Michael Hanselmann
    self._test(self.HEADERS, fields, None, data,
208 2ebf1568 Michael Hanselmann
               ["f2", "f3"], None, "m", exp)
209 2ebf1568 Michael Hanselmann
210 2ebf1568 Michael Hanselmann
  def testUnitfields(self):
211 2ebf1568 Michael Hanselmann
    expnosep = [
212 2ebf1568 Michael Hanselmann
      "Field1 Field2 Field3",
213 2ebf1568 Michael Hanselmann
      "abc      1234     0M",
214 2ebf1568 Michael Hanselmann
      "foobar     56     3M",
215 2ebf1568 Michael Hanselmann
      "b         -14      -",
216 2ebf1568 Michael Hanselmann
      ]
217 2ebf1568 Michael Hanselmann
218 2ebf1568 Michael Hanselmann
    expsep = [
219 2ebf1568 Michael Hanselmann
      "Field1:Field2:Field3",
220 2ebf1568 Michael Hanselmann
      "abc:1234:0M",
221 2ebf1568 Michael Hanselmann
      "foobar:56:3M",
222 2ebf1568 Michael Hanselmann
      "b:-14:-",
223 2ebf1568 Michael Hanselmann
      ]
224 2ebf1568 Michael Hanselmann
225 2ebf1568 Michael Hanselmann
    for sep, expected in [(None, expnosep), (":", expsep)]:
226 2ebf1568 Michael Hanselmann
      fields = ["f1", "f2", "f3"]
227 2ebf1568 Michael Hanselmann
      data = [
228 2ebf1568 Michael Hanselmann
        ["abc", 1234, 0],
229 2ebf1568 Michael Hanselmann
        ["foobar", 56, 3],
230 2ebf1568 Michael Hanselmann
        ["b", -14, "-"],
231 2ebf1568 Michael Hanselmann
        ]
232 2ebf1568 Michael Hanselmann
      self._test(self.HEADERS, fields, sep, data,
233 2ebf1568 Michael Hanselmann
                 ["f2", "f3"], ["f3"], "h", expected)
234 2ebf1568 Michael Hanselmann
235 2ebf1568 Michael Hanselmann
  def testUnusual(self):
236 2ebf1568 Michael Hanselmann
    data = [
237 2ebf1568 Michael Hanselmann
      ["%", "xyz"],
238 2ebf1568 Michael Hanselmann
      ["%%", "abc"],
239 2ebf1568 Michael Hanselmann
      ]
240 2ebf1568 Michael Hanselmann
    exp = [
241 2ebf1568 Michael Hanselmann
      "Field1 Field2",
242 2ebf1568 Michael Hanselmann
      "%      xyz",
243 2ebf1568 Michael Hanselmann
      "%%     abc",
244 2ebf1568 Michael Hanselmann
      ]
245 2ebf1568 Michael Hanselmann
    self._test(self.HEADERS, ["f1", "f2"], None, data,
246 2ebf1568 Michael Hanselmann
               None, None, "m", exp)
247 2ebf1568 Michael Hanselmann
248 2ebf1568 Michael Hanselmann
249 2241e2b9 Iustin Pop
if __name__ == '__main__':
250 25231ec5 Michael Hanselmann
  testutils.GanetiTestProgram()