Exemple #1
0
def os_run(command, status=False, print_stdout=False, print_line_check=None):
    if status:
        print("Running '{}'".format(command))
    try:
        if not print_stdout:
            proc = os_return(command, status=False)
            (stdout, strerr) = proc.communicate()
            if proc.returncode != 0:
                tools.system_exit({
                    'code': proc.returncode,
                    'message': "{}".format(strerr)
                })
                return
        else:
            #subprocess.run(parse_command(command), check = True)
            print(colorama.Style.RESET_ALL)

            asyncio.get_event_loop().run_until_complete(
                run_async(command, print_line_check))

            print(colorama.Style.RESET_ALL)

        return True
    except subprocess.CalledProcessError as err:
        tools.system_exit({
            'code': err.returncode,
            'message': "{}".format(err)
        })
        return False
Exemple #2
0
def postman(file, filter, output_to_json, grouping, delay):
    """EXAMPLE: nlc import postman --filter='name=GET Request' -f exported_collection.json
    """
    if not (file and len(file) > 0):
        raise cli_exception.CliException("You must use the --file option")

    data = None

    if is_url(file):
        logging.debug("Import file is a URL")
        resp = requests.get(file, stream=True)
        data = json.load(io.BytesIO(resp.content))
    else:
        file = os.path.expanduser(file)
        if os.path.exists(file):
            with open(file) as stm:
                data = json.load(stm)
        else:
            logging.debug("Import file does not exist")

    if data is None:
        tools.system_exit({
            'code': 1,
            'message': 'Could not determine data to import'
        })
        return

    filter_parsed = parse_filter_spec(filter)
    build = builder_data.BuilderData()
    options = {}
    options[wrap_requests_in_transactions] = (grouping == "NONE")
    options[delay_in_seconds] = delay
    options[add_javascript_lib_postman] = False

    version = None

    if 'info' in data and 'schema' in data['info']:
        schema = data['info']['schema']
        version = re.search(r"/v(.*?)/", schema).group(1)

    if version and version.startswith("2."):
        #add_headers(build, get_auth_headers(data), all=True)
        process_postman_v2(build, data, filter_parsed, options)

    if options[add_javascript_lib_postman] == True:
        build.add(
            builder_data.VariableTypeJavascript(
                name=javascript_lib_postman_var_name,
                description=
                'A javascript library for running postman tests using the Node sidecar',
                script=get_postman_var_script_source(),
                change_policy='each_use'))

    builder_data.save(build)
    output = build if output_to_json else builder_data.convert_builder_to_yaml(
        build)
    #output = builder_data.get() if output_to_json else builder_data.convert_builder_to_yaml(builder_data.get())
    print(output)
Exemple #3
0
def cli(ctx, file):
    """Uses the local NeoLoad installation to try a project
    """
    builder_data.register_context(ctx, auto_reset=False)

    yaml_str = None
    if file is None:
        yaml_str = builder_data.convert_builder_to_yaml(builder_data.get())
    else:
        yaml_str = open(file, encoding="UTF-8").read()

    full_path = None
    tmp = tempfile.TemporaryDirectory()
    dir = tmp.name
    full_path = builder_data.write_to_path(dir, yaml_str)

    nlg_app = find_nlg_app()

    event = SubprocessEvent()

    do_it = lambda app=nlg_app, project=full_path, event=event: launch_app(app,project,event)
    action_thread = Thread(target=do_it)

    # Here we start the thread and we wait 5 seconds before the code continues to execute.
    action_thread.start()
    action_thread.join(timeout=20)

    # We send a signal that the other thread should stop.
    event.set()

    msg = "This test project is valid."

    retcode = event.proc.returncode if event.proc is not None else 0
    logging.debug("EXIT_CODE: {}".format(retcode))
    if retcode is not None and retcode != 0:
        if event.stdout is not None:
            msg = event.stdout.decode("UTF-8")
        else:
            msg = "Check console for error details."

    if retcode is None:
        event.proc.kill()

    tools.system_exit({'code': 0 if retcode is None else retcode,'message':msg})
    return
def per_interval(avg_resp_time, avg_elt_per_sec, avg_throughput_per_sec,
                 errors_per_sec, error_rate, warn, fail):
    """Create a per-interval threshold
    """

    if not (avg_resp_time or avg_elt_per_sec or avg_throughput_per_sec
            or errors_per_sec or error_rate):
        tools.system_exit({
            'code': 1,
            'message': "At least one KPY flag must be provided"
        })
        return

    if warn is None and fail is None:
        tools.system_exit({
            'code':
            1,
            'message':
            "Either one or both 'warn' and 'fail' flags must be provided"
        })
        return

    global current_name

    builder = builder_data.get() \
        .add(builder_data.SLAThreshold(
            name=current_name,
            scope="per interval",
            warn=warn,
            fail=fail,
            kpis={
                'avg-resp-time': avg_resp_time,
                'avg-elt-per-sec': avg_elt_per_sec,
                'avg-throughput-per-sec': avg_throughput_per_sec,
                'errors-per-sec': errors_per_sec,
                'error-rate': error_rate
            }
        )
        ) \
        .save()
Exemple #5
0
def cli(ctx, name_or_id, zone, scenario, save, just_report_last):
    """Runs whatever is in the current buffer
    """
    builder_data.register_context(ctx, auto_reset=False)

    neoload_base_cmd = "neoload " + ("--debug " if common.get_debug() else "")

    template = get_resource(__name__,
                            "resources/dist/jinja/builtin-console-summary.j2")
    if template is not None:
        logging.debug("Using template: {}".format(template))

    if not just_report_last:
        if name_or_id and save:
            config.test_setting(name_or_id)
        if zone and save:
            config.zone(zone)

        if not name_or_id:
            name_or_id = profile.get().default_test_setting
            if not name_or_id:
                tools.system_exit({
                    'code':
                    3,
                    'message':
                    "No test settings [name_or_id] provided and no default test-setting configured!"
                })
                return

        if not zone:
            zone = profile.get().default_zone

        if zone == "any":
            proc = os_return(neoload_base_cmd + " zones", status=False)
            (stdout, strerr) = proc.communicate()
            result = json.loads(stdout)
            availables = list(
                filter(
                    lambda z: any(
                        filter(lambda c: c['status'] == "AVAILABLE", z[
                            'controllers'])) and any(
                                filter(lambda lg: lg['status'] == "AVAILABLE",
                                       z['loadgenerators'])), result))
            if len(availables) > 0:
                availables = sorted(availables,
                                    key=lambda z: 0
                                    if z['type'] == "STATIC" else 1)
                zone = availables[0]['id']
                print(
                    "Because zone is 'any', the zone '{}' ({}) has been automatically selected."
                    .format(zone, availables[0]['name']))
            else:
                tools.system_exit({
                    'code':
                    3,
                    'message':
                    "There are no zones with available load generators and controllers!!!"
                })
                return

        if not zone:
            tools.system_exit({
                'code':
                3,
                'message':
                "No --zone provided and no default zone configured!"
            })
            return

        if not os_run(neoload_base_cmd + " status", status=False):
            return

        yaml_str = builder_data.convert_builder_to_yaml(builder_data.get())
        data = yaml.safe_load(yaml_str)

        if not scenario:
            scenario = data['scenarios'][0]['name']

        dir = None
        with tempfile.TemporaryDirectory() as tmp:
            dir = tmp
            builder_data.write_to_path(dir, yaml_str)

            if not os_run(
                    neoload_base_cmd +
                    " test-settings --zone {} --lgs 1 --scenario {} createorpatch {}"
                    .format(zone, scenario, name_or_id),
                    status=True):
                return

            if not os_run(
                    neoload_base_cmd + " project --path {} up".format(dir),
                    status=True):
                return

        if not os_run(neoload_base_cmd + " run".format(scenario),
                      print_stdout=True,
                      print_line_check=check_run_line):
            print("Test failed.")

    proc = os_return(neoload_base_cmd + " report --help", status=False)
    (stdout, strerr) = proc.communicate()
    outtext = stdout.decode("UTF-8")
    if proc.returncode != 0 or 'Error:' in outtext or 'failed to start' in outtext:
        print(
            "Test ran, but could not produce final (pretty) report. {}".format(
                "" if strerr is None else strerr))
    else:
        if template is not None:
            if not os_run(
                    neoload_base_cmd +
                    " report --template {} --filter '{}' cur".format(
                        template,
                        'exclude=events,slas,all_requests,ext_data,controller_points'
                    ),
                    status=True,
                    print_stdout=True):
                return