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")