Revision 726ae450 test/py/ganeti.cli_unittest.py
b/test/py/ganeti.cli_unittest.py | ||
---|---|---|
71 | 71 |
"""Testing case for cli._SplitKeyVal""" |
72 | 72 |
DATA = "a=b,c,no_d,-e" |
73 | 73 |
RESULT = {"a": "b", "c": True, "d": False, "e": None} |
74 |
RESULT_NOPREFIX = {"a": "b", "c": {}, "no_d": {}, "-e": {}} |
|
74 | 75 |
|
75 | 76 |
def testSplitKeyVal(self): |
76 | 77 |
"""Test splitting""" |
77 |
self.failUnlessEqual(cli._SplitKeyVal("option", self.DATA), self.RESULT) |
|
78 |
self.failUnlessEqual(cli._SplitKeyVal("option", self.DATA, True), |
|
79 |
self.RESULT) |
|
78 | 80 |
|
79 | 81 |
def testDuplicateParam(self): |
80 | 82 |
"""Test duplicate parameters""" |
81 | 83 |
for data in ("a=1,a=2", "a,no_a"): |
82 | 84 |
self.failUnlessRaises(ParameterError, cli._SplitKeyVal, |
83 |
"option", data) |
|
85 |
"option", data, True)
|
|
84 | 86 |
|
85 | 87 |
def testEmptyData(self): |
86 | 88 |
"""Test how we handle splitting an empty string""" |
87 |
self.failUnlessEqual(cli._SplitKeyVal("option", ""), {}) |
|
89 |
self.failUnlessEqual(cli._SplitKeyVal("option", "", True), {})
|
|
88 | 90 |
|
89 | 91 |
|
90 | 92 |
class TestIdentKeyVal(unittest.TestCase): |
... | ... | |
101 | 103 |
self.assertEqual(cikv("no_bar"), ("bar", False)) |
102 | 104 |
self.assertRaises(ParameterError, cikv, "no_bar:foo") |
103 | 105 |
self.assertRaises(ParameterError, cikv, "no_bar:foo=baz") |
106 |
self.assertRaises(ParameterError, cikv, "bar:foo=baz,foo=baz") |
|
104 | 107 |
self.assertEqual(cikv("-foo"), ("foo", None)) |
105 | 108 |
self.assertRaises(ParameterError, cikv, "-foo:a=c") |
106 | 109 |
|
... | ... | |
115 | 118 |
for i in ["-:", "-"]: |
116 | 119 |
self.assertEqual(cikv(i), ("", None)) |
117 | 120 |
|
121 |
@staticmethod |
|
122 |
def _csikv(value): |
|
123 |
return cli._SplitIdentKeyVal("opt", value, False) |
|
124 |
|
|
125 |
def testIdentKeyValNoPrefix(self): |
|
126 |
"""Test identkeyval without prefixes""" |
|
127 |
test_cases = [ |
|
128 |
("foo:bar", None), |
|
129 |
("foo:no_bar", None), |
|
130 |
("foo:bar=baz,bar=baz", None), |
|
131 |
("foo", |
|
132 |
("foo", {})), |
|
133 |
("foo:bar=baz", |
|
134 |
("foo", {"bar": "baz"})), |
|
135 |
("no_foo:-1=baz,no_op=3", |
|
136 |
("no_foo", {"-1": "baz", "no_op": "3"})), |
|
137 |
] |
|
138 |
for (arg, res) in test_cases: |
|
139 |
if res is None: |
|
140 |
self.assertRaises(ParameterError, self._csikv, arg) |
|
141 |
else: |
|
142 |
self.assertEqual(self._csikv(arg), res) |
|
143 |
|
|
144 |
|
|
145 |
class TestListIdentKeyVal(unittest.TestCase): |
|
146 |
"""Test for cli.check_list_ident_key_val()""" |
|
147 |
|
|
148 |
@staticmethod |
|
149 |
def _clikv(value): |
|
150 |
return cli.check_list_ident_key_val("option", "opt", value) |
|
151 |
|
|
152 |
def testListIdentKeyVal(self): |
|
153 |
test_cases = [ |
|
154 |
("", |
|
155 |
None), |
|
156 |
("foo", |
|
157 |
{"foo": {}}), |
|
158 |
("foo:bar=baz", |
|
159 |
{"foo": {"bar": "baz"}}), |
|
160 |
("foo:bar=baz/foo:bat=bad", |
|
161 |
None), |
|
162 |
("foo:abc=42/bar:def=11", |
|
163 |
{"foo": {"abc": "42"}, |
|
164 |
"bar": {"def": "11"}}), |
|
165 |
("foo:abc=42/bar:def=11,ghi=07", |
|
166 |
{"foo": {"abc": "42"}, |
|
167 |
"bar": {"def": "11", "ghi": "07"}}), |
|
168 |
] |
|
169 |
for (arg, res) in test_cases: |
|
170 |
if res is None: |
|
171 |
self.assertRaises(ParameterError, self._clikv, arg) |
|
172 |
else: |
|
173 |
self.assertEqual(res, self._clikv(arg)) |
|
174 |
|
|
118 | 175 |
|
119 | 176 |
class TestToStream(unittest.TestCase): |
120 | 177 |
"""Test the ToStream functions""" |
Also available in: Unified diff