Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

driver/powerdriver: ykushpower: board_name support #1280

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,19 @@ Used by:

YKUSHPowerPort
++++++++++++++
A :any:`YKUSHPowerPort` describes a *YEPKIT YKUSH* USB (HID) switchable USB
hub.
A :any:`YKUSHPowerPort` describes a *YEPKIT YKUSH*-family USB (HID)
switchable USB hub.
Models supported include YKUSH, YKUSH 3, and YKUSH XS.

.. code-block:: yaml

YKUSHPowerPort:
serial: 'YK12345'
serial: 'Y3N12345'
index: 1

The example describes port 1 on the YKUSH USB hub with the
serial ``YK12345``.
Use ``ykushcmd -l`` to get your serial number.
The example describes port 1 on the YKUSH 3 USB hub with the
serial ``Y3N12345``.
Use ``ykushcmd ykush3 -l`` to get your serial number.

Arguments:
- serial (str): serial number of the YKUSH hub
Expand Down
19 changes: 19 additions & 0 deletions labgrid/driver/powerdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import attr

from ..exceptions import InvalidConfigError
from ..factory import target_factory
from ..protocol import PowerProtocol, DigitalOutputProtocol, ResetProtocol
from ..resource import NetworkPowerPort
Expand Down Expand Up @@ -283,11 +284,27 @@ def __attrs_post_init__(self):
else:
self.tool = 'ykushcmd'

def on_activate(self):
# Search for the model of the YKUSH device
for model in ["ykush", "ykushxs", "ykush3"]:
cmd = [
self.tool,
model,
"-l"
]
output = processwrapper.check_output(cmd)
if self.port.serial in output.decode("utf-8"):
self.model = model
break
else:
raise InvalidConfigError(f"Could not find YKUSH device with serial {self.port.serial}")

PaulVittorino marked this conversation as resolved.
Show resolved Hide resolved
@Driver.check_active
@step()
def on(self):
cmd = [
self.tool,
self.model,
"-s", f"{self.port.serial}",
"-u", f"{self.port.index}"
]
Expand All @@ -298,6 +315,7 @@ def on(self):
def off(self):
cmd = [
self.tool,
self.model,
"-s", f"{self.port.serial}",
"-d", f"{self.port.index}"
]
Expand All @@ -314,6 +332,7 @@ def cycle(self):
def get(self):
cmd = [
self.tool,
self.model,
"-s", f"{self.port.serial}",
"-g", f"{self.port.index}"
]
Expand Down
80 changes: 78 additions & 2 deletions tests/test_powerdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import pytest

from labgrid.resource import NetworkPowerPort
from labgrid.driver.powerdriver import ExternalPowerDriver, ManualPowerDriver, NetworkPowerDriver
from labgrid.resource import NetworkPowerPort, YKUSHPowerPort
from labgrid.driver.powerdriver import (
ExternalPowerDriver,
ManualPowerDriver,
NetworkPowerDriver,
YKUSHPowerDriver,
)
from labgrid.util.helper import processwrapper


class TestManualPowerDriver:
Expand Down Expand Up @@ -269,3 +275,73 @@ def test_import_backend_siglent(self):
def test_import_backend_poe_mib(self):
pytest.importorskip("pysnmp")
import labgrid.driver.power.poe_mib

class TestYKUSHPowerDriver:
YKUSH_FAKE_SERIAL = "YK12345"
YKUSH_LIST_OUTPUT = f"Attached YKUSH Boards:\n1. Board found with serial number: {YKUSH_FAKE_SERIAL}".encode(
"utf-8"
)
YKUSH3_FAKE_SERIAL = "Y3N10673"
YKUSH3_LIST_OUTPUT = f"Attached YKUSH3 Boards:\n1. Board found with serial number: {YKUSH3_FAKE_SERIAL}".encode(
"utf-8"
)
YKUSHXS_LIST_OUTPUT = (
"Attached YKUSH XS Boards:\n1. Board found with serial number: YKU1234".encode(
"utf-8"
)
)

def test_create(self, target):
resource = YKUSHPowerPort(
target, "power", serial=self.YKUSH_FAKE_SERIAL, index=1
)
device = YKUSHPowerDriver(target, "power")
assert isinstance(device, YKUSHPowerDriver)

def test_default_off(self, target, mocker):
check_output_mock = mocker.patch(
"labgrid.util.helper.processwrapper.check_output"
)
check_output_mock.side_effect = [
self.YKUSH_LIST_OUTPUT,
self.YKUSHXS_LIST_OUTPUT,
self.YKUSH3_LIST_OUTPUT,
b"",
]
resource = YKUSHPowerPort(
target, "power", serial=self.YKUSH_FAKE_SERIAL, index=2
)
resource.avail = True
device = YKUSHPowerDriver(target, "power")
target.activate(device)
device.off()

check_output_mock.assert_called_with(
["ykushcmd", "ykush", "-s", self.YKUSH_FAKE_SERIAL, "-d", "2"]
)

def test_ykush3_on(self, target, mocker):
check_output_mock = mocker.patch(
"labgrid.util.helper.processwrapper.check_output"
)
check_output_mock.side_effect = [
self.YKUSH_LIST_OUTPUT,
self.YKUSHXS_LIST_OUTPUT,
self.YKUSH3_LIST_OUTPUT,
b"",
]
resource = YKUSHPowerPort(
target, "power", serial=self.YKUSH3_FAKE_SERIAL, index=3
)
resource.avail = True
device = YKUSHPowerDriver(target, "power")
target.activate(device)
device.on()

check_output_mock.assert_called_with(
["ykushcmd", "ykush3", "-s", self.YKUSH3_FAKE_SERIAL, "-u", "3"]
)

def test_import_backend_poe_mib(self):
pytest.importorskip("pysnmp")
import labgrid.driver.power.poe_mib
Loading