Skip to content

Commit

Permalink
fix(net-schema): no warn when skipping schema check on non-netplan
Browse files Browse the repository at this point in the history
Avoid warning when network-config schema is version 2 and we
are on a system within netplan python modules because cloud-init
doesn't ye have static JSON network-config schema defined yet for
version 2.

Also fixes integration test to expect python3 netplan API
on Ubuntu Mantic and newer.

Fixes canonicalGH-4814
  • Loading branch information
blackboxsw committed Jan 29, 2024
1 parent 56aa879 commit e168b4a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
18 changes: 10 additions & 8 deletions cloudinit/config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,6 @@ def netplan_validate_network_schema(
@raises: SchemaValidationError when netplan's parser raises
NetplanParserExceptions.
"""
if network_schema_version(network_config) != 2:
return False # Netplan only validates network version 2 config

try:
from netplan import NetplanParserException, Parser # type: ignore
except ImportError:
Expand Down Expand Up @@ -715,11 +712,16 @@ def validate_cloudconfig_schema(
NETWORK_CONFIG
"""
if schema_type == SchemaType.NETWORK_CONFIG:
if netplan_validate_network_schema(
network_config=config, strict=strict, log_details=log_details
):
# Schema was validated by netplan
return True
if network_schema_version(config) == 2:
if netplan_validate_network_schema(
network_config=config, strict=strict, log_details=log_details
):
# Schema was validated by netplan
return True
# network-config schema version 2 but no netplan.
# TODO(add JSON schema definition for network version 2)
return False

if schema is None:
schema = get_schema(schema_type)
try:
Expand Down
4 changes: 2 additions & 2 deletions tests/integration_tests/cmd/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

from tests.integration_tests.instances import IntegrationInstance
from tests.integration_tests.releases import CURRENT_RELEASE, NOBLE
from tests.integration_tests.releases import CURRENT_RELEASE, MANTIC
from tests.integration_tests.util import verify_clean_log

USER_DATA = """\
Expand Down Expand Up @@ -81,7 +81,7 @@ def test_network_config_schema_validation(
"annotate": NET_V1_ANNOTATED,
},
}
if CURRENT_RELEASE >= NOBLE:
if CURRENT_RELEASE >= MANTIC:
# Support for netplan API available
content_responses[NET_CFG_V2] = {
"out": "Valid schema /root/net.yaml"
Expand Down
24 changes: 16 additions & 8 deletions tests/unittests/config/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2229,27 +2229,27 @@ class TestNetworkSchema:
net_schema = get_schema(schema_type=SchemaType.NETWORK_CONFIG)

@pytest.mark.parametrize(
"src_config, expectation",
"src_config, expectation, log",
(
pytest.param(
{"network": {"config": [], "version": 2}},
pytest.raises(
SchemaValidationError,
match=re.escape("network.version: 2 is not one of [1]"),
),
id="net_v2_invalid",
does_not_raise(),
"Skipping netplan schema validation. No netplan available",
id="net_v2_skipped",
),
pytest.param(
{"network": {"version": 1}},
pytest.raises(
SchemaValidationError,
match=re.escape("'config' is a required property"),
),
"",
id="config_key_required",
),
pytest.param(
{"network": {"version": 1, "config": []}},
does_not_raise(),
"",
id="config_key_required",
),
pytest.param(
Expand All @@ -2266,6 +2266,7 @@ class TestNetworkSchema:
" not valid under any of the given schemas"
),
),
"",
id="unknown_config_type_item",
),
pytest.param(
Expand All @@ -2274,6 +2275,7 @@ class TestNetworkSchema:
SchemaValidationError,
match=r"network.config.0: 'name' is a required property.*",
),
"",
id="physical_requires_name_property",
),
pytest.param(
Expand All @@ -2284,6 +2286,7 @@ class TestNetworkSchema:
}
},
does_not_raise(),
"",
id="physical_with_name_succeeds",
),
pytest.param(
Expand All @@ -2299,6 +2302,7 @@ class TestNetworkSchema:
SchemaValidationError,
match=r"Additional properties are not allowed.*",
),
"",
id="physical_no_additional_properties",
),
pytest.param(
Expand All @@ -2309,6 +2313,7 @@ class TestNetworkSchema:
}
},
does_not_raise(),
"",
id="physical_with_all_known_properties",
),
pytest.param(
Expand All @@ -2319,18 +2324,21 @@ class TestNetworkSchema:
}
},
does_not_raise(),
"",
id="bond_with_all_known_properties",
),
),
)
def test_network_schema(self, src_config, expectation):
def test_network_schema(self, src_config, expectation, log, caplog):
with expectation:
validate_cloudconfig_schema(
config=src_config,
schema=self.net_schema,
schema_type="netork-config",
schema_type=SchemaType.NETWORK_CONFIG,
strict=True,
)
if log:
assert log in caplog.text


class TestStrictMetaschema:
Expand Down

0 comments on commit e168b4a

Please sign in to comment.