From 21daa1f167188a0f12dda5d65d3a9291a048b9af Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 10 Jan 2013 16:48:45 +0100 Subject: [PATCH] policy: fix policy_file finding Currently, the policy.json file is only looked inside current directory. Let's use cfg.config_dir via the find_file method to find the policy file. Also add some unit test to catch theses errors. This fixes bug #1098206 Change-Id: I896108f1bd66d3be6ec32b50d9dddc6c368f3709 Signed-off-by: Julien Danjou --- ceilometer/policy.py | 6 ++++++ tests/test_policy.py | 51 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 tests/test_policy.py diff --git a/ceilometer/policy.py b/ceilometer/policy.py index 46ad54ad0..8613d9ee5 100644 --- a/ceilometer/policy.py +++ b/ceilometer/policy.py @@ -17,6 +17,8 @@ """Policy Engine For Ceilometer""" +import os + from ceilometer import utils from ceilometer.openstack.common import cfg from ceilometer.openstack.common import policy @@ -42,6 +44,10 @@ def init(): global _POLICY_CACHE if not _POLICY_PATH: _POLICY_PATH = cfg.CONF.policy_file + if not os.path.exists(_POLICY_PATH): + _POLICY_PATH = cfg.CONF.find_file(_POLICY_PATH) + if not _POLICY_PATH: + raise cfg.ConfigFilesNotFoundError([cfg.CONF.policy_file]) utils.read_cached_file(_POLICY_PATH, _POLICY_CACHE, reload_func=_set_brain) diff --git a/tests/test_policy.py b/tests/test_policy.py new file mode 100644 index 000000000..79572ead1 --- /dev/null +++ b/tests/test_policy.py @@ -0,0 +1,51 @@ +# -*- encoding: utf-8 -*- +# +# Copyright © 2013 Julien Danjou +# +# Author: Julien Danjou +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +"""Tests for ceilometer.policy""" + +import os +import tempfile + +from ceilometer.tests import base +from ceilometer import policy +from ceilometer.openstack.common import cfg + + +class TestPolicy(base.TestCase): + def setUp(self): + super(TestPolicy, self).setUp() + # Clear cache + policy._POLICY_PATH = None + policy._POLICY_CACHE = {} + + def tearDown(self): + try: + os.unlink(cfg.CONF.policy_file) + except OSError: + pass + + def test_init(self): + cfg.CONF([]) + cfg.CONF.policy_file = tempfile.mktemp() + with open(cfg.CONF.policy_file, "w") as f: + f.write("{}") + policy.init() + + def test_init_file_not_found(self): + cfg.CONF([]) + cfg.CONF.policy_file = 'foobar.json.does.not.exist' + self.assertRaises(cfg.ConfigFilesNotFoundError, policy.init)