|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +""" |
| 5 | +Test class for HANA DB primary node crash and node kill tasks |
| 6 | +
|
| 7 | +This test class uses pytest to run functional tests on the HANA DB primary node crash, kill, echo-b, |
| 8 | +and crash-index tasks defined in roles/ha_db_hana/tasks/primary-node-crash.yml. |
| 9 | +It sets up a temporary test environment, mocks necessary Python modules and commands, and verifies |
| 10 | +the execution of the tasks. |
| 11 | +""" |
| 12 | + |
| 13 | +import os |
| 14 | +import shutil |
| 15 | +from pathlib import Path |
| 16 | +import pytest |
| 17 | +from tests.roles.ha_db_hana.roles_testing_base_db import RolesTestingBaseDB |
| 18 | + |
| 19 | + |
| 20 | +class TestDbHDBOperations(RolesTestingBaseDB): |
| 21 | + """ |
| 22 | + Test class for HANA DB primary node crash, kill, echo-b, and crash-index tasks. |
| 23 | + """ |
| 24 | + |
| 25 | + @pytest.fixture( |
| 26 | + params=["primary-node-crash", "primary-node-kill", "primary-echo-b", "primary-crash-index"] |
| 27 | + ) |
| 28 | + def task_type(self, request): |
| 29 | + """ |
| 30 | + Parameterized fixture to test both primary and secondary node operations. |
| 31 | +
|
| 32 | + :param request: pytest request object containing the parameter |
| 33 | + :type request: pytest.request |
| 34 | + :return: Dictionary with task configuration details |
| 35 | + :rtype: dict |
| 36 | + """ |
| 37 | + task_name = request.param |
| 38 | + |
| 39 | + if task_name == "primary-node-crash": |
| 40 | + return { |
| 41 | + "task_name": task_name, |
| 42 | + "command_task": "Stop the HANA DB", |
| 43 | + "command_type": "stop", |
| 44 | + "validate_task": "Test execution: Validate HANA DB cluster status 2", |
| 45 | + } |
| 46 | + elif task_name == "primary-node-kill": |
| 47 | + return { |
| 48 | + "task_name": task_name, |
| 49 | + "command_task": "Test Execution: Kill the HANA DB", |
| 50 | + "command_type": "kill-9", |
| 51 | + "validate_task": "Test execution: Validate HANA DB cluster status 2", |
| 52 | + } |
| 53 | + elif task_name == "primary-echo-b": |
| 54 | + return { |
| 55 | + "task_name": task_name, |
| 56 | + "command_task": "Test Execution: Echo b", |
| 57 | + "command_type": "echo b", |
| 58 | + "validate_task": "Test Execution: Validate HANA DB cluster status 2", |
| 59 | + } |
| 60 | + elif task_name == "primary-crash-index": |
| 61 | + return { |
| 62 | + "task_name": task_name, |
| 63 | + "command_task": "Test Execution: Crash the index server", |
| 64 | + "command_type": "killall", |
| 65 | + "validate_task": "Test Execution: Validate HANA DB cluster status", |
| 66 | + } |
| 67 | + |
| 68 | + @pytest.fixture |
| 69 | + def test_environment(self, ansible_inventory, task_type): |
| 70 | + """ |
| 71 | + Set up a temporary test environment for the HANA DB primary node operations tasks. |
| 72 | +
|
| 73 | + :param ansible_inventory: Path to the Ansible inventory file. |
| 74 | + :type ansible_inventory: str |
| 75 | + :param task_type: Dictionary with task configuration details. |
| 76 | + :type task_type: dict |
| 77 | + :yield temp_dir: Path to the temporary test environment. |
| 78 | + :ytype: str |
| 79 | + """ |
| 80 | + |
| 81 | + task_counter_file = f"/tmp/get_cluster_status_counter_{task_type['task_name']}" |
| 82 | + if os.path.exists(task_counter_file): |
| 83 | + os.remove(task_counter_file) |
| 84 | + |
| 85 | + temp_dir = self.setup_test_environment( |
| 86 | + role_type="ha_db_hana", |
| 87 | + ansible_inventory=ansible_inventory, |
| 88 | + task_name=task_type["task_name"], |
| 89 | + task_description=f"The {task_type['task_name']} test validates failover scenarios", |
| 90 | + module_names=[ |
| 91 | + "project/library/get_cluster_status_db", |
| 92 | + "project/library/log_parser", |
| 93 | + "project/library/send_telemetry_data", |
| 94 | + "project/library/location_constraints", |
| 95 | + "project/library/check_indexserver", |
| 96 | + "bin/crm_resource", |
| 97 | + "bin/crm", |
| 98 | + "bin/echo", |
| 99 | + "bin/killall", |
| 100 | + ], |
| 101 | + extra_vars_override={"node_tier": "hana"}, |
| 102 | + ) |
| 103 | + |
| 104 | + os.makedirs(f"{temp_dir}/bin", exist_ok=True) |
| 105 | + self.file_operations( |
| 106 | + operation="write", |
| 107 | + file_path=f"{temp_dir}/bin/HDB", |
| 108 | + content=self.file_operations( |
| 109 | + operation="read", |
| 110 | + file_path=Path(__file__).parent.parent / "mock_data/HDB.txt", |
| 111 | + ), |
| 112 | + ) |
| 113 | + os.chmod(f"{temp_dir}/bin/HDB", 0o755) |
| 114 | + |
| 115 | + playbook_content = self.file_operations( |
| 116 | + operation="read", |
| 117 | + file_path=f"{temp_dir}/project/roles/ha_db_hana/tasks/{task_type['task_name']}.yml", |
| 118 | + ) |
| 119 | + self.file_operations( |
| 120 | + operation="write", |
| 121 | + file_path=f"{temp_dir}/project/roles/ha_db_hana/tasks/{task_type['task_name']}.yml", |
| 122 | + content=playbook_content.replace( |
| 123 | + "/usr/sap/{{ db_sid | upper }}/HDB{{ db_instance_number }}/", "" |
| 124 | + ), |
| 125 | + ) |
| 126 | + |
| 127 | + yield temp_dir |
| 128 | + shutil.rmtree(temp_dir) |
| 129 | + |
| 130 | + def test_functional_db_primary_node_success( |
| 131 | + self, test_environment, ansible_inventory, task_type |
| 132 | + ): |
| 133 | + """ |
| 134 | + Test the HANA DB primary node operations tasks using Ansible Runner. |
| 135 | +
|
| 136 | + :param test_environment: Path to the temporary test environment. |
| 137 | + :type test_environment: str |
| 138 | + :param ansible_inventory: Path to the Ansible inventory file. |
| 139 | + :type ansible_inventory: str |
| 140 | + :param task_type: Dictionary with task configuration details. |
| 141 | + :type task_type: dict |
| 142 | + """ |
| 143 | + result = self.run_ansible_playbook( |
| 144 | + test_environment=test_environment, |
| 145 | + inventory_file_name="inventory_db.txt", |
| 146 | + task_type=task_type["task_name"], |
| 147 | + ) |
| 148 | + |
| 149 | + assert result.rc == 0, ( |
| 150 | + f"Playbook failed with status: {result.rc}\n" |
| 151 | + f"STDOUT: {result.stdout.read() if result.stdout else 'No output'}\n" |
| 152 | + f"STDERR: {result.stderr.read() if result.stderr else 'No errors'}\n" |
| 153 | + f"Events: {[e.get('event') for e in result.events if 'event' in e]}" |
| 154 | + ) |
| 155 | + |
| 156 | + ok_events, failed_events = [], [] |
| 157 | + for event in result.events: |
| 158 | + if event.get("event") == "runner_on_ok": |
| 159 | + ok_events.append(event) |
| 160 | + elif event.get("event") == "runner_on_failed": |
| 161 | + failed_events.append(event) |
| 162 | + |
| 163 | + assert len(ok_events) > 0 |
| 164 | + assert len(failed_events) == 0 |
| 165 | + |
| 166 | + post_status = {} |
| 167 | + pre_status = {} |
| 168 | + |
| 169 | + for event in ok_events: |
| 170 | + task = event.get("event_data", {}).get("task") |
| 171 | + task_result = event.get("event_data", {}).get("res") |
| 172 | + |
| 173 | + if task and task_type["command_task"] in task: |
| 174 | + if task_type["command_type"] == "echo b": |
| 175 | + assert task_result.get("changed") is True |
| 176 | + else: |
| 177 | + assert task_result.get("rc") == 0 |
| 178 | + elif ( |
| 179 | + task |
| 180 | + and "Test Execution: Validate HANA DB cluster status 1" in task |
| 181 | + and task_type["task_name"] == "primary-node-crash" |
| 182 | + ): |
| 183 | + assert not task_result.get("secondary_node") |
| 184 | + elif task and task_type["validate_task"] in task: |
| 185 | + assert task_result.get("secondary_node") |
| 186 | + assert task_result.get("primary_node") |
| 187 | + post_status = task_result |
| 188 | + elif task and "Pre Validation: Validate HANA DB" in task: |
| 189 | + pre_status = task_result |
| 190 | + elif task and "Remove any location_constraints" in task: |
| 191 | + assert task_result.get("changed") |
| 192 | + |
| 193 | + assert post_status.get("primary_node") == pre_status.get("secondary_node") |
| 194 | + assert post_status.get("secondary_node") == pre_status.get("primary_node") |
0 commit comments