Merge lp:~freyes/charm-helpers/current-dc-none into lp:charm-helpers

Proposed by Felipe Reyes
Status: Merged
Merged at revision: 385
Proposed branch: lp:~freyes/charm-helpers/current-dc-none
Merge into: lp:charm-helpers
Diff against target: 98 lines (+36/-6)
2 files modified
charmhelpers/contrib/hahelpers/cluster.py (+12/-3)
tests/contrib/hahelpers/test_cluster_utils.py (+24/-3)
To merge this branch: bzr merge lp:~freyes/charm-helpers/current-dc-none
Reviewer Review Type Date Requested Status
Liam Young (community) Approve
Review via email: mp+261273@code.launchpad.net

Description of the change

Dear Charmers,

This patch improves charmhelpers.contrib.hahelpers.cluster:is_crm_dc() to consider the case when the current DC is NONE.

To allow the @retry_on_exception() decorator work when current DC is NONE or when 'crm status' fails the exception CRMDCNotFound is raised.

Best,

To post a comment you must log in.
Revision history for this message
Liam Young (gnuoy) wrote :

Approve

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/contrib/hahelpers/cluster.py'
2--- charmhelpers/contrib/hahelpers/cluster.py 2015-06-03 08:15:41 +0000
3+++ charmhelpers/contrib/hahelpers/cluster.py 2015-06-05 19:09:42 +0000
4@@ -64,6 +64,10 @@
5 pass
6
7
8+class CRMDCNotFound(Exception):
9+ pass
10+
11+
12 def is_elected_leader(resource):
13 """
14 Returns True if the charm executing this is the elected cluster leader.
15@@ -116,8 +120,9 @@
16 status = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
17 if not isinstance(status, six.text_type):
18 status = six.text_type(status, "utf-8")
19- except subprocess.CalledProcessError:
20- return False
21+ except subprocess.CalledProcessError as ex:
22+ raise CRMDCNotFound(str(ex))
23+
24 current_dc = ''
25 for line in status.split('\n'):
26 if line.startswith('Current DC'):
27@@ -125,10 +130,14 @@
28 current_dc = line.split(':')[1].split()[0]
29 if current_dc == get_unit_hostname():
30 return True
31+ elif current_dc == 'NONE':
32+ raise CRMDCNotFound('Current DC: NONE')
33+
34 return False
35
36
37-@retry_on_exception(5, base_delay=2, exc_type=CRMResourceNotFound)
38+@retry_on_exception(5, base_delay=2,
39+ exc_type=(CRMResourceNotFound, CRMDCNotFound))
40 def is_crm_leader(resource, retry=False):
41 """
42 Returns True if the charm calling this is the elected corosync leader,
43
44=== modified file 'tests/contrib/hahelpers/test_cluster_utils.py'
45--- tests/contrib/hahelpers/test_cluster_utils.py 2015-05-20 22:56:01 +0000
46+++ tests/contrib/hahelpers/test_cluster_utils.py 2015-06-05 19:09:42 +0000
47@@ -1,4 +1,3 @@
48-
49 from mock import patch, MagicMock, call
50
51 from subprocess import CalledProcessError
52@@ -24,6 +23,18 @@
53 Started: [ juju-trusty-machine-1 juju-trusty-machine-2 juju-trusty-machine-3 ]
54 '''
55
56+CRM_DC_NONE = b'''
57+Last updated: Thu May 14 14:46:35 2015
58+Last change: Thu May 14 14:43:51 2015 via crmd on juju-trusty-machine-1
59+Stack: corosync
60+Current DC: NONE
61+1 Nodes configured, 2 expected votes
62+0 Resources configured
63+
64+
65+Node node1: UNCLEAN (offline)
66+'''
67+
68
69 class ClusterUtilsTests(TestCase):
70 def setUp(self):
71@@ -68,8 +79,11 @@
72 @patch('subprocess.check_output')
73 def test_is_crm_dc_no_cluster(self, check_output):
74 '''It is not leader if there is no cluster up'''
75- check_output.side_effect = CalledProcessError(1, 'crm')
76- self.assertFalse(cluster_utils.is_crm_dc())
77+ def r(*args, **kwargs):
78+ raise CalledProcessError(1, 'crm')
79+
80+ check_output.side_effect = r
81+ self.assertRaises(cluster_utils.CRMDCNotFound, cluster_utils.is_crm_dc)
82
83 @patch('subprocess.check_output')
84 def test_is_crm_dc_false(self, check_output):
85@@ -79,6 +93,13 @@
86 self.assertFalse(cluster_utils.is_crm_dc())
87
88 @patch('subprocess.check_output')
89+ def test_is_crm_dc_current_none(self, check_output):
90+ '''It determines its unit is leader'''
91+ self.get_unit_hostname.return_value = 'juju-trusty-machine-1'
92+ check_output.return_value = CRM_DC_NONE
93+ self.assertRaises(cluster_utils.CRMDCNotFound, cluster_utils.is_crm_dc)
94+
95+ @patch('subprocess.check_output')
96 def test_is_crm_leader(self, check_output):
97 '''It determines its unit is leader'''
98 self.get_unit_hostname.return_value = 'node1'

Subscribers

People subscribed via source and target branches