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
=== added file 'charmhelpers/contrib/openstack/alternatives.py'
--- charmhelpers/contrib/openstack/alternatives.py 1970-01-01 00:00:00 +0000
+++ charmhelpers/contrib/openstack/alternatives.py 2013-10-21 23:03:59 +0000
@@ -0,0 +1,17 @@
1''' Helper for managing alternatives for file conflict resolution '''
2
3import subprocess
4import shutil
5import os
6
7
8def install_alternative(name, target, source, priority=50):
9 ''' Install alternative configuration '''
10 if (os.path.exists(target) and not os.path.islink(target)):
11 # Move existing file/directory away before installing
12 shutil.move(target, '{}.bak'.format(target))
13 cmd = [
14 'update-alternatives', '--force', '--install',
15 target, name, source, str(priority)
16 ]
17 subprocess.check_call(cmd)
018
=== added file 'tests/contrib/openstack/test_alternatives.py'
--- tests/contrib/openstack/test_alternatives.py 1970-01-01 00:00:00 +0000
+++ tests/contrib/openstack/test_alternatives.py 2013-10-21 23:03:59 +0000
@@ -0,0 +1,68 @@
1from testtools import TestCase
2from mock import patch
3
4import charmhelpers.contrib.openstack.alternatives as alternatives
5
6
7NAME = 'test'
8SOURCE = '/var/lib/charm/test/test.conf'
9TARGET = '/etc/test/test,conf'
10
11
12class AlternativesTestCase(TestCase):
13
14 @patch('subprocess.os.path')
15 @patch('subprocess.check_call')
16 def test_new_alternative(self, _check, _path):
17 _path.exists.return_value = False
18 alternatives.install_alternative(NAME,
19 TARGET,
20 SOURCE)
21 _check.assert_called_with(
22 ['update-alternatives', '--force', '--install',
23 TARGET, NAME, SOURCE, '50']
24 )
25
26 @patch('subprocess.os.path')
27 @patch('subprocess.check_call')
28 def test_priority(self, _check, _path):
29 _path.exists.return_value = False
30 alternatives.install_alternative(NAME,
31 TARGET,
32 SOURCE, 100)
33 _check.assert_called_with(
34 ['update-alternatives', '--force', '--install',
35 TARGET, NAME, SOURCE, '100']
36 )
37
38 @patch('shutil.move')
39 @patch('subprocess.os.path')
40 @patch('subprocess.check_call')
41 def test_new_alternative_existing_file(self, _check,
42 _path, _move):
43 _path.exists.return_value = True
44 _path.islink.return_value = False
45 alternatives.install_alternative(NAME,
46 TARGET,
47 SOURCE)
48 _check.assert_called_with(
49 ['update-alternatives', '--force', '--install',
50 TARGET, NAME, SOURCE, '50']
51 )
52 _move.assert_called_with(TARGET, '{}.bak'.format(TARGET))
53
54 @patch('shutil.move')
55 @patch('subprocess.os.path')
56 @patch('subprocess.check_call')
57 def test_new_alternative_existing_link(self, _check,
58 _path, _move):
59 _path.exists.return_value = True
60 _path.islink.return_value = True
61 alternatives.install_alternative(NAME,
62 TARGET,
63 SOURCE)
64 _check.assert_called_with(
65 ['update-alternatives', '--force', '--install',
66 TARGET, NAME, SOURCE, '50']
67 )
68 _move.assert_not_called()

Subscribers

People subscribed via source and target branches