forked from signalfx/collectd-haproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_haproxy.py
146 lines (112 loc) · 4.62 KB
/
test_haproxy.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
#!/usr/bin/env python
import collections
from mock import MagicMock
from mock import Mock
from mock import patch
import sys
import pytest
import csv
class MockCollectd(MagicMock):
"""
Mocks the functions and objects provided by the collectd module
"""
@staticmethod
def log(log_str):
print log_str
debug = log
info = log
warning = log
error = log
class MockHAProxySocket(object):
def __init__(self, socket_file="whatever"):
self.socket_file = socket_file
def get_server_info(self):
sample_data = {'ConnRate': '3', 'CumReq': '5', 'idle_pct': '78'}
return sample_data
def get_server_stats(self):
sample_data = [{'bin': '3120628', 'lastchg': '', 'lbt': '', 'weight': '',
'wretr': '', 'slim': '50', 'pid': '1', 'wredis': '', 'dresp': '0',
'ereq': '0', 'pxname': 'sample_proxy', 'stot': '39728',
'sid': '0', 'bout': '188112702395', 'qlimit': '', 'status': 'OPEN',
'smax': '2', 'dreq': '0', 'econ': '', 'iid': '2', 'chkfail': '',
'downtime': '', 'qcur': '', 'eresp': '', 'throttle': '', 'scur': '0',
'bck': '', 'qmax': '', 'act': '', 'chkdown': '', 'svname': 'FRONTEND'}]
return sample_data
sys.modules['collectd'] = MockCollectd()
import haproxy
ConfigOption = collections.namedtuple('ConfigOption', ('key', 'values'))
mock_config_default_values = Mock()
mock_config_default_values.children = [
ConfigOption('Testing', ('True',))
]
def test_default_config():
module_config = haproxy.config(mock_config_default_values)
assert module_config['socket'] == '/var/run/haproxy.sock'
assert not module_config['enhanced_metrics']
assert module_config['proxy_monitors'] == ['server', 'frontend', 'backend']
assert module_config['testing']
assert module_config['excluded_metrics'] == set()
mock_config_enhanced_metrics_off = Mock()
mock_config_enhanced_metrics_off.children = [
ConfigOption('Socket', ('/var/run/haproxy.sock',)),
ConfigOption('EnhancedMetrics', ('False',)),
ConfigOption('Testing', ('True',))
]
def test_enhanced_metrics_off_config():
module_config = haproxy.config(mock_config_enhanced_metrics_off)
assert module_config['socket'] == '/var/run/haproxy.sock'
assert not module_config['enhanced_metrics']
assert module_config['proxy_monitors'] == ['server', 'frontend', 'backend']
assert module_config['testing']
assert module_config['excluded_metrics'] == set()
mock_config_enhanced_metrics_on = Mock()
mock_config_enhanced_metrics_on.children = [
ConfigOption('Socket', ('/var/run/haproxy.sock',)),
ConfigOption('EnhancedMetrics', ('True',)),
ConfigOption('Testing', ('True',))
]
def test_enhanced_metrics_on_config():
module_config = haproxy.config(mock_config_enhanced_metrics_on)
assert module_config['socket'] == '/var/run/haproxy.sock'
assert module_config['enhanced_metrics']
assert module_config['proxy_monitors'] == ['server', 'frontend', 'backend']
assert module_config['testing']
assert module_config['excluded_metrics'] == set()
mock_config_exclude_idle_pct = Mock()
mock_config_exclude_idle_pct.children = [
ConfigOption('Socket', ('/var/run/haproxy.sock',)),
ConfigOption('EnhancedMetrics', ('False',)),
ConfigOption('ExcludeMetric', ('idle_pct',)),
ConfigOption('Testing', ('True',))
]
def test_exclude_metrics_config():
module_config = haproxy.config(mock_config_exclude_idle_pct)
assert module_config['socket'] == '/var/run/haproxy.sock'
assert not module_config['enhanced_metrics']
assert module_config['proxy_monitors'] == ['server', 'frontend', 'backend']
assert module_config['testing']
assert module_config['excluded_metrics'] == set(['idle_pct'])
mock_config = Mock()
mock_config.children = [
ConfigOption('Testing', ('True',))
]
@patch('haproxy.HAProxySocket', MockHAProxySocket)
def test_read():
haproxy.collect_metrics(haproxy.config(mock_config))
mock_config_exclude_bytes_out = Mock()
mock_config_exclude_bytes_out.children = [
ConfigOption('ExcludeMetric', ('bytes_out',)),
ConfigOption('Testing', ('True',))
]
@patch('haproxy.HAProxySocket', MockHAProxySocket)
def test_exclude_metric():
haproxy.collect_metrics(haproxy.config(mock_config_exclude_bytes_out))
mock_config_enhanced_sample = Mock()
mock_config_enhanced_sample.children = [
ConfigOption('ProxyMonitor', ('sample_proxy',)),
ConfigOption('EnhancedMetrics', ('True',)),
ConfigOption('Testing', ('True',))
]
@patch('haproxy.HAProxySocket', MockHAProxySocket)
def test_enhanced_metrics():
haproxy.collect_metrics(haproxy.config(mock_config_enhanced_sample))