예제 #1
0
def delegate_remote(args, exclude, require, integration_targets):
    """
    :type args: EnvironmentConfig
    :type exclude: list[str]
    :type require: list[str]
    :type integration_targets: tuple[IntegrationTarget]
    """
    parts = args.remote.split('/', 1)

    platform = parts[0]
    version = parts[1]

    core_ci = AnsibleCoreCI(args,
                            platform,
                            version,
                            stage=args.remote_stage,
                            provider=args.remote_provider)
    success = False
    raw = False

    if isinstance(args, ShellConfig):
        use_httptester = args.httptester
        raw = args.raw
    else:
        use_httptester = args.httptester and any(
            'needs/httptester/' in target.aliases
            for target in integration_targets)

    if use_httptester and not docker_available():
        display.warning(
            'Assuming --disable-httptester since `docker` is not available.')
        use_httptester = False

    httptester_id = None
    ssh_options = []

    try:
        core_ci.start()

        if use_httptester:
            httptester_id, ssh_options = start_httptester(args)

        core_ci.wait()

        if platform == 'windows':
            # Windows doesn't need the ansible-test fluff, just run the SSH command
            manage = ManageWindowsCI(core_ci)
            cmd = ['powershell.exe']
        elif raw:
            manage = ManagePosixCI(core_ci)
            cmd = create_shell_command(['bash'])
        else:
            options = {
                '--remote': 1,
            }

            python_interpreter = get_python_interpreter(
                args, get_remote_completion(), args.remote)
            cmd = generate_command(args, python_interpreter,
                                   'ansible/bin/ansible-test', options,
                                   exclude, require)

            if httptester_id:
                cmd += ['--inject-httptester']

            if isinstance(args, TestConfig):
                if args.coverage and not args.coverage_label:
                    cmd += [
                        '--coverage-label',
                        'remote-%s-%s' % (platform, version)
                    ]

            if isinstance(args, IntegrationConfig):
                if not args.allow_destructive:
                    cmd.append('--allow-destructive')

            # remote instances are only expected to have a single python version available
            if isinstance(args, UnitsConfig) and not args.python:
                cmd += ['--python', 'default']

            manage = ManagePosixCI(core_ci)

        python_version = get_python_version(args, get_remote_completion(),
                                            args.remote)
        manage.setup(python_version)

        if isinstance(args, IntegrationConfig):
            cloud_platforms = get_cloud_providers(args)

            for cloud_platform in cloud_platforms:
                ssh_options += cloud_platform.get_remote_ssh_options()

        try:
            manage.ssh(cmd, ssh_options)
            success = True
        finally:
            download = False

            if platform != 'windows':
                download = True

            if isinstance(args, ShellConfig):
                if args.raw:
                    download = False

            if download:
                manage.ssh(
                    'rm -rf /tmp/results && cp -a ansible/test/results /tmp/results && chmod -R a+r /tmp/results'
                )
                manage.download('/tmp/results', 'test')
    finally:
        if args.remote_terminate == 'always' or (args.remote_terminate
                                                 == 'success' and success):
            core_ci.stop()

        if httptester_id:
            docker_rm(args, httptester_id)
예제 #2
0
def delegate_docker(args, exclude, require, integration_targets):
    """
    :type args: EnvironmentConfig
    :type exclude: list[str]
    :type require: list[str]
    :type integration_targets: tuple[IntegrationTarget]
    """
    test_image = args.docker
    privileged = args.docker_privileged

    if isinstance(args, ShellConfig):
        use_httptester = args.httptester
    else:
        use_httptester = args.httptester and any(
            'needs/httptester/' in target.aliases
            for target in integration_targets)

    if use_httptester:
        docker_pull(args, args.httptester)

    docker_pull(args, test_image)

    httptester_id = None
    test_id = None

    options = {
        '--docker': 1,
        '--docker-privileged': 0,
        '--docker-util': 1,
    }

    python_interpreter = get_python_interpreter(args, get_docker_completion(),
                                                args.docker_raw)
    #cmd = generate_command(args, python_interpreter, '/root/ansible/bin/ansible-test', options, exclude, require)
    #cmd = generate_command(args, python_interpreter, '/root/ansible/ansible_test/bin/ansible-test', options, exclude, require)
    cmd = generate_command(
        args, python_interpreter,
        '/root/ansible/ansible_test/bin/ansible-test-container-bin', options,
        exclude, require)
    cmd = cmd[3:]

    if isinstance(args, TestConfig):
        if args.coverage and not args.coverage_label:
            image_label = args.docker_raw
            image_label = re.sub('[^a-zA-Z0-9]+', '-', image_label)
            cmd += ['--coverage-label', 'docker-%s' % image_label]

    if isinstance(args, IntegrationConfig):
        if not args.allow_destructive:
            cmd.append('--allow-destructive')

    cmd_options = []

    if isinstance(args, ShellConfig) or (isinstance(args, IntegrationConfig)
                                         and args.debug_strategy):
        cmd_options.append('-it')

    with tempfile.NamedTemporaryFile(prefix='ansible-source-',
                                     suffix='.tgz') as local_source_fd:
        try:
            if not args.explain:
                if args.docker_keep_git:
                    tar_filter = lib.pytar.AllowGitTarFilter()
                else:
                    tar_filter = lib.pytar.DefaultTarFilter()

                #lib.pytar.create_tarfile(local_source_fd.name, '.', tar_filter)
                build_src_tar(local_source_fd.name, tar_filter)

            if use_httptester:
                httptester_id = run_httptester(args)
            else:
                httptester_id = None

            test_options = [
                '--detach',
                '--volume',
                '/sys/fs/cgroup:/sys/fs/cgroup:ro',
                '--privileged=%s' % str(privileged).lower(),
            ]

            if args.docker_memory:
                test_options.extend([
                    '--memory=%d' % args.docker_memory,
                    '--memory-swap=%d' % args.docker_memory,
                ])

            docker_socket = '/var/run/docker.sock'

            if args.docker_seccomp != 'default':
                test_options += [
                    '--security-opt',
                    'seccomp=%s' % args.docker_seccomp
                ]

            if os.path.exists(docker_socket):
                test_options += [
                    '--volume',
                    '%s:%s' % (docker_socket, docker_socket)
                ]

            if httptester_id:
                test_options += ['--env', 'HTTPTESTER=1']

                for host in HTTPTESTER_HOSTS:
                    test_options += ['--link', '%s:%s' % (httptester_id, host)]

            if isinstance(args, IntegrationConfig):
                cloud_platforms = get_cloud_providers(args)

                for cloud_platform in cloud_platforms:
                    test_options += cloud_platform.get_docker_run_options()

            test_id, _ = docker_run(args, test_image, options=test_options)

            if args.explain:
                test_id = 'test_id'
            else:
                test_id = test_id.strip()

            # write temporary files to /root since /tmp isn't ready immediately on container start
            #docker_put(args, test_id, 'test/runner/setup/docker.sh', '/root/docker.sh')
            dockersh_src = os.path.join(os.path.dirname(ansible_test.__file__),
                                        'lib', 'runner', 'setup', 'docker.sh')
            docker_put(args, test_id, dockersh_src, '/root/docker.sh')
            docker_exec(args, test_id, ['/bin/bash', '/root/docker.sh'])
            docker_put(args, test_id, local_source_fd.name,
                       '/root/ansible_test.tgz')
            docker_exec(args, test_id,
                        ['mkdir', '-p', '/root/ansible/ansible_test'])
            docker_exec(args, test_id, [
                'tar', 'oxzf', '/root/ansible_test.tgz', '-C',
                '/root/ansible/ansible_test'
            ])

            # fixup the ansible-test installation
            docker_exec(args, test_id,
                        ['mkdir', '-p', '/root/ansible/test/results'])
            docker_exec(args, test_id,
                        ['mkdir', '-p', '/root/ansible/test/test'])
            docker_exec(
                args, test_id,
                ['chmod', '+x', '/root/ansible/ansible_test/bin/ansible-test'])
            docker_exec(args, test_id, [
                'chmod', '+x',
                '/root/ansible/ansible_test/bin/ansible-test-container-bin'
            ])

            # add the metadata file
            docker_put(
                args, test_id, args.metadata_path,
                '/root/ansible/test/%s' % os.path.basename(args.metadata_path))
            midx = cmd.index(os.path.basename(args.metadata_path))
            cmd[midx] = '/root/ansible/test/%s' % os.path.basename(
                args.metadata_path)

            # docker images are only expected to have a single python version available
            if isinstance(args, UnitsConfig) and not args.python:
                cmd += ['--python', 'default']

            # run unit tests unprivileged to prevent stray writes to the source tree
            # also disconnect from the network once requirements have been installed
            if isinstance(args, UnitsConfig):
                writable_dirs = [
                    '/root/ansible/.pytest_cache',
                ]

                docker_exec(args, test_id, ['mkdir', '-p'] + writable_dirs)
                docker_exec(args, test_id, ['chmod', '777'] + writable_dirs)

                docker_exec(args, test_id, [
                    'find', '/root/ansible/test/results/', '-type', 'd',
                    '-exec', 'chmod', '777', '{}', '+'
                ])

                docker_exec(args, test_id, ['chmod', '755', '/root'])
                docker_exec(
                    args, test_id,
                    ['chmod', '644',
                     '/root/ansible/%s' % args.metadata_path])

                docker_exec(args, test_id,
                            ['useradd', 'pytest', '--create-home'])

                docker_exec(args,
                            test_id,
                            cmd + ['--requirements-mode', 'only'],
                            options=cmd_options)

                networks = get_docker_networks(args, test_id)

                for network in networks:
                    docker_network_disconnect(args, test_id, network)

                cmd += ['--requirements-mode', 'skip']

                cmd_options += ['--user', 'pytest']

            #import epdb; epdb.st()
            try:
                docker_exec(args, test_id, cmd, options=cmd_options)
            finally:
                with tempfile.NamedTemporaryFile(
                        prefix='ansible-result-',
                        suffix='.tgz') as local_result_fd:
                    docker_exec(args, test_id, [
                        'tar', 'czf', '/root/results.tgz', '-C',
                        '/root/ansible/test', 'results', 'test'
                    ])
                    docker_get(args, test_id, '/root/results.tgz',
                               local_result_fd.name)
                    #import epdb; epdb.st()
                    #run_command(args, ['tar', 'oxzf', local_result_fd.name, '-C', 'test'])
                    #import epdb; epdb.st()
        finally:
            if httptester_id:
                docker_rm(args, httptester_id)

            if test_id:
                docker_rm(args, test_id)