From b795b755aeccb09b21d50d97a1fcb5a78426a9ba Mon Sep 17 00:00:00 2001 From: liu-sheng Date: Fri, 6 Mar 2015 10:56:37 +0800 Subject: [PATCH] Check the namespaces duplication for ceilometer-polling This change add validation for the namespaces duplication when launch a ceilometer-polling service with duplicated namespaces specified by --polling-namespaces option. Change-Id: Iacd80b77bda4ae16d0307ebed6aa9903204c94d2 Closes-Bug: #1428476 --- ceilometer/cmd/polling.py | 17 ++++++++++++++++- ceilometer/tests/test_bin.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/ceilometer/cmd/polling.py b/ceilometer/cmd/polling.py index d642a35d..d7238df3 100644 --- a/ceilometer/cmd/polling.py +++ b/ceilometer/cmd/polling.py @@ -17,16 +17,20 @@ from oslo_config import cfg from ceilometer.agent import manager +from ceilometer.i18n import _LW +from ceilometer.openstack.common import log from ceilometer.openstack.common import service as os_service from ceilometer import service +LOG = log.getLogger(__name__) + CONF = cfg.CONF class MultiChoicesOpt(cfg.Opt): def __init__(self, name, choices=None, **kwargs): super(MultiChoicesOpt, self).__init__(name, - type=cfg.types.List(), + type=DeduplicatedCfgList(), **kwargs) self.choices = choices @@ -39,6 +43,17 @@ class MultiChoicesOpt(cfg.Opt): kwargs['choices'] = choices return kwargs + +class DeduplicatedCfgList(cfg.types.List): + def __call__(self, *args, **kwargs): + result = super(DeduplicatedCfgList, self).__call__(*args, **kwargs) + if len(result) != len(set(result)): + LOG.warning(_LW("Duplicated values: %s found in CLI options, " + "auto de-duplidated"), result) + result = list(set(result)) + return result + + CLI_OPTS = [ MultiChoicesOpt('polling-namespaces', default=['compute', 'central'], diff --git a/ceilometer/tests/test_bin.py b/ceilometer/tests/test_bin.py index 43eceb93..ebc37ae3 100644 --- a/ceilometer/tests/test_bin.py +++ b/ceilometer/tests/test_bin.py @@ -211,3 +211,33 @@ class BinApiTestCase(base.BaseTestCase): response, content = self.get_response('v2/meters') self.assertEqual(200, response.status) self.assertEqual([], json.loads(content)) + + +class BinCeilometerPollingServiceTestCase(base.BaseTestCase): + def setUp(self): + super(BinCeilometerPollingServiceTestCase, self).setUp() + content = ("[DEFAULT]\n" + "rpc_backend=fake\n" + "[database]\n" + "connection=log://localhost\n") + self.tempfile = fileutils.write_to_tempfile(content=content, + prefix='ceilometer', + suffix='.conf') + self.subp = None + + def tearDown(self): + super(BinCeilometerPollingServiceTestCase, self).tearDown() + if self.subp: + self.subp.kill() + os.remove(self.tempfile) + + def test_starting_with_duplication_namespaces(self): + self.subp = subprocess.Popen(['ceilometer-polling', + "--config-file=%s" % self.tempfile, + "--polling-namespaces", + "compute", + "compute"], + stderr=subprocess.PIPE) + out = self.subp.stderr.read(1024) + self.assertIn('Duplicated values: [\'compute\', \'compute\'] ' + 'found in CLI options, auto de-duplidated', out)