1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
# uses the psf/black style
import pytest
import os
from _testutils import MockHidapiDevice, MockPyusbDevice, Report
from liquidctl.driver.hwmon import HwmonDevice
from liquidctl.driver.kraken3 import KrakenX3, KrakenZ3
from liquidctl.driver.kraken3 import (
_COLOR_CHANNELS_KRAKENX,
_SPEED_CHANNELS_KRAKENX,
_COLOR_CHANNELS_KRAKENZ,
_SPEED_CHANNELS_KRAKENZ,
_HWMON_CTRL_MAPPING_KRAKENX,
_HWMON_CTRL_MAPPING_KRAKENZ,
)
from test_krakenz3_response import krakenz3_response
from liquidctl.util import HUE2_MAX_ACCESSORIES_IN_CHANNEL as MAX_ACCESSORIES
from liquidctl.util import Hue2Accessory
# https://github.com/liquidctl/liquidctl/issues/160#issuecomment-664044103
X3_SAMPLE_STATUS = bytes.fromhex(
"7502200036000B51535834353320012101A80635350000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
)
# https://github.com/liquidctl/liquidctl/issues/160#issue-665781804
X3_FAULTY_STATUS = bytes.fromhex(
"7502200036000B5153583435332001FFFFCC0A64640000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
)
Z3_SAMPLE_STATUS = bytes.fromhex(
"75012E0018001051393434363731011803690314140102000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
)
test_curve_final_pwm = [
30,
32,
34,
36,
38,
40,
42,
44,
46,
48,
50,
58,
65,
72,
80,
82,
83,
85,
87,
88,
90,
91,
92,
93,
94,
95,
96,
97,
98,
99,
100,
100,
100,
100,
100,
100,
100,
100,
100,
100,
]
@pytest.fixture
def mock_krakenx3():
raw = MockKraken(raw_led_channels=len(_COLOR_CHANNELS_KRAKENX) - 1)
dev = KrakenX3(
raw,
"Mock Kraken X73",
speed_channels=_SPEED_CHANNELS_KRAKENX,
color_channels=_COLOR_CHANNELS_KRAKENX,
hwmon_ctrl_mapping=_HWMON_CTRL_MAPPING_KRAKENX,
)
dev.connect()
return dev
@pytest.fixture
def mock_krakenz3():
raw = MockKraken(raw_led_channels=1)
dev = MockKrakenZ3(
raw,
"Mock Kraken Z73",
speed_channels=_SPEED_CHANNELS_KRAKENZ,
color_channels=_COLOR_CHANNELS_KRAKENZ,
hwmon_ctrl_mapping=_HWMON_CTRL_MAPPING_KRAKENZ,
bulk_buffer_size=512,
lcd_resolution=(320, 320),
)
dev.connect()
return dev
class MockKraken(MockHidapiDevice):
def __init__(self, raw_led_channels):
super().__init__()
self.raw_led_channels = raw_led_channels
def write(self, data):
reply = bytearray(64)
if data[0:2] == [0x10, 0x01]:
reply[0:2] = [0x11, 0x01]
elif data[0:2] == [0x20, 0x03]:
reply[0:2] = [0x21, 0x03]
reply[14] = self.raw_led_channels
if self.raw_led_channels > 1:
reply[15 + 1 * MAX_ACCESSORIES] = Hue2Accessory.KRAKENX_GEN4_RING.value
reply[15 + 2 * MAX_ACCESSORIES] = Hue2Accessory.KRAKENX_GEN4_LOGO.value
elif data[0:2] == [0x30, 0x01]:
reply[0:2] = [0x31, 0x01]
reply[0x18] = 50 # lcd brightness
reply[0x1A] = 0 # lcd orientation
elif data[0:2] == [0x32, 0x1]: # setup bucket
reply[14] = 0x1
elif data[0:2] == [0x32, 0x2]: # delete bucker
reply[0:2] = [0x33, 0x02]
reply[14] = 0x1
elif data[0:2] == [0x38, 0x1]: # switch bucket
reply[14] = 0x1
self.preload_read(Report(0, reply))
return super().write(data)
class MockKrakenZ3(KrakenZ3):
def __init__(
self,
device,
description,
speed_channels,
color_channels,
bulk_buffer_size,
lcd_resolution,
**kwargs,
):
KrakenX3.__init__(self, device, description, speed_channels, color_channels, **kwargs)
self.bulk_device = MockPyusbDevice(0x1E71, 0x3008)
self.bulk_device.close_winusb_device = self.bulk_device.release
self.orientation = 0
self.brightness = 50
self.bulk_buffer_size = bulk_buffer_size
self.lcd_resolution = lcd_resolution
self.screen_mode = None
self.fw = None
def set_screen(self, channel, mode, value, **kwargs):
self.screen_mode = mode
self.hid_data_index = 0
self.bulk_data_index = 0
super().set_screen(channel, mode, value, **kwargs)
assert self.hid_data_index == len(
krakenz3_response[self.screen_mode + "_hid"]
), f"Incorrect number of hid messages sent for mode: {mode}"
if mode == "static" or mode == "gif":
assert (
self.bulk_data_index == 801
if mode == "static"
else len(krakenz3_response[self.screen_mode + "_bulk"])
), f"Incorrect number of bulk messages sent for mode: {mode}"
def _write(self, data):
if self.screen_mode:
assert (
data == krakenz3_response[self.screen_mode + "_hid"][self.hid_data_index]
), f"HID write failed, wrong data for mode: {self.screen_mode}, data index: {self.hid_data_index}"
self.hid_data_index += 1
return super()._write(data)
def _bulk_write(self, data):
fixed_data_index = self.bulk_data_index
if (
self.screen_mode == "static" and self.bulk_data_index > 1
): # the rest of the message should be identical to index 1
fixed_data_index = 1
assert (
data == krakenz3_response[self.screen_mode + "_bulk"][fixed_data_index]
), f"Bulk write failed, wrong data for mode: {self.screen_mode}, data index: {self.bulk_data_index}"
self.bulk_data_index += 1
return super()._bulk_write(data)
@pytest.mark.parametrize("has_hwmon,direct_access", [(False, False), (True, True), (True, False)])
def test_krakenx3_initializes(mock_krakenx3, has_hwmon, direct_access, tmp_path):
if has_hwmon:
mock_krakenx3._hwmon = HwmonDevice("mock_module", tmp_path)
# TODO check the result
_ = mock_krakenx3.initialize(direct_access=direct_access)
writes = len(mock_krakenx3.device.sent)
if not has_hwmon or direct_access:
assert writes == 4
else:
assert writes == 2
@pytest.mark.parametrize("has_hwmon,direct_access", [(False, False), (True, True)])
def test_krakenx3_reads_status_directly(mock_krakenx3, has_hwmon, direct_access):
if has_hwmon:
mock_krakenx3._hwmon = HwmonDevice(None, None)
mock_krakenx3.device.preload_read(Report(0, X3_SAMPLE_STATUS))
temperature, pump_speed, pump_duty = mock_krakenx3.get_status(direct_access=direct_access)
assert temperature == ("Liquid temperature", 33.1, "°C")
assert pump_speed == ("Pump speed", 1704, "rpm")
assert pump_duty == ("Pump duty", 53, "%")
def test_krakenx3_reads_status_from_hwmon(mock_krakenx3, tmp_path):
mock_krakenx3._hwmon = HwmonDevice("mock_module", tmp_path)
(tmp_path / "temp1_input").write_text("33100\n")
(tmp_path / "fan1_input").write_text("1704\n")
(tmp_path / "pwm1").write_text("135\n")
temperature, pump_speed, pump_duty = mock_krakenx3.get_status()
assert temperature == ("Liquid temperature", 33.1, "°C")
assert pump_speed == ("Pump speed", 1704, "rpm")
assert pump_duty == ("Pump duty", pytest.approx(53, rel=1.0 / 255), "%")
@pytest.mark.parametrize("has_hwmon,direct_access", [(False, False), (True, True)])
def test_krakenx3_set_fixed_speeds_directly(mock_krakenx3, has_hwmon, direct_access, tmp_path):
"""For both test cases only direct access should be used"""
if has_hwmon:
mock_krakenx3._hwmon = HwmonDevice("mock_module", tmp_path)
(tmp_path / "pwm1").write_text("0")
(tmp_path / "pwm1_enable").write_text("0")
mock_krakenx3.set_fixed_speed("pump", 84, direct_access=direct_access)
pump_report = mock_krakenx3.device.sent[0]
assert pump_report.number == 0x72
assert pump_report.data[3:43] == [84 for i in range(0, 39)] + [100]
# Assert that hwmon wasn't touched
if has_hwmon:
assert (tmp_path / "pwm1_enable").read_text() == "0"
assert (tmp_path / "pwm1").read_text() == "0"
@pytest.mark.parametrize("has_support", [False, True])
def test_krakenx3_set_fixed_speeds_hwmon(mock_krakenx3, has_support, tmp_path):
mock_krakenx3._hwmon = HwmonDevice("mock_module", tmp_path)
if has_support:
(tmp_path / "pwm1").write_text("0\n")
(tmp_path / "pwm1_enable").write_text("0\n")
mock_krakenx3.set_fixed_speed("pump", 84)
if has_support:
assert (tmp_path / "pwm1_enable").read_text() == "1"
assert (tmp_path / "pwm1").read_text() == "214"
else:
# Assert fallback to direct access
pump_report = mock_krakenx3.device.sent[0]
assert pump_report.number == 0x72
assert pump_report.data[3:43] == [84 for i in range(0, 39)] + [100]
@pytest.mark.parametrize("has_hwmon,direct_access", [(False, False), (True, True)])
def test_krakenx3_set_speed_profile_directly(mock_krakenx3, has_hwmon, direct_access, tmp_path):
"""For both test cases only direct access should be used"""
if has_hwmon:
mock_krakenx3._hwmon = HwmonDevice("mock_module", tmp_path)
(tmp_path / "pwm1").write_text("0")
(tmp_path / "pwm1_enable").write_text("0")
for i in range(1, 40 + 1):
(tmp_path / f"temp1_auto_point{i}_pwm").write_text("0")
curve_profile = zip([20, 30, 34, 40, 50], [30, 50, 80, 90, 100])
mock_krakenx3.set_speed_profile("pump", curve_profile, direct_access=direct_access)
pump_report = mock_krakenx3.device.sent[0]
assert pump_report.number == 0x72
assert pump_report.data[3:43] == test_curve_final_pwm
# Assert that hwmon wasn't touched
if has_hwmon:
assert (tmp_path / "pwm1_enable").read_text() == "0"
assert (tmp_path / "pwm1").read_text() == "0"
for i in range(1, 40):
assert (tmp_path / f"temp1_auto_point{i}_pwm").read_text() == "0"
@pytest.mark.parametrize("has_support", [False, True])
def test_krakenx3_set_speed_profile_hwmon(mock_krakenx3, has_support, tmp_path):
mock_krakenx3._hwmon = HwmonDevice("mock_module", tmp_path)
if has_support:
(tmp_path / "pwm1_enable").write_text("0\n")
for i in range(1, 40 + 1):
(tmp_path / f"temp1_auto_point{i}_pwm").write_text("0")
curve_profile = zip([20, 30, 34, 40, 50], [30, 50, 80, 90, 100])
mock_krakenx3.set_speed_profile("pump", curve_profile)
if has_support:
assert (tmp_path / "pwm1_enable").read_text() == "2"
for i in range(1, 40 + 1):
assert int((tmp_path / f"temp1_auto_point{i}_pwm").read_text()) == (
test_curve_final_pwm[i - 1] * 255 // 100
)
else:
# Assert fallback to direct access
pump_report = mock_krakenx3.device.sent[0]
assert pump_report.number == 0x72
assert pump_report.data[3:43] == test_curve_final_pwm
@pytest.mark.parametrize("has_hwmon,direct_access", [(False, False), (True, True)])
def test_krakenx3_warns_on_faulty_temperature(mock_krakenx3, has_hwmon, direct_access, caplog):
if has_hwmon:
mock_krakenx3._hwmon = HwmonDevice(None, None)
mock_krakenx3.device.preload_read(Report(0, X3_FAULTY_STATUS))
_ = mock_krakenx3.get_status(direct_access=direct_access)
assert "unexpected temperature reading" in caplog.text
def test_krakenx3_not_totally_broken(mock_krakenx3):
"""Reasonable example calls to untested APIs do not raise exceptions."""
mock_krakenx3.initialize()
mock_krakenx3.set_color(channel="ring", mode="fixed", colors=iter([[3, 2, 1]]), speed="fastest")
mock_krakenx3.set_speed_profile(channel="pump", profile=iter([(20, 20), (30, 50), (40, 100)]))
mock_krakenx3.set_fixed_speed(channel="pump", duty=50)
@pytest.mark.parametrize("has_hwmon,direct_access", [(False, False), (True, True)])
def test_krakenz3_reads_status_directly(mock_krakenz3, has_hwmon, direct_access):
if has_hwmon:
mock_krakenz3._hwmon = HwmonDevice(None, None)
mock_krakenz3.device.preload_read(Report(0, Z3_SAMPLE_STATUS))
temperature, pump_speed, pump_duty, fan_speed, fan_duty = mock_krakenz3.get_status(
direct_access=direct_access
)
assert temperature == ("Liquid temperature", 24.3, "°C")
assert pump_speed == ("Pump speed", 873, "rpm")
assert pump_duty == ("Pump duty", 20, "%")
assert fan_speed == ("Fan speed", 0, "rpm")
assert fan_duty == ("Fan duty", 0, "%")
def test_krakenz3_reads_status_from_hwmon(mock_krakenz3, tmp_path):
mock_krakenz3._hwmon = HwmonDevice("mock_module", tmp_path)
(tmp_path / "temp1_input").write_text("33100\n")
(tmp_path / "fan1_input").write_text("1704\n")
(tmp_path / "pwm1").write_text("135\n")
(tmp_path / "fan2_input").write_text("1704\n")
(tmp_path / "pwm2").write_text("135\n")
temperature, pump_speed, pump_duty, fan_speed, fan_duty = mock_krakenz3.get_status()
assert temperature == ("Liquid temperature", 33.1, "°C")
assert pump_speed == ("Pump speed", 1704, "rpm")
assert pump_duty == ("Pump duty", pytest.approx(53, rel=1.0 / 255), "%")
assert fan_speed == ("Fan speed", 1704, "rpm")
assert fan_duty == ("Fan duty", pytest.approx(53, rel=1.0 / 255), "%")
@pytest.mark.parametrize("has_hwmon,direct_access", [(False, False), (True, True)])
def test_krakenz3_set_fixed_speeds_directly(mock_krakenz3, has_hwmon, direct_access, tmp_path):
"""For both test cases only direct access should be used"""
if has_hwmon:
mock_krakenz3._hwmon = HwmonDevice("mock_module", tmp_path)
(tmp_path / "pwm1").write_text("0")
(tmp_path / "pwm1_enable").write_text("0")
(tmp_path / "pwm2").write_text("0")
(tmp_path / "pwm2_enable").write_text("0")
mock_krakenz3.set_fixed_speed("pump", 84, direct_access=direct_access)
mock_krakenz3.set_fixed_speed("fan", 50, direct_access=direct_access)
pump_report, fan_report = mock_krakenz3.device.sent
assert pump_report.number == 0x72
assert pump_report.data[3:43] == [84 for i in range(0, 39)] + [100]
assert fan_report.number == 0x72
assert fan_report.data[3:43] == [50 for i in range(0, 39)] + [100]
# Assert that hwmon wasn't touched
if has_hwmon:
assert (tmp_path / "pwm1_enable").read_text() == "0"
assert (tmp_path / "pwm1").read_text() == "0"
assert (tmp_path / "pwm2_enable").read_text() == "0"
assert (tmp_path / "pwm2").read_text() == "0"
@pytest.mark.parametrize("has_support", [False, True])
def test_krakenz3_set_fixed_speeds_hwmon(mock_krakenz3, has_support, tmp_path):
mock_krakenz3._hwmon = HwmonDevice("mock_module", tmp_path)
if has_support:
(tmp_path / "pwm1").write_text("0\n")
(tmp_path / "pwm1_enable").write_text("0\n")
(tmp_path / "pwm2").write_text("0\n")
(tmp_path / "pwm2_enable").write_text("0\n")
mock_krakenz3.set_fixed_speed("pump", 84)
mock_krakenz3.set_fixed_speed("fan", 50)
if has_support:
assert (tmp_path / "pwm1_enable").read_text() == "1"
assert (tmp_path / "pwm1").read_text() == "214"
assert (tmp_path / "pwm2_enable").read_text() == "1"
assert (tmp_path / "pwm2").read_text() == "127"
else:
# Assert fallback to direct access
pump_report, fan_report = mock_krakenz3.device.sent
assert pump_report.number == 0x72
assert pump_report.data[3:43] == [84 for i in range(0, 39)] + [100]
assert fan_report.number == 0x72
assert fan_report.data[3:43] == [50 for i in range(0, 39)] + [100]
@pytest.mark.parametrize("has_hwmon,direct_access", [(False, False), (True, True)])
def test_krakenz3_set_speed_profile_directly(mock_krakenz3, has_hwmon, direct_access, tmp_path):
"""For both test cases only direct access should be used"""
if has_hwmon:
mock_krakenz3._hwmon = HwmonDevice("mock_module", tmp_path)
(tmp_path / "pwm1").write_text("0")
(tmp_path / "pwm1_enable").write_text("0")
(tmp_path / "pwm2").write_text("0")
(tmp_path / "pwm2_enable").write_text("0")
for i in range(1, 40 + 1):
(tmp_path / f"temp1_auto_point{i}_pwm").write_text("0")
(tmp_path / f"temp2_auto_point{i}_pwm").write_text("0")
mock_krakenz3.set_speed_profile(
"pump", zip([20, 30, 34, 40, 50], [30, 50, 80, 90, 100]), direct_access=direct_access
)
mock_krakenz3.set_speed_profile(
"fan", zip([20, 30, 34, 40, 50], [30, 50, 80, 90, 100]), direct_access=direct_access
)
pump_report, fan_report = mock_krakenz3.device.sent
assert pump_report.number == 0x72
assert pump_report.data[3:43] == test_curve_final_pwm
assert fan_report.number == 0x72
assert fan_report.data[3:43] == test_curve_final_pwm
# Assert that hwmon wasn't touched
if has_hwmon:
assert (tmp_path / "pwm1_enable").read_text() == "0"
assert (tmp_path / "pwm1").read_text() == "0"
assert (tmp_path / "pwm2_enable").read_text() == "0"
assert (tmp_path / "pwm2").read_text() == "0"
for i in range(1, 40):
assert (tmp_path / f"temp1_auto_point{i}_pwm").read_text() == "0"
assert (tmp_path / f"temp2_auto_point{i}_pwm").read_text() == "0"
@pytest.mark.parametrize("has_support", [False, True])
def test_krakenz3_set_speed_profile_hwmon(mock_krakenz3, has_support, tmp_path):
mock_krakenz3._hwmon = HwmonDevice("mock_module", tmp_path)
if has_support:
(tmp_path / "pwm1_enable").write_text("0\n")
(tmp_path / "pwm2_enable").write_text("0\n")
for i in range(1, 40 + 1):
(tmp_path / f"temp1_auto_point{i}_pwm").write_text("0")
(tmp_path / f"temp2_auto_point{i}_pwm").write_text("0")
mock_krakenz3.set_speed_profile("pump", zip([20, 30, 34, 40, 50], [30, 50, 80, 90, 100]))
mock_krakenz3.set_speed_profile("fan", zip([20, 30, 34, 40, 50], [30, 50, 80, 90, 100]))
if has_support:
assert (tmp_path / "pwm1_enable").read_text() == "2"
for i in range(1, 40 + 1):
assert int((tmp_path / f"temp1_auto_point{i}_pwm").read_text()) == (
test_curve_final_pwm[i - 1] * 255 // 100
)
assert int((tmp_path / f"temp2_auto_point{i}_pwm").read_text()) == (
test_curve_final_pwm[i - 1] * 255 // 100
)
else:
# Assert fallback to direct access
pump_report, fan_report = mock_krakenz3.device.sent
assert pump_report.number == 0x72
assert pump_report.data[3:43] == test_curve_final_pwm
assert fan_report.number == 0x72
assert fan_report.data[3:43] == test_curve_final_pwm
def test_krakenz3_not_totally_broken(mock_krakenz3):
"""Reasonable example calls to untested APIs do not raise exceptions."""
mock_krakenz3.initialize()
mock_krakenz3.device.preload_read(Report(0, Z3_SAMPLE_STATUS))
_ = mock_krakenz3.get_status()
mock_krakenz3.set_speed_profile(channel="fan", profile=iter([(20, 20), (30, 50), (40, 100)]))
mock_krakenz3.set_fixed_speed(channel="pump", duty=50)
def test_krakenz3_screen_not_totally_broken(mock_krakenz3):
"""Reasonable example calls to untested APIs do not raise exceptions."""
mock_krakenz3.initialize()
mock_krakenz3.set_screen("lcd", "liquid", None)
mock_krakenz3.set_screen("lcd", "brightness", "60")
mock_krakenz3.set_screen("lcd", "orientation", "90")
mock_krakenz3.set_screen(
"lcd", "static", os.path.join(os.path.dirname(os.path.abspath(__file__)), "yellow.jpg")
)
@pytest.mark.skip("Currently broken with pillow >= 10.2.0 (see #661)")
def test_krakenz3_screen_not_totally_broken_part2(mock_krakenz3):
"""Reasonable example calls to untested APIs do not raise exceptions."""
mock_krakenz3.initialize()
mock_krakenz3.set_screen(
"lcd", "gif", os.path.join(os.path.dirname(os.path.abspath(__file__)), "rgb.gif")
)
|