12 from pykickstart
.errors
import *
13 from pykickstart
.parser
import preprocessFromString
, KickstartParser
14 from pykickstart
.version
import *
16 gettext
.textdomain("pykickstart")
17 _
= lambda x
: gettext
.ldgettext("pykickstart", x
)
19 class ParserTest(unittest
.TestCase
):
22 def __init__(self
, *args
, **kwargs
):
23 unittest
.TestCase
.__init
__(self
, *args
, **kwargs
)
28 unittest
.TestCase
.setUp(self
)
31 """Undo anything performed by setUp"""
32 unittest
.TestCase
.tearDown(self
)
35 """This function can be overriden by subclasses,
36 for example if the subclass wants to use a fresh
39 if self
._parser
is None:
40 self
._parser
= KickstartParser(self
.handler
)
45 return self
.get_parser()
49 if self
._handler
is None:
50 self
._handler
= makeVersion(self
.version
)
53 def assert_parse_error(self
, ks_string
, exception
=KickstartParseError
, regex
=r
".*"):
54 """Parsing of this command sequence is expected to raise an exception,
55 exception type can be set by the exception keyword argument.
57 By default the KickstartParseError is expected.
60 with self
.assertRaisesRegexp(exception
, regex
):
61 self
.parser
.readKickstartFromString(ks_string
)
63 def assert_parse(self
, ks_string
):
64 """Parsing of his command sequence is expected to finish without
65 raising an exception; if it raises an exception, the test failed
68 self
.parser
.readKickstartFromString(ks_string
)
70 self
.fail("Failed while parsing commands %s: %s" % (ks_string
, e
))
73 class CommandSequenceTest(ParserTest
):
74 """Kickstart command sequence testing
76 Enables testing kickstart indepdent command sequences
77 and checking if their parsing raises or doesn't raise
82 """Command sequence tests need a fresh parser
84 handler
= makeVersion(self
.version
)
85 return KickstartParser(handler
)
88 # Base class for any command test case
89 class CommandTest(unittest
.TestCase
):
91 '''Perform any command setup'''
92 unittest
.TestCase
.setUp(self
)
94 # ignore DeprecationWarning
95 warnings
.simplefilter("error", category
=UserWarning)
96 warnings
.simplefilter("ignore", category
=DeprecationWarning, append
=0)
99 '''Undo anything performed by setUp(self)'''
101 warnings
.resetwarnings()
103 unittest
.TestCase
.tearDown(self
)
105 def __init__(self
, *args
, **kwargs
):
106 unittest
.TestCase
.__init
__(self
, *args
, **kwargs
)
111 version
= self
.__class
__.__name
__.split("_")[0]
112 return returnClassForVersion(version
)
115 def optionList(self
):
119 parser
= self
.getParser(self
.command
)._getParser
()
121 for opt
in [o
for o
in parser
.option_list
if not o
.deprecated
]:
122 self
._options
.append(opt
.get_opt_string())
126 def getParser(self
, inputStr
):
127 '''Find a handler using the class name. Return the requested command
129 args
= shlex
.split(inputStr
)
132 parser
= self
.handler().commands
[cmd
]
133 parser
.currentLine
= inputStr
134 parser
.currentCmd
= args
[0]
139 def assert_parse(self
, inputStr
, expectedStr
=None, ignoreComments
=True):
140 '''KickstartParseError is not raised and the resulting string matches
142 parser
= self
.getParser(inputStr
)
143 args
= shlex
.split(inputStr
)
145 # If expectedStr supplied, we want to ensure the parsed result matches
146 if expectedStr
is not None:
147 obj
= parser
.parse(args
[1:])
150 # Strip any comment lines ... we only match on non-comments
152 result
= re
.sub("^#[^\n]*\n", "", result
)
154 # Ensure we parsed as expected
155 self
.assertEqual(result
, expectedStr
)
156 # No expectedStr supplied, just make sure it does not raise an
160 obj
= parser
.parse(args
[1:])
162 self
.fail("Failed while parsing: %s" % e
)
165 def assert_parse_error(self
, inputStr
, exception
=KickstartParseError
, regex
=r
".*"):
166 '''Assert that parsing the supplied string raises a
167 KickstartParseError'''
168 parser
= self
.getParser(inputStr
)
169 args
= shlex
.split(inputStr
)
171 with self
.assertRaisesRegexp(exception
, regex
):
172 parser
.parse(args
[1:])
174 def assert_deprecated(self
, cmd
, opt
):
175 '''Ensure that the provided option is listed as deprecated'''
176 parser
= self
.getParser(cmd
)
178 for op
in parser
.op
.option_list
:
179 if op
.get_opt_string() == opt
:
180 self
.assert_(op
.deprecated
)
182 def assert_removed(self
, cmd
, opt
):
183 '''Ensure that the provided option is not present in option_list'''
184 parser
= self
.getParser(cmd
)
185 for op
in parser
.op
.option_list
:
186 self
.assertNotEqual(op
.dest
, opt
)
188 def assert_required(self
, cmd
, opt
):
189 '''Ensure that the provided option is labelled as required in
191 parser
= self
.getParser(cmd
)
192 for op
in parser
.op
.option_list
:
193 if op
.get_opt_string() == opt
:
194 self
.assert_(op
.required
)
196 def assert_type(self
, cmd
, opt
, opt_type
):
197 '''Ensure that the provided option is of the requested type'''
198 parser
= self
.getParser(cmd
)
199 for op
in parser
.op
.option_list
:
200 if op
.get_opt_string() == opt
:
201 self
.assertEqual(op
.type, opt_type
)
204 def loadModules(moduleDir
, cls_pattern
="_TestCase", skip_list
=["__init__", "baseclass"]):
205 '''taken from firstboot/loader.py'''
207 # Guaruntee that __init__ is skipped
208 if skip_list
.count("__init__") == 0:
209 skip_list
.append("__init__")
213 # Make sure moduleDir is in the system path so imputil works.
214 if not moduleDir
in sys
.path
:
215 sys
.path
.insert(0, moduleDir
)
217 # Get a list of all *.py files in moduleDir
219 lst
= [os
.path
.splitext(os
.path
.basename(x
))[0] for x
in glob
.glob(moduleDir
+ "/*.py")]
221 # Inspect each .py file found
223 if module
in skip_list
:
226 # Attempt to load the found module.
228 found
= imputil
.imp
.find_module(module
)
229 loaded
= imputil
.imp
.load_module(module
, found
[0], found
[1], found
[2])
230 except ImportError, e
:
231 print(_("Error loading module %s: %s") % (module
, e
))
234 # Find class names that match the supplied pattern (default: "_TestCase")
235 beforeCount
= len(tstList
)
236 for obj
in list(loaded
.__dict
__.keys()):
237 if obj
.endswith(cls_pattern
):
238 tstList
.append(loaded
.__dict
__[obj
])
239 afterCount
= len(tstList
)
241 # Warn if no tests found
242 if beforeCount
== afterCount
:
243 print(_("Module %s does not contain any test cases; skipping.") % module
)
249 if __name__
== "__main__":
250 # Make sure PWD is in the path before the eggs, system paths, etc.
251 sys
.path
.insert(0, os
.environ
.get("PWD"))
253 # Create a test suite
254 PyKickstartTestSuite
= unittest
.TestSuite()
257 tstList
= loadModules(os
.path
.join(os
.environ
.get("PWD"), "tests/"))
258 tstList
.extend(loadModules(os
.path
.join(os
.environ
.get("PWD"), "tests/commands")))
259 tstList
.extend(loadModules(os
.path
.join(os
.environ
.get("PWD"), "tests/parser")))
261 PyKickstartTestSuite
.addTest(tst())
264 unittest
.main(defaultTest
="PyKickstartTestSuite")