This repository was archived by the owner on Jan 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdriver_test.py
More file actions
154 lines (123 loc) · 5.16 KB
/
driver_test.py
File metadata and controls
154 lines (123 loc) · 5.16 KB
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
from unittest.mock import MagicMock, patch
import pytest
from pysnmp.entity import config as snmp_config
from jumpstarter_driver_snmp.driver import AuthProtocol, PrivProtocol, SNMPServer
class MockMibObject:
def getInstIdFromIndices(self, *args):
return (1, 3, 6)
def setup_mock_snmp_engine():
mock_engine = MagicMock()
mock_builder = MagicMock()
mock_entry = MockMibObject()
mock_builder.import_symbols.return_value = [mock_entry]
mock_engine.get_mib_builder.return_value = mock_builder
mock_engine.transport_dispatcher = MagicMock()
mock_engine.transport_dispatcher.start = MagicMock()
mock_engine.transport_dispatcher.stop = MagicMock()
return mock_engine
@pytest.mark.parametrize(
"auth_config",
[
{
"user": "usr-no-auth",
"auth_protocol": AuthProtocol.NONE,
"auth_key": None,
"priv_protocol": PrivProtocol.NONE,
"priv_key": None,
"expected_args_len": 2, # only user and engine args for noAuth
},
{
"user": "usr-md5-none",
"auth_protocol": AuthProtocol.MD5,
"auth_key": "authkey1",
"priv_protocol": PrivProtocol.NONE,
"priv_key": None,
"expected_args_len": 4, # engine, user, auth_protocol, auth_key
},
{
"user": "usr-sha-des",
"auth_protocol": AuthProtocol.SHA,
"auth_key": "authkey1",
"priv_protocol": PrivProtocol.DES,
"priv_key": "privkey1",
"expected_args_len": 6, # engine, user, auth_protocol, auth_key, priv_protocol, priv_key
},
],
)
@pytest.mark.anyio
async def test_snmp_auth_configurations(auth_config):
"""Test different SNMP authentication configurations"""
with (
patch("pysnmp.entity.config.add_v3_user") as mock_add_user,
patch("pysnmp.entity.engine.SnmpEngine", return_value=setup_mock_snmp_engine()),
patch("pysnmp.entity.config.add_target_parameters"),
patch("pysnmp.entity.config.add_target_address"),
patch("pysnmp.entity.config.add_transport"),
):
server = SNMPServer(
host="localhost",
user=auth_config["user"],
plug=1,
auth_protocol=auth_config["auth_protocol"],
auth_key=auth_config["auth_key"],
priv_protocol=auth_config["priv_protocol"],
priv_key=auth_config["priv_key"],
)
server._setup_snmp()
args, _ = mock_add_user.call_args
assert len(args) == auth_config["expected_args_len"]
assert args[1] == auth_config["user"]
if auth_config["auth_protocol"] != AuthProtocol.NONE:
if auth_config["auth_protocol"] == AuthProtocol.MD5:
expected_auth = snmp_config.USM_AUTH_HMAC96_MD5
else:
expected_auth = snmp_config.USM_AUTH_HMAC96_SHA
assert args[2] == expected_auth
assert args[3] == auth_config["auth_key"]
if auth_config["priv_protocol"] != PrivProtocol.NONE:
if auth_config["priv_protocol"] == PrivProtocol.DES:
expected_priv = snmp_config.USM_PRIV_CBC56_DES
else:
expected_priv = snmp_config.USM_PRIV_CFB128_AES
assert args[4] == expected_priv
assert args[5] == auth_config["priv_key"]
@patch("pysnmp.entity.config.add_v3_user")
@patch("pysnmp.entity.engine.SnmpEngine")
@pytest.mark.anyio
async def test_power_on_command(mock_engine, mock_add_user):
"""Test power on command execution"""
mock_engine.return_value = setup_mock_snmp_engine()
with (
patch("pysnmp.entity.rfc3413.cmdgen.SetCommandGenerator.send_varbinds") as mock_send,
patch("pysnmp.entity.config.add_target_parameters"),
patch("pysnmp.entity.config.add_target_address"),
patch("pysnmp.entity.config.add_transport"),
):
server = SNMPServer(host="localhost", user="testuser", plug=1)
def side_effect(*args):
callback = args[-1]
callback(None, None, None, None, None, [], None)
mock_send.side_effect = side_effect
result = await server.on()
assert "Power ON command sent successfully" in result
mock_send.assert_called_once()
@patch("pysnmp.entity.config.add_v3_user")
@patch("pysnmp.entity.engine.SnmpEngine")
@pytest.mark.anyio
async def test_power_off_command(mock_engine, mock_add_user):
"""Test power off command execution"""
mock_engine.return_value = setup_mock_snmp_engine()
with (
patch("pysnmp.entity.rfc3413.cmdgen.SetCommandGenerator.send_varbinds") as mock_send,
patch("pysnmp.entity.config.add_target_parameters"),
patch("pysnmp.entity.config.add_target_address"),
patch("pysnmp.entity.config.add_transport"),
):
server = SNMPServer(host="localhost", user="testuser", plug=1)
def side_effect(*args):
callback = args[-1]
callback(None, None, None, None, None, [], None)
mock_send.side_effect = side_effect
result = await server.off()
assert "Power OFF command sent successfully" in result
mock_send.assert_called_once()