def test_send_cluster_metrics_compute_metric_raise( self, mock_make_admin_context, mock_get_notifier, mock_cluster_list, mock_create_monitor): CONF.set_override('send_cluster_metrics', True, group='drivers') mock_make_admin_context.return_value = self.context notifier = mock.MagicMock() mock_get_notifier.return_value = notifier mock_cluster_list.return_value = [self.cluster4] self.cluster4.status = cluster_status.CREATE_COMPLETE monitor = mock.MagicMock() monitor.get_metric_names.return_value = ['metric1', 'metric2'] monitor.compute_metric_value.side_effect = Exception( "error on computing metric") mock_create_monitor.return_value = monitor periodic.MagnumPeriodicTasks(CONF)._send_cluster_metrics(self.context) expected_event_type = 'magnum.cluster.metrics.update' expected_msg = { 'user_id': self.cluster4.user_id, 'project_id': self.cluster4.project_id, 'resource_id': self.cluster4.uuid, 'metrics': [] } self.assertEqual(1, mock_create_monitor.call_count) notifier.info.assert_called_once_with( self.context, expected_event_type, expected_msg)
def test_send_cluster_metrics_monitor_none( self, mock_make_admin_context, mock_get_notifier, mock_cluster_list, mock_create_monitor): CONF.set_override('send_cluster_metrics', True, group='drivers') mock_make_admin_context.return_value = self.context notifier = mock.MagicMock() mock_get_notifier.return_value = notifier mock_cluster_list.return_value = [self.cluster4] self.cluster4.status = cluster_status.CREATE_COMPLETE mock_create_monitor.return_value = None periodic.MagnumPeriodicTasks(CONF)._send_cluster_metrics(self.context) self.assertEqual(1, mock_create_monitor.call_count) self.assertEqual(0, notifier.info.call_count)
def test_send_cluster_metrics(self, mock_make_admin_context, mock_get_notifier, mock_cluster_list, mock_create_monitor): """Test if RPC notifier receives the expected message""" CONF.set_override('send_cluster_metrics', True, group='drivers') mock_make_admin_context.return_value = self.context notifier = mock.MagicMock() mock_get_notifier.return_value = notifier mock_cluster_list.return_value = [ self.cluster1, self.cluster2, self.cluster3, self.cluster4 ] self.cluster4.status = cluster_status.CREATE_COMPLETE monitor = mock.MagicMock() monitor.get_metric_names.return_value = ['metric1', 'metric2'] monitor.compute_metric_value.return_value = 30 monitor.get_metric_unit.return_value = '%' mock_create_monitor.return_value = monitor periodic.MagnumPeriodicTasks(CONF)._send_cluster_metrics(self.context) expected_event_type = 'magnum.cluster.metrics.update' expected_metrics = [ { 'name': 'metric1', 'value': 30, 'unit': '%', }, { 'name': 'metric2', 'value': 30, 'unit': '%', }, ] expected_msg = { 'user_id': self.cluster4.user_id, 'project_id': self.cluster4.project_id, 'resource_id': self.cluster4.uuid, 'metrics': expected_metrics } self.assertEqual(1, mock_create_monitor.call_count) notifier.info.assert_called_once_with(self.context, expected_event_type, expected_msg)
def test_send_cluster_metrics(self, mock_make_admin_context, mock_get_notifier, mock_cluster_list, mock_create_monitor): """Test if RPC notifier receives the expected message""" CONF.set_override('send_cluster_metrics', True, group='drivers') mock_make_admin_context.return_value = self.context notifier = mock.MagicMock() mock_get_notifier.return_value = notifier mock_cluster_list.return_value = [self.cluster1, self.cluster2, self.cluster3, self.cluster4] self.cluster4.status = cluster_status.CREATE_COMPLETE monitor = mock.MagicMock() monitor.get_metric_names.return_value = ['metric1', 'metric2'] monitor.compute_metric_value.return_value = 30 monitor.get_metric_unit.return_value = '%' mock_create_monitor.return_value = monitor periodic.MagnumPeriodicTasks(CONF)._send_cluster_metrics(self.context) expected_event_type = 'magnum.cluster.metrics.update' expected_metrics = [ { 'name': 'metric1', 'value': 30, 'unit': '%', }, { 'name': 'metric2', 'value': 30, 'unit': '%', }, ] expected_msg = { 'user_id': self.cluster4.user_id, 'project_id': self.cluster4.project_id, 'resource_id': self.cluster4.uuid, 'metrics': expected_metrics } self.assertEqual(1, mock_create_monitor.call_count) notifier.info.assert_called_once_with( self.context, expected_event_type, expected_msg)
def test_send_cluster_metrics_disable_pull_data( self, mock_make_admin_context, mock_get_notifier, mock_cluster_list, mock_create_monitor): mock_make_admin_context.return_value = self.context notifier = mock.MagicMock() mock_get_notifier.return_value = notifier mock_cluster_list.return_value = [self.cluster1, self.cluster2, self.cluster3, self.cluster4] self.cluster4.status = cluster_status.CREATE_COMPLETE monitor = mock.MagicMock() monitor.get_metric_names.return_value = ['metric1', 'metric2'] monitor.compute_metric_value.return_value = 30 monitor.get_metric_unit.return_value = '%' mock_create_monitor.return_value = monitor CONF.set_override('send_cluster_metrics', False, group='drivers') periodic.MagnumPeriodicTasks(CONF)._send_cluster_metrics(self.context) self.assertEqual(0, mock_create_monitor.call_count) self.assertEqual(0, notifier.info.call_count)
def test_send_cluster_metrics_disable_pull_data(self, mock_make_admin_context, mock_get_notifier, mock_cluster_list, mock_create_monitor): mock_make_admin_context.return_value = self.context notifier = mock.MagicMock() mock_get_notifier.return_value = notifier mock_cluster_list.return_value = [ self.cluster1, self.cluster2, self.cluster3, self.cluster4 ] self.cluster4.status = cluster_status.CREATE_COMPLETE monitor = mock.MagicMock() monitor.get_metric_names.return_value = ['metric1', 'metric2'] monitor.compute_metric_value.return_value = 30 monitor.get_metric_unit.return_value = '%' mock_create_monitor.return_value = monitor CONF.set_override('send_cluster_metrics', False, group='drivers') periodic.MagnumPeriodicTasks(CONF)._send_cluster_metrics(self.context) self.assertEqual(0, mock_create_monitor.call_count) self.assertEqual(0, notifier.info.call_count)