|
| 1 | +""" |
| 2 | +Community Edition Restrictions Utility Module |
| 3 | +
|
| 4 | +Helper methods for CE restriction testing: |
| 5 | +- Node management (add, eject, rebalance) |
| 6 | +- CE edition verification |
| 7 | +- Service restriction validation |
| 8 | +""" |
| 9 | + |
| 10 | +from cb_server_rest_util.cluster_nodes.cluster_nodes_api import ClusterRestAPI |
| 11 | +from cb_tools.cb_cli import CbCli |
| 12 | +from couchbase_utils.cluster_utils.cluster_ready_functions import ClusterUtils |
| 13 | +from couchbase_utils.rebalance_utils.rebalance_util import RebalanceUtil |
| 14 | +from custom_exceptions.exception import RebalanceFailedException |
| 15 | +from shell_util.remote_connection import RemoteMachineShellConnection |
| 16 | + |
| 17 | + |
| 18 | +class CommunityEditionRestrictionsUtil: |
| 19 | + """Utility class for CE restriction test operations.""" |
| 20 | + |
| 21 | + CE_NODE_LIMIT = 5 |
| 22 | + CE_REBALANCE_ERROR_MSG = "Cannot rebalance with more than 5" |
| 23 | + |
| 24 | + def __init__(self, cluster, cluster_util, log, sleep_func): |
| 25 | + self.cluster = cluster |
| 26 | + self.cluster_util = cluster_util |
| 27 | + self.log = log |
| 28 | + self.sleep = sleep_func |
| 29 | + self.rest = ClusterRestAPI(cluster.master) |
| 30 | + |
| 31 | + def verify_ce_edition_via_diag_eval(self): |
| 32 | + """Verify cluster is running CE via diag/eval.""" |
| 33 | + shell = RemoteMachineShellConnection(self.cluster.master) |
| 34 | + shell.enable_diag_eval_on_non_local_hosts() |
| 35 | + shell.disconnect() |
| 36 | + |
| 37 | + status, content = self.rest.diag_eval( |
| 38 | + "cluster_compat_mode:is_enterprise().") |
| 39 | + if not status: |
| 40 | + raise AssertionError("Failed to execute diag/eval") |
| 41 | + |
| 42 | + is_ce = content is False if isinstance(content, bool) \ |
| 43 | + else str(content).strip().lower() == "false" |
| 44 | + |
| 45 | + if not is_ce: |
| 46 | + raise AssertionError("Expected CE edition. Got: %s" % content) |
| 47 | + |
| 48 | + self.log.info("Verified CE edition via diag/eval") |
| 49 | + return True |
| 50 | + |
| 51 | + def add_node_without_rebalance(self, node, services=None): |
| 52 | + """Add a node without triggering rebalance.""" |
| 53 | + services = services or ["kv"] |
| 54 | + self.log.info("Adding node %s with services %s", node.ip, services) |
| 55 | + try: |
| 56 | + return self.cluster_util.add_node( |
| 57 | + self.cluster, node, services=services, rebalance=False) |
| 58 | + except Exception as e: |
| 59 | + raise AssertionError("Failed to add node %s: %s" % (node.ip, e)) |
| 60 | + |
| 61 | + def verify_node_in_pending_state(self, node_ip): |
| 62 | + """Verify node is in inactiveAdded (pending) state.""" |
| 63 | + nodes = ClusterUtils.get_nodes(self.cluster.master, inactive_added=True) |
| 64 | + for node in nodes: |
| 65 | + if node.ip == node_ip: |
| 66 | + if node.clusterMembership != "inactiveAdded": |
| 67 | + raise AssertionError( |
| 68 | + "Node %s expected 'inactiveAdded', got '%s'" |
| 69 | + % (node_ip, node.clusterMembership)) |
| 70 | + self.log.info("Node %s is in pending state", node_ip) |
| 71 | + return True |
| 72 | + raise AssertionError("Node %s not found in cluster" % node_ip) |
| 73 | + |
| 74 | + def attempt_rebalance_expect_failure(self, expected_error=None): |
| 75 | + """Attempt rebalance expecting CE restriction failure.""" |
| 76 | + expected_error = expected_error or self.CE_REBALANCE_ERROR_MSG |
| 77 | + nodes = ClusterUtils.get_nodes(self.cluster.master, inactive_added=True) |
| 78 | + known_nodes = [n.id for n in nodes] |
| 79 | + |
| 80 | + self.log.info("Attempting rebalance with %d nodes (expecting failure)", |
| 81 | + len(nodes)) |
| 82 | + |
| 83 | + status, content = self.rest.rebalance(known_nodes=known_nodes, |
| 84 | + eject_nodes=[]) |
| 85 | + if not status: |
| 86 | + error_msg = content.decode('utf-8') if isinstance(content, bytes) \ |
| 87 | + else str(content) |
| 88 | + if expected_error not in error_msg: |
| 89 | + raise AssertionError("Expected CE error. Got: %s" % error_msg) |
| 90 | + self.log.info("Rebalance rejected: %s", error_msg) |
| 91 | + return error_msg |
| 92 | + |
| 93 | + # Monitor if rebalance started |
| 94 | + try: |
| 95 | + RebalanceUtil(self.cluster).monitor_rebalance() |
| 96 | + raise AssertionError("Rebalance should have failed but succeeded") |
| 97 | + except RebalanceFailedException as e: |
| 98 | + error_msg = str(e) |
| 99 | + if expected_error not in error_msg: |
| 100 | + raise AssertionError("Expected CE error. Got: %s" % error_msg) |
| 101 | + self.log.info("Rebalance failed: %s", error_msg) |
| 102 | + return error_msg |
| 103 | + |
| 104 | + def restart_couchbase_server(self, server): |
| 105 | + """Restart Couchbase Server and wait for ready.""" |
| 106 | + self.log.info("Restarting server %s", server.ip) |
| 107 | + shell = RemoteMachineShellConnection(server) |
| 108 | + shell.restart_couchbase() |
| 109 | + shell.disconnect() |
| 110 | + self.cluster_util.wait_for_ns_servers_or_assert([server], wait_time=120) |
| 111 | + self.log.info("Server %s ready", server.ip) |
| 112 | + |
| 113 | + def rebalance_out_node(self, node_to_remove): |
| 114 | + """Rebalance out a node from the cluster.""" |
| 115 | + nodes = ClusterUtils.get_nodes(self.cluster.master, inactive_added=True) |
| 116 | + otp_node = next((n.id for n in nodes if n.ip == node_to_remove.ip), None) |
| 117 | + |
| 118 | + if not otp_node: |
| 119 | + raise AssertionError("Node %s not found" % node_to_remove.ip) |
| 120 | + |
| 121 | + self.log.info("Rebalancing out %s", node_to_remove.ip) |
| 122 | + result = ClusterUtils.rebalance( |
| 123 | + self.cluster, wait_for_completion=True, ejected_nodes=[otp_node]) |
| 124 | + |
| 125 | + if not result: |
| 126 | + raise AssertionError("Rebalance-out failed") |
| 127 | + |
| 128 | + self.cluster_util.update_cluster_nodes_service_list(self.cluster) |
| 129 | + self.log.info("Removed node %s", node_to_remove.ip) |
| 130 | + |
| 131 | + def get_active_node_count(self): |
| 132 | + """Get count of active nodes in cluster.""" |
| 133 | + return len(ClusterUtils.get_nodes(self.cluster.master)) |
| 134 | + |
| 135 | + def cleanup_pending_nodes(self): |
| 136 | + """Eject all pending (inactiveAdded) nodes.""" |
| 137 | + cleaned = 0 |
| 138 | + try: |
| 139 | + nodes = ClusterUtils.get_nodes(self.cluster.master, |
| 140 | + inactive_added=True) |
| 141 | + pending = [n for n in nodes if n.clusterMembership == "inactiveAdded"] |
| 142 | + for node in pending: |
| 143 | + try: |
| 144 | + self.rest.eject_node(node.id) |
| 145 | + cleaned += 1 |
| 146 | + except Exception as e: |
| 147 | + self.log.warning("Failed to eject %s: %s", node.id, e) |
| 148 | + if cleaned: |
| 149 | + self.log.info("Cleaned up %d pending nodes", cleaned) |
| 150 | + except Exception as e: |
| 151 | + self.log.warning("Cleanup error: %s", e) |
| 152 | + return cleaned |
| 153 | + |
| 154 | + def add_node_via_cli(self, node, services, expect_success=True): |
| 155 | + """ |
| 156 | + Add node via couchbase-cli server-add. |
| 157 | + Returns (success, error_msg) tuple. |
| 158 | + """ |
| 159 | + self._eject_node_by_ip(node.ip) |
| 160 | + self.sleep(8, "Wait after ejection") |
| 161 | + |
| 162 | + shell = RemoteMachineShellConnection(self.cluster.master) |
| 163 | + cb_cli = CbCli(shell, username=self.cluster.master.rest_username, |
| 164 | + password=self.cluster.master.rest_password) |
| 165 | + |
| 166 | + try: |
| 167 | + output = cb_cli.add_node(node, services) |
| 168 | + output_str = "\n".join(output) if isinstance(output, list) \ |
| 169 | + else str(output) |
| 170 | + shell.disconnect() |
| 171 | + |
| 172 | + if "ERROR:" in output_str: |
| 173 | + raise Exception(output_str) |
| 174 | + |
| 175 | + # Success - cleanup |
| 176 | + self._eject_node_by_ip(node.ip) |
| 177 | + self.sleep(10, "Wait after ejection") |
| 178 | + return (True, "") if expect_success else (False, "Should be rejected") |
| 179 | + |
| 180 | + except Exception as e: |
| 181 | + shell.disconnect() |
| 182 | + error_msg = str(e) |
| 183 | + return (True, error_msg) if not expect_success else (False, error_msg) |
| 184 | + |
| 185 | + def _eject_node_by_ip(self, node_ip): |
| 186 | + """Eject a pending node by IP.""" |
| 187 | + try: |
| 188 | + nodes = ClusterUtils.get_nodes(self.cluster.master, |
| 189 | + inactive_added=True) |
| 190 | + for n in nodes: |
| 191 | + if n.ip == node_ip: |
| 192 | + self.rest.eject_node(n.id) |
| 193 | + return True |
| 194 | + except Exception as e: |
| 195 | + self.log.warning("Failed to eject %s: %s", node_ip, e) |
| 196 | + return False |
| 197 | + |
| 198 | + def add_node_via_rest_and_rebalance_in(self, node, services): |
| 199 | + """Add node via REST and rebalance in.""" |
| 200 | + self._eject_node_by_ip(node.ip) |
| 201 | + self.sleep(5, "Wait after ejection") |
| 202 | + |
| 203 | + self.log.info("Adding %s with services=%s", node.ip, services) |
| 204 | + status, content = self.rest.add_node( |
| 205 | + host_name=node.ip, |
| 206 | + username=node.rest_username, |
| 207 | + password=node.rest_password, |
| 208 | + services=services) |
| 209 | + |
| 210 | + if not status: |
| 211 | + raise AssertionError("Failed to add node: %s" % content) |
| 212 | + |
| 213 | + self.cluster_util.update_cluster_nodes_service_list( |
| 214 | + self.cluster, inactive_added=True) |
| 215 | + self.verify_node_in_pending_state(node.ip) |
| 216 | + |
| 217 | + nodes = ClusterUtils.get_nodes(self.cluster.master, inactive_added=True) |
| 218 | + known_nodes = [n.id for n in nodes] |
| 219 | + |
| 220 | + status, content = self.rest.rebalance(known_nodes=known_nodes, |
| 221 | + eject_nodes=[]) |
| 222 | + if not status: |
| 223 | + raise AssertionError("Failed to start rebalance: %s" % content) |
| 224 | + |
| 225 | + if not RebalanceUtil(self.cluster).monitor_rebalance(): |
| 226 | + raise AssertionError("Rebalance-in failed") |
| 227 | + |
| 228 | + self.cluster_util.update_cluster_nodes_service_list(self.cluster) |
0 commit comments