Skip to content

Commit f3f32a1

Browse files
committed
yager: handle usb info though the HOST api
Prior to this commit yanger looked at the running system using os.path..., this meant that the static reply / capture data wasn't used properly. This resulted in strange behavior during unit testing on GitHub runners where USB ports are missing. Signed-off-by: Richard Alpe <[email protected]>
1 parent dcc1dc1 commit f3f32a1

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/statd/python/yanger/host.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,18 @@ def run(self, cmd, default=None, log=True):
159159

160160
return out
161161

162+
def exists(self, path):
163+
if not self._run(("ls", path), default="", log=False):
164+
return False
165+
166+
if self.capdir:
167+
dirname = os.path.join(self.capdir, "rootfs", os.path.dirname(path[1:]))
168+
filname = os.path.join(self.capdir, "rootfs", path[1:])
169+
os.makedirs(dirname, exist_ok=True)
170+
open(filname, "w", encoding='utf-8').close() # Create empty file
171+
172+
return True
173+
162174
def read(self, path):
163175
out = self._run(("cat", path), default="", log=False)
164176

@@ -199,6 +211,10 @@ def run(self, cmd, default=None, log=True):
199211
common.LOG.error(f"No recording found for run \"{path}\"")
200212
raise
201213

214+
def exists(self, path):
215+
path = os.path.join(self.replaydir, "rootfs", path[1:])
216+
return os.path.isfile(path)
217+
202218
def read(self, path):
203219
path = os.path.join(self.replaydir, "rootfs", path[1:])
204220
try:

src/statd/python/yanger/ietf_hardware.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,16 @@ def usb_port_components(systemjson):
6161

6262
path = usb_port["path"]
6363
if os.path.basename(path) == "authorized_default":
64-
# TODO: Untestable. Should be done via the host API
65-
if os.path.exists(path):
66-
with open(path, "r") as f:
67-
names.append(usb_port["name"])
68-
data = int(f.readline().strip())
69-
enabled = "unlocked" if data == 1 else "locked"
70-
port["state"] = {}
71-
port["state"]["admin-state"] = enabled
72-
port["name"] = usb_port["name"]
73-
port["class"] = "infix-hardware:usb"
74-
port["state"]["oper-state"] = "enabled"
75-
ports.append(port)
64+
if HOST.exists(path):
65+
names.append(usb_port["name"])
66+
data = int(HOST.read(path))
67+
enabled = "unlocked" if data == 1 else "locked"
68+
port["state"] = {}
69+
port["state"]["admin-state"] = enabled
70+
port["name"] = usb_port["name"]
71+
port["class"] = "infix-hardware:usb"
72+
port["state"]["oper-state"] = "enabled"
73+
ports.append(port)
7674

7775
return ports
7876

0 commit comments

Comments
 (0)