From 4e53bea0769d6c8c37e1e2a612c191961e738222 Mon Sep 17 00:00:00 2001 From: Felipe Reyes Date: Thu, 25 Aug 2022 12:13:33 -0400 Subject: [PATCH] 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 --- hooks/pcmk.py | 3 ++- unit_tests/test_pcmk.py | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/hooks/pcmk.py b/hooks/pcmk.py index a89e4c7..233df45 100644 --- a/hooks/pcmk.py +++ b/hooks/pcmk.py @@ -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') diff --git a/unit_tests/test_pcmk.py b/unit_tests/test_pcmk.py index a130afd..b7892ac 100644 --- a/unit_tests/test_pcmk.py +++ b/unit_tests/test_pcmk.py @@ -149,6 +149,10 @@ CRM_STATUS_XML = b""" """ # 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")