From 15b32cfcc29daaa52d12c360121de082f130847b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Walstr=C3=B6m?= Date: Tue, 13 Feb 2024 16:56:59 +0100 Subject: [PATCH] infamy: Add a simple USB test --- test/case/all.yaml | 2 + test/case/ietf_hardware/all.yaml | 2 + test/case/ietf_hardware/usb.py | 114 +++++++++++++++++++++++++++++++ test/infamy/usb.py | 18 +++++ 4 files changed, 136 insertions(+) create mode 100644 test/case/ietf_hardware/all.yaml create mode 100755 test/case/ietf_hardware/usb.py create mode 100644 test/infamy/usb.py diff --git a/test/case/all.yaml b/test/case/all.yaml index eb7b1572..1e6dd78e 100644 --- a/test/case/all.yaml +++ b/test/case/all.yaml @@ -19,5 +19,7 @@ - name: infix-dhcp suite: infix_dhcp/all.yaml +- name: ietf-hardware + suite: ietf_hardware/all.yaml #- name: infix-services # suite: infix_services/all.yaml diff --git a/test/case/ietf_hardware/all.yaml b/test/case/ietf_hardware/all.yaml new file mode 100644 index 00000000..8bc46e53 --- /dev/null +++ b/test/case/ietf_hardware/all.yaml @@ -0,0 +1,2 @@ +--- +- case: usb.py diff --git a/test/case/ietf_hardware/usb.py b/test/case/ietf_hardware/usb.py new file mode 100755 index 00000000..f2190d2e --- /dev/null +++ b/test/case/ietf_hardware/usb.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python3 + +import infamy +import copy +import infamy.usb as usb +from infamy.util import until + +with infamy.Test() as test: + with test.step("Initialize"): + env = infamy.Env(infamy.std_topology("1x1")) + target = env.attach("target", "mgmt") + available=usb.get_usb_ports(target) + if len(available) < 1: + test.skip() + + with test.step("Lock USB ports"): + components=[] + for port in available: + component = { + "name": port, + "state": { + "admin-state": "locked" + } + } + components.append(component) + + target.put_config_dict("ietf-hardware", { + "hardware": { + "component": components + } + }) + + with test.step("Verify USB ports locked"): + for port in available: + until(lambda: usb.get_usb_state(target, port) == "locked") + + + with test.step("Unlock USB ports"): + components=[] + for port in available: + component = { + "name": port, + "state": { + "admin-state": "unlocked" + } + } + components.append(component) + + target.put_config_dict("ietf-hardware", { + "hardware": { + "component": components + } + }) + + + with test.step("Verify USB ports unlocked"): + for port in available: + until(lambda: usb.get_usb_state(target, port) == "unlocked") + + if len(available) > 1: + with test.step("Lock one port"): + components=[] + component = { + "name": available[1], + "class": "infix-hardware:usb", + "state": { + "admin-state": "locked" + } + } + components.append(component) + + target.put_config_dict("ietf-hardware", { + "hardware": { + "component": components + } + }) + with test.step("Verify one port is locked and one unlocked"): + until(lambda: usb.get_usb_state(target, available[1]) == "locked") + until(lambda: usb.get_usb_state(target, available[0]) == "unlocked") + + with test.step("Remove all hardware configuration"): + running = target.get_config_dict("/ietf-hardware:hardware") + new = copy.deepcopy(running) + new["hardware"].clear() + target.put_diff_dicts("ietf-hardware",running,new) + + with test.step("Verify USB ports locked"): + for port in available: + until(lambda: usb.get_usb_state(target, port) == "locked") + + + with test.step("Unlock USB ports"): + components=[] + for port in available: + component = { + "name": port, + "class": "infix-hardware:usb", + "state": { + "admin-state": "unlocked" + } + } + components.append(component) + + target.put_config_dict("ietf-hardware", { + "hardware": { + "component": components + } + }) + + with test.step("Verify USB ports unlocked"): + for port in available: + until(lambda: usb.get_usb_state(target, port) == "unlocked") + + test.succeed() diff --git a/test/infamy/usb.py b/test/infamy/usb.py new file mode 100644 index 00000000..2f7eb4ac --- /dev/null +++ b/test/infamy/usb.py @@ -0,0 +1,18 @@ +def _get_hardware(target): + xpath="/ietf-hardware:hardware" + return target.get_data(xpath)["hardware"] + +def get_usb_ports(target): + hardware=_get_hardware(target) + ports=[] + for component in hardware["component"]: + if component.get("class") == "infix-hardware:usb": + ports.append(component["name"]) + + return ports + +def get_usb_state(target, name): + hardware=_get_hardware(target) + for component in hardware["component"]: + if component.get("name") == name and component.get("class") == "infix-hardware:usb": + return component["state"]["admin-state"]