Skip to content

Commit

Permalink
test: add test for ntp client
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiaswal committed Nov 28, 2024
1 parent d6508a7 commit 5edf95e
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 4 deletions.
2 changes: 2 additions & 0 deletions test/case/ietf_system/Readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ include::timezone_utc_offset/Readme.adoc[]
include::ssh_key_authentication/Readme.adoc[]

include::upgrade/Readme.adoc[]

include::ntp_client/Readme.adoc[]
3 changes: 3 additions & 0 deletions test/case/ietf_system/ietf_system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@

- name: ssh_key_authentication
case: ssh_key_authentication/test.py

- name: ntp_client
case: ntp_client/test.py
24 changes: 24 additions & 0 deletions test/case/ietf_system/ntp_client/Readme.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== Simple NTP client test
==== Description
Verify NTP client to multiple NTP servers and verify that one get selected

==== Topology
ifdef::topdoc[]
image::../../test/case/ietf_system/ntp_client/topology.svg[Simple NTP client test topology]
endif::topdoc[]
ifndef::topdoc[]
ifdef::testgroup[]
image::ntp_client/topology.svg[Simple NTP client test topology]
endif::testgroup[]
ifndef::testgroup[]
image::topology.svg[Simple NTP client test topology]
endif::testgroup[]
endif::topdoc[]
==== Test sequence
. Set up topology and attach to target DUT
. Configure NTP client on 'target'
. Verify one source is in 'selected' state on 'target'


<<<

107 changes: 107 additions & 0 deletions test/case/ietf_system/ntp_client/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env python3
"""
Simple NTP client test
Verify NTP client to multiple NTP servers and verify that one get selected
"""

import infamy
import infamy.ntp_server as ntp_server
import infamy.ntp as ntp
import infamy.util as util
def config_target(dut, data1, data2, data3):
dut.put_config_dicts({
"ietf-interfaces": {
"interfaces": {
"interface": [
{
"name": data1,
"enabled": True,
"ipv4": {
"address": [{
"ip": "192.168.1.2",
"prefix-length": 24
}]
}
},
{
"name": data2,
"enabled": True,
"ipv4": {
"address": [{
"ip": "192.168.2.2",
"prefix-length": 24
}]
}
},
{
"name": data3,
"enabled": True,
"ipv4": {
"address": [{

"ip": "192.168.3.2",
"prefix-length": 24
}]
}
}]
}
},
"ietf-system": {
"system": {
"ntp": {
"enabled": True,
"server": [{
"name": "Server1",
"udp": {
"address": "192.168.1.1"
},
"iburst": True
},{
"name": "Server2",
"udp": {
"address": "192.168.2.1"
},
"iburst": True
},{
"name": "Server3",
"udp": {
"address": "192.168.3.1"
},
"iburst": True
}]
}
}
}
})

with infamy.Test() as test:
with test.step("Set up topology and attach to target DUT"):
env = infamy.Env()
target = env.attach("target", "mgmt")

with test.step("Configure NTP client on 'target'"):
_, data1 = env.ltop.xlate("target", "data1")
_, data2 = env.ltop.xlate("target", "data2")
_, data3 = env.ltop.xlate("target", "data3")

config_target(target, data1, data2, data3)

_, hport1 = env.ltop.xlate("host", "data1")
_, hport2 = env.ltop.xlate("host", "data2")
_, hport3 = env.ltop.xlate("host", "data3")

with infamy.IsolatedMacVlan(hport1) as ns1, \
infamy.IsolatedMacVlan(hport2) as ns2, \
infamy.IsolatedMacVlan(hport3) as ns3:
ns1.addip("192.168.1.1")
ns2.addip("192.168.2.1")
ns3.addip("192.168.3.1")

with ntp_server.Server(ns1) as ntp1, \
ntp_server.Server(ns2) as ntp2, \
ntp_server.Server(ns3) as ntp3:
with test.step("Verify one source is in 'selected' state on 'target'"):
util.until(lambda: ntp.any_source_selected(target), attempts=200)

test.succeed()
1 change: 1 addition & 0 deletions test/case/ietf_system/ntp_client/topology.dot
60 changes: 60 additions & 0 deletions test/case/ietf_system/ntp_client/topology.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/infamy/netconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ def put_config_dicts(self, models):
infer_put_dict(self.name, models)

for model in models.keys():
print(model)
mod = self.ly.get_module(model)
lyd = mod.parse_data_dict(models[model], no_state=True, validate=False)
config+=lyd.print_mem("xml", with_siblings=True, pretty=False)+"\n"
Expand Down
28 changes: 28 additions & 0 deletions test/infamy/ntp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
NTP client helper
"""
def _get_ntp(target):
xpath="/ietf-system:system-state/infix-system:ntp"
data=target.get_data(xpath)

if data is None:
return None

return data["system-state"].get("infix-system:ntp", None) or data["system-state"].get("ntp", None)

def _get_ntp_sources(target):
ntp=_get_ntp(target)

if ntp is None:
return []

return ntp["sources"]["source"]

def any_source_selected(target):
sources=_get_ntp_sources(target)

for source in sources:
if source["source-state"] == "selected":
return True

return False
24 changes: 24 additions & 0 deletions test/infamy/ntp_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Start NTP server in the background"""
import subprocess

class Server:
def __init__(self, netns, iface="iface"):
self.iface = iface
self.process = None
self.netns = netns

def __enter__(self):
self.start()

def __exit__(self, _, __, ___):
self.stop()

def start(self):
cmd=f"ntpd -w -n -l -I {self.iface}"
self.process = self.netns.popen(cmd.split(" "),stderr=subprocess.DEVNULL)

def stop(self):
if self.process:
self.process.terminate()
self.process.wait()
self.process = None
8 changes: 4 additions & 4 deletions test/infamy/topologies/1x4.dot
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ graph "1x4" {
edge [color="cornflowerblue", penwidth="2", fontname="DejaVu Serif, Book"];

host [
label="host | { <tgt> tgt | <data0> data0 | <data1> data1 | <data2> data2 }",
label="host | { <mgmt> mgmt | <data1> data1 | <data2> data2 | <data3> data3 }",
pos="0,12!",
kind="controller",
];

target [
label="{ <mgmt> mgmt | <data0> data0 | <data1> data1 | <data2> data2 } | target",
label="{ <mgmt> mgmt | <data1> data1 | <data2> data2 | <data3> data3 } | target",
pos="10,12!",

kind="infix",
];

host:tgt -- target:mgmt [kind=mgmt]
host:data0 -- target:data0 [color=black]
host:mgmt -- target:mgmt [kind=mgmt]
host:data1 -- target:data1 [color=black]
host:data2 -- target:data2 [color=black]
host:data3 -- target:data3 [color=black]
}

0 comments on commit 5edf95e

Please sign in to comment.