def test_SetCustomHostname(self): config = configobj.ConfigObj() config["server"] = {} config["server"]["collectors_config_path"] = "" config["collectors"] = {} config["collectors"]["default"] = {"hostname": "custom.localhost"} c = Collector(config, []) self.assertEquals("custom.localhost", c.get_hostname())
def test_failed_error_metric_publish(self, mock_publish): c = Collector(self.config_object(), []) self.assertFalse(c.can_publish_metric()) with patch.object(c, 'log'): try: c.publish('metric', "baz") except DiamondException: pass self.assertEquals(len(mock_publish.mock_calls), 0)
def test_failed_error_metric_publish(self, mock_publish): c = Collector(self.config_object(), []) self.assertFalse(c.can_publish_metric()) with patch.object(c, "log"): try: c.publish("metric", "baz") except DiamondException: pass self.assertEquals(len(mock_publish.mock_calls), 0)
def test_SetCustomHostname(self): config = configobj.ConfigObj() config['server'] = {} config['server']['collectors_config_path'] = '' config['collectors'] = {} config['collectors']['default'] = { 'hostname' : 'custom.localhost', } c = Collector(config, []) self.assertEquals('custom.localhost', c.get_hostname())
def test_SetCustomHostname(self): config = configobj.ConfigObj() config['server'] = {} config['server']['collectors_config_path'] = '' config['collectors'] = {} config['collectors']['default'] = { 'hostname': 'custom.localhost', } c = Collector(config, []) self.assertEquals('custom.localhost', c.get_hostname())
def test_blacklist_metrics(self, mock_publish): c = Collector(self.config_object(), []) c.config['metrics_blacklist'] = re.compile('metric1') dimensions = { 'dim1': 'val1', 'dim2': 'val2', } c.dimensions = dimensions c.publish('metric1', 1) assert not mock_publish.called self.assertEqual(c.dimensions, None) dimensions = { 'dim3': 'val3', 'dim4': 'val4', } c.dimensions = dimensions c.publish('metric2', 2) assert mock_publish.called for call in mock_publish.mock_calls: name, args, kwargs = call metric = args[0] self.assertEquals(metric.dimensions, dimensions) self.assertEqual(c.dimensions, None)
def test_SetHostnameViaShellCmd(self): config = configobj.ConfigObj() config['server'] = {} config['server']['collectors_config_path'] = '' config['collectors'] = {} config['collectors']['default'] = { 'hostname': 'echo custom.localhost', 'hostname_method': 'shell', } c = Collector(config, []) self.assertEquals('custom.localhost', c.get_hostname())
def test_SetDimensions(self, mock_publish): c = Collector(self.config_object(), []) dimensions = {"dim1": "alice", "dim2": "chains"} c.dimensions = dimensions c.publish("metric1", 1) for call in mock_publish.mock_calls: name, args, kwargs = call metric = args[0] self.assertEquals(metric.dimensions, dimensions) self.assertEqual(c.dimensions, None)
def test_successful_error_metric(self, mock_publish): c = Collector(self.config_object(), []) mock_socket = Mock() c._socket = mock_socket with patch.object(c, 'log'): try: c.publish('metric', "bar") except DiamondException: pass for call in mock_publish.mock_calls: name, args, kwargs = call metric = args[0] self.assertEqual(metric.path, "servers.Collector.qcollect.collector_errors")
def test_successful_error_metric(self, mock_publish): c = Collector(self.config_object(), []) mock_socket = Mock() c._socket = mock_socket with patch.object(c, "log"): try: c.publish("metric", "bar") except DiamondException: pass for call in mock_publish.mock_calls: name, args, kwargs = call metric = args[0] self.assertEqual(metric.path, "servers.Collector.fullerite.collector_errors")
def test_SetDimensions(self, mock_publish): c = Collector(self.config_object(), []) dimensions = { 'dim1': 'alice', 'dim2': 'chains', } c.dimensions = dimensions c.publish('metric1', 1) for call in mock_publish.mock_calls: name, args, kwargs = call metric = args[0] self.assertEquals(metric.dimensions, dimensions) self.assertEqual(c.dimensions, None)
def test_MergeNetuitiveHandlerConfig(self): config = configobj.ConfigObj() config['server'] = {} config['server']['collectors_config_path'] = '' config['collectors'] = {} config['collectors']['default'] = { 'hostname': 'custom.localhost', } config['handlers'] = {} config['handlers']['NetuitiveHandler'] = { 'url': 'https://api.app.netuitive.com/ingest/infrastructure', 'api_key': '3bd5b41c0cbbbe3e8a1eefb16a6f8c58', } c = Collector(config, []) c.merge_config(config['handlers']['NetuitiveHandler'], prefix='netuitive_') self.assertEquals('https://api.app.netuitive.com/ingest/infrastructure', c.config['netuitive_url']) self.assertEquals('3bd5b41c0cbbbe3e8a1eefb16a6f8c58', c.config['netuitive_api_key'])
def test_SetHostnameViaShellCmdWithErrorSkip(self, patch_time): reset_hostname_cache() config = configobj.ConfigObj() config['server'] = {} config['server']['collectors_config_path'] = '' config['collectors'] = {} config['collectors']['default'] = { 'hostname': 'exit 1', 'hostname_method': 'shell', 'hostname_cache_expiration_interval': '300', 'hostname_cache_skip_errors': True } # Should fail the first time. c = Collector(config, []) self.assertRaises(CalledProcessError, c.get_hostname) # Success patch_time.return_value = 0 config['collectors']['default']['hostname'] = 'echo custom.localhost' c = Collector(config, []) self.assertEquals('custom.localhost', c.get_hostname()) # Should NOT fail before the timeout. patch_time.return_value = 299 config['collectors']['default']['hostname'] = 'exit 1' c = Collector(config, []) self.assertEquals('custom.localhost', c.get_hostname()) # Should NOT fail again after the timeout. patch_time.return_value = 301 config['collectors']['default']['hostname'] = 'exit 1' c = Collector(config, []) self.assertEquals('custom.localhost', c.get_hostname())
def test_batch_size_flush(self): c = Collector(self.config_object(), []) mock_socket = Mock() c._socket = mock_socket c._reconnect = False c.config['max_buffer_size'] = 2 with patch.object(c, 'log'): try: c.publish('metric1', 1) c.publish('metric2', 2) c.publish('metric3', 3) except DiamondException: pass self.assertEquals(mock_socket.sendall.call_count, 1) self.assertEquals(len(c.payload), 1)
def test_SetHostnameViaShellJson(self): reset_hostname_cache() config = configobj.ConfigObj() config['server'] = {} config['server']['collectors_config_path'] = '' config['collectors'] = {} config['collectors']['default'] = { 'hostname': 'python -c "import json; print json.dumps({\'test_key\': \'test_value\'})"', 'hostname_method': 'shell', 'shell_json_key': 'test_key' } c = Collector(config, []) self.assertEquals('test_value', c.get_hostname()) reset_hostname_cache() config['collectors']['default']['shell_json_key'] = 'bad_key' c = Collector(config, []) self.assertRaisesRegexp( DiamondException, 'No value found for shell key', c.get_hostname)
def test_get_metric_path(self, get_hostname_mock): config = configobj.ConfigObj() config['collectors'] = {} config['collectors']['default'] = {} config['collectors']['default']['path_prefix'] = 'poof' config['collectors']['default']['path'] = 'xyz' get_hostname_mock.return_value = 'bar' result = Collector(config, []).get_metric_path('foo') self.assertEqual('poof.bar.xyz.foo', result)
def test_get_metric_path_no_prefix_no_path(self, get_hostname_mock): config = configobj.ConfigObj() config['collectors'] = {} config['collectors']['default'] = {} config['collectors']['default']['path_prefix'] = '' config['collectors']['default']['path'] = '' get_hostname_mock.return_value = None result = Collector(config, []).get_metric_path('foo') self.assertEqual('foo', result)
def test_batch_size_flush(self): c = Collector(self.config_object(), []) mock_socket = Mock() c._socket = mock_socket c._reconnect = False c.config["max_buffer_size"] = 2 with patch.object(c, "log"): try: c.publish("metric1", 1) c.publish("metric2", 2) c.publish("metric3", 3) except DiamondException: pass self.assertEquals(mock_socket.sendall.call_count, 1) self.assertEquals(len(c.payload), 1)
def test_SubConfigOverridesCollectorsConfig(self): temp_dir = tempfile.gettempdir() if not temp_dir: self.skipTest("No temporary directory in system") config = configobj.ConfigObj() config['server'] = {} config['server']['collectors_config_path'] = temp_dir config['collectors'] = {} config['collectors']['default'] = {} self.tmpfile = os.path.join(temp_dir, 'Collector.conf') collectorConfig = configobj.ConfigObj(self.tmpfile) collectorConfig['hostname'] = "Collector.hostname" collectorConfig['named instances'] = {} collectorConfig['named instances']['Instance B'] = {} collectorConfig['named instances']['Instance B']['hostname'] = 'custom.hostname' collectorConfig.write() c = Collector(config, [], instance_name='Instance B') self.assertEquals('custom.hostname', c.get_hostname())
def test_SourceMerge(self): reset_hostname_cache() config = configobj.ConfigObj() config['server'] = {} config['server']['collectors_config_path'] = '' config['collectors'] = {} config['collectors']['default'] = { 'hostname': 'custom.localhost', } c = Collector(config, []) self.assertEquals('custom.localhost', c.get_hostname()) self.assertEquals('custom.localhost', c.construct_host(None)) self.assertEquals('test', c.construct_host('test')) config['collectors']['default']['merge_sources'] = True c = Collector(config, []) self.assertEquals('custom.localhost.test', c.construct_host('test')) config['collectors']['default']['merge_sources_separator'] = '!' c = Collector(config, []) self.assertEquals('custom.localhost!test', c.construct_host('test'))
def test_configure_collector(self): c = Collector(self.config_object(), [], configfile=self.configfile()) self.assertEquals( c.config, { 'ttl_multiplier': 2, 'path_suffix': '', 'measure_collector_time': False, 'metrics_blacklist': None, 'byte_unit': ['byte'], 'instance_prefix': 'instances', 'conf': 'val', 'fulleritePort': 0, 'interval': 5, 'enabled': True, 'alice': 'bob', 'metrics_whitelist': None, 'max_buffer_size': 300, 'path_prefix': 'servers' })
def test_SetHostnameViaShellCmdWithErrorSkipJsonKey(self, patch_time): reset_hostname_cache() config = configobj.ConfigObj() config['server'] = {} config['server']['collectors_config_path'] = '' config['collectors'] = {} config['collectors']['default'] = { 'hostname': 'exit 1', 'hostname_method': 'shell', 'hostname_cache_expiration_interval': '300', 'hostname_cache_skip_errors': True, 'shell_json_key': 'test_key' } # Should fail the first time. c = Collector(config, []) self.assertRaises(CalledProcessError, c.get_hostname) script = 'python -c "import json; print json.dumps({\'test_key\': \'test_value\'})"' # Success patch_time.return_value = 0 config['collectors']['default']['hostname'] = script c = Collector(config, []) self.assertEquals('test_value', c.get_hostname()) # Should NOT fail before the timeout. patch_time.return_value = 299 config['collectors']['default']['hostname'] = 'exit 1' c = Collector(config, []) self.assertEquals('test_value', c.get_hostname()) # Should NOT fail again after the timeout. patch_time.return_value = 301 config['collectors']['default']['hostname'] = 'exit 1' c = Collector(config, []) self.assertEquals('test_value', c.get_hostname())
def test_SetHostnameViaShellCmdWithExpiration(self, patch_time): reset_hostname_cache() config = configobj.ConfigObj() config['server'] = {} config['server']['collectors_config_path'] = '' config['collectors'] = {} config['collectors']['default'] = { 'hostname': 'echo custom.localhost', 'hostname_method': 'shell', 'hostname_cache_expiration_interval': '300' } patch_time.return_value = 0 c = Collector(config, []) self.assertEquals('custom.localhost', c.get_hostname()) patch_time.return_value = 100 config['collectors']['default']['hostname'] = 'echo custom.localhost2' c = Collector(config, []) self.assertEquals('custom.localhost', c.get_hostname()) patch_time.return_value = 301 config['collectors']['default']['hostname'] = 'echo custom.localhost2' c = Collector(config, []) self.assertEquals('custom.localhost2', c.get_hostname())
def test_can_publish_metric(self): c = Collector(self.config_object(), []) self.assertFalse(c.can_publish_metric()) c._socket = "socket" self.assertTrue(c.can_publish_metric())