From e9814b78b195b4769c2079de1d5ac0756aa96a03 Mon Sep 17 00:00:00 2001 From: Dawei Pang Date: Mon, 16 Mar 2026 16:36:59 +0800 Subject: [PATCH] check_cluster_conf_ssh --- e2e_test/hawk_test_driver.py | 3 ++- e2e_test/hawk_test_ssh.py | 35 +++++++++++++---------------------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/e2e_test/hawk_test_driver.py b/e2e_test/hawk_test_driver.py index fff91bed1..77fb19b78 100644 --- a/e2e_test/hawk_test_driver.py +++ b/e2e_test/hawk_test_driver.py @@ -954,7 +954,8 @@ def test_check_cluster_configuration(self, ssh): # First try to get the values from the crm_attribute if ssh.is_valid_command("crm_attribute --list-options=cluster --all --output-as=xml"): - if not ssh.check_cluster_conf_ssh("crm_attribute --list-options=cluster --all --output-as=xml | grep 'advanced=\"0\"' | sed 's/.*name=\"*\\([^\\\"]*\\)\".*/\\1/'", lst, silent=True, anycheck=False): + out = ssh.get_cluster_conf_ssh_output("crm_attribute --list-options=cluster --all --output-as=xml | grep 'advanced=\"0\"' | sed 's/.*name=\"*\\([^\\\"]*\\)\".*/\\1/'") + if not all(_ in out for _ in lst): print(f'ERROR: {Error.CRM_CONFIG_ADVANCED_ATTRIBUTES}') return False # If the crm_attribute too old for this diff --git a/e2e_test/hawk_test_ssh.py b/e2e_test/hawk_test_ssh.py index d0d76529b..4ca5b7fca 100644 --- a/e2e_test/hawk_test_ssh.py +++ b/e2e_test/hawk_test_ssh.py @@ -36,35 +36,22 @@ def is_valid_command(self, command): return False return True - def check_cluster_conf_ssh(self, command, mustmatch, silent=False, anycheck=False): + def get_cluster_conf_ssh_output(self, command): ''' Execute command via SSH connection and compare if its output matches expectation Args: command (str): input command - mustmatch (object): expected string or list - silent (boolean): print info or not - anycheck (boolean): if match at least one element in the list - Raises: - ValueError: No value matches expected string or list Return: - boolean or matched value + The stdout of SSH command when no error detected + Else False (0) ''' _, out, err = self.ssh.exec_command(command) out, err = map(lambda f: f.read().decode().rstrip('\n'), (out, err)) - if not silent: - print(f"INFO: ssh command [{command}] got output [{out}] and error [{err}]") + print(f"INFO: ssh command [{command}] got output [{out}] and error [{err}]") if err: print(f"ERROR: got an error over SSH: [{err}]") return False - if isinstance(mustmatch, str): - return mustmatch in out - if isinstance(mustmatch, list) and anycheck: - # Output has to match at least one element in the list - return any(_ in out for _ in mustmatch) - if isinstance(mustmatch, list) and not anycheck: - # Output has to match all elements in list - return all(_ in out for _ in mustmatch) - raise ValueError("check_cluster_conf_ssh: mustmatch must be str or list") + return out @staticmethod def set_test_status(results, test, status): @@ -88,7 +75,8 @@ def verify_stonith_in_maintenance(self, results): False when stonith-sbd is not unmanaged nor in maintenance ''' print("TEST: verify_stonith_in_maintenance") - if self.check_cluster_conf_ssh("crm status | grep stonith-sbd", ["unmanaged", "maintenance"], anycheck=True): + out = self.get_cluster_conf_ssh_output("crm status | grep stonith-sbd") + if any(_ in out for _ in ('unmanaged', 'maintenance')): print("INFO: stonith-sbd is unmanaged/maintenance") self.set_test_status(results, 'verify_stonith_in_maintenance', 'passed') return True @@ -107,7 +95,8 @@ def verify_node_maintenance(self, results): False when node is in maintenance mode ''' print("TEST: verify_node_maintenance: check cluster node is in maintenance mode") - if self.check_cluster_conf_ssh("crm status | grep -i node", "maintenance"): + out = self.get_cluster_conf_ssh_output("crm status | grep -i node") + if "maintenance" in out: print("INFO: cluster node set successfully in maintenance mode") self.set_test_status(results, 'verify_node_maintenance', 'passed') return True @@ -134,7 +123,8 @@ def verify_primitive(self, primitive, version, results): matches.append("op stop timeout=15s") else: matches.append("op stop timeout=15s on-fail=stop") - if self.check_cluster_conf_ssh("crm configure show", matches): + out = self.get_cluster_conf_ssh_output("crm configure show") + if all(_ in out for _ in matches): print(f"INFO: primitive [{primitive}] correctly defined in the cluster configuration") self.set_test_status(results, 'verify_primitive', 'passed') return True @@ -154,7 +144,8 @@ def verify_primitive_removed(self, primitive, results): False when configuration is not removed ''' print(f"TEST: verify_primitive_removed: check primitive [{primitive}] is removed") - if self.check_cluster_conf_ssh("crm resource status | grep ocf::heartbeat:Dummy", ''): + out = self.get_cluster_conf_ssh_output("crm resource status | grep ocf::heartbeat:Dummy") + if out == '': print("INFO: primitive successfully removed") self.set_test_status(results, 'verify_primitive_removed', 'passed') return True