1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#!/usr/bin/env python
# ClusterShell.CLI.Display test suite
# Written by S. Thiell 2010-09-25
"""Unit test for CLI.Display"""
import os
import sys
import tempfile
import unittest
sys.path.insert(0, '../lib')
from ClusterShell.CLI.Display import Display, WHENCOLOR_CHOICES, VERB_STD
from ClusterShell.CLI.OptionParser import OptionParser
from ClusterShell.MsgTree import MsgTree
from ClusterShell.NodeSet import NodeSet
from ClusterShell.NodeUtils import GroupResolverConfig
def makeTestFile(text):
"""Create a temporary file with the provided text."""
f = tempfile.NamedTemporaryFile()
f.write(text)
f.flush()
return f
class CLIDisplayTest(unittest.TestCase):
"""This test case performs a complete CLI.Display verification. Also
CLI.OptionParser is used and some parts are verified btw.
"""
def testDisplay(self):
"""test CLI.Display"""
parser = OptionParser("dummy")
parser.install_display_options(verbose_options=True)
options, _ = parser.parse_args([])
ns = NodeSet("localhost")
mtree = MsgTree()
mtree.add("localhost", "message0")
mtree.add("localhost", "message1")
for whencolor in WHENCOLOR_CHOICES: # test whencolor switch
for label in [True, False]: # test no-label switch
options.label = label
options.whencolor = whencolor
disp = Display(options)
# inhibit output
disp.out = open("/dev/null", "w")
disp.err = open("/dev/null", "w")
self.assert_(disp != None)
# test print_* methods...
disp.print_line(ns, "foo bar")
disp.print_line_error(ns, "foo bar")
disp.print_gather(ns, list(mtree.walk())[0][0])
# test also string nodeset as parameter
disp.print_gather("localhost", list(mtree.walk())[0][0])
# test line_mode property
self.assertEqual(disp.line_mode, False)
disp.line_mode = True
self.assertEqual(disp.line_mode, True)
disp.print_gather("localhost", list(mtree.walk())[0][0])
disp.line_mode = False
self.assertEqual(disp.line_mode, False)
def testDisplayRegroup(self):
"""test CLI.Display (regroup)"""
parser = OptionParser("dummy")
parser.install_display_options(verbose_options=True)
options, _ = parser.parse_args(["-r"])
mtree = MsgTree()
mtree.add("localhost", "message0")
mtree.add("localhost", "message1")
disp = Display(options)
self.assertEqual(disp.regroup, True)
disp.out = open("/dev/null", "w")
disp.err = open("/dev/null", "w")
self.assert_(disp != None)
self.assertEqual(disp.line_mode, False)
f = makeTestFile("""
# A comment
[Main]
default: local
[local]
map: echo localhost
#all:
list: echo all
#reverse:
""")
res = GroupResolverConfig(f.name)
ns = NodeSet("localhost", resolver=res)
# nodeset.regroup() is performed by print_gather()
disp.print_gather(ns, list(mtree.walk())[0][0])
def testDisplayClubak(self):
"""test CLI.Display for clubak"""
parser = OptionParser("dummy")
parser.install_display_options(separator_option=True, dshbak_compat=True)
options, _ = parser.parse_args([])
disp = Display(options)
self.assertEqual(bool(disp.gather), False)
self.assertEqual(disp.line_mode, False)
self.assertEqual(disp.label, True)
self.assertEqual(disp.regroup, False)
self.assertEqual(bool(disp.groupsource), False)
self.assertEqual(disp.noprefix, False)
self.assertEqual(disp.maxrc, False)
self.assertEqual(disp.node_count, True)
self.assertEqual(disp.verbosity, VERB_STD)
if __name__ == '__main__':
suites = [unittest.TestLoader().loadTestsFromTestCase(CLIDisplayTest)]
unittest.TextTestRunner(verbosity=2).run(unittest.TestSuite(suites))
|