def test_munin_output(self): cfg = self.make_basic_cfg() plugin = self.create_plugin(cfg) jsfr = JSONFileReporter(output_file=self.filename) register_reporter(jsfr) test1 = EventCounter("test1") register_counter(test1) try: test1.report_event("test1", "value", 2) output_report() plugin.output_data(cfg) self.assertEqual(self.get_last_plugin_output(), ["multigraph test", "test1.value 2"]) plugin.max_file_age_in_seconds = 0.00001 self.clear_last_plugin_output() plugin.output_data(cfg) self.assertEqual(self.get_last_plugin_output(), []) plugin.max_file_age_in_seconds = None plugin.output_data(cfg) self.assertEqual(self.get_last_plugin_output(), ["multigraph test", "test1.value 2"]) os.unlink(self.filename) finally: unregister_counter(counter=test1) unregister_reporter(jsfr)
def test_multiple_counters_one_event(self): test1 = TotalCounter("test1", events=["test"]) register_counter(test1) test2 = TotalCounter("test2", events=["test"]) register_counter(test2) v = SimpleValueReporter() register_reporter(v) report_value("test", 1) output_report() self.assertEquals(v.values_wo_metadata, dict(test1=1, test2=1)) unregister_counter(counter=test1) unregister_counter(counter=test2) unregister_reporter(reporter=v)
def test_json_output(self): filename = "/tmp/json_test.txt" jsfr = JSONFileReporter(output_file=filename) test1 = EventCounter("test1") register_counter(test1) register_reporter(jsfr) try: test1.report_event("test1", "value", 2) output_report() report = JSONFileReporter.safe_read(filename) report = dict([(k, v) for k, v in report.iteritems() if not k.startswith("__")]) self.assertEqual(report, {"test1": 2}) os.unlink(filename) finally: unregister_counter(counter=test1) unregister_reporter(jsfr)
def test_basic_collection(self): test1 = TotalCounter("test1") register_counter(test1) test2 = TotalCounter("test2") register_counter(test2) v = SimpleValueReporter() register_reporter(v) report_value("test1", 1) report_value("test2", 2) output_report() self.assertTrue("__collection_time__" in v.last_values) self.assertEquals(v.values_wo_metadata,dict(test1=1,test2=2)) unregister_counter(counter=test1) unregister_counter(counter=test2) unregister_reporter(reporter=v)