Skip to content

Commit b3fe0d5

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 15a2221 commit b3fe0d5

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/statd/python/yanger/host.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ def read(self, path):
116116

117117
return None
118118

119+
def exists(self, path: str) -> bool:
120+
try:
121+
return os.path.exists(path)
122+
except OSError:
123+
return False
119124

120125
class Remotehost(Localhost):
121126
def __init__(self, prefix, capdir):
@@ -159,6 +164,18 @@ def run(self, cmd, default=None, log=True):
159164

160165
return out
161166

167+
def exists(self, path: str) -> bool:
168+
if not self._run(("ls", path), default="", log=False):
169+
return False
170+
171+
if self.capdir:
172+
dirname = os.path.join(self.capdir, "rootfs", os.path.dirname(path[1:]))
173+
filname = os.path.join(self.capdir, "rootfs", path[1:])
174+
os.makedirs(dirname, exist_ok=True)
175+
open(filname, "w", encoding='utf-8').close() # Create empty file
176+
177+
return True
178+
162179
def read(self, path):
163180
out = self._run(("cat", path), default="", log=False)
164181

@@ -199,6 +216,13 @@ def run(self, cmd, default=None, log=True):
199216
common.LOG.error(f"No recording found for run \"{path}\"")
200217
raise
201218

219+
def exists(self, path: str) -> bool:
220+
path = os.path.join(self.replaydir, "rootfs", path[1:])
221+
try:
222+
return os.path.exists(path)
223+
except OSError:
224+
return False
225+
202226
def read(self, path):
203227
path = os.path.join(self.replaydir, "rootfs", path[1:])
204228
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)