Merge lp:~mew/charm-helpers/no-host-magic into lp:charm-helpers

Proposed by Matthew Wedgwood
Status: Merged
Merged at revision: 59
Proposed branch: lp:~mew/charm-helpers/no-host-magic
Merge into: lp:charm-helpers
Diff against target: 190 lines (+26/-57)
2 files modified
charmhelpers/core/host.py (+7/-10)
tests/core/test_host.py (+19/-47)
To merge this branch: bzr merge lp:~mew/charm-helpers/no-host-magic
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Review via email: mp+174038@code.launchpad.net

Description of the change

Remove magic value interpolation from host.py. Similar functionality exists
in contrib.templating.

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

Is host.symlink() useful at all now? I guess os.symlink() doesn't force creation and no logging.

Looks good to me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/core/host.py'
2--- charmhelpers/core/host.py 2013-07-09 10:43:41 +0000
3+++ charmhelpers/core/host.py 2013-07-10 20:02:32 +0000
4@@ -14,7 +14,7 @@
5
6 from collections import OrderedDict
7
8-from hookenv import log, execution_environment
9+from hookenv import log
10
11
12 def service_start(service_name):
13@@ -74,36 +74,33 @@
14
15 def rsync(from_path, to_path, flags='-r', options=None):
16 """Replicate the contents of a path"""
17- context = execution_environment()
18 options = options or ['--delete', '--executability']
19 cmd = ['/usr/bin/rsync', flags]
20 cmd.extend(options)
21- cmd.append(from_path.format(**context))
22- cmd.append(to_path.format(**context))
23+ cmd.append(from_path)
24+ cmd.append(to_path)
25 log(" ".join(cmd))
26 return subprocess.check_output(cmd).strip()
27
28
29 def symlink(source, destination):
30 """Create a symbolic link"""
31- context = execution_environment()
32 log("Symlinking {} as {}".format(source, destination))
33 cmd = [
34 'ln',
35 '-sf',
36- source.format(**context),
37- destination.format(**context)
38+ source,
39+ destination,
40 ]
41 subprocess.check_call(cmd)
42
43
44 def mkdir(path, owner='root', group='root', perms=0555, force=False):
45 """Create a directory"""
46- context = execution_environment()
47 log("Making dir {} {}:{} {:o}".format(path, owner, group,
48 perms))
49- uid = pwd.getpwnam(owner.format(**context)).pw_uid
50- gid = grp.getgrnam(group.format(**context)).gr_gid
51+ uid = pwd.getpwnam(owner).pw_uid
52+ gid = grp.getgrnam(group).gr_gid
53 realpath = os.path.abspath(path)
54 if os.path.exists(realpath):
55 if force and not os.path.isdir(realpath):
56
57=== modified file 'tests/core/test_host.py'
58--- tests/core/test_host.py 2013-07-09 10:43:41 +0000
59+++ tests/core/test_host.py 2013-07-10 20:02:32 +0000
60@@ -252,15 +252,10 @@
61 ])
62
63 @patch('subprocess.check_output')
64- @patch.object(host, 'execution_environment')
65 @patch.object(host, 'log')
66- def test_rsyncs_a_path(self, log, execution_environment, check_output):
67- execution_environment.return_value = {
68- 'foo': 'FOO',
69- 'bar': 'BAR',
70- }
71- from_path = '/from/this/path/{foo}'
72- to_path = '/to/this/path/{bar}'
73+ def test_rsyncs_a_path(self, log, check_output):
74+ from_path = '/from/this/path/foo'
75+ to_path = '/to/this/path/bar'
76 check_output.return_value = ' some output '
77
78 result = host.rsync(from_path, to_path)
79@@ -268,42 +263,31 @@
80 self.assertEqual(result, 'some output')
81 check_output.assert_called_with(['/usr/bin/rsync', '-r', '--delete',
82 '--executability',
83- '/from/this/path/FOO',
84- '/to/this/path/BAR'])
85+ '/from/this/path/foo',
86+ '/to/this/path/bar'])
87
88 @patch('subprocess.check_call')
89- @patch.object(host, 'execution_environment')
90 @patch.object(host, 'log')
91- def test_creates_a_symlink(self, log, execution_environment, check_call):
92- execution_environment.return_value = {
93- 'foo': 'FOO',
94- 'bar': 'BAR',
95- }
96- source = '/from/this/path/{foo}'
97- destination = '/to/this/path/{bar}'
98+ def test_creates_a_symlink(self, log, check_call):
99+ source = '/from/this/path/foo'
100+ destination = '/to/this/path/bar'
101
102 host.symlink(source, destination)
103
104 check_call.assert_called_with(['ln', '-sf',
105- '/from/this/path/FOO',
106- '/to/this/path/BAR'])
107+ '/from/this/path/foo',
108+ '/to/this/path/bar'])
109
110 @patch('pwd.getpwnam')
111 @patch('grp.getgrnam')
112- @patch.object(host, 'execution_environment')
113 @patch.object(host, 'log')
114 @patch.object(host, 'os')
115 def test_creates_a_directory_if_it_doesnt_exist(self, os_, log,
116- execution_environment,
117 getgrnam, getpwnam):
118- execution_environment.return_value = {
119- 'foo': 'FOO',
120- 'bar': 'BAR',
121- }
122 uid = 123
123 gid = 234
124- owner = 'some-user-{foo}'
125- group = 'some-group-{bar}'
126+ owner = 'some-user'
127+ group = 'some-group'
128 path = '/some/other/path/from/link'
129 realpath = '/some/path'
130 path_exists = False
131@@ -316,22 +300,16 @@
132
133 host.mkdir(path, owner=owner, group=group, perms=perms)
134
135- getpwnam.assert_called_with('some-user-FOO')
136- getgrnam.assert_called_with('some-group-BAR')
137+ getpwnam.assert_called_with('some-user')
138+ getgrnam.assert_called_with('some-group')
139 os_.path.abspath.assert_called_with(path)
140 os_.path.exists.assert_called_with(realpath)
141 os_.makedirs.assert_called_with(realpath, perms)
142 os_.chown.assert_called_with(realpath, uid, gid)
143
144- @patch.object(host, 'execution_environment')
145 @patch.object(host, 'log')
146 @patch.object(host, 'os')
147- def test_creates_a_directory_with_defaults(self, os_, log,
148- execution_environment):
149- execution_environment.return_value = {
150- 'foo': 'FOO',
151- 'bar': 'BAR',
152- }
153+ def test_creates_a_directory_with_defaults(self, os_, log):
154 uid = 0
155 gid = 0
156 path = '/some/other/path/from/link'
157@@ -351,20 +329,14 @@
158
159 @patch('pwd.getpwnam')
160 @patch('grp.getgrnam')
161- @patch.object(host, 'execution_environment')
162 @patch.object(host, 'log')
163 @patch.object(host, 'os')
164 def test_removes_file_with_same_path_before_mkdir(self, os_, log,
165- execution_environment,
166 getgrnam, getpwnam):
167- execution_environment.return_value = {
168- 'foo': 'FOO',
169- 'bar': 'BAR',
170- }
171 uid = 123
172 gid = 234
173- owner = 'some-user-{foo}'
174- group = 'some-group-{bar}'
175+ owner = 'some-user'
176+ group = 'some-group'
177 path = '/some/other/path/from/link'
178 realpath = '/some/path'
179 path_exists = True
180@@ -380,8 +352,8 @@
181
182 host.mkdir(path, owner=owner, group=group, perms=perms, force=force)
183
184- getpwnam.assert_called_with('some-user-FOO')
185- getgrnam.assert_called_with('some-group-BAR')
186+ getpwnam.assert_called_with('some-user')
187+ getgrnam.assert_called_with('some-group')
188 os_.path.abspath.assert_called_with(path)
189 os_.path.exists.assert_called_with(realpath)
190 os_.unlink.assert_called_with(realpath)

Subscribers

People subscribed via source and target branches