Change timezone test to check timezone in operational instead of checking time

Libyang is smart, it "fixes" the timezone for you, this was overriden by a
hack in netconf.py and restconf.py, but what you really want is to see
in operational what the current timezone is.
This commit is contained in:
Mattias Walström
2025-04-30 11:51:45 +02:00
parent 0b29e9eee4
commit cfbdce1b90
8 changed files with 40 additions and 51 deletions
+29
View File
@@ -1,5 +1,6 @@
import subprocess
import ipaddress
import re
from .common import insert,YangDate
from .host import HOST
@@ -210,6 +211,33 @@ def add_hostname(out):
hostname = HOST.run(tuple(["hostname"]))
out["hostname"] = hostname.strip()
def add_timezone(out):
path = HOST.run(tuple("realpath /etc/localtime".split()), "")
timezone = None
prefixes = [
'/usr/share/zoneinfo/posix/',
'/usr/share/zoneinfo/right/',
'/usr/share/zoneinfo/'
]
for prefix in prefixes:
if path is not None and path.startswith(prefix):
timezone = path[len(prefix):]
break
if timezone is not None:
timezone=timezone.strip()
pattern = r'Etc/GMT([\+\-]\d{1,2})$'
match = re.search(pattern, timezone)
if match:
offset = -int(match.group(1))
insert(out, "clock", "timezone-utc-offset", offset)
else:
if timezone == "Etc/UTC":
insert(out, "clock", "timezone-utc-offset", 0)
else:
insert(out, "clock", "timezone-name", timezone)
def add_users(out):
shadow_output = HOST.run_multiline(["getent", "shadow"], [])
users = []
@@ -257,6 +285,7 @@ def operational():
out_system = out["ietf-system:system"]
add_hostname(out_system)
add_users(out_system)
add_timezone(out_system)
add_software(out_state)
add_ntp(out_state)
add_dns(out_state)
+5 -6
View File
@@ -17,15 +17,14 @@ with infamy.Test() as test:
target.put_config_dicts({"ietf-system": {
"system": {
"clock": {
"timezone-name": "Australia/Perth" # always +8:00, no DTS
"timezone-name": "Australia/Perth"
}
}
}})
with test.step("Verify current time offset is +08:00"):
current_datetime=target.get_current_time_with_offset()
offset=current_datetime[-6:]
assert(offset == "+08:00")
with test.step("Verify timezone is Australia/Perth"):
tz=target.get_data("/ietf-system:system/clock/timezone-name")
name=tz.get("system", {}).get("clock",{}).get("timezone-name", "")
assert(name == "Australia/Perth")
test.succeed()
+1 -1
View File
@@ -17,7 +17,7 @@ endif::topdoc[]
==== Test sequence
. Set up topology and attach to target DUT
. Set timezone to Australia/Perth
. Verify current time offset is +08:00
. Verify timezone is Australia/Perth
<<<
@@ -21,10 +21,9 @@ with infamy.Test() as test:
}
}})
with test.step("Verify current time offset is +12:00"):
current_datetime=target.get_current_time_with_offset()
offset=current_datetime[-6:]
assert(offset == "+12:00")
with test.step("Verify current timezone is UTC+12:00"):
tz=target.get_data("/ietf-system:system/clock/timezone-utc-offset")
offset=tz.get("system", {}).get("clock",{}).get("timezone-utc-offset", 0)
assert(offset == 12)
test.succeed()
@@ -17,7 +17,7 @@ endif::topdoc[]
==== Test sequence
. Set up topology and attach to target DUT
. Set timezone UTC offset to +12
. Verify current time offset is +12:00
. Verify current timezone is UTC+12:00
<<<
-20
View File
@@ -411,23 +411,3 @@ class Device(Transport):
# Apply the configuration change
return self.put_config(lyd.print_mem("xml", with_siblings=True,
pretty=False))
def get_current_time_with_offset(self):
"""
Return current datetime with offset.
This method retrieves the current datetime from the raw data
before it is passed through libyang. This is necessary because
libyang "adjusts" the time for the offset, and we need the
unadjusted time.
"""
data = self.get_data("/ietf-system:system-state/clock", parse=False)
parsed_data = lxml.etree.fromstring(data)
xpath = './/{urn:ietf:params:xml:ns:yang:ietf-system}current-datetime'
current_datetime = parsed_data.find(xpath)
if current_datetime is not None:
return current_datetime.text
else:
raise ValueError("current-datetime element not found in the response")
-13
View File
@@ -359,19 +359,6 @@ class Device(Transport):
return response.content
def get_current_time_with_offset(self):
"""
Return current datetime with offset.
This method retrieves the current datetime from the raw data
before it is passed through libyang. This is necessary because
libyang "adjusts" the time for the offset, and we need the
unadjusted time.
"""
data = self.get_data("/ietf-system:system-state/clock", parse=False)
data = json.loads(data)
return data["ietf-system:system-state"]["clock"]["current-datetime"]
def delete_xpath(self, xpath):
"""Delete XPath from running config"""
path = f"/ds/ietf-datastores:running{xpath_to_uri(xpath)}"
-5
View File
@@ -42,11 +42,6 @@ class Transport(ABC):
def reboot(self):
pass
@abstractmethod
def get_current_time_with_offset(self):
"""Needed since libyang is too nice and removes the original offset"""
pass
@abstractmethod
def call_dict(self, module, call):
pass