Skip to content

Commit

Permalink
Improve schema validator warning messages (canonical#5404)
Browse files Browse the repository at this point in the history
During initialization, schema validator may throw warning messages if it finds
the provided configuration does not agree with the schema definition:

schema.py[WARNING]: Invalid cloud-config provided

In reality, even if the configuration does not agree with the schema, it may
sometimes still work. Improve the error message to make it more factual instead
of broadly claiming that the config is broken.

fixes: canonicalGH-5399

Signed-off-by: Ani Sinha <anisinha@redhat.com>
  • Loading branch information
ani-sinha authored Jun 20, 2024
1 parent 1b8030e commit 9c3258f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
17 changes: 9 additions & 8 deletions cloudinit/config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,15 +679,16 @@ def netplan_validate_network_schema(
message = _format_schema_problems(
errors,
prefix=(
f"Invalid {SchemaType.NETWORK_CONFIG.value} provided:\n"
f"{SchemaType.NETWORK_CONFIG.value} failed "
"schema validation!\n"
),
separator="\n",
)
else:
message = (
f"Invalid {SchemaType.NETWORK_CONFIG.value} provided: "
"Please run 'sudo cloud-init schema --system' to "
"see the schema errors."
f"{SchemaType.NETWORK_CONFIG.value} failed schema validation! "
"You may run 'sudo cloud-init schema --system' to "
"check the details."
)
LOG.warning(message)
return True
Expand Down Expand Up @@ -807,14 +808,14 @@ def validate_cloudconfig_schema(
if log_details:
details = _format_schema_problems(
errors,
prefix=f"Invalid {schema_type.value} provided:\n",
prefix=f"{schema_type.value} failed schema validation!\n",
separator="\n",
)
else:
details = (
f"Invalid {schema_type.value} provided: "
"Please run 'sudo cloud-init schema --system' to "
"see the schema errors."
f"{schema_type.value} failed schema validation! "
"You may run 'sudo cloud-init schema --system' to "
"check the details."
)
LOG.warning(details)
return True
Expand Down
14 changes: 8 additions & 6 deletions tests/unittests/config/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,9 @@ def test_network_config_schema_validation_false_when_skipped(
column=12,
message="incorrect YAML value: yes for dhcp value",
),
r"Invalid network-config provided:.*format-l1.c12: Invalid"
" netplan schema. incorrect YAML value: yes for dhcp value",
r"network-config failed schema validation!.*format-l1.c12: "
"Invalid netplan schema. incorrect YAML value: yes for dhcp "
"value",
),
),
)
Expand Down Expand Up @@ -513,8 +514,8 @@ def test_validateconfig_schema_non_strict_emits_warnings(self, caplog):
assert "cloudinit.config.schema" == module
assert logging.WARNING == log_level
assert (
"Invalid cloud-config provided:\np1: -1 is not of type 'string'"
== log_msg
"cloud-config failed schema validation!\n"
"p1: -1 is not of type 'string'" == log_msg
)

@skipUnlessJsonSchema()
Expand All @@ -534,8 +535,9 @@ def test_validateconfig_schema_sensitive(self, caplog):
assert "cloudinit.config.schema" == module
assert logging.WARNING == log_level
assert (
"Invalid cloud-config provided: Please run 'sudo cloud-init "
"schema --system' to see the schema errors." == log_msg
"cloud-config failed schema validation! You may run "
"'sudo cloud-init schema --system' to check the details."
== log_msg
)

@skipUnlessJsonSchema()
Expand Down

0 comments on commit 9c3258f

Please sign in to comment.