def _validate_generic_container_options(container_options, force_options=False): validators = [ validate.NamesIn( GENERIC_CONTAINER_OPTIONS, option_type="container", **validate.set_warning(report_codes.FORCE_OPTIONS, force_options), ), validate.IsRequiredAll(["image"], option_type="container"), validate.ValueNotEmpty("image", "image name"), validate.ValueNonnegativeInteger("masters"), validate.ValueNonnegativeInteger("promoted-max"), validate.MutuallyExclusive( ["masters", "promoted-max"], option_type="container", ), validate.ValuePositiveInteger("replicas"), validate.ValuePositiveInteger("replicas-per-host"), ] deprecation_reports = [] if "masters" in container_options: deprecation_reports.append( ReportItem.warning( reports.messages.DeprecatedOption( "masters", ["promoted-max"], "container", ))) return (validate.ValidatorAll(validators).validate(container_options) + deprecation_reports)
def _validate_storage_map_list(options_list, id_provider, force_options): kwargs = validate.set_warning(report_codes.FORCE_OPTIONS, force_options) option_type = "storage-map" validators = [ validate.NamesIn(STORAGE_MAP_OPTIONS, option_type=option_type, **kwargs), validate.ValueId( "id", option_name_for_report="storage-map id", id_provider=id_provider, ), validate.IsRequiredSome( ["source-dir", "source-dir-root"], option_type=option_type, ), validate.MutuallyExclusive( ["source-dir", "source-dir-root"], option_type=option_type, ), validate.IsRequiredAll(["target-dir"], option_type=option_type), ] validator_all = validate.ValidatorAll(validators) report_list = [] for options in options_list: report_list.extend(validator_all.validate(options)) return report_list
def test_returns_no_report_when_required_is_present(self): assert_report_item_list_equal( validate.IsRequiredAll(["name"], "some type").validate( {"name": "monitor"} ), [], )
def validate_operation_list(operation_list, allowed_operation_name_list, allow_invalid=False): kwargs = validate.set_warning(report_codes.FORCE_OPTIONS, allow_invalid) option_type = "resource operation" validators = [ validate.NamesIn(ATTRIBUTES, option_type=option_type), validate.IsRequiredAll(["name"], option_type=option_type), validate.ValueIn( "name", allowed_operation_name_list, option_name_for_report="operation name", **kwargs, ), validate.ValueIn("role", RESOURCE_ROLES), validate.ValueIn("on-fail", ON_FAIL_VALUES), validate.ValueIn("record-pending", BOOLEAN_VALUES), validate.ValueIn("enabled", BOOLEAN_VALUES), validate.MutuallyExclusive(["interval-origin", "start-delay"], option_type=option_type), validate.ValueId("id", option_name_for_report="operation id"), ] validator_all = validate.ValidatorAll(validators) report_list = [] for operation in operation_list: report_list.extend(validator_all.validate(operation)) return report_list
def test_returns_report_when_required_is_missing(self): assert_report_item_list_equal( validate.IsRequiredAll(["name"], "some type").validate({}), [ fixture.error( report_codes.REQUIRED_OPTIONS_ARE_MISSING, option_names=["name"], option_type="some type", ), ])
def test_more_options(self): assert_report_item_list_equal( validate.IsRequiredAll(["name1", "name2", "name3"], "some type").validate({"name2": "value2"}), [ fixture.error( report_codes.REQUIRED_OPTIONS_ARE_MISSING, option_names=["name1", "name3"], option_type="some type", ), ])
def get_validators_required_parameters( self, force: bool = False, only_parameters: Optional[Iterable[str]] = None, ) -> List[validate.ValidatorInterface]: """ Return validators checking if required parameters were specified force -- if True, validators produce a warning instead of an error only_parameters -- if set, only specified parameters are checked """ validators: List[validate.ValidatorInterface] = [] severity = reports.item.get_severity(reports.codes.FORCE, force) only_parameters = only_parameters or set() required_not_obsoleting: Set[str] = set() all_params_deprecated_by = self._get_all_params_deprecated_by() for param in self.metadata.parameters: if not param.required or param.deprecated: continue deprecated_by_param = all_params_deprecated_by[param.name] if only_parameters and not ({param.name} | deprecated_by_param ).intersection(only_parameters): continue if deprecated_by_param: validators.append( validate.IsRequiredSome( {param.name} | deprecated_by_param, self._validator_option_type, deprecated_option_name_list=deprecated_by_param, severity=severity, )) else: required_not_obsoleting.add(param.name) if required_not_obsoleting: validators.append( validate.IsRequiredAll( required_not_obsoleting, self._validator_option_type, severity, )) return validators
def _validate_container(container_type, container_options, force_options=False): if not container_type in GENERIC_CONTAINER_TYPES: return [ reports.invalid_option_value( "container type", container_type, GENERIC_CONTAINER_TYPES, ) ] validators = [ validate.NamesIn( GENERIC_CONTAINER_OPTIONS, option_type="container", **validate.set_warning(report_codes.FORCE_OPTIONS, force_options) ), validate.IsRequiredAll(["image"], option_type="container"), validate.ValueNotEmpty("image", "image name"), validate.ValueNonnegativeInteger("masters"), validate.ValueNonnegativeInteger("promoted-max"), validate.MutuallyExclusive( ["masters", "promoted-max"], option_type="container", ), validate.ValuePositiveInteger("replicas"), validate.ValuePositiveInteger("replicas-per-host"), ] deprecation_reports = [] if "masters" in container_options: deprecation_reports.append( reports.deprecated_option( "masters", ["promoted-max"], "container", severity=ReportItemSeverity.WARNING ) ) return ( validate.ValidatorAll(validators).validate(container_options) + deprecation_reports )
def _validate_operation_list( operation_list, allowed_operation_name_list, allow_invalid=False ): severity = reports.item.get_severity(reports.codes.FORCE, allow_invalid) option_type = "resource operation" validators = [ validate.NamesIn(ATTRIBUTES, option_type=option_type), validate.IsRequiredAll(["name"], option_type=option_type), validate.ValueIn( "name", allowed_operation_name_list, option_name_for_report="operation name", severity=severity, ), validate.ValueIn("role", const.PCMK_ROLES), validate.ValueDeprecated( "role", { const.PCMK_ROLE_PROMOTED_LEGACY: const.PCMK_ROLE_PROMOTED, const.PCMK_ROLE_UNPROMOTED_LEGACY: const.PCMK_ROLE_UNPROMOTED, }, reports.ReportItemSeverity.deprecation(), ), validate.ValueIn("on-fail", ON_FAIL_VALUES), validate.ValueIn("record-pending", _BOOLEAN_VALUES), validate.ValueIn("enabled", _BOOLEAN_VALUES), validate.MutuallyExclusive( ["interval-origin", "start-delay"], option_type=option_type ), validate.ValueId("id", option_name_for_report="operation id"), ] validator_all = validate.ValidatorAll(validators) report_list = [] for operation in operation_list: report_list.extend(validator_all.validate(operation)) return report_list