-
Notifications
You must be signed in to change notification settings - Fork 30
/
test_fritzcollectd.py
310 lines (254 loc) · 11 KB
/
test_fritzcollectd.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
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
# fritzcollectd - FRITZ!Box collectd plugin
# Copyright (c) 2014-2017 Christian Fetzer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
""" Tests for fritzcollectd """
from __future__ import print_function
import collections
import sys
import mock
from lxml.etree import XMLSyntaxError # pylint: disable=no-name-in-module
from nose.tools import raises, nottest, with_setup
class CollectdMock(object):
""" Mocks the collectd object that is injected into plugins when
collectd loads them. This allows to use the fritzcollectd module
independently from collectd. """
def __init__(self):
self._cb_init = None
self._cb_config = None
self._cb_read = None
self._cb_shutdown = None
self.values = []
self.info = mock.Mock()
self.warning = mock.Mock()
def reset_mock(self):
""" Reset values and mock calls. Must be called for every test. """
del self.values[:]
self.info.reset_mock()
self.warning.reset_mock()
def register_config(self, cb_config):
""" Plugins are required to register a configuration callback. The
callback is called when the plugin configuration is loaded. """
self._cb_config = cb_config
def register_init(self, cb_init):
""" Plugins are required to register an initialization callback. The
callback is called when a plugin is loaded. """
self._cb_init = cb_init
def register_read(self, cb_read):
""" Plugins are required to register a read callback. The callback is
called periodically to read data. """
self._cb_read = cb_read
def register_shutdown(self, cb_shutdown):
""" Plugins can register a shutdown callback. The callback is called
when collectd shuts down. """
self._cb_shutdown = cb_shutdown
def process(self, config=None):
""" Simulates collectd. Call callbacks once. """
if config is None:
config = CollectdConfig()
try:
self._cb_config(config)
self._cb_init()
self._cb_read()
finally:
self._cb_shutdown()
def Values(self): # pylint: disable=invalid-name
""" Plugins call this in their read callback in order to report
measurements to collectd. """
return CollectdValues(self)
class CollectdConfig(object): # pylint: disable=too-few-public-methods
""" Config element passed to the collectd configuration callback. """
def __init__(self, config=None):
if config is None:
self._config = {}
else:
self._config = config
def __str__(self):
return str(self.children)
@property
def children(self):
""" Property passed to the collectd configuration callback. """
node = collections.namedtuple('Node', ['key', 'values'])
return [node(key=k, values=[v]) for k, v in self._config.items()]
class CollectdValues(object): # pylint: disable=too-few-public-methods
""" Represents a container class in which plugins can report
measurements to collectd. """
def __init__(self, collectd_mock):
self.host = ''
self.plugin = ''
self.plugin_instance = ''
self.type = ''
self.type_instance = ''
self.values = []
self._collectd_mock = collectd_mock
def __repr__(self):
return 'Values({}, {}, {})'.format(
self.type, self.type_instance, self.values[0])
def dispatch(self):
""" Dispatch measurements to collectd. """
self._collectd_mock.values.append(self)
class FritzConnectionMock(object): # pylint: disable=too-few-public-methods
""" Mock for fritzconnection so that the fritzcollectd module can be used
without a real router in unit tests. The mock is default configured
to support the normal (good case) tests. """
FRITZBOX_DATA = {
('WANIPConnection', 'GetStatusInfo'):
{'NewConnectionStatus': 'Connected',
'NewUptime': 35307},
('WANCommonInterfaceConfig', 'GetCommonLinkProperties'):
{'NewLayer1DownstreamMaxBitRate': 10087000,
'NewLayer1UpstreamMaxBitRate': 2105000,
'NewPhysicalLinkStatus': 'Up'},
('WANCommonInterfaceConfig', 'GetAddonInfos'):
{'NewByteSendRate': 3438,
'NewByteReceiveRate': 67649,
'NewTotalBytesSent': 1712232562,
'NewTotalBytesReceived': 5221019883},
('LANEthernetInterfaceConfig', 'GetStatistics'):
{'NewBytesSent': 23004321,
'NewBytesReceived': 12045}
}
FRITZBOX_DATA_INDEXED = {
('X_AVM-DE_Homeauto:1', 'GetGenericDeviceInfos'):
[{'NewSwitchIsValid': 'VALID',
'NewMultimeterIsValid': 'VALID',
'NewTemperatureIsValid': 'VALID',
'NewDeviceId': 16,
'NewAIN': '08761 0114116',
'NewDeviceName': 'FRITZ!DECT 200 #1',
'NewTemperatureOffset': '0',
'NewSwitchLock': '0',
'NewProductName': 'FRITZ!DECT 200',
'NewPresent': 'CONNECTED',
'NewMultimeterPower': 1673,
'NewHkrComfortTemperature': '0',
'NewSwitchMode': 'AUTO',
'NewManufacturer': 'AVM',
'NewMultimeterIsEnabled': 'ENABLED',
'NewHkrIsTemperature': '0',
'NewFunctionBitMask': 2944,
'NewTemperatureIsEnabled': 'ENABLED',
'NewSwitchState': 'ON',
'NewSwitchIsEnabled': 'ENABLED',
'NewFirmwareVersion': '03.87',
'NewHkrSetVentilStatus': 'CLOSED',
'NewMultimeterEnergy': 5182,
'NewHkrComfortVentilStatus': 'CLOSED',
'NewHkrReduceTemperature': '0',
'NewHkrReduceVentilStatus': 'CLOSED',
'NewHkrIsEnabled': 'DISABLED',
'NewHkrSetTemperature': '0',
'NewTemperatureCelsius': '225',
'NewHkrIsValid': 'INVALID'}, {}]
}
MODELNAME = 'FRITZ!Box 7490'
def __init__(self):
type(self).modelname = mock.PropertyMock(return_value=self.MODELNAME)
self.call_action = mock.Mock(side_effect=self._side_effect_callaction)
def _side_effect_callaction(self, service, action, **kwargs):
if kwargs:
index = next(iter(kwargs.values()))
return self.FRITZBOX_DATA_INDEXED[(service, action)][index]
return self.FRITZBOX_DATA[(service, action)]
# Instantiate mock so that tests can be executed without collectd.
MOCK = CollectdMock()
sys.modules['collectd'] = MOCK
import fritzcollectd # noqa, pylint: disable=unused-import, wrong-import-position
@mock.patch('fritzconnection.FritzConnection', autospec=True)
@with_setup(teardown=MOCK.reset_mock)
def test_basic(fc_class_mock):
""" Basic test with default configuration. """
fc_class_mock.return_value = FritzConnectionMock()
MOCK.process()
assert MOCK.values
@mock.patch('fritzconnection.FritzConnection', autospec=True)
@with_setup(teardown=MOCK.reset_mock)
def test_configuration(fc_class_mock):
""" Test if configuration parameters have the intended behavior. """
config = CollectdConfig({'Address': 'localhost', 'Port': 1234,
'User': 'user', 'Password': 'password',
'Hostname': 'hostname', 'Instance': 'instance',
'Verbose': 'False', 'UNKNOWN': 'UNKNOWN'})
fc_class_mock.return_value = FritzConnectionMock()
MOCK.process(config)
fc_class_mock.assert_has_calls(
[mock.call(address='localhost', password='password',
port=1234, user='user')])
assert MOCK.warning.called
assert MOCK.values
assert MOCK.values[0].host == 'hostname'
assert MOCK.values[0].plugin_instance == 'instance'
@mock.patch('fritzconnection.FritzConnection', autospec=True)
@with_setup(teardown=MOCK.reset_mock)
def test_configuration_verbose(fc_class_mock):
""" Test if the verbose setting causes info messages to appear. """
config = CollectdConfig({'Password': 'password', 'Verbose': 'True'})
fc_class_mock.return_value = FritzConnectionMock()
MOCK.process(config)
assert MOCK.info.called
assert MOCK.values
@mock.patch('fritzconnection.FritzConnection', autospec=True)
@with_setup(teardown=MOCK.reset_mock)
@raises(IOError)
def test_connection_error(fc_class_mock):
""" Simulate connection error to router. """
fc_mock = FritzConnectionMock()
fc_class_mock.return_value = fc_mock
type(fc_mock).modelname = mock.PropertyMock(return_value=None)
MOCK.process()
@mock.patch('fritzconnection.FritzConnection', autospec=True)
@with_setup(teardown=MOCK.reset_mock)
@raises(IOError)
def test_upnp_status_disabled(fc_class_mock):
""" Simulate that UPnP status is deactivated on router. """
fc_mock = FritzConnectionMock()
fc_class_mock.return_value = fc_mock
fc_mock.call_action.side_effect = [{}]
MOCK.process()
@mock.patch('fritzconnection.FritzConnection', autospec=True)
@with_setup(teardown=MOCK.reset_mock)
@raises(IOError)
def test_incorrect_password(fc_class_mock):
""" Simulate an incorrect password on router. """
fc_mock = FritzConnectionMock()
fc_class_mock.return_value = fc_mock
fc_mock.call_action.side_effect = [{0}, XMLSyntaxError(0, 0, 0, 0)]
MOCK.process(CollectdConfig({'Password': 'incorrect'}))
@mock.patch('fritzconnection.FritzConnection', autospec=True)
@with_setup(teardown=MOCK.reset_mock)
def test_xmlsyntaxerror_in_read(fc_class_mock):
""" Simulate an XMLSyntaxError exception when reading data. """
fc_mock = FritzConnectionMock()
fc_class_mock.return_value = fc_mock
fc_mock.call_action.side_effect = [{0}, XMLSyntaxError(0, 0, 0, 0), {0}]
MOCK.process()
# System tests that try to interact with a real hardware device.
@nottest
@with_setup(teardown=MOCK.reset_mock)
def test_system_connection():
""" System test: Read values of real router. """
MOCK.process()
print(MOCK.values)
assert MOCK.values
@nottest
@with_setup(teardown=MOCK.reset_mock)
@raises(IOError)
def test_system_connectionfailure():
""" System test: Attempt to connect to localhost (connection failure). """
MOCK.process(CollectdConfig({'Address': 'localhost'}))