Exemple #1
0
def api(file_name, login=False):
    """Factory to create either Mock or Wrap api"""
    base_name = os.path.basename(file_name).replace('.pyc', '.py')
    job_name_prefix = _file_name_subst.sub('', base_name)
    func_name = None
    func_num_params = 0
    if '_test' in file_name:
        func_name = sys._getframe().f_back.f_code.co_name  # pylint: disable=protected-access
        func_num_params = sys._getframe().f_back.f_code.co_argcount  # pylint: disable=protected-access
        file_name = base_name
        func_name = func_name.replace('test_', '')
        assert func_name[0:len(job_name_prefix)] == job_name_prefix, \
            "Naming standard not followed: " + repr('test_' + func_name) + " defined in file: " + repr(base_name) + " should be 'test_" + job_name_prefix + "_<sub test>'"
        job_name_prefix = 'jenkinsflow_test__' + func_name + '__'
    else:
        job_name_prefix = 'jenkinsflow_demo__' + job_name_prefix + '__'
        file_name = base_name.replace('_jobs', '')

    print()
    print("--- Preparing api for ", repr(job_name_prefix), "---")
    global hyperspeed
    hyperspeed = HyperSpeed()
    if hyperspeed.is_mocked:
        print('Using Mocked API')
        return MockApi(job_name_prefix, test_cfg.direct_url())
    else:
        print('Using Real Jenkins API with wrapper')
        reload_jobs = not test_cfg.skip_job_load()
        pre_delete_jobs = not test_cfg.skip_job_delete()
        return JenkinsTestWrapperApi(file_name, func_name, func_num_params, job_name_prefix, reload_jobs, pre_delete_jobs,
                                     test_cfg.direct_url(), security.username, security.password, security.securitytoken, login=login)
Exemple #2
0
def api(file_name, api_type, login=False, fixed_prefix=None, url_or_dir=None, fake_public_uri=None, invocation_class=None,
        username=None, password=None):
    """Factory to create either Mock or Wrap api"""
    base_name = os.path.basename(file_name).replace('.pyc', '.py')
    job_name_prefix = _file_name_subst.sub('', base_name)
    func_name = None
    func_num_params = 0
    if fixed_prefix:
        job_name_prefix = fixed_prefix
        file_name = base_name
    elif '_test' in file_name:
        func_name = sys._getframe().f_back.f_code.co_name  # pylint: disable=protected-access
        func_num_params = sys._getframe().f_back.f_code.co_argcount  # pylint: disable=protected-access
        file_name = base_name
        func_name = func_name.replace('test_', '')
        assert func_name[0:len(job_name_prefix)] == job_name_prefix, \
            "Naming standard not followed: " + repr('test_' + func_name) + " defined in file: " + repr(base_name) + " should be 'test_" + job_name_prefix + "_<sub test>'"
        job_name_prefix = 'jenkinsflow_test__' + func_name + '__'
    else:
        job_name_prefix = 'jenkinsflow_demo__' + job_name_prefix + '__'
        file_name = base_name.replace('_jobs', '')

    print()
    print("--- Preparing api for ", repr(job_name_prefix), "---")

    print('Using:', api_type)

    url_or_dir = url_or_dir or test_cfg.direct_url(api_type)
    reload_jobs = not test_cfg.skip_job_load() and not fixed_prefix
    pre_delete_jobs = not test_cfg.skip_job_delete()

    import demo_security as security
    if password is not None or username is not None:
        assert password is not None and username is not None
        login = True

    if username is None:
        assert password is None
        username = security.username
        password = security.password

    if api_type == test_cfg.ApiType.JENKINS:
        from .api_wrapper import JenkinsTestWrapperApi
        return JenkinsTestWrapperApi(file_name, func_name, func_num_params, job_name_prefix, reload_jobs, pre_delete_jobs,
                                     url_or_dir, fake_public_uri, username, password, security.securitytoken, login=login,
                                     invocation_class=invocation_class)
    if api_type == test_cfg.ApiType.SCRIPT:
        from .api_wrapper import ScriptTestWrapperApi
        return ScriptTestWrapperApi(file_name, func_name, func_num_params, job_name_prefix, reload_jobs, pre_delete_jobs,
                                    url_or_dir, fake_public_uri, username, password, security.securitytoken, login=login,
                                    invocation_class=invocation_class)
    if api_type == test_cfg.ApiType.MOCK:
        from .mock_api import MockApi
        return MockApi(job_name_prefix, test_cfg.speedup(), test_cfg.direct_url(api_type))
    else:
        raise Exception("Unhandled api_type:" + repr(api_type))
Exemple #3
0
def main():
    args = args_parser().parse_args()
    print("Creating temporary test installation in", repr(config.pseudo_install_dir), "to make files available to Jenkins.")
    install_script = jp(here, 'tmp_install.sh')
    rc = subprocess.call([install_script])
    if rc:
        print("Failed test installation to. Install script is:", repr(install_script), file=sys.stderr)
        print("Warning: Some tests will fail!", file=sys.stderr)

    print("\nRunning tests")
    try:
        if False or args.pytest_args or args.files:
            extra_args = args.pytest_args.split(' ') + args.files if args.pytest_args else args.files
            subprocess.check_call(['py.test', '--capture=sys', '--instafail'] + extra_args)
            test_cfg.unmock()
            sys.exit(subprocess.call(['py.test', '--capture=sys', '--instafail'] + extra_args))

        run_tests(False, here + '/.coverage_mocked_rc')
        validate_all_demos()

        test_cfg.unmock()
        parallel = test_cfg.skip_job_load() | test_cfg.skip_job_delete()
        if test_cfg.use_jenkinsapi():
            print("Using jenkinsapi_wrapper")
            run_tests(parallel, here + '/.coverage_jenkinsapi_rc')
        else:
            print("Using specialized_api")
            run_tests(parallel, here + '/.coverage_real_rc')
        validate_all_demos(execute_script=True)

        print("\nTesting setup.py")
        user = getpass.getuser()
        install_prefix = '/tmp/' + user
        tmp_packages_dir = install_prefix + '/lib/python2.7/site-packages'
        os.environ['PYTHONPATH'] = tmp_packages_dir
        if os.path.exists(tmp_packages_dir):
            shutil.rmtree(tmp_packages_dir)
        os.makedirs(tmp_packages_dir)
        subprocess.check_call([sys.executable, jp(here, '../setup.py'), 'install', '--prefix', install_prefix])
        shutil.rmtree(jp(here, '../build'))
    except Exception as ex:
        print('*** ERROR: There were errors! Check output! ***', repr(ex), file=sys.stderr)
        raise

    sys.exit(rc)
Exemple #4
0
def main():
    pytest_args, files = args_parser()

    print("Creating temporary test installation in", repr(config.pseudo_install_dir), "to make files available to Jenkins.")
    install_script = jp(here, 'tmp_install.sh')
    rc = subprocess.call([install_script])
    if rc:
        print("Failed test installation to. Install script is:", repr(install_script), file=sys.stderr)
        print("Warning: Some tests will fail!", file=sys.stderr)

    print("\nRunning tests")
    try:
        if False or pytest_args or files:
            extra_args = pytest_args.split(' ') + files if pytest_args else files
            subprocess.check_call(['py.test', '--capture=sys', '--instafail'] + extra_args)
            test_cfg.unmock()
            sys.exit(subprocess.call(['py.test', '--capture=sys', '--instafail'] + extra_args))

        run_tests(False, ApiType.MOCK)
        test_cfg.unmock()

        parallel = test_cfg.skip_job_load() | test_cfg.skip_job_delete()
        # TODO run both, use extra job prefix
        if not test_cfg.selected_api() == ApiType.JENKINSAPI:
            run_tests(parallel, ApiType.SPECIALIZED)
        else:
            run_tests(parallel, ApiType.JENKINSAPI)
        run_tests(parallel, ApiType.SCRIPT)

        start_msg("Testing setup.py")
        user = getpass.getuser()
        install_prefix = '/tmp/' + user
        tmp_packages_dir = install_prefix + '/lib/python2.7/site-packages'
        os.environ['PYTHONPATH'] = tmp_packages_dir
        if os.path.exists(tmp_packages_dir):
            shutil.rmtree(tmp_packages_dir)
        os.makedirs(tmp_packages_dir)
        subprocess.check_call([sys.executable, jp(here, '../setup.py'), 'install', '--prefix', install_prefix])
        shutil.rmtree(jp(here, '../build'))
    except Exception as ex:
        print('*** ERROR: There were errors! Check output! ***', repr(ex), file=sys.stderr)
        raise

    sys.exit(rc)
Exemple #5
0
def api(file_name, login=False, fixed_prefix=None):
    """Factory to create either Mock or Wrap api"""
    base_name = os.path.basename(file_name).replace('.pyc', '.py')
    job_name_prefix = _file_name_subst.sub('', base_name)
    func_name = None
    func_num_params = 0
    if fixed_prefix:
        job_name_prefix = fixed_prefix
        file_name = base_name
    elif '_test' in file_name:
        func_name = sys._getframe().f_back.f_code.co_name  # pylint: disable=protected-access
        func_num_params = sys._getframe().f_back.f_code.co_argcount  # pylint: disable=protected-access
        file_name = base_name
        func_name = func_name.replace('test_', '')
        assert func_name[0:len(job_name_prefix)] == job_name_prefix, \
            "Naming standard not followed: " + repr('test_' + func_name) + " defined in file: " + repr(base_name) + " should be 'test_" + job_name_prefix + "_<sub test>'"
        job_name_prefix = 'jenkinsflow_test__' + func_name + '__'
    else:
        job_name_prefix = 'jenkinsflow_demo__' + job_name_prefix + '__'
        file_name = base_name.replace('_jobs', '')

    print()
    print("--- Preparing api for ", repr(job_name_prefix), "---")
    if mocked:
        print('Using Mocked API')
        from .mock_api import MockApi
        return MockApi(job_name_prefix, test_cfg.direct_url())
    else:
        api_type = test_cfg.selected_api()
        print('Using:', api_type)
        url_or_dir = test_cfg.direct_url()

        reload_jobs = not test_cfg.skip_job_load() and not fixed_prefix
        pre_delete_jobs = not test_cfg.skip_job_delete()
        import demo_security as security
        from .api_wrapper import JenkinsTestWrapperApi
        return JenkinsTestWrapperApi(file_name, func_name, func_num_params, job_name_prefix, reload_jobs, pre_delete_jobs,
                                     url_or_dir, security.username, security.password, security.securitytoken, login=login)
Exemple #6
0
def cli(mock_speedup=1000,
        direct_url=test_cfg.direct_url(test_cfg.ApiType.JENKINS),
        apis=None,
        pytest_args=None,
        job_delete=False,
        job_load=True,
        testfile=None):
    """
    Test jenkinsflow.
    First runs all tests mocked in hyperspeed, then runs against Jenkins, using jenkins_api, then run script_api jobs.

    Normally jobs will be run in parallel, specifying --job-delete disables this.
    The default options assumes that re-loading without deletions generates correct job config.
    Tests that require jobs to be deleted/non-existing will delete the jobs, regardless of the --job-delete option.

    [TESTFILE]... File names to pass to py.test
    """

    os.environ[test_cfg.DIRECT_URL_NAME] = direct_url
    os.environ[test_cfg.SKIP_JOB_DELETE_NAME] = 'false' if job_delete else 'true'
    os.environ[test_cfg.SKIP_JOB_LOAD_NAME] = 'false' if job_load else 'true'

    args = ['--capture=sys', '--instafail']

    if apis is None:
        api_types = [ApiType.MOCK, ApiType.SCRIPT, ApiType.JENKINS]
    else:
        api_types = [ApiType[api_name.strip().upper()] for api_name in apis.split(',')]
    args.extend(['-k', ' or '.join([apit.name for apit in api_types])])

    rc = 0
    target_dir = "/tmp/jenkinsflow-test/jenkinsflow"
    try:
        os.makedirs(target_dir)
    except OSError as ex:
        if ex.errno != errno.EEXIST:
            raise

    if api_types != [ApiType.MOCK]:
        print("Creating temporary test installation in", repr(config.pseudo_install_dir), "to make files available to Jenkins.")
        install_script = jp(here, 'tmp_install.sh')
        rc = subprocess.call([install_script, target_dir])
        if rc:
            print("Failed test installation to", repr(config.pseudo_install_dir), "Install script is:", repr(install_script), file=sys.stderr)
            print("Warning: Some tests will fail!", file=sys.stderr)

    cov_file = ".coverage"
    for cov_file in jp(here, cov_file), jp(top_dir, cov_file):
        if os.path.exists(cov_file):
            os.remove(cov_file)

    print("\nRunning tests")
    try:
        if pytest_args or testfile:
            coverage = False
            args.extend(pytest_args.split(' ') + list(testfile) if pytest_args else list(testfile))
        else:
            coverage = True
            args.append('--ff')

        hudson = os.environ.get('HUDSON_URL')
        if hudson:
            print("Disabling parallel run, Hudson can't handle it :(")
        parallel = test_cfg.skip_job_load() or test_cfg.skip_job_delete() and not hudson
        run_tests(parallel, api_types, args, coverage, mock_speedup)

        # start_msg("Testing setup.py")
        # user = getpass.getuser()
        # install_prefix = '/tmp/' + user
        # tmp_packages_dir = install_prefix + '/lib/python{major}.{minor}/site-packages'.format(major=major_version, minor=sys.version_info.minor)
        # os.environ['PYTHONPATH'] = tmp_packages_dir
        # if os.path.exists(tmp_packages_dir):
        #     shutil.rmtree(tmp_packages_dir)
        # os.makedirs(tmp_packages_dir)
        #
        # os.chdir(top_dir)
        # subprocess.check_call([sys.executable, jp(top_dir, 'setup.py'), 'install', '--prefix', install_prefix])
        # shutil.rmtree(jp(top_dir, 'build'))

        if not testfile and os.environ.get('CI', 'false').lower() != 'true':
            # This is automatically tested by readdthedocs, so no need to test on Travis
            start_msg("Testing documentation generation")

            os.chdir(jp(top_dir, 'doc/source'))
            del os.environ['PYTHONPATH']
            subprocess.check_call(['make', 'html'])
    except Exception as ex:
        print('*** ERROR: There were errors! Check output! ***', repr(ex), file=sys.stderr)
        raise

    sys.exit(rc)
Exemple #7
0
def api(file_name,
        api_type,
        login=None,
        fixed_prefix=None,
        url_or_dir=None,
        fake_public_uri=None,
        invocation_class=None,
        username=None,
        password=None):
    """Factory to create either Mock or Wrap api"""
    base_name = os.path.basename(file_name).replace('.pyc', '.py')
    job_name_prefix = _file_name_subst.sub('', base_name)
    func_name = None
    func_num_params = 0
    if fixed_prefix:
        job_name_prefix = fixed_prefix
        file_name = base_name
    elif '_test' in file_name:
        func_name = sys._getframe().f_back.f_code.co_name  # pylint: disable=protected-access
        func_num_params = sys._getframe().f_back.f_code.co_argcount  # pylint: disable=protected-access
        file_name = base_name
        func_name = func_name.replace('test_', '')
        assert func_name[0:len(job_name_prefix)] == job_name_prefix, \
            "Naming standard not followed: " + repr('test_' + func_name) + " defined in file: " + repr(base_name) + " should be 'test_" + job_name_prefix + "_<sub test>'"
        job_name_prefix = 'jenkinsflow_test__' + func_name + '__'
    else:
        job_name_prefix = 'jenkinsflow_demo__' + job_name_prefix + '__'
        file_name = base_name.replace('_jobs', '')

    print()
    print("--- Preparing api for ", repr(job_name_prefix), "---")

    if api_type == 0:
        # Invocation from actual Jenkins flow job calls with api_type == 0
        api_type = test_cfg.ApiType.JENKINS
    print('Using:', api_type)

    url_or_dir = url_or_dir or test_cfg.direct_url(api_type)
    reload_jobs = not test_cfg.skip_job_load() and not fixed_prefix
    pre_delete_jobs = not test_cfg.skip_job_delete()

    import demo_security as security
    if login is None:
        login = security.default_use_login

    if password is not None or username is not None:
        assert password is not None and username is not None
        login = True

    if username is None:
        assert password is None
        username = security.username
        password = security.password

    if api_type == test_cfg.ApiType.JENKINS:
        from .api_wrapper import JenkinsTestWrapperApi
        return JenkinsTestWrapperApi(file_name,
                                     func_name,
                                     func_num_params,
                                     job_name_prefix,
                                     reload_jobs,
                                     pre_delete_jobs,
                                     url_or_dir,
                                     fake_public_uri,
                                     username,
                                     password,
                                     security.securitytoken,
                                     login=login,
                                     invocation_class=invocation_class)
    if api_type == test_cfg.ApiType.SCRIPT:
        from .api_wrapper import ScriptTestWrapperApi
        return ScriptTestWrapperApi(file_name,
                                    func_name,
                                    func_num_params,
                                    job_name_prefix,
                                    reload_jobs,
                                    pre_delete_jobs,
                                    url_or_dir,
                                    fake_public_uri,
                                    username,
                                    password,
                                    security.securitytoken,
                                    login=login,
                                    invocation_class=invocation_class)
    if api_type == test_cfg.ApiType.MOCK:
        from .mock_api import MockApi
        return MockApi(job_name_prefix, test_cfg.speedup(),
                       test_cfg.direct_url(api_type))

    raise Exception("Unhandled api_type:" + repr(api_type))
Exemple #8
0
def cli(mock_speedup, direct_url, api, pytest_args, job_delete, job_load, testfile):
    """
    Test jenkinsflow.
    First runs all tests mocked in hyperspeed, then runs against Jenkins, using jenkins_api, then run script_api jobs.

    Normally jobs will be run in parallel, specifying --job-delete disables this.
    The default options assumes that re-loading without deletions generates correct job config.
    Tests that require jobs to be deleted/non-existing will delete the jobs, regardless of the --job-delete option.

    [TESTFILE]... File names to pass to py.test
    """

    os.environ[test_cfg.DIRECT_URL_NAME] = direct_url
    os.environ[test_cfg.SKIP_JOB_DELETE_NAME] = 'false' if job_delete else 'true'
    os.environ[test_cfg.SKIP_JOB_LOAD_NAME] = 'false' if job_load else 'true'
    os.environ[test_cfg.SCRIPT_DIR_NAME] = test_cfg.script_dir()

    print("Creating temporary test installation in", repr(config.pseudo_install_dir), "to make files available to Jenkins.")
    install_script = jp(here, 'tmp_install.sh')
    rc = subprocess.call([install_script])
    if rc:
        print("Failed test installation to", repr(config.pseudo_install_dir), "Install script is:", repr(install_script), file=sys.stderr)
        print("Warning: Some tests will fail!", file=sys.stderr)

    cov_file = ".coverage"
    for cov_file in jp(here, cov_file), jp(top_dir, cov_file):
        if os.path.exists(cov_file):
            os.remove(cov_file)

    print("\nRunning tests")
    try:
        args = ['--capture=sys', '--instafail']

        if pytest_args or testfile:
            coverage = False
            args.extend(pytest_args.split(' ') + list(testfile) if pytest_args else list(testfile))
        else:
            coverage = True
            args.append('--ff')

        hudson = os.environ.get('HUDSON_URL')
        if hudson:
            print("Disabling parallel run, Hudson can't handle it :(")
        parallel = test_cfg.skip_job_load() or test_cfg.skip_job_delete() and not hudson
        run_tests(parallel, api, args, coverage, mock_speedup)

        start_msg("Testing setup.py")
        user = getpass.getuser()
        install_prefix = '/tmp/' + user
        tmp_packages_dir = install_prefix + '/lib/python{major}.{minor}/site-packages'.format(major=major_version, minor=sys.version_info.minor)
        os.environ['PYTHONPATH'] = tmp_packages_dir
        if os.path.exists(tmp_packages_dir):
            shutil.rmtree(tmp_packages_dir)
        os.makedirs(tmp_packages_dir)

        os.chdir(top_dir)
        subprocess.check_call([sys.executable, jp(top_dir, 'setup.py'), 'install', '--prefix', install_prefix])
        shutil.rmtree(jp(top_dir, 'build'))

        start_msg("Testing documentation generation")

        os.chdir('doc/source')
        subprocess.check_call(['make', 'html'])
    except Exception as ex:
        print('*** ERROR: There were errors! Check output! ***', repr(ex), file=sys.stderr)
        raise

    sys.exit(rc)
Exemple #9
0
def cli(mock_speedup=1000,
        direct_url=test_cfg.direct_url(test_cfg.ApiType.JENKINS),
        apis=None,
        pytest_args=None,
        job_delete=False,
        job_load=True,
        testfile=None):
    """
    Test jenkinsflow.
    First runs all tests mocked in hyperspeed, then runs against Jenkins, using jenkins_api, then run script_api jobs.

    Normally jobs will be run in parallel, specifying --job-delete disables this.
    The default options assumes that re-loading without deletions generates correct job config.
    Tests that require jobs to be deleted/non-existing will delete the jobs, regardless of the --job-delete option.

    [TESTFILE]... File names to pass to py.test
    """

    os.environ[test_cfg.DIRECT_URL_NAME] = direct_url
    os.environ[
        test_cfg.SKIP_JOB_DELETE_NAME] = 'false' if job_delete else 'true'
    os.environ[test_cfg.SKIP_JOB_LOAD_NAME] = 'false' if job_load else 'true'

    args = ['--capture=sys', '--instafail']

    if apis is None:
        api_types = [ApiType.MOCK, ApiType.SCRIPT, ApiType.JENKINS]
    else:
        api_types = [
            ApiType[api_name.strip().upper()] for api_name in apis.split(',')
        ]
    args.extend(['-k', ' or '.join([apit.name for apit in api_types])])

    rc = 0
    target_dir = "/tmp/jenkinsflow-test/jenkinsflow"
    try:
        os.makedirs(target_dir)
    except OSError as ex:
        if ex.errno != errno.EEXIST:
            raise

    if api_types != [ApiType.MOCK]:
        print("Creating temporary test installation in",
              repr(config.pseudo_install_dir),
              "to make files available to Jenkins.")
        install_script = jp(here, 'tmp_install.sh')
        rc = subprocess.call([install_script, target_dir])
        if rc:
            print("Failed test installation to",
                  repr(config.pseudo_install_dir),
                  "Install script is:",
                  repr(install_script),
                  file=sys.stderr)
            print("Warning: Some tests will fail!", file=sys.stderr)

    cov_file = ".coverage"
    for cov_file in jp(here, cov_file), jp(top_dir, cov_file):
        if os.path.exists(cov_file):
            os.remove(cov_file)

    print("\nRunning tests")
    try:
        if pytest_args or testfile:
            coverage = False
            args.extend(
                pytest_args.split(' ') +
                list(testfile) if pytest_args else list(testfile))
        else:
            coverage = True
            args.append('--ff')

        hudson = os.environ.get('HUDSON_URL')
        if hudson:
            print("Disabling parallel run, Hudson can't handle it :(")
        parallel = test_cfg.skip_job_load(
        ) or test_cfg.skip_job_delete() and not hudson
        run_tests(parallel, api_types, args, coverage, mock_speedup)

        if not testfile and os.environ.get('CI', 'false').lower() != 'true':
            # This is automatically tested by readdthedocs, so no need to test on Travis
            start_msg("Testing documentation generation")

            os.chdir(jp(top_dir, 'doc/source'))
            del os.environ['PYTHONPATH']
            subprocess.check_call(['make', 'html'])
    except Exception as ex:
        print('*** ERROR: There were errors! Check output! ***',
              repr(ex),
              file=sys.stderr)
        raise

    sys.exit(rc)