def current(): """Prints the current configuration """ yaml.register_class(profile.ProfileData) #yaml.register_class(dict) builder_data.register_classes(yaml) data = { 'profile': profile.get(), 'builder': { 'filepath': builder_data.get_storage_filepath(), #'data': builder_data.get() } } yaml.dump(data, sys.stdout)
def convert_builder_to_yaml(builder): user_paths = [] slas = [] default_population = Population(name='DefaultPopulation') default_scenario = Scenario(name='DefaultScenario') current_path = UserPath('UserPath1') container_heirarchy = [current_path.actions] last_item = container_heirarchy[-1] current_population = default_population current_scenario = default_scenario global_duration = DurationPolicy("1m") current_duration = global_duration global_variables = [] all_requests_no_transactions = ( len(list(filter(lambda item: type(item) is HttpRequest, builder.stack))) > 0 and len(list(filter(lambda item: type(item) is Transaction, builder.stack))) < 1) current_request = None global_headers = [] all_slas = [] temp_transaction_counter = 0 # recurse for appliables for item in builder.stack: parent = get_parent_for_item(item, container_heirarchy, current_path) if all_requests_no_transactions and type(item) is HttpRequest: temp_transaction_counter += 1 temp_transaction = Transaction( name="Transaction {}".format(temp_transaction_counter), inside=parent.name) parent.steps.append(temp_transaction) parent = temp_transaction if issubclass(type(item), ContainableItem): parent.steps.append(item) if type(item) is Container or issubclass(type(item), Container): container_heirarchy.append(item) if issubclass(type(item), VariationPolicy): current_scenario.populations.append({ 'population': current_population, 'variation_policy': item }) if type(item) is Population: current_population = item if type(item) is Scenario: current_scenario = item if type(item) is DurationPolicy: current_duration = item if type(item) is HttpRequest: current_request = item if type(item) is Header: if item.all: global_headers.append(item) else: if current_request is not None: current_request.headers.append(item) if type(item) is Extractor: if current_request is not None: current_request.extractors.append(item) if type(item) is SLAThreshold: # add an SLA object to all_slas threshold = item sla_name = threshold.details['name'] found_sla = list(filter(lambda sla: sla.name == sla_name, all_slas)) if len(found_sla) > 0: found_sla = found_sla[0] else: found_sla = SLA(sla_name) all_slas.append(found_sla) # add this threshold to that object's thresholds found_sla.thresholds.append(threshold) # apply this named sla to the last_item if request or transaction prior_sla_item = current_request if prior_sla_item is None: prior_sla_item = container_heirarchy[-1] if type(prior_sla_item) is Transaction or type( prior_sla_item) is HttpRequest: prior_sla_item.sla_profile = found_sla.name if type(item) is Variable or issubclass(type(item), Variable): global_variables.append(item) last_item = item for header in global_headers: apply_header_to_user_path(header, current_path) current_population.paths.append(current_path) user_paths.append(current_path) # if no explicit policy defined if len(current_scenario.populations) < 1: current_scenario.populations.append({ 'population': current_population, 'variation_policy': ConstantPolicy() }) scenarios = [current_scenario] for scn in scenarios: for holder in scn.populations: if holder['variation_policy'].duration is None: holder['variation_policy'].duration = current_duration.duration project_name = profile.get().default_test_setting if not (project_name is not None and len(project_name.strip())): project_name = "Project1" project = { 'name': project_name, 'user_paths': user_paths, 'populations': [], 'scenarios': scenarios, 'sla_profiles': [], 'variables': global_variables } if len(project['variables']) < 1: del project['variables'] for scn in scenarios: for holder in scn.populations: project['populations'].append({ 'name': holder['population'].name, 'user_paths': list( map(lambda p: { 'name': p.name, 'distribution': '100%' }, holder['population'].paths)) }) for sla in all_slas: project['sla_profiles'].append(sla) if len(project['sla_profiles']) < 1: del project['sla_profiles'] yaml = ruamel.yaml.YAML() yaml.encoding = None register_classes(yaml) fun = MyLogger() yaml.encoding = None yaml.dump(project, fun, transform=strip_python_tags) return fun.readAll()
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
def test_setting(name_or_id): """Set the default test-setting to use for instant run and other subcommands """ profile.get() \ .set_default_test_setting(name_or_id) \ .save()
def zone(name_or_id): """Sets the default zone to use in zone-related subcommands; can also be 'any' """ profile.get() \ .set_default_zone(name_or_id) \ .save()