def test_when_config_is_empty_json_then_schema_requires_monitors( config_json_schema): empty_config = read_json_from_file(EMPTY_JSON_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(empty_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "'monitors' is a required property", [])
def test_when_config_contains_metric_with_no_name_then_schema_requires_metric_name( config_json_schema): no_metric_name_config = read_json_from_file(NO_METRIC_NAME_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(no_metric_name_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "'name' is a required property", ['monitors', 0, 'metric'])
def test_when_config_contains_empty_monitor_then_schema_requires_monitor_name( config_json_schema): empty_monitor_config = read_json_from_file(EMPTY_MONITOR_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(empty_monitor_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "'name' is a required property", ['monitors', 0])
def test_when_config_contains_wrong_monitor_type_then_schema_requires_object_monitor( config_json_schema): wrong_monitor_type_config = read_json_from_file( WRONG_MONITOR_TYPE_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(wrong_monitor_type_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "'' is not of type 'object'", ['monitors', 0])
def test_when_config_monitors_is_not_array_then_schema_requires_array( config_json_schema): monitors_not_array_config = read_json_from_file( MONITORS_NOT_ARRAY_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(monitors_not_array_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "is not of type 'array'", ['monitors'])
def test_when_config_threshold_value_not_string_then_schema_requires_string( config_json_schema): threshold_value_not_string_config = read_json_from_file( THRESHOLD_VALUE_NOT_STRING_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(threshold_value_not_string_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "is not of type 'string'", ['monitors', 0, 'threshold', 'value'])
def test_when_config_threshold_clear_points_not_integer_then_schema_requires_integer( config_json_schema): threshold_clear_points_not_integer_config = read_json_from_file( THRESHOLD_CLEAR_POINTS_NOT_INTEGER_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(threshold_clear_points_not_integer_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "is not of type 'integer'", ['monitors', 0, 'threshold', 'clear_points'])
def test_when_config_contains_monitor_with_no_notifiers_then_schema_requires_monitor_notifiers( config_json_schema): no_monitor_notifiers_config = read_json_from_file( NO_MONITOR_NOTIFIERS_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(no_monitor_notifiers_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "'notifiers' is a required property", ['monitors', 0])
def test_when_config_contains_monitor_with_no_interval_then_schema_requires_monitor_interval( config_json_schema): no_monitor_interval_config = read_json_from_file( NO_MONITOR_INTERVAL_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(no_monitor_interval_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "'interval' is a required property", ['monitors', 0])
def test_when_config_metric_name_not_string_then_schema_requires_string( config_json_schema): metric_name_not_string_config = read_json_from_file( METRIC_NAME_NOT_STRING_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(metric_name_not_string_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "is not of type 'string'", ['monitors', 0, 'metric', 'name'])
def test_when_config_metric_configuration_not_object_then_schema_requires_string( config_json_schema): metric_configuration_not_object_config = read_json_from_file( METRIC_CONFIGURATION_NOT_OBJECT_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(metric_configuration_not_object_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "is not of type 'object'", ['monitors', 0, 'metric', 'configuration'])
def test_when_config_monitor_interval_not_number_then_schema_requires_number( config_json_schema): monitor_interval_not_number_config = read_json_from_file( MONITOR_INTERVAL_NOT_NUMBER_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(monitor_interval_not_number_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "is not of type 'number'", ['monitors', 0, 'interval'])
def test_when_config_monitor_alarm_not_object_then_schema_requires_object( config_json_schema): monitor_alarm_not_object_config = read_json_from_file( MONITOR_ALARM_NOT_OBJECT_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(monitor_alarm_not_object_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "is not of type 'object'", ['monitors', 0, 'notifiers', 'monitor_alarm'])
def test_when_config_alarm_failure_not_string_then_schema_requires_string( config_json_schema): alarm_failure_not_string_config = read_json_from_file( FAILURE_NAME_NOT_STRING_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(alarm_failure_not_string_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "is not of type 'string'", ['monitors', 0, 'notifiers', 'monitor_failure', 'name'])
def test_when_config_failure_configuration_not_object_then_schema_requires_object( config_json_schema): failure_configuration_not_object_config = read_json_from_file( FAILURE_CONFIGURATION_NOT_OBJECT_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(failure_configuration_not_object_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "is not of type 'object'", ['monitors', 0, 'notifiers', 'monitor_failure', 'configuration'])
def test_when_config_threshold_not_object_then_schema_requires_object( config_json_schema): threshold_not_object_config = read_json_from_file( THRESHOLD_NOT_OBJECT_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(threshold_not_object_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "is not of type 'object'", ['monitors', 0, 'threshold'])
def test_when_config_contains_monitor_failure_with_no_name_then_schema_requires_name( config_json_schema): no_monitor_failure_name_config = read_json_from_file( NO_MONITOR_FAILURE_NAME_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(no_monitor_failure_name_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "'name' is a required property", ['monitors', 0, 'notifiers', 'monitor_failure'])
def test_when_config_contains_monitor_with_no_threshold_then_schema_requires_monitor_threshold( config_json_schema): no_monitor_threshold_config = read_json_from_file( NO_MONITOR_THRESHOLD_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(no_monitor_threshold_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "'threshold' is a required property", ['monitors', 0])
def test_when_config_monitor_description_not_string_then_schema_requires_string( config_json_schema): monitor_description_not_string_config = read_json_from_file( MONITOR_DESCRIPTION_NOT_STRING_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(monitor_description_not_string_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "is not of type 'string'", ['monitors', 0, 'description'])
def test_when_config_contains_threshold_with_no_clear_points_then_schema_requires_clear_points( config_json_schema): no_threshold_clear_points_config = read_json_from_file( NO_THRESHOLD_CLEAR_POINTS_CONFIG_FILE) exception_validator = RunnableExceptionValidator( lambda: jsonschema.validate(no_threshold_clear_points_config, config_json_schema)) exception_validator.verify_json_schema_exception( jsonschema.exceptions.ValidationError, "'clear_points' is a required property", ['monitors', 0, 'threshold'])
def test_when_config_monitor_invalid_then_exception(): config = Config(click=click, jsonschema=jsonschema) expected_monitor_config = read_json_from_file( NEGATIVE_ALARM_POINTS_CONFIG_FILE_NAME)['monitors'][0] exception_validator = RunnableExceptionValidator( lambda: config.from_cli_file_path_param( config_file_path=NEGATIVE_ALARM_POINTS_CONFIG_FILE_NAME)) exception_validator.verify_exception( click.BadParameter, f"Error initializing monitor from configuration:\n" f"{json.dumps(expected_monitor_config)}\n" f"The threshold alarm points must be a positive integer, but got '-5'!" )
def from_cli_file_path_param(self, cli_context=None, param_name=None, config_file_path=None): if not os.path.isfile(config_file_path): raise self.click.BadParameter( 'The supplied config file path must exist and not be a directory.' ) try: config_json = read_json_from_file(config_file_path) except json.JSONDecodeError as err: raise self.click.BadParameter( f"Could not parse JSON file at '{os.path.abspath(config_file_path)}'. Failure at line '{err.lineno}', " f"column '{err.colno}': '{err.msg}'.") try: self.jsonschema.validate(config_json, self._read_config_json_schema()) except self.jsonschema.exceptions.ValidationError as err: raise self.click.BadParameter( f"Error parsing config file at '{os.path.abspath(config_file_path)}', path '{list(err.absolute_path)}':" f" '{err.args[0]}'.") for monitor_config_dict in config_json['monitors']: try: monitor = Monitor(monitor_config_dict) except AssertionError as err: raise self.click.BadParameter( f"Error initializing monitor from configuration:\n" f"{json.dumps(monitor_config_dict)}\n" f"{err.args[0]}") self.monitors.append(monitor) self._assert_unique_monitor_names(self.monitors, config_file_path) return self
def test_when_config_well_formed_then_no_validation_error(config_json_schema): single_monitor_config = read_json_from_file(SINGLE_MONITOR_CONFIG) jsonschema.validate(single_monitor_config, config_json_schema)
def _read_config_json_schema(self): json_schema_path = os.path.join(os.path.dirname(__file__), '..', 'config', 'config_file_json_schema.json') return read_json_from_file(json_schema_path)
def config_json_schema(): json_schema_path = os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', 'hardware_usage_notifier', 'cli', 'config', 'config_file_json_schema.json') return read_json_from_file(json_schema_path)
def test_when_config_contains_empty_monitors_then_schema_validates_successfully( config_json_schema): empty_monitors_config = read_json_from_file(EMPTY_MONITORS_CONFIG_FILE) jsonschema.validate(empty_monitors_config, config_json_schema)
def test_when_config_contains_unspecified_fields_then_schema_validates_successfully( config_json_schema): unspecified_fields_config = read_json_from_file( UNSPECIFIED_FIELDS_CONFIG_FILE) jsonschema.validate(unspecified_fields_config, config_json_schema)
def test_when_config_contains_metric_with_no_configuration_then_schema_validates_successfully( config_json_schema): no_metric_configuration_config = read_json_from_file( NO_METRIC_CONFIGURATION_CONFIG_FILE) jsonschema.validate(no_metric_configuration_config, config_json_schema)
def test_when_config_contains_notifiers_with_no_monitor_failure_then_schema_validates_successfully( config_json_schema): no_notifiers_monitor_failure_config = read_json_from_file( NO_NOTIFIERS_MONITOR_FAILURE_CONFIG_FILE) jsonschema.validate(no_notifiers_monitor_failure_config, config_json_schema)
def test_when_config_contains_monitor_failure_with_no_configuration_then_schema_validates_successfully( config_json_schema): no_monitor_failure_configuration_config = read_json_from_file( NO_MONITOR_FAILURE_CONFIGURATION_CONFIG_FILE) jsonschema.validate(no_monitor_failure_configuration_config, config_json_schema)