Merge lp:~openstack-charmers/charm-helpers/os-alternatives into lp:charm-helpers

Proposed by James Page
Status: Merged
Merged at revision: 93
Proposed branch: lp:~openstack-charmers/charm-helpers/os-alternatives
Merge into: lp:charm-helpers
Diff against target: 94 lines (+85/-0)
2 files modified
charmhelpers/contrib/openstack/alternatives.py (+17/-0)
tests/contrib/openstack/test_alternatives.py (+68/-0)
To merge this branch: bzr merge lp:~openstack-charmers/charm-helpers/os-alternatives
Reviewer Review Type Date Requested Status
Charm Helper Maintainers Pending
Review via email: mp+192060@code.launchpad.net

Description of the change

Alternatives helper to allow multiple charms to write the same configuration
files which priority determining which is to be used.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'charmhelpers/contrib/openstack/alternatives.py'
2--- charmhelpers/contrib/openstack/alternatives.py 1970-01-01 00:00:00 +0000
3+++ charmhelpers/contrib/openstack/alternatives.py 2013-10-21 23:03:59 +0000
4@@ -0,0 +1,17 @@
5+''' Helper for managing alternatives for file conflict resolution '''
6+
7+import subprocess
8+import shutil
9+import os
10+
11+
12+def install_alternative(name, target, source, priority=50):
13+ ''' Install alternative configuration '''
14+ if (os.path.exists(target) and not os.path.islink(target)):
15+ # Move existing file/directory away before installing
16+ shutil.move(target, '{}.bak'.format(target))
17+ cmd = [
18+ 'update-alternatives', '--force', '--install',
19+ target, name, source, str(priority)
20+ ]
21+ subprocess.check_call(cmd)
22
23=== added file 'tests/contrib/openstack/test_alternatives.py'
24--- tests/contrib/openstack/test_alternatives.py 1970-01-01 00:00:00 +0000
25+++ tests/contrib/openstack/test_alternatives.py 2013-10-21 23:03:59 +0000
26@@ -0,0 +1,68 @@
27+from testtools import TestCase
28+from mock import patch
29+
30+import charmhelpers.contrib.openstack.alternatives as alternatives
31+
32+
33+NAME = 'test'
34+SOURCE = '/var/lib/charm/test/test.conf'
35+TARGET = '/etc/test/test,conf'
36+
37+
38+class AlternativesTestCase(TestCase):
39+
40+ @patch('subprocess.os.path')
41+ @patch('subprocess.check_call')
42+ def test_new_alternative(self, _check, _path):
43+ _path.exists.return_value = False
44+ alternatives.install_alternative(NAME,
45+ TARGET,
46+ SOURCE)
47+ _check.assert_called_with(
48+ ['update-alternatives', '--force', '--install',
49+ TARGET, NAME, SOURCE, '50']
50+ )
51+
52+ @patch('subprocess.os.path')
53+ @patch('subprocess.check_call')
54+ def test_priority(self, _check, _path):
55+ _path.exists.return_value = False
56+ alternatives.install_alternative(NAME,
57+ TARGET,
58+ SOURCE, 100)
59+ _check.assert_called_with(
60+ ['update-alternatives', '--force', '--install',
61+ TARGET, NAME, SOURCE, '100']
62+ )
63+
64+ @patch('shutil.move')
65+ @patch('subprocess.os.path')
66+ @patch('subprocess.check_call')
67+ def test_new_alternative_existing_file(self, _check,
68+ _path, _move):
69+ _path.exists.return_value = True
70+ _path.islink.return_value = False
71+ alternatives.install_alternative(NAME,
72+ TARGET,
73+ SOURCE)
74+ _check.assert_called_with(
75+ ['update-alternatives', '--force', '--install',
76+ TARGET, NAME, SOURCE, '50']
77+ )
78+ _move.assert_called_with(TARGET, '{}.bak'.format(TARGET))
79+
80+ @patch('shutil.move')
81+ @patch('subprocess.os.path')
82+ @patch('subprocess.check_call')
83+ def test_new_alternative_existing_link(self, _check,
84+ _path, _move):
85+ _path.exists.return_value = True
86+ _path.islink.return_value = True
87+ alternatives.install_alternative(NAME,
88+ TARGET,
89+ SOURCE)
90+ _check.assert_called_with(
91+ ['update-alternatives', '--force', '--install',
92+ TARGET, NAME, SOURCE, '50']
93+ )
94+ _move.assert_not_called()

Subscribers

People subscribed via source and target branches