def setUp(self): config = get_collector_config('RedisCollector', { 'interval': '1', 'databases': 1, }) self.collector = RedisCollector(config, None)
def test_key_naming_when_using_instances(self, publish_mock): config_data = { 'instances': ['nick1@host1:1111', 'nick2@:2222', 'nick3@host3', 'bla'] } get_info_data = { 'total_connections_received': 200, 'total_commands_processed': 100, } expected_calls = [ call('nick1.process.connections_received', 200, precision=0, metric_type='GAUGE'), call('nick1.process.commands_processed', 100, precision=0, metric_type='GAUGE'), call('nick2.process.connections_received', 200, precision=0, metric_type='GAUGE'), call('nick2.process.commands_processed', 100, precision=0, metric_type='GAUGE'), call('nick3.process.connections_received', 200, precision=0, metric_type='GAUGE'), call('nick3.process.commands_processed', 100, precision=0, metric_type='GAUGE'), call('6379.process.connections_received', 200, precision=0, metric_type='GAUGE'), call('6379.process.commands_processed', 100, precision=0, metric_type='GAUGE'), ] config = get_collector_config('RedisCollector', config_data) collector = RedisCollector(config, None) patch_c = patch.object(RedisCollector, '_get_info', Mock(return_value=get_info_data)) patch_c.start() collector.collect() patch_c.stop() self.assertEqual(publish_mock.call_count, len(expected_calls)) for exp_call in expected_calls: # Test expected calls 1 by 1, # because self.instances is a dict (=random order) publish_mock.assert_has_calls(exp_call)
def test_process_config_with_instances(self, publish_mock): config_data = { 'instances': [ 'nick1@host1:1111', 'nick2@:2222', 'nick3@host3', 'nick4@host4:3333/@pass/word', 'bla' ] } expected_processed_config = { 'nick2': ('localhost', 2222, None, None), 'nick3': ('host3', 6379, None, None), 'nick1': ('host1', 1111, None, None), 'nick4': ('host4', 3333, None, '@pass/word'), '6379': ('bla', 6379, None, None) } config = get_collector_config('RedisCollector', config_data) collector = RedisCollector(config, None) self.assertEqual(collector.instances, expected_processed_config)
def test_hostport_or_instance_config(self, publish_mock): testcases = { 'default': { 'config': {}, # test default settings 'calls': [call('6379', 'localhost', 6379, None, None)], }, 'host_set': { 'config': {'host': 'myhost'}, 'calls': [call('6379', 'myhost', 6379, None, None)], }, 'port_set': { 'config': {'port': 5005}, 'calls': [call('5005', 'localhost', 5005, None, None)], }, 'hostport_set': { 'config': {'host': 'megahost', 'port': 5005}, 'calls': [call('5005', 'megahost', 5005, None, None)], }, 'portauth_set': { 'config': {'port': 5005, 'auth': 'pass'}, 'calls': [call('5005', 'localhost', 5005, None, 'pass')], }, 'unix_socket_host_set': { 'config': {'host': 'unix:/var/run/redis/myhost.sock'}, 'calls': [call('myhost', 'localhost', 6379, '/var/run/redis/myhost.sock', None)], }, 'instance_1_host': { 'config': {'instances': ['nick@myhost']}, 'calls': [call('nick', 'myhost', 6379, None, None)], }, 'unix_socket_instance_1_host': { 'config': {'instances': [ 'nick@unix:/var/run/redis/myhost.sock' ]}, 'calls': [call('nick', 'localhost', 6379, '/var/run/redis/myhost.sock', None)], }, 'unix_socket_instance_1_hostauth': { 'config': {'instances': [ 'nick@unix:/var/run/redis/myhost.sock:/pass' ]}, 'calls': [call('nick', 'localhost', 6379, '/var/run/redis/myhost.sock', 'pass')], }, 'instance_1_port': { 'config': {'instances': ['nick@:9191']}, 'calls': [call('nick', 'localhost', 9191, None, None)], }, 'instance_1_hostport': { 'config': {'instances': ['nick@host1:8765']}, 'calls': [call('nick', 'host1', 8765, None, None)], }, 'instance_2': { 'config': {'instances': [ 'foo@hostX', 'bar@:1000/pass', 'unix:/var/run/redis/myhost.sock:1/pass' ]}, 'calls': [ call('foo', 'hostX', 6379, None, None), call('bar', 'localhost', 1000, None, 'pass'), call('myhost', 'localhost', 6379, '/var/run/redis/myhost.sock', 'pass'), ], }, 'old_and_new': { 'config': { 'host': 'myhost', 'port': 1234, 'instances': [ 'foo@hostX', 'bar@:1000', 'hostonly', ':1234' ] }, 'calls': [ call('foo', 'hostX', 6379, None, None), call('bar', 'localhost', 1000, None, None), call('6379', 'hostonly', 6379, None, None), call('1234', 'localhost', 1234, None, None), ], }, } for testname, data in testcases.items(): config = get_collector_config('RedisCollector', data['config']) collector = RedisCollector(config, None) mock = Mock(return_value={}, name=testname) patch_c = patch.object(RedisCollector, 'collect_instance', mock) patch_c.start() collector.collect() patch_c.stop() expected_call_count = len(data['calls']) self.assertEqual(mock.call_count, expected_call_count, msg='[%s] mock.calls=%d != expected_calls=%d' % (testname, mock.call_count, expected_call_count)) for exp_call in data['calls']: # Test expected calls 1 by 1, # because self.instances is a dict (=random order) mock.assert_has_calls(exp_call)
def test_key_naming_when_using_instances(self, publish_mock): config_data = { 'instances': [ 'nick1@host1:1111', 'nick2@:2222', 'nick3@host3', 'nick4@host4:3333/@password', 'bla' ] } get_info_data = { 'role': 'slave', 'total_connections_received': 200, 'total_commands_processed': 100, } expected_calls = [ call('nick1.process.connections_received', 200, precision=0, metric_type='GAUGE'), call('nick1.process.commands_processed', 100, precision=0, metric_type='GAUGE'), call('nick1.replication.master', 0, precision=0, metric_type='GAUGE'), call('nick2.process.connections_received', 200, precision=0, metric_type='GAUGE'), call('nick2.process.commands_processed', 100, precision=0, metric_type='GAUGE'), call('nick2.replication.master', 0, precision=0, metric_type='GAUGE'), call('nick3.process.connections_received', 200, precision=0, metric_type='GAUGE'), call('nick3.process.commands_processed', 100, precision=0, metric_type='GAUGE'), call('nick3.replication.master', 0, precision=0, metric_type='GAUGE'), call('nick4.process.connections_received', 200, precision=0, metric_type='GAUGE'), call('nick4.process.commands_processed', 100, precision=0, metric_type='GAUGE'), call('nick4.replication.master', 0, precision=0, metric_type='GAUGE'), call('6379.process.connections_received', 200, precision=0, metric_type='GAUGE'), call('6379.process.commands_processed', 100, precision=0, metric_type='GAUGE'), call('6379.replication.master', 0, precision=0, metric_type='GAUGE'), ] config = get_collector_config('RedisCollector', config_data) collector = RedisCollector(config, None) patch_c = patch.object(RedisCollector, '_get_info', Mock(return_value=get_info_data)) patch_c.start() collector.collect() patch_c.stop() self.assertEqual(publish_mock.call_count, len(expected_calls)) publish_mock.assert_has_calls(expected_calls, any_order=True)