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
1=== modified file 'charmhelpers/contrib/openstack/utils.py'
2--- charmhelpers/contrib/openstack/utils.py 2017-05-25 13:46:14 +0000
3+++ charmhelpers/contrib/openstack/utils.py 2017-06-22 14:59:18 +0000
4@@ -1982,3 +1982,15 @@
5 if enable_memcache(source=source, release=release):
6 packages.extend(['memcached', 'python-memcache'])
7 return packages
8+
9+
10+def update_json_file(filename, items):
11+ """Updates the json `filename` with a given dict.
12+ :param filename: json filename (i.e.: /etc/glance/policy.json)
13+ :param items: dict of items to update
14+ """
15+ with open(filename) as fd:
16+ policy = json.load(fd)
17+ policy.update(items)
18+ with open(filename, "w") as fd:
19+ fd.write(json.dumps(policy, indent=4))
20
21=== modified file 'tests/contrib/openstack/test_os_utils.py'
22--- tests/contrib/openstack/test_os_utils.py 2017-04-10 18:10:37 +0000
23+++ tests/contrib/openstack/test_os_utils.py 2017-06-22 14:59:18 +0000
24@@ -1,9 +1,15 @@
25 import json
26 import mock
27 import unittest
28+import six
29
30 from charmhelpers.contrib.openstack import utils
31
32+if not six.PY3:
33+ builtin_open = '__builtin__.open'
34+else:
35+ builtin_open = 'builtins.open'
36+
37
38 class UtilsTests(unittest.TestCase):
39 def setUp(self):
40@@ -188,3 +194,31 @@
41 _enable_memcache.return_value = True
42 self.assertEqual(utils.token_cache_pkgs(source='distro'),
43 ['memcached', 'python-memcache'])
44+
45+ def test_update_json_file(self):
46+ TEST_POLICY = """{
47+ "delete_image_location": "",
48+ "get_image_location": "",
49+ "set_image_location": "",
50+ "extra_property": "False"
51+ }"""
52+
53+ TEST_POLICY_FILE = "/etc/glance/policy.json"
54+
55+ item_to_update = {
56+ "get_image_location": "role:admin",
57+ "extra_policy": "extra",
58+ }
59+
60+ mock_open = mock.mock_open(read_data=TEST_POLICY)
61+ with mock.patch(builtin_open, mock_open) as mock_file:
62+ utils.update_json_file(TEST_POLICY_FILE, item_to_update)
63+ mock_file.assert_has_calls([
64+ mock.call(TEST_POLICY_FILE),
65+ mock.call(TEST_POLICY_FILE, 'w'),
66+ ], any_order=True)
67+
68+ modified_policy = json.loads(TEST_POLICY)
69+ modified_policy.update(item_to_update)
70+ mock_open().write.assert_called_with(
71+ json.dumps(modified_policy, indent=4))

Subscribers

People subscribed via source and target branches