def test_register_exporter(self): exporter = mock.Mock() execution_context.clear() execution_context.set_measure_to_view_map(MeasureToViewMap()) view_manager = view_manager_module.ViewManager() view_manager.register_exporter(exporter) count_registered_exporters = len( view_manager.measure_to_view_map.exporters) self.assertEqual(1, count_registered_exporters)
def test_constructor_defaults(self): execution_context.clear() self.assertEqual({}, execution_context.get_measure_to_view_map()) stats_recorder = stats_recorder_module.StatsRecorder() self.assertIsNotNone(execution_context.get_measure_to_view_map()) self.assertIs(stats_recorder.measure_to_view_map, execution_context.get_measure_to_view_map()) self.assertIsNotNone(stats_recorder.measure_to_view_map)
def test_get_and_set(self): execution_context.clear() execution_context.get_measure_to_view_map() self.assertEqual({}, execution_context.get_measure_to_view_map()) measure_to_view_map = {'key1', 'val1'} execution_context.set_measure_to_view_map(measure_to_view_map) execution_context.get_measure_to_view_map() self.assertEqual(measure_to_view_map, execution_context.get_measure_to_view_map())
def test_get_all_exported_views(self): execution_context.clear() execution_context.set_measure_to_view_map(MeasureToViewMap()) view_manager = view_manager_module.ViewManager() exported_views = view_manager.get_all_exported_views() self.assertIsNotNone(exported_views) view_manager_mock = mock.Mock() view_manager = view_manager_mock view_manager.get_all_exported_views() self.assertTrue(view_manager_mock.get_all_exported_views.called)
def test_get_view(self): view_name = mock.Mock() execution_context.clear() execution_context.set_measure_to_view_map(MeasureToViewMap()) view_manager = view_manager_module.ViewManager() view_manager.get_view(view_name=view_name) view_manager_mock = mock.Mock() view_manager = view_manager_mock view_manager.get_view(view_name=mock.Mock()) self.assertTrue(view_manager_mock.get_view.called)
def test_constructor(self): execution_context.clear() self.assertEqual({}, execution_context.get_measure_to_view_map()) view_manager = view_manager_module.ViewManager() self.assertIsNotNone(view_manager.time) self.assertEqual(view_manager.measure_to_view_map, execution_context.get_measure_to_view_map()) execution_context.clear() measure_to_view_map = {'key1': 'val1'} execution_context.set_measure_to_view_map(measure_to_view_map) self.assertEqual(measure_to_view_map, execution_context.get_measure_to_view_map())
def setup_create_timeseries_test(self): client = mock.Mock() execution_context.clear() option = stackdriver.Options(project_id="project-test", resource="global") exporter = stackdriver.StackdriverStatsExporter(options=option, client=client) stats = stats_module.Stats() view_manager = stats.view_manager stats_recorder = stats.stats_recorder if len(view_manager.measure_to_view_map.exporters) > 0: view_manager.unregister_exporter( view_manager.measure_to_view_map.exporters[0]) view_manager.register_exporter(exporter) return view_manager, stats_recorder, exporter
def test_create_timeseries_with_resource(self, monitor_resource_mock): client = mock.Mock() execution_context.clear() option = stackdriver.Options(project_id="project-test", resource="") exporter = stackdriver.StackdriverStatsExporter(options=option, client=client) stats = stats_module.stats view_manager = stats.view_manager stats_recorder = stats.stats_recorder if len(view_manager.measure_to_view_map.exporters) > 0: view_manager.unregister_exporter( view_manager.measure_to_view_map.exporters[0]) view_manager.register_exporter(exporter) view_manager.register_view(VIDEO_SIZE_VIEW) tag_value = tag_value_module.TagValue("1200") tag_map = tag_map_module.TagMap() tag_map.insert(FRONTEND_KEY, tag_value) measure_map = stats_recorder.new_measurement_map() measure_map.measure_int_put(VIDEO_SIZE_MEASURE, 25 * MiB) measure_map.record(tag_map) v_data = measure_map.measure_to_view_map.get_view( VIDEO_SIZE_VIEW_NAME, None) v_data = metric_utils.view_data_to_metric(v_data, TEST_TIME) # check for gce_instance monitored resource mocked_labels = { 'instance_id': 'my-instance', 'project_id': 'my-project', 'zone': 'us-east1', 'pod_id': 'localhost', 'namespace_id': 'namespace' } mock_resource = mock.Mock() mock_resource.get_type.return_value = 'gce_instance' mock_resource.get_labels.return_value = mocked_labels monitor_resource_mock.return_value = mock_resource time_series_list = exporter.create_time_series_list(v_data) self.assertEqual(len(time_series_list), 1) time_series = time_series_list[0] self.assertEqual(time_series.resource.type, "gce_instance") self.check_labels( time_series.resource.labels, { 'instance_id': 'my-instance', 'project_id': 'my-project', 'zone': 'us-east1', }) self.assertEqual( time_series.metric.type, "custom.googleapis.com/opencensus/my.org/views/video_size_test2") self.assertIsNotNone(time_series) time_series_list = exporter.create_time_series_list(v_data) self.assertEqual(len(time_series_list), 1) time_series = time_series_list[0] self.assertEqual( time_series.metric.type, "custom.googleapis.com/opencensus/my.org/views/video_size_test2") # check for gke_container monitored resource mocked_labels = { 'instance_id': 'my-instance', 'project_id': 'my-project', 'zone': 'us-east1', 'pod_id': 'localhost', 'cluster_name': 'cluster', 'namespace_id': 'namespace' } mock_resource = mock.Mock() mock_resource.get_type.return_value = 'gke_container' mock_resource.get_labels.return_value = mocked_labels monitor_resource_mock.return_value = mock_resource time_series_list = exporter.create_time_series_list(v_data) self.assertEqual(len(time_series_list), 1) time_series = time_series_list[0] self.assertEqual(time_series.resource.type, "k8s_container") self.check_labels( time_series.resource.labels, { 'project_id': 'my-project', 'location': 'us-east1', 'cluster_name': 'cluster', 'pod_name': 'localhost', 'namespace_name': 'namespace', }) self.assertEqual( time_series.metric.type, "custom.googleapis.com/opencensus/my.org/views/video_size_test2") self.assertIsNotNone(time_series) # check for aws_ec2_instance monitored resource mocked_labels = { 'instance_id': 'my-instance', 'aws_account': 'my-project', 'region': 'us-east1', } mock_resource = mock.Mock() mock_resource.get_type.return_value = 'aws_ec2_instance' mock_resource.get_labels.return_value = mocked_labels monitor_resource_mock.return_value = mock_resource time_series_list = exporter.create_time_series_list(v_data) self.assertEqual(len(time_series_list), 1) time_series = time_series_list[0] self.assertEqual(time_series.resource.type, "aws_ec2_instance") self.check_labels( time_series.resource.labels, { 'instance_id': 'my-instance', 'aws_account': 'my-project', 'region': 'aws:us-east1', }) self.assertEqual( time_series.metric.type, "custom.googleapis.com/opencensus/my.org/views/video_size_test2") self.assertIsNotNone(time_series) # check for out of box monitored resource mock_resource = mock.Mock() mock_resource.get_type.return_value = '' mock_resource.get_labels.return_value = mock.Mock() monitor_resource_mock.return_value = mock_resource time_series_list = exporter.create_time_series_list(v_data) self.assertEqual(len(time_series_list), 1) time_series = time_series_list[0] self.assertEqual(time_series.resource.type, 'global') self.check_labels(time_series.resource.labels, {}) self.assertEqual( time_series.metric.type, "custom.googleapis.com/opencensus/my.org/views/video_size_test2") self.assertIsNotNone(time_series)
def tearDown(self): execution_context.clear()