From e9f9607de2ad300c1e324ba603f444507c5f1e1e Mon Sep 17 00:00:00 2001 From: Mark Stumpf Date: Fri, 15 Sep 2017 14:01:56 -0400 Subject: [PATCH] add test functions around haproxy module_config and reads, to be run with pytest --- test_haproxy.py | 146 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 test_haproxy.py diff --git a/test_haproxy.py b/test_haproxy.py new file mode 100644 index 0000000..e25bc4b --- /dev/null +++ b/test_haproxy.py @@ -0,0 +1,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))