This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_agent.py
115 lines (98 loc) · 3.72 KB
/
test_agent.py
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
# System under test
import agent as testagent
from StringIO import StringIO
from config import Config
from unittest import TestCase
from mock import patch
test_config = """# we only support one cluster for now
clusters:
[
{
name: "clustername"
port:8000
retries:10
retry_delay:10
# use persistent IP addresses instead of floating here
# listing them in node order is helpful
nodes: [ "10.220.200.1",
"10.220.200.2",
]
# If your cluster has IPMI enabled and you have
# ipmitool installed on the box running this agent,
# set this flag to true
ipmi: {
enabled: True
# IPMI address order should match the order of the nodes above
ipmi_servers: [ "10.10.10.1",
"10.10.10.2",
]
}
}
]
# Specify IP address of SNMP trap receiver
snmp: {
enabled: True
snmp_trap_receiver: '192.168.11.1'
}
# If email is enabled, you will need to set
# environment variables where you are running the
# agent to specify the account and password to
# authorize email access:
#
# SNMP_AGENT_EMAIL_ACCT
# SNMP_AGENT_EMAIL_PWD
#
email: {
enabled: True
server: "smtp.gmail.com"
tls_port:587
ssl_port:465
address_from: "[email protected]"
address_to: "[email protected]"
}
"""
class TestAgent(TestCase):
@patch('qumulo_client.QumuloClient.login')
@patch('pysnmp.carrier.asynsock.dgram.udp.UdpTransport.openServerMode')
@patch('pysnmp.entity.config.addSocketTransport')
def test_check_power_no_ipmi(self, MockAddSocketTransport, MockOpenServerMode, MockQClogin):
"""Test that lack of ipmi connectivity warns, but does not break"""
test_cfg_file = StringIO(test_config)
MockAddSocketTransport.return_value = None
MockOpenServerMode.return_value = None
MockQClogin.return_value = None
cfg = Config(test_cfg_file)
mib = testagent.Mib()
objects = [
testagent.MibObject('QUMULO-MIB', 'testDescription', mib.getTestDescription),
testagent.MibObject('QUMULO-MIB', 'testCount', mib.getTestCount)]
agent = testagent.SNMPAgent(objects)
if cfg.snmp.enabled:
agent.setTrapReceiver(cfg.snmp.snmp_trap_receiver, 'traps')
W = testagent.Worker(agent, mib, cfg)
W.check_power('127.0.0.1', 1)
@patch('agent.SNMPAgent.sendTrap')
@patch('qumulo_client.QumuloClient.get_power_state')
@patch('qumulo_client.QumuloClient.login')
@patch('pysnmp.carrier.asynsock.dgram.udp.UdpTransport.openServerMode')
@patch('pysnmp.entity.config.addSocketTransport')
def test_check_power_good(self, MockAddSocketTransport, MockOpenServerMode, MockQClogin, MockGetPowerState, MockSendTrap):
"""Test that notification of PS recovery does not throw exception"""
test_cfg_file = StringIO(test_config)
MockAddSocketTransport.return_value = None
MockOpenServerMode.return_value = None
MockQClogin.return_value = None
MockGetPowerState.return_value = {'GOOD': {'PS1', 'PS2'}, 'FAIL': set()}
MockSendTrap.return_value = None
cfg = Config(test_cfg_file)
mib = testagent.Mib()
objects = [
testagent.MibObject('QUMULO-MIB', 'testDescription', mib.getTestDescription),
testagent.MibObject('QUMULO-MIB', 'testCount', mib.getTestCount)]
agent = testagent.SNMPAgent(objects)
if cfg.snmp.enabled:
agent.setTrapReceiver(cfg.snmp.snmp_trap_receiver, 'traps')
W = testagent.Worker(agent, mib, cfg)
W.notified_power_supply_failure[0]['PS1'] = True
print W.notified_power_supply_failure
W.check_power('127.0.0.1', 0)