def mock_patch_remote_spec_major_version(directory): final_name = "plugin.spec.remote.yaml" # if the "remote" spec exists, use that if os.path.exists(os.path.join(directory, final_name)): spec = KomandPluginSpec(directory, final_name) # otherwise, just use a copy of the existing spec else: spec = KomandPluginSpec(directory, "plugin.spec.yaml") spec_dict = spec.spec_dictionary() return spec_dict
def validate(self, spec: KomandPluginSpec): for required_key, message in self.missing_key_message.items(): if required_key not in spec.spec_dictionary(): RequiredKeysValidator.raise_exception(required_key, message) RequiredKeysValidator.validate_support(spec.spec_dictionary()) RequiredKeysValidator.validate_resources(spec.spec_dictionary()) RequiredKeysValidator.validate_extension(spec.spec_dictionary()) RequiredKeysValidator.validate_products(spec.spec_dictionary()) RequiredKeysValidator.validate_hub_tags(spec.spec_dictionary()) RequiredKeysValidator.validate_existing_resources( spec.spec_dictionary())
def validate_keywords_exists(spec: KomandPluginSpec): try: keywords = spec.spec_dictionary()["hub_tags"]["keywords"] if not keywords or not len(keywords): raise ValidationException("Empty required field 'keywords' in key 'hub_tags'.") except KeyError: raise ValidationException("Missing required field 'keywords' in key 'hub_tags'.")
def validate(self, spec: KomandPluginSpec): try: result = UseCaseValidator.validate_use_cases(spec.spec_dictionary()["hub_tags"]["use_cases"]) if len(result): err = ", ".join(result) raise ValidationException(f"Invalid use cases: {err}.") except KeyError: raise ValidationException("Missing required field 'use_cases' in key 'hub_tags'.")
def validate_use_case_exists(spec: KomandPluginSpec): try: use_cases = spec.spec_dictionary()["hub_tags"]["use_cases"] if not use_cases: raise ValidationException( "Empty required field 'use_cases' in key 'hub_tags'.") except KeyError: raise ValidationException( "Missing required field 'use_cases' in key 'hub_tags'.")
def validate( directory, spec_file_name="plugin.spec.yaml", fail_fast=False, run_all=False, validators=list(), ): spec = KomandPluginSpec(directory, spec_file_name) status = 0 # Resultant return code start_time = time_now() print(f"{BULLET_OK} {BOLD}Running Integration Validators...{CEND}") if not validators: if spec_file_name == "plugin.spec.yaml": validators = VALIDATORS if run_all: validators += JENKINS_VALIDATORS elif spec_file_name == "workflow.spec.yaml": validators = WORKFLOW_VALIDATORS validation_failures: [str] = [] for v in validators: print(f"{BULLET_OK} Executing validator {v.name}") try: v.validate(spec) success = True except ValidationException as e: validation_failures.append( f'Validator "{v.name}" failed! \n\tCause: {e}') # print(f"Validator \"{v.name}\" failed! \n\tCause: {e}") status = 1 success = False if not success and fail_fast: break end_time = time_now() time_elapsed = format_time(start=start_time, end=end_time) extension = spec_file_name.split(".")[0].capitalize() if status == 0: print(f"{BULLET_OK} {BOLD}{extension} successfully validated!{CEND}") else: print( f"{BULLET_FAIL} {extension} failed validation! The following validation errors occurred:\n" ) for vf in validation_failures: print(f"{vf}\n") print( f"\n----\n{BULLET_OK}{BOLD} Total time elapsed: {time_elapsed}ms{CEND}" ) return status
def validate(self, spec: KomandPluginSpec): plugin_spec = spec.spec_dictionary() action_messages = self.get_all_empty_example_message( plugin_spec.get("actions", {}), "action", "input") trigger_messages = self.get_all_empty_example_message( plugin_spec.get("triggers", {}), "trigger", "input") connection_messages = self.get_empty_example_message( plugin_spec.get("connection", {}), "connection") all_offenses = "" if action_messages: all_offenses += "\n" + "\n".join(action_messages) if trigger_messages: all_offenses += "\n" + "\n".join(trigger_messages) if connection_messages: all_offenses += "\n" + "\n".join(connection_messages) if all_offenses: raise ValidationException( f"All inputs should have example field in plugin.spec. {all_offenses}" )
def validate(self, spec: KomandPluginSpec): WorkflowUseCaseValidator.validate_use_case_exists(spec) WorkflowUseCaseValidator.validate_use_cases( spec.spec_dictionary()["hub_tags"]["use_cases"]) WorkflowUseCaseValidator.validate_use_case_in_keywords( spec.spec_dictionary()["hub_tags"].get("keywords"))
def validate(self, spec: KomandPluginSpec): raw_connection, raw_actions, raw_triggers, raw_tasks = spec.connection(), spec.actions(), \ spec.triggers(), spec.tasks() # Get components connection: PluginComponent = PluginComponent.new_connection( raw=raw_connection) actions: [PluginComponent] = \ [PluginComponent.new_action(raw={k: v}) for k, v in raw_actions.items()] if raw_actions else [] triggers: [PluginComponent] = \ [PluginComponent.new_trigger(raw={k: v}) for k, v in raw_triggers.items()] if raw_triggers else [] tasks: [PluginComponent] = \ [PluginComponent.new_task(raw={k: v}) for k, v in raw_tasks.items()] if raw_tasks else [] for input_ in connection.inputs: diff: {str} = input_.raw_parameters.difference( self._PROPERTIES_WHITELIST) if diff: self._add_property_offense_string( component=connection.identifier, identifier=input_.identifier, offenders=diff) for action in actions: diff: {str} = action.raw_parameters.difference( self._COMPONENT_WHITELIST) if diff: self._add_component_offense_string(component=action.identifier, offenders=diff) for input_ in action.inputs: diff: {str} = input_.raw_parameters.difference( self._PROPERTIES_WHITELIST) if diff: self._add_property_offense_string( component=action.identifier, identifier=input_.identifier, offenders=diff) for output in action.outputs: diff: {str} = output.raw_parameters.difference( self._PROPERTIES_WHITELIST) if diff: self._add_property_offense_string( component=action.identifier, identifier=output.identifier, offenders=diff) for trigger in triggers: diff: {str} = trigger.raw_parameters.difference( self._COMPONENT_WHITELIST) if diff: self._add_component_offense_string( component=trigger.identifier, offenders=diff) for input_ in trigger.inputs: diff: {str} = input_.raw_parameters.difference( self._PROPERTIES_WHITELIST) if diff: self._add_property_offense_string( component=trigger.identifier, identifier=input_.identifier, offenders=diff) for output in trigger.outputs: diff: {str} = output.raw_parameters.difference( self._PROPERTIES_WHITELIST) if diff: self._add_property_offense_string( component=trigger.identifier, identifier=output.identifier, offenders=diff) for task in tasks: # check for "state" and "schedule" in task diff: {str} = self._COMPONENT_TASK_WHITELIST.difference( task.raw_parameters) if "state" in diff or "schedule" in diff: self._add_component_task_missing_state_schedule_offense_string( component=task.identifier, offenders=diff) diff: {str} = task.raw_parameters.difference( self._COMPONENT_TASK_WHITELIST) if diff: self._add_component_task_offense_string( component=task.identifier, offenders=diff) for input_ in task.inputs: diff: {str} = input_.raw_parameters.difference( self._PROPERTIES_WHITELIST) if diff: self._add_property_offense_string( component=task.identifier, identifier=input_.identifier, offenders=diff) for output in task.outputs: diff: {str} = output.raw_parameters.difference( self._TASK_OUTPUT_PROPERTY_WHITELIST) if diff: self._add_property_offense_string( component=task.identifier, identifier=output.identifier, offenders=diff) for state in task.state: diff: {str} = state.raw_parameters.difference( self._TASK_STATE_PROPERTY_WHITELIST) if diff: self._add_task_state_property_offense_string( component=task.identifier, identifier=state.identifier, offenders=diff) if task.schedule: diff: {str} = task.schedule[0].raw_parameters.difference( self._TASK_SCHEDULE_PROPERTY_WHITELIST) if diff: self._add_task_schedule_property_offense_string( component=task.identifier, identifier=task.schedule[0].identifier, offenders=diff) # Create a string of all offenses all_offenses: str = "" if self.component_offenses: all_offenses += "The follow invalid component properties were found:\n" + \ "\n".join(self.component_offenses) + "\n\n" if self.property_offenses: all_offenses += "The following invalid input/output properties were found:\n" + \ "\n".join(self.property_offenses) + "\n\n" if self.component_task_offenses: all_offenses += "The following invalid component properties for task were found:\n" + \ "\n".join(self.component_task_offenses) + "\n\n" if self.task_state_property_offenses: all_offenses += "The following invalid task's state properties were found:\n" + \ "\n".join(self.task_state_property_offenses) + "\n\n" if self.task_schedule_property_offenses: all_offenses += "The following invalid task's schedule properties were found:\n" + \ "\n".join(self.task_schedule_property_offenses) + "\n\n" if self.component_task_missing_state_schedule_offenses: all_offenses += "The following tasks are missing properties:\n" + \ "\n".join(self.component_task_missing_state_schedule_offenses) + "\n\n" # If there is an offense string at all (indicative of an offense), raise an Exception if all_offenses: raise ValidationException(all_offenses)
def validate(self, spec: KomandPluginSpec): UnapprovedKeywordsValidator.validate_keywords_exists(spec) UnapprovedKeywordsValidator.validate_keywords(spec.spec_dictionary()["hub_tags"]["keywords"])