class TestCommandStats(bg_test_utils.TestCaseWithFakeAccessor): metrics = ['metric1', 'metric2'] metadata = MetricMetadata(retention=Retention.from_string('1440*60s')) @patch('sys.stdout', new_callable=StringIO) def get_output(self, args, mock_stdout): self.accessor.drop_all_metrics() for metric in self.metrics: self.accessor.create_metric(self.make_metric( metric, self.metadata)) cmd = command_stats.CommandStats() parser = argparse.ArgumentParser(add_help=False) bg_utils.add_argparse_arguments(parser) cmd.add_arguments(parser) opts = parser.parse_args(args) cmd.run(self.accessor, opts) return mock_stdout.getvalue() def test_simple(self): output = self.get_output([]) self.assertIn('Namespace Metrics Points', output) self.assertIn('2 2880', output) def test_graphite(self): output = self.get_output(['--format', 'graphite']) self.assertIn('metrics.none 2', output) self.assertIn('points.none 2880', output)
class TestCommandDu(bg_test_utils.TestCaseWithFakeAccessor): metrics = ['metric1', 'metric2'] metadata = MetricMetadata(retention=Retention.from_string('1440*60s')) @patch('sys.stdout', new_callable=StringIO) def get_output(self, args, mock_stdout): self.accessor.drop_all_metrics() for metric in self.metrics: self.accessor.create_metric(self.make_metric( metric, self.metadata)) cmd = command_du.CommandDu() parser = argparse.ArgumentParser(add_help=False) bg_utils.add_argparse_arguments(parser) cmd.add_arguments(parser) opts = parser.parse_args(args) cmd.run(self.accessor, opts) return mock_stdout.getvalue() def test_1_metric_default_args(self): output = self.get_output(['-h', '-s', 'metric1']) self.assertIn('metric1', output) self.assertIn('33.8K', output) def test_glob_default_args(self): output = self.get_output(['-h', '-s', '*']) for elmt in self.metrics + ['33.8K', '67.5K', 'TOTAL']: self.assertIn(elmt, output) def test_glob_no_unit_no_total(self): output = self.get_output(['*']) self.assertIn('34560', output) self.assertNotIn('TOTAL', output) def test_human_size(self): du = command_du.CommandDu() self.assertEqual(du._human_size_of_points(1), '24B') self.assertEqual( du._human_size_of_points(1023 / command_du._BYTES_PER_POINT), '1008B') self.assertEqual( du._human_size_of_points(1024 / (command_du._BYTES_PER_POINT - 1)), '1.0K')
def test_document_from_metric_should_build_a_document_from_a_metric(self): p0 = "foo" p1 = "bar" p2 = "baz" metric_name = "%s.%s.%s" % (p0, p1, p2) metric_id = uuid.uuid5( uuid.UUID("{00000000-1111-2222-3333-444444444444}"), metric_name) aggregator = Aggregator.maximum retention_str = "42*1s:43*60s" retention = Retention.from_string(retention_str) carbon_xfilesfactor = 0.5 metadata = MetricMetadata(aggregator, retention, carbon_xfilesfactor) metric = Metric(metric_name, metric_id, metadata) document = bg_elasticsearch.document_from_metric(metric) self.__check_document_value(document, "depth", 2) self.__check_document_value(document, "uuid", metric_id) self.__check_document_value(document, "p0", p0) self.__check_document_value(document, "p1", p1) self.__check_document_value(document, "p2", p2) self.assertTrue("config" in document) document_config = document['config'] self.__check_document_value(document_config, "aggregator", aggregator.name) self.__check_document_value(document_config, "retention", retention_str) self.__check_document_value(document_config, "carbon_xfilesfactor", "%f" % carbon_xfilesfactor) self.assertTrue("created_on" in document) self.assertTrue(isinstance(document['created_on'], datetime.datetime)) self.assertTrue("updated_on" in document) self.assertTrue(isinstance(document['updated_on'], datetime.datetime)) self.assertTrue("read_on" in document) self.assertEqual(document['read_on'], None)