Merge lp:~james-page/charm-helpers/configure_source_proposed into lp:charm-helpers

Proposed by James Page
Status: Merged
Merged at revision: 43
Proposed branch: lp:~james-page/charm-helpers/configure_source_proposed
Merge into: lp:charm-helpers
Diff against target: 188 lines (+75/-12)
4 files modified
charmhelpers/core/host.py (+16/-6)
charmhelpers/fetch/__init__.py (+9/-1)
tests/core/test_host.py (+39/-5)
tests/fetch/test_fetch.py (+11/-0)
To merge this branch: bzr merge lp:~james-page/charm-helpers/configure_source_proposed
Reviewer Review Type Date Requested Status
Matthew Wedgwood (community) Approve
Review via email: mp+172796@code.launchpad.net

Description of the change

Add support for 'proposed' as a valid source configuration.

I needed this to enable easy testing of proposed SRU changes.

To post a comment you must log in.
Revision history for this message
Matthew Wedgwood (mew) wrote :

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/host.py'
2--- charmhelpers/core/host.py 2013-06-24 03:26:19 +0000
3+++ charmhelpers/core/host.py 2013-07-03 11:53:59 +0000
4@@ -48,13 +48,13 @@
5 log('creating user {0}'.format(username))
6 cmd = ['useradd']
7 if system_user or password is None:
8- cmd.append('--system')
9+ cmd.append('--system')
10 else:
11- cmd.extend([
12- '--create-home',
13- '--shell', shell,
14- '--password', password,
15- ])
16+ cmd.extend([
17+ '--create-home',
18+ '--shell', shell,
19+ '--password', password,
20+ ])
21 cmd.append(username)
22 subprocess.check_call(cmd)
23 user_info = pwd.getpwnam(username)
24@@ -261,3 +261,13 @@
25 service('restart', service_name)
26 return wrapped_f
27 return wrap
28+
29+
30+def lsb_release():
31+ '''Return /etc/lsb-release in a dict'''
32+ d = {}
33+ with open('/etc/lsb-release', 'r') as lsb:
34+ for l in lsb:
35+ k, v = l.split('=')
36+ d[k.strip()] = v.strip()
37+ return d
38
39=== modified file 'charmhelpers/fetch/__init__.py'
40--- charmhelpers/fetch/__init__.py 2013-06-27 15:59:39 +0000
41+++ charmhelpers/fetch/__init__.py 2013-07-03 11:53:59 +0000
42@@ -3,7 +3,8 @@
43 from charmhelpers.core.host import (
44 apt_install,
45 apt_update,
46- filter_installed_packages
47+ filter_installed_packages,
48+ lsb_release
49 )
50 from urlparse import (
51 urlparse,
52@@ -18,6 +19,9 @@
53 CLOUD_ARCHIVE = """# Ubuntu Cloud Archive
54 deb http://ubuntu-cloud.archive.canonical.com/ubuntu {} main
55 """
56+PROPOSED_POCKET = """# Proposed
57+deb http://archive.ubuntu.com/ubuntu {}-proposed main universe multiverse restricted
58+"""
59
60
61 def add_source(source, key=None):
62@@ -30,6 +34,10 @@
63 pocket = source.split(':')[-1]
64 with open('/etc/apt/sources.list.d/cloud-archive.list', 'w') as apt:
65 apt.write(CLOUD_ARCHIVE.format(pocket))
66+ elif source == 'proposed':
67+ release = lsb_release()['DISTRIB_CODENAME']
68+ with open('/etc/apt/sources.list.d/proposed.list', 'w') as apt:
69+ apt.write(PROPOSED_POCKET.format(release))
70 if key:
71 subprocess.check_call(['apt-key', 'import', key])
72
73
74=== modified file 'tests/core/test_host.py'
75--- tests/core/test_host.py 2013-06-24 03:26:19 +0000
76+++ tests/core/test_host.py 2013-07-03 11:53:59 +0000
77@@ -1,7 +1,7 @@
78 from collections import OrderedDict
79 from contextlib import contextmanager
80-import os
81 import subprocess
82+import io
83
84 from mock import patch, call, MagicMock
85 from testtools import TestCase
86@@ -28,6 +28,13 @@
87 }
88 }
89
90+LSB_RELEASE = u'''DISTRIB_ID=Ubuntu
91+DISTRIB_RELEASE=13.10
92+DISTRIB_CODENAME=saucy
93+DISTRIB_DESCRIPTION="Ubuntu Saucy Salamander (development branch)"
94+'''
95+
96+
97 def fake_apt_cache():
98 def _get(package):
99 pkg = MagicMock()
100@@ -43,6 +50,7 @@
101 cache.__getitem__.side_effect = _get
102 return cache
103
104+
105 @contextmanager
106 def patch_open():
107 '''Patch open() to allow mocking both open() itself and the file that is
108@@ -61,6 +69,18 @@
109 yield mock_open, mock_file
110
111
112+@contextmanager
113+def mock_open(filename, contents=None):
114+ ''' Slightly simpler mock of open to return contents for filename '''
115+ def mock_file(*args):
116+ if args[0] == filename:
117+ return io.StringIO(contents)
118+ else:
119+ return open(*args)
120+ with patch('__builtin__.open', mock_file):
121+ yield
122+
123+
124 class HelpersTest(TestCase):
125 @patch('subprocess.call')
126 def test_runs_service_action(self, mock_call):
127@@ -120,9 +140,9 @@
128 host.service_reload(service_name, restart_on_failure=True)
129
130 service.assert_has_calls([
131- call('reload', service_name),
132- call('restart', service_name)
133- ])
134+ call('reload', service_name),
135+ call('restart', service_name)
136+ ])
137
138 @patch.object(host, 'service')
139 def test_failed_reload_without_restart(self, service):
140@@ -711,7 +731,7 @@
141 def test_multiservice_restart_on_change_in_order(self, exists, service):
142 restart_map = OrderedDict([
143 ('/etc/cinder/cinder.conf', ['some-api']),
144- ('/etc/haproxy/haproxy.conf', ['haproxy'])
145+ ('/etc/haproxy/haproxy.conf', ['haproxy'])
146 ])
147 exists.side_effect = [False, True, True, True]
148
149@@ -730,3 +750,17 @@
150 call('restart', 'haproxy')
151 ]
152 self.assertEquals(expected, service.call_args_list)
153+
154+ def test_lsb_release(self):
155+ result = {
156+ "DISTRIB_ID": "Ubuntu",
157+ "DISTRIB_RELEASE": "13.10",
158+ "DISTRIB_CODENAME": "saucy",
159+ "DISTRIB_DESCRIPTION": "\"Ubuntu Saucy Salamander "
160+ "(development branch)\""
161+ }
162+ with mock_open('/etc/lsb-release', LSB_RELEASE):
163+ lsb_release = host.lsb_release()
164+ for key in result:
165+ print lsb_release
166+ self.assertEqual(result[key], lsb_release[key])
167
168=== modified file 'tests/fetch/test_fetch.py'
169--- tests/fetch/test_fetch.py 2013-06-27 15:59:39 +0000
170+++ tests/fetch/test_fetch.py 2013-07-03 11:53:59 +0000
171@@ -55,6 +55,17 @@
172 mock_file.write.assert_called_with(result)
173 filter_pkg.assert_called_with(['ubuntu-cloud-keyring'])
174
175+ @patch.object(fetch, 'lsb_release')
176+ def test_add_source_proposed(self, lsb_release):
177+ source = "proposed"
178+ result = """# Proposed
179+deb http://archive.ubuntu.com/ubuntu precise-proposed main universe multiverse restricted
180+"""
181+ lsb_release.return_value = {'DISTRIB_CODENAME': 'precise'}
182+ with patch_open() as (mock_open, mock_file):
183+ fetch.add_source(source=source)
184+ mock_file.write.assert_called_with(result)
185+
186 @patch('subprocess.check_call')
187 def test_add_source_http_and_key(self, check_call):
188 source = "http://archive.ubuntu.com/ubuntu raring-backports main"

Subscribers

People subscribed via source and target branches