Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from unittest.mock import MagicMock
2import pytest
4from pdu import PDUState, PDUPort, PDU
7def test_PDUState_UNKNOW_is_invalid_action():
8 assert PDUState.UNKNOWN not in PDUState.valid_actions()
9 assert PDUState.UNKNOWN.is_valid_action is False
12def test_PDUState_valid_actions_contain_basics():
13 for action in ["ON", "OFF", "REBOOT"]:
14 assert action in [s.name for s in PDUState.valid_actions()]
15 assert getattr(PDUState, action).is_valid_action is True
18def test_PDUPort_get_set():
19 pdu = MagicMock(get_port_state=MagicMock(return_value=PDUState.OFF))
20 port = PDUPort(pdu, 42, label="My Port")
21 assert port.label == "My Port"
23 pdu.set_port_state.assert_not_called()
24 pdu.get_port_state.assert_not_called()
26 port.set(PDUState.ON)
27 pdu.set_port_state.assert_called_with(42, PDUState.ON)
29 assert port.state == PDUState.OFF
30 pdu.get_port_state.assert_called_with(42)
33def test_PDU_defaults():
34 pdu = PDU("MyPDU")
36 assert pdu.name == "MyPDU"
37 assert pdu.ports == []
38 assert pdu.set_port_state(42, PDUState.ON) is False
39 assert pdu.get_port_state(42) == PDUState.UNKNOWN
42def test_PDU_instances():
43 pdu1 = PDU("MyPDU")
44 PDU("MyPDU 2")
45 pdu2 = PDU("MyPDU 2")
46 pdu3 = PDU("MyPDU 3")
48 assert PDU.get_by_name("MyPDU 2") == pdu2
50 assert PDU.registered_pdus() == {
51 "MyPDU": pdu1,
52 "MyPDU 2": pdu2,
53 "MyPDU 3": pdu3,
54 }
55 pdu2.unregister()
56 assert PDU.registered_pdus() == {
57 "MyPDU": pdu1,
58 "MyPDU 3": pdu3,
59 }
61 PDU.full_reset()
62 assert PDU.registered_pdus() == {}
65def test_PDU_supported_pdus():
66 pdus = PDU.supported_pdus()
67 assert "dummy" in pdus
70def test_PDU_register():
71 with pytest.raises(ValueError):
72 PDU.register("not_a_real_driver", "MyPDU", config={})
74 pdu = PDU.register("dummy", "MyPDU", config={"ports": ["p1", "p2"]})
75 assert [p.label for p in pdu.ports] == ['p1', 'p2']