Example #1
0
def pytest_sessionstart(session):
    actor_path = os.environ.get('LEAPP_TESTED_ACTOR', None)
    library_path = os.environ.get('LEAPP_TESTED_LIBRARY', None)

    if actor_path:
        repo = find_and_scan_repositories(find_repository_basedir(actor_path),
                                          include_locals=True)
        repo.load()

        actor = None
        # find which actor is being tested
        for a in repo.actors:
            if a.full_path == actor_path.rstrip('/'):
                actor = a
                break

        if not actor:
            return

        # load actor context so libraries can be imported on module level
        session.leapp_repository = repo
        session.actor_context = actor.injected_context()
        session.actor_context.__enter__()
    elif library_path:
        repo = find_and_scan_repositories(
            find_repository_basedir(library_path), include_locals=True)
        repo.load()
        os.chdir(library_path)
def test_find_and_scan_repositories_no_user_repos_config(empty_repository_dir):
    repo_path = empty_repository_dir.dirpath().strpath
    with mock.patch('leapp.repository.manager.RepositoryManager.get_missing_repo_links', return_value={'42'}):
        with mock.patch('leapp.utils.repository.get_global_repositories_data', return_value={}):
            with mock.patch('leapp.utils.repository.get_user_config_repos', return_value='/no/such/file.json'):
                with pytest.raises(RepositoryConfigurationError) as err:
                    find_and_scan_repositories(repo_path, include_locals=True)
                assert 'No repos configured' in err.value.message
Example #3
0
    def impl(context=None):
        configure_logger()
        repository = find_and_scan_repositories(find_repository_basedir('.'),
                                                include_locals=True)
        try:
            repository.load()
        except LeappError as exc:
            sys.stderr.write(exc.message)
            sys.stderr.write('\n')
            sys.exit(1)

        wf = repository.lookup_workflow(params.name)
        if not wf:
            raise CommandError('Could not find any workflow named "{}"'.format(
                params.name))

        instance = wf()
        for actor_name in params.whitelist_experimental or ():
            actor = repository.lookup_actor(actor_name)
            if actor:
                instance.whitelist_experimental_actor(actor)

        with beautify_actor_exception():
            instance.run(context=context,
                         until_phase=params.until_phase,
                         until_actor=params.until_actor)

        report_errors(instance.errors)
Example #4
0
def cli(args):
    base_dir = find_repository_basedir('.')
    repository = find_and_scan_repositories(base_dir, include_locals=True)
    try:
        repository.load()
    except LeappError as exc:
        sys.stderr.write(exc.message)
        sys.exit(1)

    actors = [actor for actor in repository.actors]
    topics = [
        topic for topic in get_topics()
        if _is_local(repository, topic, base_dir, all_repos=args.all)
    ]
    models = [
        model for model in get_models()
        if _is_local(repository, model, base_dir, all_repos=args.all)
    ]
    tags = [
        tag for tag in get_tags()
        if _is_local(repository, tag, base_dir, all_repos=args.all)
    ]
    workflows = [
        workflow for workflow in get_workflows()
        if _is_local(repository, workflow, base_dir, all_repos=args.all)
    ]
    if not args.json:
        sys.stdout.write(
            'Repository:\n  Name: {repository}\n  Path: {base_dir}\n\n'.format(
                repository=get_repository_name(base_dir), base_dir=base_dir))
        _print_group('Actors',
                     actors,
                     name_resolver=lambda x: x.class_name,
                     path_resolver=_get_actor_path)
        _print_group('Models', models)
        _print_group('Tags', tags)
        _print_group('Topics', topics)
        _print_group('Workflows', workflows)
    else:
        output = {
            'repository':
            get_repository_name(base_dir),
            'base_dir':
            base_dir,
            'topics':
            dict((topic.__name__, _get_topic_details(topic))
                 for topic in topics),
            'models':
            dict((model.__name__, _get_model_details(model))
                 for model in models),
            'actors':
            dict((actor.class_name, _get_actor_details(actor))
                 for actor in actors),
            'tags':
            dict((tag.name, _get_tag_details(tag)) for tag in tags),
            'workflows':
            dict((workflow.__name__, _get_workflow_details(workflow))
                 for workflow in workflows)
        }
        json_mod.dump(output, sys.stdout, indent=2)
Example #5
0
def cli(args):
    log = configure_logger()
    basedir = find_repository_basedir('.')
    repository = find_and_scan_repositories(basedir, include_locals=True)
    try:
        repository.load()
    except LeappError as exc:
        sys.stderr.write(exc.message)
        sys.stderr.write('\n')
        sys.exit(1)
    actor_logger = log.getChild('actors')
    actor = repository.lookup_actor(args.actor_name)
    if not actor:
        raise CommandError('Actor "{}" not found!'.format(args.actor_name))
    messaging = InProcessMessaging(stored=args.save_output)
    messaging.load(actor.consumes)

    failure = False
    with beautify_actor_exception():
        try:
            actor(messaging=messaging, logger=actor_logger).run()
        except BaseException:
            failure = True
            raise

    report_errors(messaging.errors())

    if failure or messaging.errors():
        sys.exit(1)

    if args.print_output:
        json.dump(messaging.messages(), sys.stdout, indent=2)
        sys.stdout.write('\n')
Example #6
0
def pytest_collectstart(collector):
    if collector.nodeid:
        current_repo_basedir = find_repository_basedir(collector.nodeid)
        # loading the current repo
        if (
            not hasattr(collector.session, "leapp_repository")
            or current_repo_basedir != collector.session.repo_base_dir
        ):
            repo = find_and_scan_repositories(
                find_repository_basedir(collector.nodeid), include_locals=True
            )
            repo.load(skip_actors_discovery=True)
            collector.session.leapp_repository = repo
            collector.session.repo_base_dir = current_repo_basedir

        # we're forcing the actor context switch only when traversing new
        # actor
        if "/actors/" in str(collector.fspath) and (
            not hasattr(collector.session, "current_actor_path")
            or collector.session.current_actor_path + os.sep
            not in str(collector.fspath)
        ):
            actor = None
            for a in collector.session.leapp_repository.actors:
                if a.full_path == collector.fspath.dirpath().dirname:
                    actor = a
                    break

            if not actor:
                logger.info("No actor found, exiting collection...")
                return
            # we need to tear down the context from the previous
            # actor
            try:
                collector.session.current_actor_context.__exit__(
                    None, None, None
                )
            except AttributeError:
                pass
            else:
                logger.info(
                    "Actor %r context teardown complete",
                    collector.session.current_actor.name,
                )

            logger.info("Injecting actor context for %r", actor.name)
            collector.session.current_actor = actor
            collector.session.current_actor_context = actor.injected_context()
            collector.session.current_actor_context.__enter__()
            collector.session.current_actor_path = (
                collector.session.current_actor.full_path
            )
            logger.info("Actor %r context injected", actor.name)
Example #7
0
def load_repo(path):
    """
    Load repository on demand.

    Do not require paths initialized if no environment is set.
    Allows some parts to be tested without working leapp installation.
    """
    from leapp.utils.repository import find_repository_basedir
    from leapp.repository.scan import find_and_scan_repositories

    repo = find_and_scan_repositories(find_repository_basedir(path),
                                      include_locals=True)
    repo.load()
    return repo
Example #8
0
def cli(params):
    configure_logger()
    repository = find_and_scan_repositories(find_repository_basedir('.'), include_locals=True)
    try:
        repository.load()
    except LeappError as exc:
        sys.stderr.write(exc.message)
        sys.exit(1)

    wf = repository.lookup_workflow(params.name)
    if not wf:
        raise CommandError('Could not find any workflow named "{}"'.format(params.name))

    instance = wf()
    instance.run(until_phase=params.until_phase, until_actor=params.until_actor)
    report_errors(instance.errors)
Example #9
0
def main():
    logging.basicConfig(level=logging.INFO, filename='/dev/null')
    logger = logging.getLogger('run_pytest.py')

    BASE_REPO = 'repos'
    repos = find_and_scan_repositories(BASE_REPO, include_locals=True)
    repos.load()
    if len(sys.argv) > 1:
        actors = repos._lookup_actors(sys.argv[1])
        if not actors:
            sys.stderr.write('ERROR: No actor found for search "{}"\n'.format(sys.argv[1]))
            err_exit()
        print(' '.join([actor.full_path for actor in actors]))
    else:
        sys.stderr.write('ERROR: Missing commandline argument\n')
        err_exit()
Example #10
0
def _load_and_add_repo(manager, repo_path):
    repo = find_and_scan_repositories(repo_path, include_locals=True)
    unloaded = set()
    loaded = {r.repo_id for r in manager.repos}
    if hasattr(repo, 'repos'):
        for repo in repo.repos:
            if not manager.repo_by_id(repo.repo_id):
                manager.add_repo(repo)
                unloaded.add(repo.repo_id)
    else:
        manager.add_repo(repo)
    if not loaded:
        manager.load(skip_actors_discovery=True)
    else:
        for repo_id in unloaded:
            manager.repo_by_id(repo_id).load(skip_actors_discovery=True)
Example #11
0
def cli(args):
    log = configure_logger()
    basedir = find_repository_basedir('.')
    repository = find_and_scan_repositories(basedir, include_locals=True)
    try:
        repository.load()
    except LeappError as exc:
        sys.stderr.write(exc.message)
        sys.exit(1)
    actor_logger = log.getChild('actors')
    actor = repository.lookup_actor(args.actor_name)
    messaging = InProcessMessaging(stored=args.save_output)
    messaging.load(actor.consumes)

    actor(messaging=messaging, logger=actor_logger).run()

    report_errors(messaging.errors())

    if args.print_output:
        json.dump(messaging.messages(), sys.stdout, indent=2)
        sys.stdout.write('\n')
Example #12
0
def pytest_sessionstart(session):
    actor_path = os.environ.get('LEAPP_TESTED_ACTOR', None)
    if not actor_path:
        return
    repo = find_and_scan_repositories(('/'.join(actor_path.split('/')[:-2])), include_locals=True)
    repo.load()

    actor = None
    # find which actor is being tested
    for a in repo.actors:
        if a.full_path == actor_path:
            actor = a
            break

    if not actor:
        return

    # load actor context so libraries can be imported on module level
    session.leapp_repository = repo
    session.actor_context = actor.injected_context()
    session.actor_context.__enter__()
Example #13
0
def cli(args):
    start = datetime.datetime.utcnow()
    log = configure_logger()
    basedir = find_repository_basedir('.')
    repository = find_and_scan_repositories(basedir, include_locals=True)
    try:
        repository.load()
    except LeappError as exc:
        sys.stderr.write(exc.message)
        sys.stderr.write('\n')
        sys.exit(1)
    actor_logger = log.getChild('actors')
    actor = repository.lookup_actor(args.actor_name)
    if not actor:
        raise CommandError('Actor "{}" not found!'.format(args.actor_name))
    config_model = getattr(import_module('leapp.models'),
                           args.actor_config) if args.actor_config else None
    messaging = InProcessMessaging(stored=args.save_output,
                                   config_model=config_model)
    messaging.load(actor.consumes)

    failure = False
    with beautify_actor_exception():
        try:
            actor(messaging=messaging,
                  logger=actor_logger,
                  config_model=config_model).run()
        except BaseException:
            failure = True
            raise

    report_errors(messaging.errors())
    report_deprecations(os.getenv('LEAPP_EXECUTION_ID'), start=start)

    if failure or messaging.errors():
        sys.exit(1)

    if args.print_output:
        json.dump(messaging.messages(), sys.stdout, indent=2)
        sys.stdout.write('\n')
Example #14
0
def cli(params):
    configure_logger()
    repository = find_and_scan_repositories(find_repository_basedir('.'),
                                            include_locals=True)
    try:
        repository.load()
    except LeappError as exc:
        sys.stderr.write(exc.message)
        sys.stderr.write('\n')
        sys.exit(1)

    wf = repository.lookup_workflow(params.name)
    if not wf:
        raise CommandError('Could not find any workflow named "{}"'.format(
            params.name))

    instance = wf()
    produced_late = set(instance.initial).intersection(set(instance.produces))
    if produced_late:
        print_fail(
            _DESCRIPTION.format(' '.join([m.__name__ for m in produced_late])))
        sys.exit(1)
Example #15
0
def loaded_leapp_repository(request):
    """
    This fixture will ensure that the repository for the current test run is loaded with all its links etc.

    This enables running actors and using models, tags, topics, workflows etc.

    Additionally loaded_leapp_repository gives you access to a :py:class:`leapp.repository.manager.RepositoryManager`
    instance.

    :Example:

    .. code-block:: python

        from leapp.snactor.fixture import loaded_leapp_repository
        from leapp.models import ExampleModel, ProcessedExampleModel

        def my_repository_library_test(loaded_leapp_repository):
            from leapp.libraries.common import global
            e = ExampleModel(value='Some string')
            result = global.process_function(e)
            assert type(result) is ProcessedExampleModel

    """
    repository_path = find_repository_basedir(request.module.__file__)
    os.environ['LEAPP_CONFIG'] = os.path.join(repository_path, '.leapp',
                                              'leapp.conf')
    os.environ['LEAPP_HOSTNAME'] = socket.getfqdn()
    context = str(uuid.uuid4())
    with get_connection(None):
        Execution(context=context, kind='snactor-test-run',
                  configuration='').store()
        os.environ["LEAPP_EXECUTION_ID"] = context

        manager = getattr(request.session, 'leapp_repository', None)
        if not manager:
            manager = find_and_scan_repositories(repository_path,
                                                 include_locals=True)
            manager.load(resolve=True)
        yield manager
 def _run_test(repo_path):
     with repo_path.as_cwd():
         repository = find_and_scan_repositories(repo_path.dirpath().strpath)
         assert repository
         repository.load(resolve=True)
         assert getattr(leapp.tags, 'TestTag')
         assert repository.lookup_actor('TestActor')
         assert repository.lookup_workflow('TestWorkflow')
         assert not repository.lookup_workflow('MissingWorkflow')
         assert not repository.lookup_actor('MissingActor')
         assert repository.repos
         assert len(repository.dump()) >= 1
         assert repository.actors
         assert not repository.topics
         assert not repository.models
         assert repository.tags
         assert repository.workflows
         assert repository.tools
         assert repository.libraries
         assert repository.files
         with pytest.raises(LeappRuntimeError):
             repository.lookup_actor('TestActor')().run()
Example #17
0
def load_repositories_from(name, repo_path, manager=None):
    if get_config().has_option('repositories', name):
        repo_path = get_config().get('repositories', name)
    return find_and_scan_repositories(repo_path, manager=manager)
Example #18
0
        if "tests" in root and "tests/" not in root:
            for item in files:
                if item.endswith(".py"):
                    old_item = os.path.join(root, item)
                    new_item = old_item.replace(".", "_" + str(actor_id) + ".")
                    shutil.move(old_item, new_item)
                    actor_id += 1

    # Register repos. This may take a while.
    snactor_register(TMP_BASE_REPO)

    # Find and collect leapp repositories.
    repos = {}
    for root, dirs, files in os.walk(BASE_REPO):
        if ".leapp" in dirs:
            repository = find_and_scan_repositories(root, include_locals=True)
            try:
                repository.load()
            except LeappError as exc:
                sys.stderr.write(exc.message)
                sys.exit(2)
            repos[repository] = root

    # Scan repositories for tests and print status.
    logger.info(" = Scanning Leapp repositories for tests")
    for repo, repo_path in repos.items():
        for actor in repo.actors:
            if not actor.tests:
                status = " Tests MISSING: {ACTOR} | class={CLASS}"
                status = status.format(ACTOR=actor.name,
                                       CLASS=actor.class_name)
Example #19
0
def cli(args):
    logging.basicConfig(level=logging.WARNING, stream=sys.stderr)
    base_dir = find_repository_basedir('.')

    if args.safe and args.json:
        sys.stderr.write(
            'The options --safe and --json are currently mutually exclusive\n')
        sys.exit(1)

    if args.safe:
        sys.stdout.write(
            'Repository:\n  Name: {repository}\n  Path: {base_dir}\n\n'.format(
                repository=get_repository_name(base_dir), base_dir=base_dir))
        safe_discover(base_dir)
        sys.exit(0)

    repository = find_and_scan_repositories(base_dir, include_locals=True)
    try:
        repository.load()
    except LeappError as exc:
        sys.stderr.write(exc.message)
        sys.stderr.write('\n')
        sys.exit(1)

    actors = repository.actors
    topics = [
        topic for topic in get_topics()
        if _is_local(repository, topic, base_dir, all_repos=args.all)
    ]
    models = [
        model for model in get_models()
        if _is_local(repository, model, base_dir, all_repos=args.all)
    ]
    tags = [
        tag for tag in get_tags()
        if _is_local(repository, tag, base_dir, all_repos=args.all)
    ]
    workflows = [
        workflow for workflow in get_workflows()
        if _is_local(repository, workflow, base_dir, all_repos=args.all)
    ]
    if not args.json:
        sys.stdout.write(
            'Repository:\n  Name: {repository}\n  Path: {base_dir}\n\n'.format(
                repository=get_repository_name(base_dir), base_dir=base_dir))
        _print_group('Actors',
                     actors,
                     name_resolver=lambda x: x.class_name,
                     path_resolver=_get_actor_path)
        _print_group('Models', models)
        _print_group('Tags', tags)
        _print_group('Topics', topics)
        _print_group('Workflows', workflows)
    else:
        output = {
            'repository':
            get_repository_name(base_dir),
            'base_dir':
            base_dir,
            'topics':
            dict((topic.__name__, _get_topic_details(topic))
                 for topic in topics),
            'models':
            dict((model.__name__, _get_model_details(model))
                 for model in models),
            'actors':
            dict((actor.class_name, _get_actor_details(actor))
                 for actor in actors),
            'tags':
            dict((tag.name, _get_tag_details(tag)) for tag in tags),
            'workflows':
            dict((workflow.__name__, _get_workflow_details(workflow))
                 for workflow in workflows)
        }
        json.dump(output, sys.stdout, indent=2)
        sys.stdout.write('\n')
Example #20
0
import logging
import sys

from leapp.repository.scan import find_and_scan_repositories


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO, filename='/dev/null')
    logger = logging.getLogger('run_pytest.py')

    BASE_REPO = 'repos'
    repos = find_and_scan_repositories(BASE_REPO, include_locals=True)
    repos.load()

    print(','.join(repos.libraries))
Example #21
0
                        help='path to a dot file that should be generated')
    parser.add_argument('-x',
                        '--regex',
                        help='A regex that actors name should match')
    clusters_help = (
        'cluster actors by tags\n'
        'WARNING: the edges are too close to each other, so it is usually hard to say which label '
        'belongs to which edge')
    parser.add_argument('-c',
                        '--clusters',
                        default=False,
                        action='store_true',
                        help=clusters_help)
    args = parser.parse_args()

    repository = find_and_scan_repositories(args.repo, include_locals=True)
    if not repository:
        print('Could not find repository in specified path: {path}'.format(
            path=args.repo),
              file=sys.stderr)
        sys.exit(1)
    try:
        repository.load()
    except Exception as e:
        print(
            'Could not load repository:\n{message}'.format(message=e.message),
            file=sys.stderr)
        sys.exit(1)

    serializable_tags()
from __future__ import print_function
import json

from leapp.utils.repository import find_repository_basedir
from leapp.repository.scan import find_and_scan_repositories

base_dir = find_repository_basedir('.')
repository = find_and_scan_repositories(base_dir, include_locals=True)

repository.load()

if not hasattr(repository, 'repos'):
    repository.repos = [repository]

print(json.dumps([repo.serialize() for repo in repository.repos]))