def mock_structure(file_path=None, current_file=None, old_file=None): with patch.object(StructureValidator, '__init__', lambda a, b: None): structure = StructureValidator(file_path) structure.is_valid = True structure.scheme_name = 'classifier' structure.file_path = file_path structure.current_file = current_file structure.old_file = old_file structure.prev_ver = 'master' structure.branch_name = '' structure.quiet_bc = False return structure
def test_is_valid_content_flag_sanity(self, current_file, answer): with patch.object(StructureValidator, '__init__', lambda a, b: None): structure = StructureValidator("") structure.current_file = current_file structure.old_file = None structure.file_path = "random_path" structure.is_valid = True structure.prev_ver = 'master' structure.branch_name = '' validator = IncidentFieldValidator(structure) validator.current_file = current_file assert validator.is_valid_content_flag() is answer
def mock_structure(file_path=None, current_file=None, old_file=None): # type: (Optional[str], Optional[dict], Optional[dict]) -> StructureValidator with patch.object(StructureValidator, '__init__', lambda a, b: None): structure = StructureValidator(file_path) structure.is_valid = True structure.scheme_name = 'dashboard' structure.file_path = file_path structure.current_file = current_file structure.old_file = old_file structure.prev_ver = 'master' structure.branch_name = '' return structure
def test_is_valid_system_flag_sanity(self, current_file, answer): with patch.object(StructureValidator, '__init__', lambda a, b: None): structure = StructureValidator("") structure.current_file = current_file structure.old_file = None structure.file_path = "random_path" structure.is_valid = True structure.prev_ver = 'master' structure.branch_name = '' structure.specific_validations = None validator = FieldBaseValidator(structure, set(), set()) validator.current_file = current_file assert validator.is_valid_system_flag() is answer
def test_is_cliname_is_builtin_key_invalid(self, cliname, group): with patch.object(StructureValidator, '__init__', lambda a, b: None): current_file = {"cliName": cliname, "group": group} structure = StructureValidator("") structure.current_file = current_file structure.old_file = None structure.file_path = "random_path" structure.is_valid = True structure.prev_ver = 'master' structure.branch_name = '' validator = IncidentFieldValidator(structure) validator.current_file = current_file assert not validator.is_cliname_is_builtin_key()
def test_is_valid_cli_name_invalid(self, cliname, group): current_file = {"cliName": cliname, "group": group} with patch.object(StructureValidator, '__init__', lambda a, b: None): structure = StructureValidator("") structure.current_file = current_file structure.old_file = None structure.file_path = "random_path" structure.is_valid = True structure.prev_ver = 'master' structure.branch_name = '' validator = FieldBaseValidator(structure, set(), {'id'}) validator.current_file = current_file assert not validator.is_valid_cli_name()
def get_validator(current_file=None, old_file=None, file_path=""): with patch.object(StructureValidator, '__init__', lambda a, b: None): structure = StructureValidator("") structure.current_file = current_file structure.old_file = old_file structure.file_path = file_path structure.is_valid = True structure.prev_ver = 'master' structure.branch_name = '' validator = ScriptValidator(structure) validator.old_script = old_file validator.current_script = current_file return validator
def test_matching_cliname_regex_invalid(self, cliname): with patch.object(StructureValidator, '__init__', lambda a, b: None): current_file = {"cliName": cliname} structure = StructureValidator("") structure.current_file = current_file structure.old_file = None structure.file_path = "random_path" structure.is_valid = True structure.prev_ver = 'master' structure.branch_name = '' validator = FieldBaseValidator(structure, set(), set()) validator.current_file = current_file assert not validator.is_matching_cli_name_regex()
def get_validator(current_file=None, old_file=None, file_path="Packs/exists"): with patch.object(StructureValidator, '__init__', lambda a, b: None): structure = StructureValidator("") structure.current_file = current_file structure.old_file = old_file structure.file_path = file_path structure.is_valid = True structure.prev_ver = 'master' structure.branch_name = '' structure.quiet_bc = False structure.specific_validations = None validator = WizardValidator(structure) validator.current_file = current_file return validator
def mock_structure(file_path=None, current_file=None, old_file=None): with patch.object(StructureValidator, '__init__', lambda a, b: None): structure = StructureValidator(file_path) structure.is_valid = True structure.scheme_name = 'list' structure.file_path = file_path file = open(file_path, "r") structure.current_file = json.loads(file.read()) file.close() structure.old_file = old_file structure.prev_ver = 'master' structure.branch_name = '' structure.quiet_bc = False return structure
def test_is_valid_cliname(self, _id, cliname, group): current_file = {'id': _id, 'cliName': cliname, 'group': group} with patch.object(StructureValidator, '__init__', lambda a, b: None): structure = StructureValidator("") structure.current_file = current_file structure.old_file = None structure.file_path = "random_path" structure.is_valid = True structure.prev_ver = 'master' structure.branch_name = '' structure.specific_validations = None validator = FieldBaseValidator(structure, set(), set()) validator.current_file = current_file assert validator.is_valid_cli_name()
def test_is_cliname_is_builtin_key(self, cliname, group): with patch.object(StructureValidator, '__init__', lambda a, b: None): current_file = {"cliName": cliname, "group": group} structure = StructureValidator("") structure.current_file = current_file structure.old_file = None structure.file_path = "random_path" structure.is_valid = True structure.prev_ver = 'master' structure.branch_name = '' structure.specific_validations = None validator = FieldBaseValidator(structure, set(), set()) validator.current_file = current_file assert validator.is_cli_name_is_builtin_key()
def mock_structure(file_path=None, current_file=None, old_file=None): with patch.object(StructureValidator, '__init__', lambda a, b: None): structure = StructureValidator(file_path) structure.is_valid = True structure.scheme_name = 'genericfield' structure.file_path = file_path structure.current_file = current_file structure.old_file = old_file structure.prev_ver = 'master' structure.branch_name = '' structure.quiet_bc = False structure.skip_schema_check = False structure.pykwalify_logs = False structure.scheme_name = namedtuple('scheme_name', 'value')(value='genericfield') structure.checked_files = set() structure.ignored_errors = dict() structure.suppress_print = True structure.json_file_path = None return structure
def test_is_valid_name_sanity(self, current_file, answer): import sys import os with patch.object(StructureValidator, '__init__', lambda a, b: None): structure = StructureValidator("") structure.current_file = current_file structure.old_file = None structure.file_path = "random_path" structure.is_valid = True validator = IncidentFieldValidator(structure) validator.current_file = current_file with open("file", 'w') as temp_out: old_stdout = sys.stdout sys.stdout = temp_out validator.is_valid_name() sys.stdout = old_stdout with open('file', 'r') as temp_out: output = temp_out.read() assert ('IF100' in str(output)) is answer # remove the temp file os.system('rm -rf file')
def validate_added_files(self, added_files, file_type: str = None): # noqa: C901 """Validate the added files from your branch. In case we encounter an invalid file we set the self._is_valid param to False. Args: added_files (set): A set of the modified files in the current branch. file_type (str): Used only with -p flag (the type of the file). """ for file_path in added_files: print('Validating {}'.format(file_path)) if re.match(TEST_PLAYBOOK_REGEX, file_path, re.IGNORECASE) and not file_type: continue elif 'README' in file_path: readme_validator = ReadMeValidator(file_path) if not readme_validator.is_valid_file(): self._is_valid = False continue structure_validator = StructureValidator( file_path, is_new_file=True, predefined_scheme=file_type) if not structure_validator.is_valid_file(): self._is_valid = False if self.validate_id_set: if not self.id_set_validator.is_file_valid_in_set(file_path): self._is_valid = False if self.id_set_validator.is_file_has_used_id(file_path): self._is_valid = False elif re.match(PLAYBOOK_REGEX, file_path, re.IGNORECASE) or file_type == 'playbook': playbook_validator = PlaybookValidator(structure_validator) if not playbook_validator.is_valid_playbook(): self._is_valid = False elif checked_type( file_path, YML_INTEGRATION_REGEXES) or file_type == 'integration': image_validator = ImageValidator(file_path) # if file_type(non git path) the image is not in a separate path image_validator.file_path = file_path if file_type else image_validator.file_path if not image_validator.is_valid(): self._is_valid = False description_validator = DescriptionValidator(file_path) if not description_validator.is_valid(): self._is_valid = False integration_validator = IntegrationValidator( structure_validator) if not integration_validator.is_valid_file( validate_rn=not file_type): self._is_valid = False elif checked_type( file_path, PACKAGE_SCRIPTS_REGEXES) or file_type == 'script': unifier = Unifier(os.path.dirname(file_path)) yml_path, _ = unifier.get_script_package_data() # Set file path to the yml file structure_validator.file_path = yml_path script_validator = ScriptValidator(structure_validator) if not script_validator.is_valid_file( validate_rn=not file_type): self._is_valid = False elif re.match(BETA_INTEGRATION_REGEX, file_path, re.IGNORECASE) or \ re.match(BETA_INTEGRATION_YML_REGEX, file_path, re.IGNORECASE): description_validator = DescriptionValidator(file_path) if not description_validator.is_valid_beta_description(): self._is_valid = False integration_validator = IntegrationValidator( structure_validator) if not integration_validator.is_valid_beta_integration(): self._is_valid = False elif re.match(IMAGE_REGEX, file_path, re.IGNORECASE): image_validator = ImageValidator(file_path) if not image_validator.is_valid(): self._is_valid = False # incident fields and indicator fields are using the same scheme. elif checked_type(file_path, JSON_INDICATOR_AND_INCIDENT_FIELDS) or \ file_type in ('incidentfield', 'indicatorfield'): incident_field_validator = IncidentFieldValidator( structure_validator) if not incident_field_validator.is_valid_file( validate_rn=not file_type): self._is_valid = False elif checked_type(file_path, [REPUTATION_REGEX]) or file_type == 'reputation': reputation_validator = ReputationValidator(structure_validator) if not reputation_validator.is_valid_file( validate_rn=not file_type): self._is_valid = False elif checked_type( file_path, JSON_ALL_LAYOUT_REGEXES) or file_type == 'layout': layout_validator = LayoutValidator(structure_validator) if not layout_validator.is_valid_layout( validate_rn=not file_type): self._is_valid = False elif checked_type( file_path, JSON_ALL_DASHBOARDS_REGEXES) or file_type == 'dashboard': dashboard_validator = DashboardValidator(structure_validator) if not dashboard_validator.is_valid_dashboard( validate_rn=not file_type): self._is_valid = False elif checked_type(file_path, JSON_ALL_INCIDENT_TYPES_REGEXES): incident_type_validator = IncidentTypeValidator( structure_validator) if not incident_type_validator.is_valid_incident_type( validate_rn=not file_type): self._is_valid = False elif 'CHANGELOG' in file_path: self.is_valid_release_notes(file_path) elif checked_type(file_path, CHECKED_TYPES_REGEXES): pass else: print_error( "The file type of {} is not supported in validate command". format(file_path)) print_error( "validate command supports: Integrations, Scripts, Playbooks, " "Incident fields, Indicator fields, Images, Release notes, Layouts and Descriptions" ) self._is_valid = False
def validate_modified_files(self, modified_files): # noqa: C901 """Validate the modified files from your branch. In case we encounter an invalid file we set the self._is_valid param to False. Args: modified_files (set): A set of the modified files in the current branch. """ for file_path in modified_files: old_file_path = None if isinstance(file_path, tuple): old_file_path, file_path = file_path print('Validating {}'.format(file_path)) if not checked_type(file_path): print_warning( '- Skipping validation of non-content entity file.') continue if re.match(TEST_PLAYBOOK_REGEX, file_path, re.IGNORECASE): continue elif 'README' in file_path: readme_validator = ReadMeValidator(file_path) if not readme_validator.is_valid_file(): self._is_valid = False continue structure_validator = StructureValidator( file_path, old_file_path=old_file_path) if not structure_validator.is_valid_file(): self._is_valid = False if self.validate_id_set: if not self.id_set_validator.is_file_valid_in_set(file_path): self._is_valid = False elif checked_type(file_path, YML_INTEGRATION_REGEXES): image_validator = ImageValidator(file_path) if not image_validator.is_valid(): self._is_valid = False description_validator = DescriptionValidator(file_path) if not description_validator.is_valid(): self._is_valid = False integration_validator = IntegrationValidator( structure_validator) if self.is_backward_check and not integration_validator.is_backward_compatible( ): self._is_valid = False if not integration_validator.is_valid_file(): self._is_valid = False elif checked_type(file_path, YML_BETA_INTEGRATIONS_REGEXES): image_validator = ImageValidator(file_path) if not image_validator.is_valid(): self._is_valid = False description_validator = DescriptionValidator(file_path) if not description_validator.is_valid_beta_description(): self._is_valid = False integration_validator = IntegrationValidator( structure_validator) if not integration_validator.is_valid_beta_integration(): self._is_valid = False elif checked_type(file_path, [SCRIPT_REGEX]): script_validator = ScriptValidator(structure_validator) if self.is_backward_check and not script_validator.is_backward_compatible( ): self._is_valid = False if not script_validator.is_valid_file(): self._is_valid = False elif checked_type(file_path, PLAYBOOKS_REGEXES_LIST): playbook_validator = PlaybookValidator(structure_validator) if not playbook_validator.is_valid_playbook( is_new_playbook=False): self._is_valid = False elif checked_type(file_path, PACKAGE_SCRIPTS_REGEXES): unifier = Unifier(os.path.dirname(file_path)) yml_path, _ = unifier.get_script_package_data() # Set file path to the yml file structure_validator.file_path = yml_path script_validator = ScriptValidator(structure_validator) if self.is_backward_check and not script_validator.is_backward_compatible( ): self._is_valid = False if not script_validator.is_valid_file(): self._is_valid = False elif re.match(IMAGE_REGEX, file_path, re.IGNORECASE): image_validator = ImageValidator(file_path) if not image_validator.is_valid(): self._is_valid = False # incident fields and indicator fields are using the same scheme. elif checked_type(file_path, JSON_INDICATOR_AND_INCIDENT_FIELDS): incident_field_validator = IncidentFieldValidator( structure_validator) if not incident_field_validator.is_valid_file( validate_rn=True): self._is_valid = False if self.is_backward_check and not incident_field_validator.is_backward_compatible( ): self._is_valid = False elif checked_type(file_path, [REPUTATION_REGEX]): reputation_validator = ReputationValidator(structure_validator) if not reputation_validator.is_valid_file(validate_rn=True): self._is_valid = False elif checked_type(file_path, JSON_ALL_LAYOUT_REGEXES): layout_validator = LayoutValidator(structure_validator) if not layout_validator.is_valid_layout(validate_rn=True): self._is_valid = False elif checked_type(file_path, JSON_ALL_DASHBOARDS_REGEXES): dashboard_validator = DashboardValidator(structure_validator) if not dashboard_validator.is_valid_dashboard( validate_rn=True): self._is_valid = False elif checked_type(file_path, JSON_ALL_INCIDENT_TYPES_REGEXES): incident_type_validator = IncidentTypeValidator( structure_validator) if not incident_type_validator.is_valid_incident_type( validate_rn=True): self._is_valid = False if self.is_backward_check and not incident_type_validator.is_backward_compatible( ): self._is_valid = False elif 'CHANGELOG' in file_path: self.is_valid_release_notes(file_path) elif checked_type(file_path, CHECKED_TYPES_REGEXES): pass else: print_error( "The file type of {} is not supported in validate command". format(file_path)) print_error( "'validate' command supports: Integrations, Scripts, Playbooks, " "Incident fields, Indicator fields, Images, Release notes, Layouts and Descriptions" ) self._is_valid = False
def validate_added_files(self, added_files): # noqa: C901 """Validate the added files from your branch. In case we encounter an invalid file we set the self._is_valid param to False. Args: added_files (set): A set of the modified files in the current branch. """ for file_path in added_files: print('Validating {}'.format(file_path)) if re.match(TEST_PLAYBOOK_REGEX, file_path, re.IGNORECASE): continue structure_validator = StructureValidator(file_path, is_new_file=True) if not structure_validator.is_valid_file(): self._is_valid = False if self.validate_id_set: if not self.id_set_validator.is_file_valid_in_set(file_path): self._is_valid = False if self.id_set_validator.is_file_has_used_id(file_path): self._is_valid = False elif re.match(PLAYBOOK_REGEX, file_path, re.IGNORECASE): playbook_validator = PlaybookValidator(structure_validator) if not playbook_validator.is_valid_playbook(): self._is_valid = False elif checked_type(file_path, YML_INTEGRATION_REGEXES): image_validator = ImageValidator(file_path) if not image_validator.is_valid(): self._is_valid = False description_validator = DescriptionValidator(file_path) if not description_validator.is_valid(): self._is_valid = False integration_validator = IntegrationValidator(structure_validator) if not integration_validator.is_valid_file(validate_rn=False): self._is_valid = False elif checked_type(file_path, PACKAGE_SCRIPTS_REGEXES): unifier = Unifier(os.path.dirname(file_path)) yml_path, _ = unifier.get_script_package_data() # Set file path to the yml file structure_validator.file_path = yml_path script_validator = ScriptValidator(structure_validator) if not script_validator.is_valid_file(validate_rn=False): self._is_valid = False elif re.match(BETA_INTEGRATION_REGEX, file_path, re.IGNORECASE) or \ re.match(BETA_INTEGRATION_YML_REGEX, file_path, re.IGNORECASE): description_validator = DescriptionValidator(file_path) if not description_validator.is_valid_beta_description(): self._is_valid = False integration_validator = IntegrationValidator(structure_validator) if not integration_validator.is_valid_beta_integration(): self._is_valid = False elif re.match(IMAGE_REGEX, file_path, re.IGNORECASE): image_validator = ImageValidator(file_path) if not image_validator.is_valid(): self._is_valid = False # incident fields and indicator fields are using the same scheme. elif checked_type(file_path, JSON_INDICATOR_AND_INCIDENT_FIELDS): incident_field_validator = IncidentFieldValidator(structure_validator) if not incident_field_validator.is_valid_file(): self._is_valid = False elif checked_type(file_path, JSON_ALL_LAYOUT_REGEXES): layout_validator = LayoutValidator(structure_validator) if not layout_validator.is_valid_layout(): self._is_valid = False elif 'CHANGELOG' in file_path: self.is_valid_release_notes(file_path) elif checked_type(file_path, [REPUTATION_REGEX]): print_color( F'Skipping validation for file {file_path} since no validation is currently defined.', LOG_COLORS.YELLOW) elif checked_type(file_path, CHECKED_TYPES_REGEXES): pass else: print_error("The file type of {} is not supported in validate command".format(file_path)) print_error("validate command supports: Integrations, Scripts, Playbooks, " "Incident fields, Indicator fields, Images, Release notes, Layouts and Descriptions") self._is_valid = False
def run_all_validations_on_file(self, file_path: str, file_type: str = None) -> None: """ Runs all validations on file specified in 'file_path' Args: file_path: A relative content path to a file to be validated file_type: The output of 'find_type' method """ file_extension = os.path.splitext(file_path)[-1] # We validate only yml json and .md files if file_extension not in ['.yml', '.json', '.md']: return # Ignoring changelog and description files since these are checked on the integration validation if 'changelog' in file_path.lower( ) or 'description' in file_path.lower(): return # unified files should not be validated if file_path.endswith('_unified.yml'): return print(f'Validating {file_path}') if 'README' in file_path: readme_validator = ReadMeValidator(file_path) if not readme_validator.is_valid_file(): self._is_valid = False return structure_validator = StructureValidator(file_path, predefined_scheme=file_type) if not structure_validator.is_valid_file(): self._is_valid = False elif re.match(TEST_PLAYBOOK_REGEX, file_path, re.IGNORECASE): playbook_validator = PlaybookValidator(structure_validator) if not playbook_validator.is_valid_playbook(): self._is_valid = False elif re.match(PLAYBOOK_REGEX, file_path, re.IGNORECASE) or file_type == 'playbook': playbook_validator = PlaybookValidator(structure_validator) if not playbook_validator.is_valid_playbook(validate_rn=False): self._is_valid = False elif checked_type(file_path, INTEGRATION_REGXES) or file_type == 'integration': integration_validator = IntegrationValidator(structure_validator) if not integration_validator.is_valid_file(validate_rn=False): self._is_valid = False elif checked_type(file_path, YML_ALL_SCRIPTS_REGEXES) or file_type == 'script': # Set file path to the yml file structure_validator.file_path = file_path script_validator = ScriptValidator(structure_validator) if not script_validator.is_valid_file(validate_rn=False): self._is_valid = False elif checked_type(file_path, YML_BETA_INTEGRATIONS_REGEXES ) or file_type == 'betaintegration': integration_validator = IntegrationValidator(structure_validator) if not integration_validator.is_valid_beta_integration(): self._is_valid = False # incident fields and indicator fields are using the same scheme. elif checked_type(file_path, JSON_INDICATOR_AND_INCIDENT_FIELDS) or \ file_type in ('incidentfield', 'indicatorfield'): incident_field_validator = IncidentFieldValidator( structure_validator) if not incident_field_validator.is_valid_file(validate_rn=False): self._is_valid = False elif checked_type( file_path, JSON_ALL_INDICATOR_TYPES_REGEXES) or file_type == 'reputation': reputation_validator = ReputationValidator(structure_validator) if not reputation_validator.is_valid_file(validate_rn=False): self._is_valid = False elif checked_type(file_path, JSON_ALL_LAYOUT_REGEXES) or file_type == 'layout': layout_validator = LayoutValidator(structure_validator) if not layout_validator.is_valid_layout(validate_rn=False): self._is_valid = False elif checked_type( file_path, JSON_ALL_DASHBOARDS_REGEXES) or file_type == 'dashboard': dashboard_validator = DashboardValidator(structure_validator) if not dashboard_validator.is_valid_dashboard(validate_rn=False): self._is_valid = False elif checked_type(file_path, JSON_ALL_INCIDENT_TYPES_REGEXES ) or file_type == 'incidenttype': incident_type_validator = IncidentTypeValidator( structure_validator) if not incident_type_validator.is_valid_incident_type( validate_rn=False): self._is_valid = False elif checked_type(file_path, CHECKED_TYPES_REGEXES): print(f'Could not find validations for file {file_path}') else: print_error( 'The file type of {} is not supported in validate command'. format(file_path)) print_error( 'validate command supports: Integrations, Scripts, Playbooks, dashboards, incident types, ' 'reputations, Incident fields, Indicator fields, Images, Release notes, ' 'Layouts and Descriptions') self._is_valid = False