Ejemplo n.º 1
0
def test_validator(test_name, zone_name, file_name,
                   extra_defines=None, dnssec_type='detect'):
    '''
    Tests a validator using SprocketImpl instance.

    :param str test_name: The name of the test to run.
    :param str zone_name: The name of the zone to load.
    :param str file_name: The name of the file to load (w.r.t. dns_sprockets_lib/tests/data)
    :param list extra_defines: List of extra defines, in command-line format.
    :param str dnssec_type: Set to force dnssec_type.
    :return: A 2-tuple (test_cnt, error_cnt).
    '''

    class TestArgs(object):
        # pylint: disable=too-few-public-methods
        '''
        Simulated command-line arguments.
        '''
        zone = zone_name
        loader = 'file'
        source = 'dns_sprockets_lib/tests/data/%s' % (file_name)
        include_tests = [test_name]
        exclude_tests = []
        force_dnssec_type = dnssec_type
        errors_only = False
        defines = extra_defines
        verbose = False

    avail_loaders = utils.public_modules_in_package(loaders, ['tests'])
    avail_tests = utils.public_modules_in_package(validators, ['tests'])

    test_args_inst = TestArgs()
    if test_args_inst.defines is None:
        test_args_inst.defines = []
    for sec_param in test_args_inst.defines:
        (p_name, p_val) = sec_param.split('=')[:2]
        setattr(test_args_inst, p_name, p_val)

    sprocket = DNSSprocketsImpl(avail_loaders, avail_tests, test_args_inst)
    (_, test_cnt, err_cnt) = sprocket.run()
    return (test_cnt, err_cnt)
Ejemplo n.º 2
0
def load_all():
    '''
    Scans the sprocket_support.validators package for "public" modules and loads
    their classes into the ALL_CLASSES dictionary.
    '''
    if not ALL_CLASSES:
        avail_tests = utils.public_modules_in_package(validators, ['tests'])
        for test_name in avail_tests:
            test_module_name = 'dns_sprockets_lib.validators.%s' % (test_name)
            test_module = importlib.import_module(test_module_name)
            ALL_CLASSES[test_name] = getattr(
                test_module, utils.underscores_to_camelcase(test_name))
def test_SprocketImpl():

    class TestArgs(object):
        zone = '001.cst.net'
        loader = 'file'
        source = 'dns_sprockets_lib/tests/data/001.cst.net.'
        include_tests = ['soa_origin', 'soa_unique']
        exclude_tests = ['soa_unique']
        force_dnssec_type = 'detect'
        errors_only = False
        defines = ['file_allow_include=0']
        file_allow_include = '0'

    avail_loaders = utils.public_modules_in_package(loaders, ['tests'])
    avail_tests = utils.public_modules_in_package(validators, ['tests'])

    test_args_inst = TestArgs()
    sprocket = DNSSprocketsImpl(avail_loaders, avail_tests, test_args_inst)
    (_, test_cnt, err_cnt) = sprocket.run()
    assert test_cnt == 1
    assert err_cnt == 0
Ejemplo n.º 4
0
def test_public_modules_in_package():

    mods = utils.public_modules_in_package(dns_sprockets_lib.tests)
    assert 'test_utils' in mods
    assert '__init__' not in mods
Ejemplo n.º 5
0
def run():
    '''
    Run the sprocket command-line application.
    '''
    # Print startup banner:
    print '#', _BANNER

    # Determine available loaders and tests:
    avail_loaders = utils.public_modules_in_package(loaders, ['tests'])
    avail_tests = utils.public_modules_in_package(validators, ['tests'])

    # Parse command-line:
    import argparse
    parser = argparse.ArgumentParser(
        epilog='''
Use @filename to read some/all arguments from a file.\n\n
Use -d's to define optional, module-specific parameters if desired (e.g. to tell
'xfr' loader to use a specific source address, use "-d xfr_source=1.2.3.4").
The optional parameters are listed under each loader and validator description
in DEFINE lines, if available.  The "short form" of parameter names (i.e. 
without the module name prefix) may be used instead, if multiple modules define
the same "short form" name (e.g. -d now=20160328120000 can be used, and will set
both 'rrsig_missing' and 'rrsig_orphan' validators' rrsig_missing_now and 
rrsig_orphan_now values, respectively).\n\n
By default, all tests are run.  Use -i\'s to explicitly specify desired tests,
or -x\'s to eliminate undesired tests.\n\n
The list of available loaders is:
---------------------------------------------------------------------------\n%s\n\n
The list of available validators is:
---------------------------------------------------------------------------\n%s
            ''' % (loader_classes.get_formatted_descriptions(),
                   validator_classes.get_formatted_descriptions()),
        formatter_class=argparse.RawDescriptionHelpFormatter,
        fromfile_prefix_chars='@')
    parser.add_argument('-z', '--zone', dest='zone',
        default=_DEFAULT_ZONE, metavar='s',
        help='Name of zone to validate [%(default)s]')
    parser.add_argument('-l', '--loader', dest='loader',
        default=_DEFAULT_LOADER, metavar='s', choices=avail_loaders,
        help='Zone loader method to use (one of: %(choices)s) [%(default)s]')
    parser.add_argument('-s', '--source', dest='source',
        default=_DEFAULT_SOURCE, metavar='s',
        help='Loader source to use [%(default)s]')
    parser.add_argument('-i', '--include', dest='include_tests',
        action='append', metavar='s', choices=avail_tests,
        help='Only include this validator (can use multiple times)')
    parser.add_argument('-x', '--exclude', dest='exclude_tests',
        action='append', metavar='s', choices=avail_tests,
        help='Exclude this validator (can use multiple times)')
    parser.add_argument('-d', '--define', dest='defines',
        action='append', metavar='s',
        help='Define other params (can use multiple times)')
    parser.add_argument('-f', '--force-dnssec-type', dest='force_dnssec_type',
        default='detect', metavar='s',
        choices=['detect', 'unsigned', 'NSEC', 'NSEC3'],
        help='Use DNSSEC type (one of: %(choices)s) [%(default)s]')
    parser.add_argument('-e', '--errors-only', dest='errors_only',
        default=False, action='store_true',
        help='Show validation errors only [%(default)s]')
    parser.add_argument('-v', '--verbose', dest='verbose',
        default=False, action='store_true',
        help='Show detailed processing info [%(default)s]')
    args = parser.parse_args()

    # Fix-up args:
    if args.include_tests is None:
        args.include_tests = []
    if args.exclude_tests is None:
        args.exclude_tests = []
    if args.defines is None:
        args.defines = []
    for sec_param in args.defines:
        (p_name, p_val) = sec_param.split('=')[:2]
        setattr(args, p_name, p_val)

    # Run the app:
    _run_main(avail_loaders, avail_tests, args)