-
Iustin Pop authored
We also add two function for printing messages, so that scripts won't have to import logger to get these. They are a simple extension over the logger ones, as they accept the call style from logging: ToStdout("Message: %s", msg) (instead of requiring formatting by the client. Reviewed-by: imsnah
46fbdd04
ganeti.cli_unittest.py 2.95 KiB
#!/usr/bin/python
#
# Copyright (C) 2008 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
"""Script for unittesting the cli module"""
import unittest
from cStringIO import StringIO
import ganeti
import testutils
from ganeti import constants
from ganeti import cli
from ganeti.errors import OpPrereqError, ParameterError
class TestParseTimespec(unittest.TestCase):
"""Testing case for ParseTimespec"""
def testValidTimes(self):
"""Test valid timespecs"""
test_data = [
('1s', 1),
('1', 1),
('1m', 60),
('1h', 60 * 60),
('1d', 60 * 60 * 24),
('1w', 60 * 60 * 24 * 7),
('4h', 4 * 60 * 60),
('61m', 61 * 60),
]
for value, expected_result in test_data:
self.failUnlessEqual(cli.ParseTimespec(value), expected_result)
def testInvalidTime(self):
"""Test invalid timespecs"""
test_data = [
'1y',
'',
'aaa',
's',
]
for value in test_data:
self.failUnlessRaises(OpPrereqError, cli.ParseTimespec, value)
class TestSplitKeyVal(unittest.TestCase):
"""Testing case for cli._SplitKeyVal"""
DATA = "a=b,c,no_d,-e"
RESULT = {"a": "b", "c": True, "d": False, "e": None}
def testSplitKeyVal(self):
"""Test splitting"""
self.failUnlessEqual(cli._SplitKeyVal("option", self.DATA), self.RESULT)
def testDuplicateParam(self):
"""Test duplicate parameters"""
for data in ("a=1,a=2", "a,no_a"):
self.failUnlessRaises(ParameterError, cli._SplitKeyVal,
"option", data)
class TestToStream(unittest.TestCase):
"""Thes the ToStream functions"""
def testBasic(self):
for data in ["foo",
"foo %s",
"foo %(test)s",
"foo %s %s",
"",
]:
buf = StringIO()
cli._ToStream(buf, data)
self.failUnlessEqual(buf.getvalue(), data+'\n')
def testParams(self):
buf = StringIO()
cli._ToStream(buf, "foo %s", 1)
self.failUnlessEqual(buf.getvalue(), "foo 1\n")
buf = StringIO()
cli._ToStream(buf, "foo %s", (15,16))
self.failUnlessEqual(buf.getvalue(), "foo (15, 16)\n")
buf = StringIO()
cli._ToStream(buf, "foo %s %s", "a", "b")
self.failUnlessEqual(buf.getvalue(), "foo a b\n")
if __name__ == '__main__':
unittest.main()