def validate_yaml(yaml_file_path, schema_spec, ssl_cert='', check_schema=True): json_schema = init_yaml_schema_with_checks(schema_spec,ssl_cert,check_schema) try: schema_as_object = json.loads(json_schema) except JSONDecodeError as err: raise bad_as_code_exception.BadAsCodeSchemaException( 'This is not a valid json schema [{}] :\n{}'.format(schema_spec, err)) try: yaml_content = open(yaml_file_path) except Exception as err: raise cli_exception.CliException('Unable to open file %s:\n%s' % (yaml_file_path, str(err))) try: yaml_as_object = yaml.load(yaml_content, yaml.FullLoader) if yaml_as_object is None: raise cli_exception.CliException('Empty file: ' + str(yaml_file_path)) except ScannerError as err: raise cli_exception.CliException('This is not a valid yaml file [{}] :\n{}'.format(yaml_file_path,err)) v = jsonschema.validators.Draft7Validator(schema_as_object) try: v.validate(yaml_as_object) except jsonschema.SchemaError as err: raise cli_exception.CliException('This is not a valid json schema:\n%s' % str(err)) except jsonschema.ValidationError as err: msgs = "" for error in sorted(v.iter_errors(yaml_as_object), key=str): path = "\\".join(list(map(lambda x: str(x), error.path))) msgs += "\n" + (error.message if hasattr(error, 'message') else str(error)) + "\n\tat: " + path + "\n\tgot: \n" + (yaml.dump(error.instance) if hasattr(error, 'instance') else '') + "\n" msgs = ("in file %s" % yaml_file_path) + msgs raise cli_exception.CliException(YAML_NOT_CONFIRM_MESSAGE + '\n' + msgs)
def check_zone(zone_set): zone = get_zone(zone_set) if not zone: raise cli_exception.CliException("zone " + zone_set + " doesn't exist") if zone.get('type') != "STATIC": raise cli_exception.CliException("zone " + zone_set + " is not static !") if zone.get('controller'): raise cli_exception.CliException( "controller with " + zone_set + " zone is not empty. Try neoload docker clean") if zone.get('loadgenerators'): logging.warning("lg zone with " + zone_set + " is not empty. Try neoload docker clean")
def __handle_error(response): status_code = response.status_code if status_code > 299: request = response.request if status_code == 401: raise cli_exception.CliException( "Server has returned 401 Access denied. Please check your token and rights" ) else: raise cli_exception.CliException("Error " + str(status_code) + " during the request: " + request.method + " " + request.url + "\n" + response.text) return response
def display_project(res): if 299 > res.status_code > 199: tools.print_json(res.json()) else: print(res.text) raise cli_exception.CliException("Error during get meta data code: " + res.status_code)
def parse_filter_spec(filter_spec): ret = {} ret['time_filter'] = None ret['results_filter'] = None ret['elements_filter'] = None ret['exclude_filter'] = None ret['include_filter'] = None if filter_spec is not None: filter_parts = filter_spec.split(";") for s in filter_parts: index_of_equals = s.find('=') + 1 if len(s.strip()) > 0 and index_of_equals == 0: raise cli_exception.CliException( 'Bad syntax for filter option. Did you forget the = sign ? See help with command "neoload report"') if s.startswith("timespan"): ret['time_filter'] = s[index_of_equals:] elif s.startswith("result"): ret['results_filter'] = s[index_of_equals:] elif s.startswith("element"): ret['elements_filter'] = s[index_of_equals:] elif s.startswith("exclude"): ret['exclude_filter'] = s[index_of_equals:] elif s.startswith("include"): ret['include_filter'] = s[index_of_equals:] return ret
def cli(file, refresh, schema_url): """Verify that the yaml FILE matches the neoload as-code file format""" try: schema_validation.validate_yaml(file, schema_url if refresh else None) except Exception as err: raise cli_exception.CliException(str(err)) print('Yaml file is valid.')
def create_json(name, description, scenario, controller_zone_id, lg_zone_ids, naming_pattern, file): if file: try: return json.load(file) except json.JSONDecodeError as err: raise cli_exception.CliException('%s\nThis command requires a valid Json input.\n' 'Example: neoload test-settings create {"name":"TestName"}' % str(err)) data = {} if name is not None: data['name'] = name if description is not None: data['description'] = description if scenario is not None: data['scenarioName'] = scenario if controller_zone_id is not None: data['controllerZoneId'] = controller_zone_id if lg_zone_ids is not None: data['lgZoneIds'] = parse_zone_ids(lg_zone_ids, controller_zone_id) if naming_pattern is not None: data['testResultNamingPattern'] = naming_pattern if len(data) == 0: data = manual_json(data) return data
def load_from_file(file): try: return json.load(file) except json.JSONDecodeError as err: raise cli_exception.CliException( '%s\nThis command requires a valid Json input.\n' 'Example: neoload test-settings create {"name":"TestName"}' % str(err))
def get_file_storage_from_swagger(): response = rest_crud.get_raw('explore/v2/swagger.yaml') spec = yaml.load(response.text, Loader=yaml.FullLoader) if isinstance(spec, basestring) or 'paths' not in spec.keys(): raise cli_exception.CliException( 'Unable to reach Neoload Web API. Bad URL or bad swagger file at /explore/v2/swagger.yaml.' ) return spec['paths']['/tests/{testId}/project']['servers'][0]['url']
def resolve_name(self, name, return_none=False): ws = str(rest_crud.get_workspace()) __id = self.__map.get(ws, {}).get(name, None) if __id is None: self.__fill_map(ws, name) __id = self.__map.get(ws, {}).get(name, None) if not __id and not return_none: raise cli_exception.CliException(f"No id associated to the name '{name}'") return __id
def resolve_name(self, name): __id = self.__map.get(name, None) if __id is None: self.__fill_map(name) __id = self.__map.get(name, None) if not __id: raise cli_exception.CliException( f"No id associated to the name '{name}'") return __id
def resolve_name_or_json(self, name): __id = self.__map.get(name, None) if __id is None: __json = self.__fill_map(name) if __json: return __json else: raise cli_exception.CliException( f"No object associated to the name '{name}'") return __id
def cli(name, force): if not name or name == "cur": name = user_data.get_meta(test_results.meta_key) if not name: raise cli_exception.CliException('No test id is provided') if not tools.is_id(name): name = test_results.__resolver.resolve_name(name) running_tools.stop(name, force)
def resolve_name_or_json(self, name): ws = str(rest_crud.get_workspace()) __id = self.__map.get(ws, {}).get(name, None) if __id is None: __json = self.__fill_map(ws, name) if __json: return __json else: raise cli_exception.CliException(f"No object associated to the name '{name}'") return __id
def get_nlweb_information(): try: response = rest_crud.get_raw('v3/information') if response.status_code == 401: raise cli_exception.CliException(response.text) elif response.status_code == 200: json = response.json() __user_data_singleton.set_url(json['front_url'], json['filestorage_url'], json['version']) return True else: return False except requests.exceptions.MissingSchema as err: raise cli_exception.CliException('Unable to reach Neoload Web API. The URL must start with https:// or http://' + '. Details: ' + str(err)) except requests.exceptions.ConnectionError as err: raise cli_exception.CliException('Unable to reach Neoload Web API. Bad URL. Details: ' + str(err)) except JSONDecodeError as err: raise cli_exception.CliException('Unable to parse the response of the server. Did you set the frontend URL' + ' instead of the API url ? Details: ' + str(err))
def do_login(token, url, no_write): global __no_write __no_write = no_write if token is None: raise cli_exception.CliException( 'token is mandatory. please see neoload login --help.') global __user_data_singleton __user_data_singleton = UserData.from_login(token, url) __compute_version_and_path() __save_user_data() return __user_data_singleton
def get_command(self, ctx, name): """Dynamically get the command.""" ns = {} fn = os.path.join(plugin_folder, name.replace('-', '_') + '.py') if os.path.isfile(fn): with open(fn) as f: code = compile(f.read(), fn, 'exec') eval(code, ns, ns) return ns['cli'] else: raise cli_exception.CliException("\"" + name + "\" is not a neoload command")
def get_docker_client(): global client if not client: try: client = docker.from_env() except Exception: if cli_exception.CliException.is_debug(): logging.exception( "Exception occurs during docker client creation.") raise cli_exception.CliException( "Exception occurs during docker client creation.") return client
def do_login(token, url, no_write, ssl_cert=''): global __no_write __no_write = no_write if token is None: raise cli_exception.CliException('token is mandatory. please see neoload login --help.') global __user_data_singleton __user_data_singleton = UserData.from_login(token, url) __user_data_singleton.set_ssl_cert(ssl_cert) __user_data_singleton.set_metadata_names_resolver(resolve_user_data_metadata_name) __compute_version_and_path() __save_user_data() return __user_data_singleton
def cli(file, refresh, schema_url, ssl_cert): """Verify that the yaml FILE matches the neoload as-code file format""" path = os.path.abspath(file) try: if os.path.isdir(path): schema_validation.validate_yaml_dir(path, schema_url, ssl_cert) print('All yaml files underneath the path provided are valid.') else: schema_validation.validate_yaml(file, schema_url, ssl_cert) print('Yaml file is valid.') except Exception as err: raise cli_exception.CliException(str(err))
def cli(file, refresh, schema_url, ssl_cert): """Verify that the yaml FILE matches the neoload as-code file format""" force_schema = os.environ.get('NLCLI_FORCE_SCHEMA') if force_schema is not None and len(force_schema) > 0: schema_url = force_schema path = os.path.abspath(file) try: if os.path.isdir(path): schema_validation.validate_yaml_dir(path, schema_url, ssl_cert) print('All yaml files underneath the path provided are valid.') else: schema_validation.validate_yaml(file, schema_url, ssl_cert) print('Yaml file is valid.') except Exception as err: raise cli_exception.CliException(str(err))
def manual_json(data): if sys.stdin.isatty(): for field in [ 'name', 'description', 'scenarioName', 'controllerZoneId', 'testResultNamingPattern' ]: data[field] = input(field) data['lgZoneIds'] = parse_zone_ids(input("lgZoneIds"), data['controllerZoneId']) else: try: data = json.loads(sys.stdin.read()) except json.JSONDecodeError as err: raise cli_exception.CliException( '%s\nThis command requires a valid Json input.\n' 'Example: neoload test-settings create {"name":"TestName"}' % str(err)) return data
def create_json(name, description, quality_status): data = {} if name is not None: data['name'] = name if description is not None: data['description'] = description if quality_status is not None: data['qualityStatus'] = quality_status if len(data) == 0: if sys.stdin.isatty(): for field in ['name', 'description', 'qualityStatus']: data[field] = input(field) else: try: return json.loads(sys.stdin.read()) except json.JSONDecodeError as err: raise cli_exception.CliException('%s\nThis command requires a valid Json input.\n' 'Example: neoload test-results put {"name":"TestResultName"}' % str( err)) return data
def zip_dir(path, save): # validate save parameter, if provided, is a zip if save is not None: save = os.path.abspath(save) if not save.endswith(".zip"): raise cli_exception.CliException( 'If you specify where to save the zip file, it must end with .zip' ) # find and load .nlignore ignore_file = os.path.join(path, '.nlignore') nl_ignore_matcher = parse_gitignore(ignore_file) if os.path.exists( ignore_file) else None # always to work in a separate file, if save is provided, or otherwise temp_zip = tempfile.NamedTemporaryFile('w+b', delete=False) ziph = zipfile.ZipFile(temp_zip, 'x', zipfile.ZIP_DEFLATED) for root, dirs, files in os.walk(path): for file in files: file_path = os.path.join(root, file) if not is_not_to_be_included(file_path, nl_ignore_matcher): ziph.write(file_path, file_path.replace(str(path), '')) ziph.close() # by default we return the temp file file_stream = temp_zip # if save file specified, copy temp to the specified save file if save is not None: # only delete the specified save if temp was created temp_zip.close() shutil.move(temp_zip.name, save) # return an open file stream to the new file specified by save file_stream = open(save) # always ensure that stream starts at beginning file_stream.seek(0) return file_stream
def get_user_data(throw=True): if __user_data_singleton is None and throw: raise cli_exception.CliException( "You are'nt logged. Please use command \"neoload login\" first") return __user_data_singleton
def get_yaml_schema(throw=True): if __yaml_schema_singleton is None and throw: raise cli_exception.CliException( "No yaml schema found. Please add --refresh option to download it first" ) return __yaml_schema_singleton
def get_meta_required(key): if key not in get_user_data().metadata: raise cli_exception.CliException( 'No name or id provided. Please specify the object name or id.') return get_user_data().metadata.get(key)