Merge lp:~niedbalski/charm-helpers/fix-1699565 into lp:charm-helpers

Proposed by Jorge Niedbalski
Status: Merged
Merged at revision: 756
Proposed branch: lp:~niedbalski/charm-helpers/fix-1699565
Merge into: lp:charm-helpers
Diff against target: 71 lines (+46/-0)
2 files modified
charmhelpers/contrib/openstack/utils.py (+12/-0)
tests/contrib/openstack/test_os_utils.py (+34/-0)
To merge this branch: bzr merge lp:~niedbalski/charm-helpers/fix-1699565
Reviewer Review Type Date Requested Status
Alex Kavanagh Approve
Felipe Reyes (community) Approve
Review via email: mp+326158@code.launchpad.net

This proposal supersedes a proposal from 2017-06-21.

Description of the change

Partial fix required for LP: #1699565.

To post a comment you must log in.
Revision history for this message
Alex Kavanagh (ajkavanagh) wrote : Posted in a previous version of this proposal

Does this really need to be in charm-helpers? It seems to be something that could just be done in the glance charm. The function 'update_policy(...)' would have to also be mocked out in the glance charm to test the code that it will be using.

Are you seeing other use cases that means that it will be used across many charms? Thanks.

Revision history for this message
Felipe Reyes (freyes) wrote : Posted in a previous version of this proposal

I agree with having this in charm-helpers, because I'm aware of at least 1 other charm that could benefit from this (keystone charm). We update the policy.json when v3 is enabled.

review: Approve
Revision history for this message
Alex Kavanagh (ajkavanagh) wrote : Posted in a previous version of this proposal

Okay, if it is needed, then it should have a more generic name. It's essentially updating a json file, not particularly a policy file, so:

    def update_json_file(filename, items):
        ....

would make it more usable from a re-use perspective. i.e. there's nothing in the function that dictates that it's a policy file only. And this is a utils library module.

Revision history for this message
Jorge Niedbalski (niedbalski) wrote :

Addressed ajkavanagh comments in the latest commit, please review.

Revision history for this message
Felipe Reyes (freyes) :
review: Approve
Revision history for this message
Alex Kavanagh (ajkavanagh) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'charmhelpers/contrib/openstack/utils.py'
--- charmhelpers/contrib/openstack/utils.py 2017-05-25 13:46:14 +0000
+++ charmhelpers/contrib/openstack/utils.py 2017-06-22 14:59:18 +0000
@@ -1982,3 +1982,15 @@
1982 if enable_memcache(source=source, release=release):1982 if enable_memcache(source=source, release=release):
1983 packages.extend(['memcached', 'python-memcache'])1983 packages.extend(['memcached', 'python-memcache'])
1984 return packages1984 return packages
1985
1986
1987def update_json_file(filename, items):
1988 """Updates the json `filename` with a given dict.
1989 :param filename: json filename (i.e.: /etc/glance/policy.json)
1990 :param items: dict of items to update
1991 """
1992 with open(filename) as fd:
1993 policy = json.load(fd)
1994 policy.update(items)
1995 with open(filename, "w") as fd:
1996 fd.write(json.dumps(policy, indent=4))
19851997
=== modified file 'tests/contrib/openstack/test_os_utils.py'
--- tests/contrib/openstack/test_os_utils.py 2017-04-10 18:10:37 +0000
+++ tests/contrib/openstack/test_os_utils.py 2017-06-22 14:59:18 +0000
@@ -1,9 +1,15 @@
1import json1import json
2import mock2import mock
3import unittest3import unittest
4import six
45
5from charmhelpers.contrib.openstack import utils6from charmhelpers.contrib.openstack import utils
67
8if not six.PY3:
9 builtin_open = '__builtin__.open'
10else:
11 builtin_open = 'builtins.open'
12
713
8class UtilsTests(unittest.TestCase):14class UtilsTests(unittest.TestCase):
9 def setUp(self):15 def setUp(self):
@@ -188,3 +194,31 @@
188 _enable_memcache.return_value = True194 _enable_memcache.return_value = True
189 self.assertEqual(utils.token_cache_pkgs(source='distro'),195 self.assertEqual(utils.token_cache_pkgs(source='distro'),
190 ['memcached', 'python-memcache'])196 ['memcached', 'python-memcache'])
197
198 def test_update_json_file(self):
199 TEST_POLICY = """{
200 "delete_image_location": "",
201 "get_image_location": "",
202 "set_image_location": "",
203 "extra_property": "False"
204 }"""
205
206 TEST_POLICY_FILE = "/etc/glance/policy.json"
207
208 item_to_update = {
209 "get_image_location": "role:admin",
210 "extra_policy": "extra",
211 }
212
213 mock_open = mock.mock_open(read_data=TEST_POLICY)
214 with mock.patch(builtin_open, mock_open) as mock_file:
215 utils.update_json_file(TEST_POLICY_FILE, item_to_update)
216 mock_file.assert_has_calls([
217 mock.call(TEST_POLICY_FILE),
218 mock.call(TEST_POLICY_FILE, 'w'),
219 ], any_order=True)
220
221 modified_policy = json.loads(TEST_POLICY)
222 modified_policy.update(item_to_update)
223 mock_open().write.assert_called_with(
224 json.dumps(modified_policy, indent=4))

Subscribers

People subscribed via source and target branches