Skip to content
Draft
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
22 changes: 14 additions & 8 deletions labgrid/driver/power/poe_netgear_plus.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from ..exception import ExecutionError


def _get_hostname_and_password(url: str) -> tuple[str, str]:
def _get_hostname_port_and_password(url: str) -> tuple[str, int, str]:
"""Obtain credentials from url or default and return hostname and password.

If no password is in the URL return "P4ssword", which fulfills the minimal requirements from Netgear:
Expand All @@ -35,20 +35,23 @@ def _get_hostname_and_password(url: str) -> tuple[str, str]:
- at least one lower case character
- at least one number

If no port is in the URL, return 80.

Args:
url: A URL with an optional basic auth prefix.

Returns:
A tuple of the hostname, and the extracted or default password
A tuple of the hostname, port and the extracted or default password

"""
parse_result = urlparse(url)
if parse_result.scheme != "http":
raise ExecutionError(f"URL must start with http://, found {parse_result.scheme} for {url}.")

password = "P4ssword" if parse_result.password is None else parse_result.password
password = parse_result.password or "P4ssword"
port = parse_result.port or 80

return parse_result.hostname, password
return parse_result.hostname, port, password


def power_set(host: str, _port: int, index: int, value: bool) -> None:
Expand All @@ -64,9 +67,11 @@ def power_set(host: str, _port: int, index: int, value: bool) -> None:
index = int(index)
netgear_port_number = index + 1

(hostname, password) = _get_hostname_and_password(host)
(hostname, port, password) = _get_hostname_port_and_password(host)

network_address = f"{hostname}:{port}"

sw = NetgearSwitchConnector(hostname, password)
sw = NetgearSwitchConnector(network_address, password)
sw.autodetect_model()
try:
sw.get_login_cookie()
Expand Down Expand Up @@ -97,9 +102,10 @@ def power_get(host: str, _port: int, index: int) -> bool:
index = int(index)
netgear_port_number = index + 1

(hostname, password) = _get_hostname_and_password(host)
(hostname, port, password) = _get_hostname_port_and_password(host)
network_address = f"{hostname}:{port}"

sw = NetgearSwitchConnector(hostname, password)
sw = NetgearSwitchConnector(network_address, password)
sw.autodetect_model()
try:
sw.get_login_cookie()
Expand Down
7 changes: 4 additions & 3 deletions tests/test_powerdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from labgrid.driver import ExecutionError
from labgrid.driver.power.poe_netgear_plus import _get_hostname_and_password
from labgrid.driver.power.poe_netgear_plus import _get_hostname_port_and_password
from labgrid.resource import NetworkPowerPort, YKUSHPowerPort
from labgrid.driver.powerdriver import (
ExternalPowerDriver,
Expand Down Expand Up @@ -393,10 +393,11 @@ class TestPoeNetgearPlusPowerDriver:
]
)
def test_get_hostname_and_password(self, url: str, expected_host: str, expected_pw: str):
returned_host, returned_pw = _get_hostname_and_password(url)
returned_host, returned_port, returned_pw = _get_hostname_port_and_password(url)
assert returned_host == expected_host
assert returned_port == 80
assert returned_pw == expected_pw

def test_get_hostname_and_pw_non_http_raises(self):
with pytest.raises(ExecutionError, match="URL must start with http://"):
_get_hostname_and_password("no_http_protocol")
_get_hostname_port_and_password("no_http_protocol")
Loading