Fix 'crm node show' parsing to get list of nodes.

The command 'crm node show' is used on jammy to retrieve the list of
nodes defined in a cluster, although this command also includes the
properties set on a node (e.g. standby=off) which breaks the current
logic parsing.

This change uses a regular expresion to filter out all the lines from
the output that don't start with a non-white character (^\S+).

Change-Id: I3e00daa1b877a7faae1370f08b2d9c5bd7795c5f
Closes-Bug: #1987685
Related-Bug: #1972022
This commit is contained in:
Felipe Reyes 2022-08-25 12:13:33 -04:00
parent cf1c3eeeb3
commit 4e53bea076
2 changed files with 18 additions and 2 deletions

View File

@ -192,7 +192,8 @@ def list_nodes():
cmd = ['crm', 'node', 'show']
out = subprocess.check_output(cmd).decode('utf-8')
for line in out.strip().split('\n'):
nodes.append(line.split('(')[0])
if re.match(r'^\S+', line):
nodes.append(line.split('(')[0])
else:
cmd = ['crm', 'node', 'status']
out = subprocess.check_output(cmd).decode('utf-8')

View File

@ -149,6 +149,10 @@ CRM_STATUS_XML = b"""
<status code="0" message="OK"/>
</pacemaker-result>
""" # noqa
CRM_NODE_SHOW_OUTPUT = b"""juju-c1-zaza-3(1000): member
standby=off maintenance=true
juju-c1-zaza-5(1002): member
"""
class TestPcmk(unittest.TestCase):
@ -358,7 +362,8 @@ class TestPcmk(unittest.TestCase):
self.assertEqual(r, 'ef395293b1b7c29c5bf1c99774f75cf4')
@mock.patch('subprocess.check_output', return_value=CRM_NODE_STATUS_XML)
def test_list_nodes(self, mock_check_output):
@mock.patch.object(pcmk, 'get_distrib_codename', return_value='focal')
def test_list_nodes(self, get_distrib_codename, mock_check_output):
self.assertSequenceEqual(
pcmk.list_nodes(),
[
@ -367,6 +372,16 @@ class TestPcmk(unittest.TestCase):
'juju-982848-zaza-ce47c58f6c88-9'])
mock_check_output.assert_called_once_with(['crm', 'node', 'status'])
@mock.patch('subprocess.check_output', return_value=CRM_NODE_SHOW_OUTPUT)
@mock.patch.object(pcmk, 'get_distrib_codename', return_value='jammy')
def test_list_nodes_jammy(self, get_distrib_codename, check_output):
self.assertSequenceEqual(
pcmk.list_nodes(),
['juju-c1-zaza-3', 'juju-c1-zaza-5']
)
check_output.assert_called_with(['crm', 'node', 'show'])
def test_get_tag(self):
"""Test get element by tag if exists else empty element."""
main = etree.Element("test")