* test_tdl.py - redesigned to comply with py.test format
* test_tdl.py - use libxml2 directly for xmllint validation
* test.cfg - Expected test results no longer hard-coded, results are managed
using ConfigParser-style test.cfg file (see [DEFAULT] for description of
options)
---
tests/tdl/test.cfg | 135 +++++++++++++++++++++++++++++++++++++++++++++++++
tests/tdl/test_tdl.py | 131 ++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 262 insertions(+), 4 deletions(-)
create mode 100644 tests/tdl/test.cfg
diff --git a/tests/tdl/test.cfg b/tests/tdl/test.cfg
new file mode 100644
index 0000000..f929f04
--- /dev/null
+++ b/tests/tdl/test.cfg
@@ -0,0 +1,135 @@
+# Main config file for oz tdl tests
+
+[DEFAULT]
+# Unique test name, if no name is provided, the section name is used.
+name = %(__name__)s
+
+# Expected overall test result. When set, individual test results are ignored.
+# If no value is provided, individual results are used.
+result =
+
+# Ozlib test result. If no value is provided, test is assumed pass.
+ozlib_result =
+
+# xmllint schema test result. If no value is provided, test is assumed pass.
+schema_result =
+
+# (optional) - Specify exact exception expected from test failure (default:
+# oz.OzException.OzException). Note, the value used is passed to eval(). Use
+# with caution!!
+# ozlib_exception =
+
+# (optional) - Specify exact exception expected from test failure (default:
+# Exception). Note, the value used is passed to eval(). Use with caution!!
+# schema_exception =
+
+# All section names must match a .tdl file name
+# Refer to [DEFAULT] for description of available options
+[test-01-simple-iso.tdl]
+
+[test-02-simple-url.tdl]
+
+[test-03-empty-template.tdl]
+result = fail
+
+[test-04-no-os.tdl]
+result = fail
+
+[test-05-no-name.tdl]
+result = fail
+
+[test-06-simple-iso-description.tdl]
+
+[test-07-packages-no-package.tdl]
+
+[test-08-repositories-no-repository.tdl]
+
+[test-09-os-invalid-arch.tdl]
+result = fail
+
+[test-10-os-invalid-install-type.tdl]
+result = fail
+
+[test-11-description-packages-repositories.tdl]
+
+[test-12-os-no-name.tdl]
+result = fail
+
+[test-13-os-no-version.tdl]
+result = fail
+
+[test-14-os-no-arch.tdl]
+result = fail
+
+[test-15-os-no-install.tdl]
+result = fail
+
+[test-16-signed-repository.tdl]
+
+[test-17-repo-invalid-signed.tdl]
+result = fail
+
+[test-18-rootpw.tdl]
+
+[test-19-key.tdl]
+
+[test-20-multiple-install.tdl]
+result = fail
+
+[test-21-missing-install-type.tdl]
+result = fail
+
+[test-22-md5sum.tdl]
+
+[test-23-sha1sum.tdl]
+
+[test-24-sha256sum.tdl]
+
+[test-25-md5sum-and-sha1sum.tdl]
+result = fail
+
+[test-26-md5sum-and-sha256sum.tdl]
+result = fail
+
+[test-27-sha1sum-and-sha256sum.tdl]
+result = fail
+
+[test-28-package-no-name.tdl]
+result = fail
+
+[test-29-files.tdl]
+
+[test-30-file-no-name.tdl]
+result = fail
+
+[test-31-file-raw-type.tdl]
+
+[test-32-file-base64-type.tdl]
+
+[test-33-file-invalid-type.tdl]
+result = fail
+
+[test-34-file-invalid-base64.tdl]
+result = fail
+ozlib_exception = TypeError
+
+[test-35-repository-no-name.tdl]
+result = fail
+
+[test-36-repository-no-url.tdl]
+result = fail
+
+[test-37-command.tdl]
+
+[test-38-command-no-name.tdl]
+result = fail
+
+[test-39-command-raw-type.tdl]
+[test-40-command-base64-type.tdl]
+
+[test-41-command-bogus-base64.tdl]
+result = fail
+ozlib_exception = TypeError
+
+[test-42-command-bogus-type.tdl]
+result = fail
diff --git a/tests/tdl/test_tdl.py b/tests/tdl/test_tdl.py
index 75ea3f8..a2e03c4 100644
--- a/tests/tdl/test_tdl.py
+++ b/tests/tdl/test_tdl.py
@@ -1,8 +1,131 @@
-import oz.TDL
import sys
+import os
+import glob
+import ConfigParser
-if len(sys.argv) != 2:
- print "Usage: test_tdl.py <tdl>"
+try:
+ import libxml2
+except ImportError:
+ print 'Unable to import libxml2. Is libxml2-python installed?'
sys.exit(1)
-tdl = oz.TDL.TDL(open(sys.argv[1], 'r').read())
+try:
+ import py.test
+except ImportError:
+ print 'Unable to import py.test. Is py.test installed?'
+ sys.exit(1)
+
+try:
+ import oz
+ import oz.TDL
+except ImportError:
+ print 'Unable to import oz. Is oz installed?'
+ sys.exit(1)
+
+# Validate oz handling of tdl file
+def validate_ozlib(tdl_file):
+ xmldata = open(tdl_file, 'r').read()
+ tdl = oz.TDL.TDL(xmldata)
+
+# Validate schema
+def validate_schema(tdl_file):
+
+ # Locate relaxng schema
+ rng_file = None
+ for tryme in ['../../docs/tdl.rng',
+ '../docs/tdl.rng',
+ 'docs/tdl.rng',
+ 'tdl.rng',]:
+ if os.path.isfile(tryme):
+ rng_file = tryme
+ break
+
+ if rng_file is None:
+ raise Exception('RelaxNG schema file not found: tdl.rng')
+
+ # Load relaxng schema
+ schema = open(rng_file, 'r').read()
+ rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
+ rngs = rngp.relaxNGParse()
+
+ # Define callback for error handling
+ def error_cb(ctx, str):
+ print "%s: %s" % (ctx, str.strip())
+ libxml2.registerErrorHandler(error_cb, tdl_file)
+
+ # Attempt to validate
+ reader = libxml2.newTextReaderFilename(tdl_file)
+ #reader.SetParserProp(libxml2.PARSER_VALIDATE, 1)
+ reader.RelaxNGSetSchema(rngs)
+ ret = reader.Read()
+ while ret == 1:
+ ret = reader.Read()
+
+ if ret != 0:
+ raise Exception('Error parsing the document: %s' % tdl_file)
+ if reader.IsValid() != 1:
+ raise Exception('Document failed to validate: %s' % tdl_file)
+
+# Test generator that iterates over all .tdl files
+def test_all_tdls():
+
+ # Define a helper to expect an exception
+ def handle_exception(exc, func, *args):
+ with py.test.raises(exc):
+ func(*args)
+
+ # Load test configuration file
+ test_path = os.path.join('tests', 'tdl')
+ cfg = ConfigParser.ConfigParser()
+ cfg.read(os.path.join(test_path, 'test.cfg'))
+
+ # Sanity check to see if any tests are unaccounted for in the config file
+ tdl_files = glob.glob('tests/tdl/*.tdl')
+ for tdl_file in tdl_files:
+ # we want the file, not the full file path
+ tdl_file = os.path.basename(tdl_file)
+ if not cfg.has_section(tdl_file):
+ py.test.fail('Missing test.cfg section for: %s' % tdl_file)
+
+ # Iterate over all expected good tdl files
+ for test_name in cfg.sections():
+
+ test_desc = cfg.get(test_name, 'name')
+ tdl_path = os.path.join(test_path, test_name)
+
+ # load expected results
+ expected_overall = cfg.get(test_name, 'result', '')
+ expected_ozlib = cfg.get(test_name, 'ozlib_result', '')
+ expected_schema = cfg.get(test_name, 'schema_result', '')
+
+ # load possible expected failure conditions
+ ozlib_exception = cfg.has_option(test_name, 'ozlib_exception') \
+ and cfg.get(test_name, 'ozlib_exception') or
oz.OzException.OzException
+ schema_exception = cfg.has_option(test_name, 'schema_exception') \
+ and cfg.get(test_name, 'schema_exception') or Exception
+
+ # Convert an exception string into an exception object
+ if isinstance(ozlib_exception, str):
+ try:
+ ozlib_exception = eval(ozlib_exception)
+ except NameError:
+ py.test.fail('Unknown exception: %s' % ozlib_exception)
+ if isinstance(schema_exception, str):
+ try:
+ schema_exception = eval(schema_exception)
+ except NameError:
+ py.test.fail('Unknown exception: %s' % schema_exception)
+
+ # Run validate_ozlib test
+ if expected_overall in ['','pass'] and expected_ozlib in
['', 'pass']:
+ yield '%s (ozlib)' % test_desc, validate_ozlib, tdl_path
+ else: # expected FAIL
+ yield '%s (ozlib)' % test_desc, handle_exception, \
+ ozlib_exception, validate_ozlib, tdl_path
+
+ # Run validate_schema test
+ if expected_overall in ['','pass'] and expected_schema in
['', 'pass']:
+ yield '%s (schema)' % test_desc, validate_schema, tdl_path
+ else: # expected FAIL
+ yield '%s (schema)' % test_desc, handle_exception, \
+ schema_exception, validate_schema, tdl_path
--
1.7.6