Skip to content

Commit

Permalink
chore: update netplan import semantics and related tests
Browse files Browse the repository at this point in the history
The way the netplan API was being imported caused test issues.
Imports were moved and tests updated accordingly.

Fixes GH-5804
  • Loading branch information
TheRealFalcon committed Oct 9, 2024
1 parent 5d2ff73 commit ad31f17
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
14 changes: 10 additions & 4 deletions cloudinit/config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
except ImportError:
ValidationError = Exception # type: ignore

try:
from netplan import NetplanParserException, Parser # type: ignore

LIBNETPLAN_AVAILABLE = True
except ImportError:
LIBNETPLAN_AVAILABLE = False


LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -615,14 +622,13 @@ def netplan_validate_network_schema(
@raises: SchemaValidationError when netplan's parser raises
NetplanParserExceptions.
"""
try:
from netplan import NetplanParserException, Parser # type: ignore
except ImportError:
if LIBNETPLAN_AVAILABLE:
LOG.debug("Validating network-config with netplan API")
else:
LOG.debug(
"Skipping netplan schema validation. No netplan API available"
)
return False

# netplan Parser looks at all *.yaml files in the target directory underA
# /etc/netplan. cloud-init should only validate schema of the
# network-config it generates, so create a <tmp_dir>/etc/netplan
Expand Down
46 changes: 24 additions & 22 deletions tests/unittests/config/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,11 @@ class TestNetplanValidateNetworkSchema:
),
)
def test_network_config_schema_validation_false_when_skipped(
self, config, expected_log, caplog
self, config, expected_log, caplog, mocker
):
"""netplan_validate_network_schema returns false when skipped."""
with mock.patch.dict("sys.modules"):
sys.modules.pop("netplan", None)
assert False is netplan_validate_network_schema(config)
mocker.patch(f"{M_PATH}LIBNETPLAN_AVAILABLE", False)
assert False is netplan_validate_network_schema(config)
assert expected_log in caplog.text

@pytest.mark.parametrize(
Expand All @@ -456,9 +455,8 @@ def test_network_config_schema_validation_false_when_skipped(
),
)
def test_network_config_schema_validation(
self, error, error_log, caplog, tmpdir
self, error, error_log, caplog, tmpdir, mocker
):

fake_tmpdir = tmpdir.join("mkdtmp")

class FakeParser:
Expand All @@ -469,21 +467,21 @@ def load_yaml_hierarchy(self, parse_dir):
raise error

# Mock expected imports
with mock.patch.dict(
"sys.modules",
netplan=mock.MagicMock(
NetplanParserException=FakeNetplanParserException,
Parser=FakeParser,
),
):
with mock.patch(
"cloudinit.config.schema.mkdtemp",
return_value=fake_tmpdir.strpath,
):
with caplog.at_level(logging.WARNING):
assert netplan_validate_network_schema({"version": 2})
if error_log:
assert re.match(error_log, caplog.records[0].msg, re.DOTALL)
mocker.patch(f"{M_PATH}LIBNETPLAN_AVAILABLE", True)
mocker.patch(
f"{M_PATH}NetplanParserException",
FakeNetplanParserException,
create=True,
)
mocker.patch(f"{M_PATH}Parser", FakeParser, create=True)
mocker.patch(
"cloudinit.config.schema.mkdtemp",
return_value=fake_tmpdir.strpath,
)
with caplog.at_level(logging.WARNING):
assert netplan_validate_network_schema({"version": 2})
if error_log:
assert re.match(error_log, caplog.records[0].msg, re.DOTALL)


class TestValidateCloudConfigSchema:
Expand Down Expand Up @@ -1995,9 +1993,10 @@ def test_main_validates_config_file(
expected,
tmpdir,
capsys,
caplog,
mocker,
):
"""When --config-file parameter is provided, main validates schema."""
mocker.patch(f"{M_PATH}LIBNETPLAN_AVAILABLE", False)
myyaml = tmpdir.join("my.yaml")
myargs = ["mycmd", "--config-file", myyaml.strpath]
if schema_type:
Expand Down Expand Up @@ -2172,6 +2171,7 @@ def test_main_validates_system_userdata_vendordata_and_network_config(
paths,
):
"""When --system is provided, main validates all config userdata."""
mocker.patch(f"{M_PATH}LIBNETPLAN_AVAILABLE", False)
paths.get_ipath = paths.get_ipath_cur
read_cfg_paths.return_value = paths
cloud_config_file = paths.get_ipath_cur("cloud_config")
Expand Down Expand Up @@ -2562,7 +2562,9 @@ def test_network_schema(
expectation,
log,
caplog,
mocker,
):
mocker.patch(f"{M_PATH}LIBNETPLAN_AVAILABLE", False)
net_schema = get_schema(schema_type=schema_type_version)
with expectation:
validate_cloudconfig_schema(
Expand Down

0 comments on commit ad31f17

Please sign in to comment.