def test_when_notifier_name_empty_then_exception(notifier_config): notifier_config['name'] = '' exception_validator = RunnableExceptionValidator( lambda: Notifier(notifier_config, Monitor.NOTIFIERS_DIRECTORY, Monitor. NOTIFIER_INSTANCES_PARENT_MODULE)) exception_validator.verify_exception( AssertionError, 'The name of the notifier must contain at least one character!')
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_comparator_empty_then_exception(threshold_config): threshold_config['comparator'] = '' exception_validator = RunnableExceptionValidator( lambda: Threshold( config=threshold_config, comparator_directory=Monitor.COMPARATORS_DIRECTORY, comparator_parent_module=Monitor.COMPARATOR_INSTANCES_PARENT_MODULE)) exception_validator.verify_exception( AssertionError, 'The threshold comparator must contain at least one character!')
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_clear_points_not_positive_integer_then_exception(threshold_config): threshold_config['clear_points'] = 0 exception_validator = RunnableExceptionValidator( lambda: Threshold( config=threshold_config, comparator_directory=Monitor.COMPARATORS_DIRECTORY, comparator_parent_module=Monitor.COMPARATOR_INSTANCES_PARENT_MODULE)) exception_validator.verify_exception( AssertionError, "The threshold clear points must be a positive integer, but got '0'!")
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_notifier_file_not_in_directory_then_exception(notifier_config): notifier_config['name'] = 'non_existent_notifier.py' exception_validator = RunnableExceptionValidator(lambda: Notifier( config=notifier_config, notifier_directory=NOTIFIERS_TEST_INSTANCES_DIRECTORY, notifier_parent_module=NOTIFIERS_TEST_INSTANCES_MODULE)) exception_validator.verify_exception( AssertionError, f"The notifier name must be the Python file name placed in the path '{NOTIFIERS_TEST_INSTANCES_DIRECTORY}'," f" but got '{notifier_config['name']}'!")
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_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_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_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_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_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_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_comparator_file_not_in_directory_then_exception(threshold_config): threshold_config['comparator'] = 'non_existent_comparator.py' exception_validator = RunnableExceptionValidator( lambda: Threshold( config=threshold_config, comparator_directory=COMPARATORS_TEST_INSTANCES_DIRECTORY, comparator_parent_module=COMPARATORS_TEST_INSTANCES_MODULE)) exception_validator.verify_exception( AssertionError, f"The comparator name must be the Python file name placed in the path '{COMPARATORS_TEST_INSTANCES_DIRECTORY}'," f" but got '{threshold_config['comparator']}'!")
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_metric_file_not_in_directory_then_exception(metric_config): metric_config['name'] = 'non_existent_metric.py' exception_validator = RunnableExceptionValidator( lambda: Metric( config=metric_config, metric_directory=METRIC_TEST_INSTANCES_DIRECTORY, metric_parent_module=METRIC_TEST_INSTANCES_MODULE)) exception_validator.verify_exception( AssertionError, f"The metric name must be the Python file name placed in the path '{METRIC_TEST_INSTANCES_DIRECTORY}', " f"but got '{metric_config['name']}'!")
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_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_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_metric_class_constructor_missing_configuration_parameter_then_exception(metric_config): metric_config['name'] = 'metric_class_constructor_missing_configuration.py' exception_validator = RunnableExceptionValidator( lambda: Metric( config=metric_config, metric_directory=METRIC_TEST_INSTANCES_DIRECTORY, metric_parent_module=METRIC_TEST_INSTANCES_MODULE)) exception_validator.verify_exception( AssertionError, f"Metric() takes no arguments. Please make sure that the metric class constructor takes a single argument " f"representing the metric configuration (which might be empty/undefined)!")
def test_when_config_contains_duplicate_monitor_names_then_exception(): config = Config(click=click, jsonschema=jsonschema) exception_validator = RunnableExceptionValidator( lambda: config.from_cli_file_path_param( config_file_path=DUPLICATE_MONITOR_NAMES_CONFIG_FILE_NAME)) exception_validator.verify_exception( click.BadParameter, f"Error parsing config file at '{os.path.abspath(DUPLICATE_MONITOR_NAMES_CONFIG_FILE_NAME)}': " f"Duplicate monitor names are not allowed. However, detected duplicate name 'Monitor_1'!" )
def test_when_metric_class_is_not_subclass_of_abstract_metric_class_then_exception(metric_config): metric_config['name'] = 'metric_class_not_subclass_of_abstract_metric_class.py' exception_validator = RunnableExceptionValidator( lambda: Metric( config=metric_config, metric_directory=METRIC_TEST_INSTANCES_DIRECTORY, metric_parent_module=METRIC_TEST_INSTANCES_MODULE)) exception_validator.verify_exception( AssertionError, f"The metric class defined in '{metric_config['name']}' must be a subclass of the abstract Metric class " f"defined in {os.path.join(Metric.METRIC_ABSTRACT_CLASS_MODULE, Metric.METRIC_ABSTRACT_CLASS_NAME)}")
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_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_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_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_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_comparator_class_constructor_missing_reference_value_parameter_then_exception(threshold_config): threshold_config['comparator'] = 'comparator_class_constructor_missing_reference_value.py' exception_validator = RunnableExceptionValidator( lambda: Threshold( config=threshold_config, comparator_directory=COMPARATORS_TEST_INSTANCES_DIRECTORY, comparator_parent_module=COMPARATORS_TEST_INSTANCES_MODULE)) exception_validator.verify_exception( AssertionError, f"Comparator() takes no arguments. Please make sure that the comparator class constructor takes a single " f"argument representing the reference value for the comparison operation!")