Merge branch 'machine-model-defaults' into machine-repository

This commit is contained in:
Mike Salvatore 2022-08-29 20:31:44 -04:00
commit 1383332d33
2 changed files with 48 additions and 3 deletions

View File

@ -20,16 +20,16 @@ class Machine(MutableInfectionMonkeyBaseModel):
hardware_id: Optional[HardwareID] hardware_id: Optional[HardwareID]
"""An identifier generated by the agent that uniquely identifies a machine""" """An identifier generated by the agent that uniquely identifies a machine"""
network_interfaces: Sequence[IPv4Interface] network_interfaces: Sequence[IPv4Interface] = Field(default_factory=tuple)
"""The machine's networking interfaces""" """The machine's networking interfaces"""
operating_system: Optional[OperatingSystem] operating_system: Optional[OperatingSystem]
"""The operating system the machine is running""" """The operating system the machine is running"""
operating_system_version: str operating_system_version: str = Field(default="")
"""The specific version of the operating system the machine is running""" """The specific version of the operating system the machine is running"""
hostname: str hostname: str = Field(default="")
"""The hostname of the machine""" """The hostname of the machine"""
_make_immutable_sequence = validator("network_interfaces", pre=True, allow_reuse=True)( _make_immutable_sequence = validator("network_interfaces", pre=True, allow_reuse=True)(

View File

@ -121,6 +121,15 @@ def test_hardware_id_validate_on_set():
m.hardware_id = -50 m.hardware_id = -50
def test_hardware_id_default():
missing_hardware_id_dict = MACHINE_OBJECT_DICT.copy()
del missing_hardware_id_dict["hardware_id"]
m = Machine(**missing_hardware_id_dict)
assert m.hardware_id is None
def test_network_interfaces_set_valid_value(): def test_network_interfaces_set_valid_value():
m = Machine(**MACHINE_OBJECT_DICT) m = Machine(**MACHINE_OBJECT_DICT)
@ -141,6 +150,15 @@ def test_network_interfaces_sequence_is_immutable():
assert not isinstance(m.network_interfaces, MutableSequence) assert not isinstance(m.network_interfaces, MutableSequence)
def test_network_interfaces_default():
missing_network_interfaces_dict = MACHINE_OBJECT_DICT.copy()
del missing_network_interfaces_dict["network_interfaces"]
m = Machine(**missing_network_interfaces_dict)
assert len(m.network_interfaces) == 0
def test_operating_system_set_valid_value(): def test_operating_system_set_valid_value():
m = Machine(**MACHINE_OBJECT_DICT) m = Machine(**MACHINE_OBJECT_DICT)
@ -155,6 +173,15 @@ def test_operating_system_set_invalid_value():
m.operating_system = "MacOS" m.operating_system = "MacOS"
def test_operating_system_default_value():
missing_operating_system_dict = MACHINE_OBJECT_DICT.copy()
del missing_operating_system_dict["operating_system"]
m = Machine(**missing_operating_system_dict)
assert m.operating_system is None
def test_set_operating_system_version(): def test_set_operating_system_version():
m = Machine(**MACHINE_OBJECT_DICT) m = Machine(**MACHINE_OBJECT_DICT)
@ -162,8 +189,26 @@ def test_set_operating_system_version():
m.operating_system_version = "1234" m.operating_system_version = "1234"
def test_operating_system_version_default_value():
missing_operating_system_version_dict = MACHINE_OBJECT_DICT.copy()
del missing_operating_system_version_dict["operating_system_version"]
m = Machine(**missing_operating_system_version_dict)
assert m.operating_system_version == ""
def test_set_hostname(): def test_set_hostname():
m = Machine(**MACHINE_OBJECT_DICT) m = Machine(**MACHINE_OBJECT_DICT)
# Raises exception_on_failure # Raises exception_on_failure
m.operating_system_version = "wopr" m.operating_system_version = "wopr"
def test_hostname_default_value():
missing_hostname_dict = MACHINE_OBJECT_DICT.copy()
del missing_hostname_dict["hostname"]
m = Machine(**missing_hostname_dict)
assert m.hostname == ""