|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +""" |
| 5 | +Test class for HANA DB block network between primary and secondary nodes. |
| 6 | +
|
| 7 | +This test class uses pytest to run functional tests on the HANA DB block-network |
| 8 | +tasks defined in roles/ha_db_hana/tasks/block-network.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 TestBlockNetworkTest(RolesTestingBaseDB): |
| 21 | + """ |
| 22 | + Test class for HANA DB block network between primary and secondary nodes. |
| 23 | + """ |
| 24 | + |
| 25 | + @pytest.fixture |
| 26 | + def test_environment(self, ansible_inventory): |
| 27 | + """ |
| 28 | + Set up a temporary test environment for the HANA DB block-network test |
| 29 | +
|
| 30 | + :param ansible_inventory: Path to the Ansible inventory file. |
| 31 | + :type ansible_inventory: str |
| 32 | + :param task_type: Dictionary with task configuration details. |
| 33 | + :type task_type: dict |
| 34 | + :yield temp_dir: Path to the temporary test environment. |
| 35 | + :type: str |
| 36 | + """ |
| 37 | + |
| 38 | + task_counter_file = "/tmp/get_cluster_status_counter_block-network" |
| 39 | + if os.path.exists(task_counter_file): |
| 40 | + os.remove(task_counter_file) |
| 41 | + |
| 42 | + if os.path.exists("/tmp/ping_counter_block-network"): |
| 43 | + os.remove("/tmp/ping_counter_block-network") |
| 44 | + |
| 45 | + module_names = [ |
| 46 | + "project/library/get_cluster_status_db", |
| 47 | + "project/library/log_parser", |
| 48 | + "project/library/send_telemetry_data", |
| 49 | + "project/library/location_constraints", |
| 50 | + "project/library/check_indexserver", |
| 51 | + "project/library/filesystem_freeze", |
| 52 | + "bin/crm_resource", |
| 53 | + "bin/iptables", |
| 54 | + "bin/ping", |
| 55 | + "bin/echo", |
| 56 | + "bin/sleep", |
| 57 | + ] |
| 58 | + |
| 59 | + temp_dir = self.setup_test_environment( |
| 60 | + role_type="ha_db_hana", |
| 61 | + ansible_inventory=ansible_inventory, |
| 62 | + task_name="block-network", |
| 63 | + task_description="The block network test validates failover scenarios", |
| 64 | + module_names=module_names, |
| 65 | + extra_vars_override={ |
| 66 | + "node_tier": "hana", |
| 67 | + "NFS_provider": "ANF", |
| 68 | + "database_cluster_type": "ISCSI", |
| 69 | + }, |
| 70 | + ) |
| 71 | + |
| 72 | + playbook_content = self.file_operations( |
| 73 | + operation="read", |
| 74 | + file_path=f"{temp_dir}/project/roles/ha_db_hana/tasks/block-network.yml", |
| 75 | + ) |
| 76 | + self.file_operations( |
| 77 | + operation="write", |
| 78 | + file_path=f"{temp_dir}/project/roles/ha_db_hana/tasks/block-network.yml", |
| 79 | + content=playbook_content.replace( |
| 80 | + "for i in $(seq 1 20); do", |
| 81 | + "for i in {1..20}; do", |
| 82 | + ), |
| 83 | + ) |
| 84 | + |
| 85 | + yield temp_dir |
| 86 | + shutil.rmtree(temp_dir) |
| 87 | + |
| 88 | + def test_functional_db_primary_node_success(self, test_environment, ansible_inventory): |
| 89 | + """ |
| 90 | + Test the HANA DB block network between primary and secondary nodes. |
| 91 | + This test verifies the successful execution of the block network task and checks |
| 92 | +
|
| 93 | + :param test_environment: Path to the temporary test environment. |
| 94 | + :type test_environment: str |
| 95 | + :param ansible_inventory: Path to the Ansible inventory file. |
| 96 | + :type ansible_inventory: str |
| 97 | + """ |
| 98 | + result = self.run_ansible_playbook( |
| 99 | + test_environment=test_environment, |
| 100 | + inventory_file_name="inventory_db.txt", |
| 101 | + task_type="block-network", |
| 102 | + ) |
| 103 | + |
| 104 | + assert result.rc == 0, ( |
| 105 | + f"Playbook failed with status: {result.rc}\n" |
| 106 | + f"STDOUT: {result.stdout.read() if result.stdout else 'No output'}\n" |
| 107 | + f"STDERR: {result.stderr.read() if result.stderr else 'No errors'}\n" |
| 108 | + f"Events: {[e.get('event') for e in result.events if 'event' in e]}" |
| 109 | + ) |
| 110 | + |
| 111 | + ok_events, failed_events = [], [] |
| 112 | + for event in result.events: |
| 113 | + if event.get("event") == "runner_on_ok": |
| 114 | + ok_events.append(event) |
| 115 | + elif event.get("event") == "runner_on_failed": |
| 116 | + failed_events.append(event) |
| 117 | + |
| 118 | + assert len(ok_events) > 0 |
| 119 | + # There will be 1 failed event, connection failure to primary node |
| 120 | + # This is the behavior be have mocked in the ping functionality |
| 121 | + assert len(failed_events) == 1 |
| 122 | + |
| 123 | + post_status = {} |
| 124 | + pre_status = {} |
| 125 | + |
| 126 | + for event in ok_events: |
| 127 | + task = event.get("event_data", {}).get("task") |
| 128 | + task_result = event.get("event_data", {}).get("res") |
| 129 | + |
| 130 | + if task and "Create a firewall" in task: |
| 131 | + assert task_result.get("rc") == 0 |
| 132 | + elif task and "Pre Validation: Validate HANA DB" in task: |
| 133 | + pre_status = task_result |
| 134 | + elif task and "Test Execution: Validate HANA DB cluster status 2" in task: |
| 135 | + post_status = task_result |
| 136 | + elif task and "Remove any location_constraints" in task: |
| 137 | + assert task_result.get("changed") |
| 138 | + |
| 139 | + assert post_status.get("primary_node") == pre_status.get("secondary_node") |
| 140 | + assert post_status.get("secondary_node") == pre_status.get("primary_node") |
0 commit comments