Merge lp:~bloodearnest/charm-helpers/ppa-tools into lp:charm-helpers

Proposed by Simon Davy
Status: Work in progress
Proposed branch: lp:~bloodearnest/charm-helpers/ppa-tools
Merge into: lp:charm-helpers
Prerequisite: lp:~michael.nelson/charm-helpers/namespace-relation-data
Diff against target: 97 lines (+69/-0)
2 files modified
charmhelpers/core/host.py (+21/-0)
tests/core/test_host.py (+48/-0)
To merge this branch: bzr merge lp:~bloodearnest/charm-helpers/ppa-tools
Reviewer Review Type Date Requested Status
Matthew Wedgwood (community) Needs Fixing
Michael Nelson Approve
Review via email: mp+175308@code.launchpad.net

Commit message

Added enable_private_ppas and add_apt_key helperss

Description of the change

Added enable_private_ppas and add_apt_key helperss

To post a comment you must log in.
Revision history for this message
Michael Nelson (michael.nelson) wrote :

14:56 <bloodearnest> https://code.launchpad.net/~bloodearnest/charm-helpers/ppa-tools/+merge/175308
14:56 <bloodearnest> be nice if we could a "shared pipeline" for a fast moving dep branch like that
14:57 <noodles> Yeah
14:57 <noodles> Nice branch!
14:58 <bloodearnest> simple functions
14:58 <noodles> I think mock expectations are used a bit much in charmhelpers... what I've been doing...
14:58 * noodles checks on his own MP
14:58 <bloodearnest> I think a mock filesystem would be better
15:00 <noodles> Yeah - well, I'm not sure if it's what you mean, but you can do that really easily...
15:00 <noodles> If you look at test_updates_existing_values()
15:00 <noodles> Teh actual code writes a file to /etc/salt/grains
15:01 <noodles> But the test can just mock the path to the grains file with a tmp directory, then the rest of the test is on the actual filesystem.
15:01 <bloodearnest> looking
15:31 <bloodearnest> hmm - not sure how to apply the tmp dir to my test, as the path is a hardcoded one
15:31 <noodles> Oh - you can't make it a module-level path? Yeah, that doesn't work then.
15:32 <bloodearnest> I could, I guess
15:32 <bloodearnest> python needs better whole fs mocking tools
15:32 <noodles> I love mock - so much better than what was available before...
15:35 <bloodearnest> mock is great
15:35 <bloodearnest> I loved how easy it was to mock my new hooks logic with self.mock_charmhelpers
15:35 <noodles> Yeah!

review: Approve
48. By Simon Davy

Refactor tests to use filesystem, and fix check_call args in add_apt_key

Revision history for this message
Matthew Wedgwood (mew) wrote :

Hi, Simon. Thanks for the MP!

Although there are vestigial helpers for package management in charmhelpers.core, this functionality should live in charmhelpers.fetch. Would you mind reshuffling and resubmitting?

Thanks.

review: Needs Fixing

Unmerged revisions

48. By Simon Davy

Refactor tests to use filesystem, and fix check_call args in add_apt_key

47. By Simon Davy

typo

46. By Simon Davy

Added enable_private_ppas and add_apt_key helpers

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-18 13:24:23 +0000
3+++ charmhelpers/core/host.py 2013-07-18 13:24:23 +0000
4@@ -192,6 +192,27 @@
5 apt_update(fatal=fatal)
6
7
8+DISABLE_PROXY_PPA_APTCONF = '/etc/apt/apt.conf.d/97disable-ppa-proxy'
9+
10+
11+def enable_private_ppas():
12+ """Bypass apt proxies for private-ppa.launchpad.net to allow auth"""
13+ if not os.path.exists(DISABLE_PROXY_PPA_APTCONF):
14+ write_file(
15+ DISABLE_PROXY_PPA_APTCONF,
16+ 'Acquire::HTTP::Proxy::private-ppa.launchpad.net "DIRECT";',
17+ perms=0644
18+ )
19+
20+
21+def add_apt_key(key):
22+ """Add an apt key. Useful for ppas"""
23+ cmd = ['apt-key', 'adv',
24+ '--keyserver', 'keyserver.ubuntu.com',
25+ '--recv-keys', key]
26+ subprocess.check_call(cmd)
27+
28+
29 def mount(device, mountpoint, options=None, persist=False):
30 '''Mount a filesystem'''
31 cmd_args = ['mount']
32
33=== modified file 'tests/core/test_host.py'
34--- tests/core/test_host.py 2013-07-18 13:24:23 +0000
35+++ tests/core/test_host.py 2013-07-18 13:24:23 +0000
36@@ -2,6 +2,9 @@
37 from contextlib import contextmanager
38 import subprocess
39 import io
40+import tempfile
41+import os.path
42+import shutil
43
44 from mock import patch, call, MagicMock
45 from testtools import TestCase
46@@ -556,6 +559,51 @@
47 call(['apt-get', 'update']),
48 ])
49
50+ def _setup_etc_with_path(self, *paths):
51+ etc_dir = tempfile.mkdtemp()
52+ self.addCleanup(shutil.rmtree, etc_dir)
53+ file_path = os.path.join(etc_dir, *paths)
54+ dir_path = os.path.dirname(file_path)
55+ os.makedirs(dir_path)
56+ return file_path
57+
58+ @patch.object(host, 'execution_environment')
59+ @patch('os.fchown', MagicMock())
60+ @patch('os.fchmod', MagicMock())
61+ def test_enable_private_ppa_conf_not_exist(self, mock_exec_env):
62+ mock_exec_env.return_value = {}
63+ aptpath = self._setup_etc_with_path('apt', 'apt.conf.d', 'test')
64+ with patch.object(host, 'DISABLE_PROXY_PPA_APTCONF', aptpath):
65+ host.enable_private_ppas()
66+
67+ self.assertTrue(os.path.exists(aptpath))
68+ aptconfig = open(aptpath).read()
69+ self.assertEqual(
70+ aptconfig,
71+ 'Acquire::HTTP::Proxy::private-ppa.launchpad.net "DIRECT";'
72+ )
73+
74+ def test_enable_private_ppa_conf_does_exist(self):
75+ aptpath = self._setup_etc_with_path('apt', 'apt.conf.d', 'test')
76+ with open(aptpath, 'w+') as aptconf:
77+ aptconf.write("ORIGNAL")
78+ with patch.object(host, 'DISABLE_PROXY_PPA_APTCONF', aptpath):
79+ host.enable_private_ppas()
80+
81+ self.assertTrue(os.path.exists(aptpath))
82+ aptconfig = open(aptpath).read()
83+ self.assertEqual(aptconfig, "ORIGNAL")
84+
85+ @patch('subprocess.check_call')
86+ def test_add_apt_key_calls_correctly(self, mock_call):
87+ host.add_apt_key('key')
88+ expected = [
89+ 'apt-key', 'adv',
90+ '--keyserver', 'keyserver.ubuntu.com',
91+ '--recv-keys', 'key'
92+ ]
93+ mock_call.assert_called_once_with(expected)
94+
95 @patch('apt_pkg.Cache')
96 def test_filter_packages_missing(self, cache):
97 cache.side_effect = fake_apt_cache

Subscribers

People subscribed via source and target branches