Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recognise and add tests for various types of invalid MAC addresses #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions vnet_manager/config/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ def validate_interface_config(self, machine: str):
if "mac" not in int_vals:
logger.debug(f"MAC not found for interface {int_name} on machine {machine}, generating a random one")
self._new_config["machines"][machine]["interfaces"][int_name]["mac"] = random_mac_generator()
elif isinstance(int_vals["mac"], int):
logger.error(
f"MAC {int_vals['mac']} for interface {int_name} on machine {machine} was parsed as a sexagesimal integer. "
"Please wrap the MAC address in 'quotes'"
)
self._all_ok = False
# From: https://stackoverflow.com/a/7629690/8632038
elif not fullmatch(r"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", int_vals["mac"]):
logger.error(
Expand Down
18 changes: 18 additions & 0 deletions vnet_manager/tests/config/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,24 @@ def test_validate_machine_config_fails_if_bridge_config_is_not_a_dict(self):
self.assertFalse(self.validator.config_validation_successful)
self.logger.error.assert_called_once()

def test_validate_interface_config_sexagesimal_mac_address(self):
# 0d33212743741 is the sexagesimal representation of 42:42:42:42:09:01
# cf. https://github.com/Erik-Lamers1/vnet-manager/issues/62
self.validator.config["machines"]["router100"]["interfaces"]["eth12"]["mac"] = 33212743741
self.validator.validate_interface_config("router100")
self.assertFalse(self.validator.config_validation_successful)
self.logger.error.assert_called_once_with(
f"MAC 33212743741 for interface eth12 on machine router100 was parsed as a sexagesimal integer. Please wrap the MAC address in 'quotes'"
)

def test_validate_interface_config_invalid_mac_address(self):
self.validator.config["machines"]["router100"]["interfaces"]["eth12"]["mac"] = "gg:hh:ii:jj:kk:ll"
self.validator.validate_interface_config("router100")
self.assertFalse(self.validator.config_validation_successful)
self.logger.error.assert_called_once_with(
f"MAC gg:hh:ii:jj:kk:ll for interface eth12 on machine router100, does not seem to be valid. Please check your settings"
)


class TestValidateConfigValidateMachineFilesParameters(VNetTestCase):
def setUp(self) -> None:
Expand Down
Loading