From 80045c13e2ec1f6f74e220c24416d98ed258fc0d Mon Sep 17 00:00:00 2001 From: Igor Degtiarov Date: Tue, 28 Apr 2015 18:56:09 +0300 Subject: [PATCH] [unittests] Increase agent module unittests coverage Add tests to increase unittest coverage of ceilometer.agent and ceilometer.hardware modules. Change-Id: I8ca246f9ace4427c3cf0dd4d314995c4815b59e4 --- ceilometer/tests/agent/test_discovery.py | 55 ++++++++++++++++++++++++ ceilometer/tests/agent/test_manager.py | 12 +++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/ceilometer/tests/agent/test_discovery.py b/ceilometer/tests/agent/test_discovery.py index 882dd5187..a2827c8dc 100644 --- a/ceilometer/tests/agent/test_discovery.py +++ b/ceilometer/tests/agent/test_discovery.py @@ -20,6 +20,8 @@ from oslo_config import fixture as fixture_config from oslotest import base from ceilometer.agent.discovery import endpoint +from ceilometer.agent.discovery import localnode +from ceilometer.hardware import discovery as hardware class TestEndpointDiscovery(base.BaseTestCase): @@ -51,3 +53,56 @@ class TestEndpointDiscovery(base.BaseTestCase): self.assertEqual(expected, self.manager.keystone.service_catalog.get_urls .call_args_list) + + def test_keystone_called_no_endpoints(self): + self.manager.keystone.service_catalog.get_urls.return_value = [] + self.assertEqual([], self.discovery.discover(self.manager)) + + +class TestLocalnodeDiscovery(base.BaseTestCase): + def setUp(self): + super(TestLocalnodeDiscovery, self).setUp() + self.discovery = localnode.LocalNodeDiscovery() + self.manager = mock.MagicMock() + + def test_lockalnode_discovery(self): + self.assertEqual(['local_host'], self.discovery.discover(self.manager)) + + +class TestHardwareDiscovery(base.BaseTestCase): + class MockInstance(object): + addresses = {'ctlplane': [ + {'addr': '0.0.0.0', + 'OS-EXT-IPS-MAC:mac_addr': '01-23-45-67-89-ab'} + ]} + id = 'resource_id' + image = {'id': 'image_id'} + flavor = {'id': 'flavor_id'} + + expected = { + 'resource_id': 'resource_id', + 'resource_url': 'snmp://ro_snmp_user:password@0.0.0.0', + 'mac_addr': '01-23-45-67-89-ab', + 'image_id': 'image_id', + 'flavor_id': 'flavor_id', + } + + def setUp(self): + super(TestHardwareDiscovery, self).setUp() + self.discovery = hardware.NodesDiscoveryTripleO() + self.discovery.nova_cli = mock.MagicMock() + self.manager = mock.MagicMock() + + def test_hardware_discovery(self): + self.discovery.nova_cli.instance_get_all.return_value = [ + self.MockInstance()] + resources = self.discovery.discover(self.manager) + self.assertEqual(1, len(resources)) + self.assertEqual(self.expected, resources[0]) + + def test_hardware_discovery_without_flavor(self): + instance = self.MockInstance() + instance.flavor = {} + self.discovery.nova_cli.instance_get_all.return_value = [instance] + resources = self.discovery.discover(self.manager) + self.assertEqual(0, len(resources)) diff --git a/ceilometer/tests/agent/test_manager.py b/ceilometer/tests/agent/test_manager.py index ff928ae16..0111c36e1 100644 --- a/ceilometer/tests/agent/test_manager.py +++ b/ceilometer/tests/agent/test_manager.py @@ -20,6 +20,7 @@ from oslotest import base from oslotest import mockpatch from stevedore import extension +from ceilometer.agent import base as agent_base from ceilometer.agent import manager from ceilometer.agent import plugin_base from ceilometer import pipeline @@ -69,7 +70,9 @@ class TestManager(base.BaseTestCase): mock.Mock(return_value=None)) @mock.patch('ceilometer.agent.base.LOG') def test_load_failed_plugins(self, LOG): - mgr = manager.AgentManager(namespaces=['ipmi'], + # Here we additionally check that namespaces will be converted to the + # list if param was not set as a list. + mgr = manager.AgentManager(namespaces='ipmi', pollster_list=['hardware.ipmi.node.*']) # 0 pollsters self.assertEqual(0, len(mgr.extensions)) @@ -104,6 +107,13 @@ class TestManager(base.BaseTestCase): ['ipmi'], ['hardware.ipmi.node.*']) + def test_load_plugins_pollster_list_forbidden(self): + manager.cfg.CONF.set_override('backend_url', 'http://', + group='coordination') + self.assertRaises(agent_base.PollsterListForbidden, + manager.AgentManager, + pollster_list=['disk.*']) + class TestPollsterKeystone(agentbase.TestPollster): @plugin_base.check_keystone