示例#1
0
def pytest_sessionstart(session):
    """
    pytest hook to configure plugin.
    """
    config = session.config
    # Get registered options
    platform = config.getoption('--topology-platform')
    injection_file = config.getoption('--topology-inject')
    log_dir = config.getoption('--topology-log-dir')
    szn_dir = config.getoption('--topology-szn-dir')
    platform_options = config.getoption('--topology-platform-options')
    build_retries = config.getoption('--topology-build-retries')

    if build_retries < 0:
        raise Exception('--topology-build-retries can\'t be less than 0')

    def create_dir(path):
        if path:
            if not isabs(path):
                path = join(abspath(getcwd()), path)
            if not exists(path):
                makedirs(path)

    # Determine log directory paths and create them if required
    create_dir(log_dir)

    # Parse attributes injection file
    from pyszn.injection import parse_attribute_injection
    injected_attr = None
    if injection_file is not None:
        log.info('Processing attribute injection...')
        start_time = time()
        # Get a list of all testing directories
        search_paths = [realpath(arg) for arg in config.args if isdir(arg)]

        injected_attr = parse_attribute_injection(
            injection_file,
            search_paths=search_paths,
            ignored_paths=config.getini('norecursedirs'),
            szn_dir=szn_dir)
        log.info('Attribute injection completed after {}s'.format(time() -
                                                                  start_time))

    # Create and register plugin
    config._topology_plugin = TopologyPlugin(platform, injected_attr, log_dir,
                                             szn_dir,
                                             parse_options(platform_options),
                                             build_retries)
    config.pluginmanager.register(config._topology_plugin)

    # Add test_id marker
    config.addinivalue_line(
        'markers', 'test_id(id): assign a test identifier to the test')

    # Add topology_compatible marker
    config.addinivalue_line(
        'markers', 'platform_incompatible(platforms, reason=None): '
        'mark a test as incompatible with a list of platform engines. '
        'Optionally specify a reason for better reporting')
示例#2
0
def pytest_configure(config):
    """
    pytest hook to configure plugin.
    """
    # Get registered options
    platform = config.getoption('--topology-platform')
    plot_format = config.getoption('--topology-plot-format')
    plot_dir = config.getoption('--topology-plot-dir')
    nml_dir = config.getoption('--topology-nml-dir')
    injection_file = config.getoption('--topology-inject')
    log_dir = config.getoption('--topology-log-dir')

    def create_dir(path):
        if path:
            if not isabs(path):
                path = join(abspath(getcwd()), path)
            if not exists(path):
                makedirs(path)

    # Determine plot, NML and log directory paths and create them if required
    create_dir(plot_dir)
    create_dir(nml_dir)
    create_dir(log_dir)

    # Parse attributes injection file
    from pyszn.injection import parse_attribute_injection
    injected_attr = None
    if injection_file is not None:

        # Get a list of all testing directories
        search_paths = [abspath(arg) for arg in config.args if isdir(arg)]

        injected_attr = parse_attribute_injection(injection_file,
                                                  search_paths=search_paths)

    # Create and register plugin
    config._topology_plugin = TopologyPlugin(platform, plot_dir,
                                             plot_format.lstrip('.'), nml_dir,
                                             injected_attr, log_dir)
    config.pluginmanager.register(config._topology_plugin)

    # Add test_id marker
    config.addinivalue_line(
        'markers', 'test_id(id): assign a test identifier to the test')

    # Add topology_compatible marker
    config.addinivalue_line(
        'markers', 'platform_incompatible(platforms, reason=None): '
        'mark a test as incompatible with a list of platform engines. '
        'Optionally specify a reason for better reporting')
示例#3
0
def test_attribute_injection(tmpdir):
    """
    Test the configuration file is parsed correctly.
    """
    workdir = str(tmpdir)
    search_path = str(tmpdir.mkdir('test'))
    subfolder = str(tmpdir.mkdir('test/subfolder'))

    try:
        # Write matching topologies
        for basepath, matches in (
                (search_path, TOPOLOGY_MATCHES),
                (subfolder, TOPOLOGY_MATCHES_FOLDER)):
            for count, content in matches.items():
                output_filename = join(
                    basepath, 'test_topology_match_{}.py'.format(count)
                )
                with open(output_filename, 'w') as fd:
                    fd.write('TOPOLOGY = """\n')
                    fd.write(content)
                    fd.write('"""')

        # Write the attributes injection file
        injection_path = join(workdir, 'attributes_injection.json')
        with open(injection_path, 'w') as fd:
            fd.write(INJECTION_FILE.format(search_path=search_path))

        # Change keys of the expected parsed injection file
        expected = OrderedDict()
        for key, value in EXPECTED_PARSED_INJECTION_FILE.items():
            expected[key.format(search_path=search_path)] = value

        # Actually parse the injection file
        actual = parse_attribute_injection(
            injection_path, search_paths=[search_path]
        )

        # Compare the actual and the expected
        differences = DeepDiff(actual, expected)
        assert not differences

    finally:
        try:
            rmtree(workdir)
        except Exception:
            pass
示例#4
0
文件: main.py 项目: eduar-m/topology
def main(args):
    """
    Application main function.

    :param args: An arguments namespace.
    :type args: :py:class:`argparse.Namespace`
    :return: Exit code.
    :rtype: int
    """
    print('Starting Network Topology Framework v{}'.format(__version__))

    # Setup framework logging
    logmanager.logging_context = None
    if args.log_dir:
        logmanager.logging_directory = args.log_dir

    # Parse attributes injection file
    injected_attr = None
    if args.inject is not None:
        injection_spec = parse_attribute_injection(args.inject)
        injected_attr = injection_spec.get(args.topology, None)

    # Create manager
    mgr = TopologyManager(args.platform)

    # Read topology
    if args.topology.endswith('.py'):

        topology = find_topology_in_python(args.topology)

        if topology is None:
            log.error(
                'TOPOLOGY variable could not be found in file {}'.format(
                    args.topology
                )
            )
            return 1

        mgr.parse(topology, inject=injected_attr)
    else:
        with open(args.topology, 'r') as fd:
            mgr.parse(fd.read(), inject=injected_attr)

    print('Building topology, please wait...')

    # Capture stdout to hide commands during build
    if not args.show_build_commands:
        sys.stdout = StringIO()

    # Build topology
    mgr.build()

    # Restore stdout after build
    if not args.show_build_commands:
        sys.stdout = sys.__stdout__

    # Start interactive mode if required
    if not args.non_interactive:

        # Register unbuild
        def unbuild():
            print('Unbuilding topology, please wait...')
            mgr.unbuild()
        register(unbuild)

        interact(mgr)

    # Plot and export topology
    module = splitext(basename(args.topology))[0]
    if args.plot_dir:
        plot_file = join(
            args.plot_dir, '{}.{}'.format(module, args.plot_format)
        )
        mgr.nml.save_graphviz(
            plot_file, keep_gv=True
        )

    # Export topology as NML
    if args.nml_dir:
        nml_file = join(
            args.nml_dir, '{}.xml'.format(module)
        )
        mgr.nml.save_nml(
            nml_file, pretty=True
        )

    return 0