Merge lp:~stub/charm-helpers/fix-config into lp:charm-helpers

Proposed by Stuart Bishop
Status: Merged
Merged at revision: 408
Proposed branch: lp:~stub/charm-helpers/fix-config
Merge into: lp:charm-helpers
Diff against target: 56 lines (+15/-2)
3 files modified
charmhelpers/core/hookenv.py (+2/-1)
tests/core/test_hookenv.py (+11/-0)
tests/fetch/test_fetch.py (+2/-1)
To merge this branch: bzr merge lp:~stub/charm-helpers/fix-config
Reviewer Review Type Date Requested Status
Tim Van Steenburgh Approve
Review via email: mp+265475@code.launchpad.net

Description of the change

The charmhelpers.core.hookenv.Config class loads state from disk into its
'previous' snapshot, and then selectively copies items into its 'current'
snapshot. It failed to deepcopy, so dicts and lists were copied by reference
and mutating them would not flag them as changed.

Fix this.

To post a comment you must log in.
Revision history for this message
Stuart Bishop (stub) wrote :

Also fix some test log spam that should never have been landed

Revision history for this message
Tim Van Steenburgh (tvansteenburgh) wrote :

+1, LGTM.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/core/hookenv.py'
2--- charmhelpers/core/hookenv.py 2015-06-30 15:28:49 +0000
3+++ charmhelpers/core/hookenv.py 2015-07-22 06:02:10 +0000
4@@ -21,6 +21,7 @@
5 # Charm Helpers Developers <juju@lists.ubuntu.com>
6
7 from __future__ import print_function
8+import copy
9 from distutils.version import LooseVersion
10 from functools import wraps
11 import glob
12@@ -263,7 +264,7 @@
13 self.path = path or self.path
14 with open(self.path) as f:
15 self._prev_dict = json.load(f)
16- for k, v in self._prev_dict.items():
17+ for k, v in copy.deepcopy(self._prev_dict).items():
18 if k not in self:
19 self[k] = v
20
21
22=== modified file 'tests/core/test_hookenv.py'
23--- tests/core/test_hookenv.py 2015-06-26 14:17:43 +0000
24+++ tests/core/test_hookenv.py 2015-07-22 06:02:10 +0000
25@@ -127,6 +127,17 @@
26 self.assertEqual(c, json.load(f))
27 self.assertEqual(c, dict(foo='bar', a='b'))
28
29+ def test_deep_change(self):
30+ # After loading stored data into our previous dictionary,
31+ # it gets copied into our current dictionary. If this is not
32+ # a deep copy, then mappings and lists will be shared instances
33+ # and changes will not be detected.
34+ c = hookenv.Config(dict(l=[]))
35+ c.save()
36+ c = hookenv.Config()
37+ c['l'].append(42)
38+ self.assertTrue(c.changed('l'), 'load_previous() did not deepcopy')
39+
40 def test_getitem(self):
41 c = hookenv.Config(dict(foo='bar'))
42 c.save()
43
44=== modified file 'tests/fetch/test_fetch.py'
45--- tests/fetch/test_fetch.py 2015-07-14 18:12:04 +0000
46+++ tests/fetch/test_fetch.py 2015-07-22 06:02:10 +0000
47@@ -336,8 +336,9 @@
48 "garbage",
49 )
50
51+ @patch('charmhelpers.fetch.log')
52 @patch('charmhelpers.fetch.plugins')
53- def test_installs_remote(self, _plugins):
54+ def test_installs_remote(self, _plugins, _log):
55 h1 = MagicMock(name="h1")
56 h1.can_handle.return_value = "Nope"
57

Subscribers

People subscribed via source and target branches