Example #1
0
def test(role,
         config,
         # path args
         roles_path, library_path, plugins_action_path,
         plugins_filter_path, plugins_lookup_path,
         # ansible-playbook args
         extra_vars, limit, skip_tags, tags, verbosity,
         # misc
         ansible_version, privileged, cache, save):
    """
    Run tests

    ROLE can be either be a local path, a git repository or an ansible-galaxy
    role name.
    """
    with ContainerManager(docker_client()) as docker:
        ansible_paths = {
            'roles': roles_path,
            'library': library_path,
            'plugins': {
                'action': plugins_action_path,
                'filter': plugins_filter_path,
                'lookup': plugins_lookup_path,
            }
        }

        _load_config(ansible_paths, config)

        framework = TestFramework(docker, role, ansible_paths,
                                  ansible_version)
        res = framework.run(
            extra_vars=extra_vars,
            limit=limit,
            skip_tags=skip_tags,
            tags=tags,
            verbosity=verbosity,
            privileged=privileged,
            cache=cache,
            save=save
        )

        if res != 0 and save != 'failed':
            click.secho('''
info: some of the tests have failed. If you wish to inspect the failed
      containers, rerun the command while adding the --save=failed flag
      to your command line.''', fg='blue')
    sys.exit(res)
def snapshots_view(image):
    """
    Display the output of the ansible play that was run on this snapshot
    """
    docker = docker_client()

    image, image_name = _resolve_image(docker, image)

    try:
        res = docker.inspect_image(image=image_name)
    except APIError as e:
        click.secho('error: %s' % e.explanation.decode('utf-8'), fg='red', err=True)
        sys.exit(1)

    try:
        play = json.loads(res.get('Comment'))
    except ValueError as e:
        click.secho('error: %s' % str(e), fr='red')
        sys.exit(1)

    repo, tag = image_name.split(':')
    repo = repo.split('.')
    host = repo.pop()

    TestFramework.print_header('PLAY [%s]' % host)

    for task in play['tasks']:
        TestFramework.print_header('TASK: [%s]' % task['name'])
        if task['state'] == 'ok':
            if 'changed' in task['res'] and task['res']['changed'] is True:
                click.secho('changed: [%s]' % host, fg='yellow')
            else:
                click.secho('ok: [%s]' % host, fg='green')
        elif task['state'] == 'skipped':
            click.secho('skipped: [%s]' % host, fg='cyan')
        elif task['state'] == 'failed':
            click.secho('failed: [%s]\n%s' % (host, task['res']), fg='red')

    TestFramework.print_header('PLAY RECAP [%s]' % image_name)

    click.secho('{host:<27s}: {ok} {changed} {unreachable} {failed}\n'.format(
        host=click.style(host, fg='yellow'),
        ok=click.style('ok=%-4d' % play['stats']['ok'], fg='green'),
        changed=click.style('changed=%-4d' % play['stats']['changed'], fg='yellow'),
        unreachable='unreachable=%-4d' % play['stats']['unreachable'],
        failed=click.style('failed=%-4d' % play['stats']['failed'], fg='red'),
    ))