#2042: Add time attribute
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
from datetime import datetime
|
||||||
from ipaddress import IPv4Address
|
from ipaddress import IPv4Address
|
||||||
from typing import Dict, Optional
|
from typing import Dict, Optional
|
||||||
|
|
||||||
@@ -16,6 +17,7 @@ class NTPClient(Service):
|
|||||||
ip_addr: Optional[IPv4Address] = None
|
ip_addr: Optional[IPv4Address] = None
|
||||||
ntp_server: Optional[IPv4Address] = None
|
ntp_server: Optional[IPv4Address] = None
|
||||||
"The NTP server the client sends requests to."
|
"The NTP server the client sends requests to."
|
||||||
|
time: Optional[datetime] = None
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
kwargs["name"] = "NTPClient"
|
kwargs["name"] = "NTPClient"
|
||||||
@@ -94,6 +96,7 @@ class NTPClient(Service):
|
|||||||
f"{self.name}: Received time \
|
f"{self.name}: Received time \
|
||||||
update from NTP server{payload.ntp_reply.ntp_datetime}"
|
update from NTP server{payload.ntp_reply.ntp_datetime}"
|
||||||
)
|
)
|
||||||
|
self.time = payload.ntp_reply.ntp_datetime
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def apply_timestep(self, timestep: int) -> None:
|
def apply_timestep(self, timestep: int) -> None:
|
||||||
|
|||||||
@@ -53,38 +53,39 @@ def test_ntp_client_server():
|
|||||||
|
|
||||||
assert ntp_server.operating_state == ServiceOperatingState.RUNNING
|
assert ntp_server.operating_state == ServiceOperatingState.RUNNING
|
||||||
assert ntp_client.operating_state == ServiceOperatingState.RUNNING
|
assert ntp_client.operating_state == ServiceOperatingState.RUNNING
|
||||||
|
assert ntp_client.time is None
|
||||||
|
|
||||||
ntp_request = NTPRequest(ntp_client="192.168.1.3")
|
ntp_request = NTPRequest(ntp_client="192.168.1.3")
|
||||||
ntp_packet = NTPPacket(ntp_request=ntp_request)
|
ntp_packet = NTPPacket(ntp_request=ntp_request)
|
||||||
ntp_client.send(payload=ntp_packet)
|
ntp_client.send(payload=ntp_packet)
|
||||||
assert ntp_server.receive(payload=ntp_packet) is True
|
assert ntp_server.receive(payload=ntp_packet) is True
|
||||||
assert ntp_client.receive(payload=ntp_packet) is True
|
assert ntp_client.receive(payload=ntp_packet) is True
|
||||||
|
assert ntp_client.time is not None
|
||||||
assert ntp_client.apply_timestep(1) is True
|
assert ntp_client.apply_timestep(1) is True
|
||||||
|
|
||||||
|
|
||||||
# TODO: Disabled until a service such as NTP can introspect to see if it's running.
|
|
||||||
# Test ntp client behaviour when ntp server is unavailable.
|
# Test ntp client behaviour when ntp server is unavailable.
|
||||||
# def test_ntp_server_failure():
|
@pytest.mark.skip(reason="NTP needs to know if underly node is RUNNING")
|
||||||
# network = create_ntp_network()
|
def test_ntp_server_failure():
|
||||||
# server: Server = network.get_node_by_hostname("ntp_server")
|
network = create_ntp_network()
|
||||||
# client: Computer = network.get_node_by_hostname("ntp_client")
|
server: Server = network.get_node_by_hostname("ntp_server")
|
||||||
|
client: Computer = network.get_node_by_hostname("ntp_client")
|
||||||
|
|
||||||
# ntp_server: NTPServer = server.software_manager.software["NTPServer"]
|
ntp_server: NTPServer = server.software_manager.software["NTPServer"]
|
||||||
# ntp_client: NTPClient = client.software_manager.software["NTPClient"]
|
ntp_client: NTPClient = client.software_manager.software["NTPClient"]
|
||||||
|
|
||||||
# assert ntp_client.operating_state == ServiceOperatingState.RUNNING
|
assert ntp_client.operating_state == ServiceOperatingState.RUNNING
|
||||||
|
|
||||||
# # Turn off ntp server.
|
# Turn off ntp server.
|
||||||
# ntp_server.stop()
|
ntp_server.stop()
|
||||||
# assert ntp_server.operating_state == ServiceOperatingState.STOPPED
|
assert ntp_server.operating_state == ServiceOperatingState.STOPPED
|
||||||
# # And request a time update.
|
# And request a time update.
|
||||||
# ntp_request = NTPRequest(ntp_client="192.168.1.3")
|
ntp_request = NTPRequest(ntp_client="192.168.1.3")
|
||||||
# ntp_packet = NTPPacket(ntp_request=ntp_request)
|
ntp_packet = NTPPacket(ntp_request=ntp_request)
|
||||||
# ntp_client.send(payload=ntp_packet)
|
ntp_client.send(payload=ntp_packet)
|
||||||
# assert ntp_server.receive(payload=ntp_packet) is False
|
assert ntp_server.receive(payload=ntp_packet) is False
|
||||||
# assert ntp_client.receive(payload=ntp_packet) is False
|
assert ntp_client.receive(payload=ntp_packet) is False
|
||||||
|
|
||||||
# # Restart ntp server.
|
# Restart ntp server.
|
||||||
# ntp_server.start()
|
ntp_server.start()
|
||||||
# assert ntp_server.operating_state == ServiceOperatingState.RUNNING
|
assert ntp_server.operating_state == ServiceOperatingState.RUNNING
|
||||||
|
|||||||
Reference in New Issue
Block a user